diff --git a/cmd/kubeadm/app/cmd/phases/reset/BUILD b/cmd/kubeadm/app/cmd/phases/reset/BUILD index ab185270034..47924268239 100644 --- a/cmd/kubeadm/app/cmd/phases/reset/BUILD +++ b/cmd/kubeadm/app/cmd/phases/reset/BUILD @@ -7,6 +7,8 @@ go_library( "data.go", "preflight.go", "removeetcdmember.go", + "unmount.go", + "unmount_linux.go", "updateclusterstatus.go", ], importpath = "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/reset", diff --git a/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go b/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go index df58fbc73d5..7224cad7d8c 100644 --- a/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go +++ b/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" "os" - "os/exec" "path/filepath" "k8s.io/klog" @@ -100,13 +99,10 @@ func absoluteKubeletRunDirectory() (string, error) { klog.Warningf("[reset] Failed to evaluate the %q directory. Skipping its unmount and cleanup: %v\n", kubeadmconstants.KubeletRunDirectory, err) return "", err } - - // Only unmount mount points which start with "/var/lib/kubelet" or absolute path of symbolic link, and avoid using empty absoluteKubeletRunDirectory - umountDirsCmd := fmt.Sprintf("awk '$2 ~ path {print $2}' path=%s/ /proc/mounts | xargs -r umount", absoluteKubeletRunDirectory) - klog.V(1).Infof("[reset] Executing command %q", umountDirsCmd) - umountOutputBytes, err := exec.Command("sh", "-c", umountDirsCmd).Output() + err = unmountKubeletDirectory(absoluteKubeletRunDirectory) if err != nil { - klog.Warningf("[reset] Failed to unmount mounted directories in %s: %s\n", kubeadmconstants.KubeletRunDirectory, string(umountOutputBytes)) + klog.Warningf("[reset] Failed to unmount mounted directories in %s \n", kubeadmconstants.KubeletRunDirectory) + return "", err } return absoluteKubeletRunDirectory, nil } diff --git a/cmd/kubeadm/app/cmd/phases/reset/unmount.go b/cmd/kubeadm/app/cmd/phases/reset/unmount.go new file mode 100644 index 00000000000..2a98c3e6dc4 --- /dev/null +++ b/cmd/kubeadm/app/cmd/phases/reset/unmount.go @@ -0,0 +1,29 @@ +// +build !linux + +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package phases + +import ( + "k8s.io/klog" +) + +// unmountKubeletDirectory is a NOOP on all but linux. +func unmountKubeletDirectory(absoluteKubeletRunDirectory string) error { + klog.Warning("Cannot unmount filesystems on current OS, all mounted file systems will need to be manually unmounted") + return nil +} diff --git a/cmd/kubeadm/app/cmd/phases/reset/unmount_linux.go b/cmd/kubeadm/app/cmd/phases/reset/unmount_linux.go new file mode 100644 index 00000000000..ccdc7702338 --- /dev/null +++ b/cmd/kubeadm/app/cmd/phases/reset/unmount_linux.go @@ -0,0 +1,46 @@ +// +build linux + +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package phases + +import ( + "io/ioutil" + "strings" + "syscall" + + "k8s.io/klog" +) + +// unmountKubeletDirectory unmounts all paths that contain KubeletRunDirectory +func unmountKubeletDirectory(absoluteKubeletRunDirectory string) error { + raw, err := ioutil.ReadFile("/proc/mounts") + if err != nil { + return err + } + mounts := strings.Split(string(raw), "\n") + for _, mount := range mounts { + m := strings.Split(mount, " ") + if len(m) < 2 || !strings.HasPrefix(m[1], absoluteKubeletRunDirectory) { + continue + } + if err := syscall.Unmount(m[1], 0); err != nil { + klog.Warningf("[reset] Failed to unmount mounted directory in %s: %s", absoluteKubeletRunDirectory, m[1]) + } + } + return nil +}