Skip to content

Commit 17a1859

Browse files
authored
Merge pull request kubernetes#81494 from Klaven/remove_awk
Removed awk from kubeadm reset
2 parents 3b09dee + 6845c66 commit 17a1859

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

cmd/kubeadm/app/cmd/phases/reset/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ go_library(
77
"data.go",
88
"preflight.go",
99
"removeetcdmember.go",
10+
"unmount.go",
11+
"unmount_linux.go",
1012
"updateclusterstatus.go",
1113
],
1214
importpath = "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/reset",

cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"errors"
2121
"fmt"
2222
"os"
23-
"os/exec"
2423
"path/filepath"
2524

2625
"k8s.io/klog"
@@ -100,13 +99,10 @@ func absoluteKubeletRunDirectory() (string, error) {
10099
klog.Warningf("[reset] Failed to evaluate the %q directory. Skipping its unmount and cleanup: %v\n", kubeadmconstants.KubeletRunDirectory, err)
101100
return "", err
102101
}
103-
104-
// Only unmount mount points which start with "/var/lib/kubelet" or absolute path of symbolic link, and avoid using empty absoluteKubeletRunDirectory
105-
umountDirsCmd := fmt.Sprintf("awk '$2 ~ path {print $2}' path=%s/ /proc/mounts | xargs -r umount", absoluteKubeletRunDirectory)
106-
klog.V(1).Infof("[reset] Executing command %q", umountDirsCmd)
107-
umountOutputBytes, err := exec.Command("sh", "-c", umountDirsCmd).Output()
102+
err = unmountKubeletDirectory(absoluteKubeletRunDirectory)
108103
if err != nil {
109-
klog.Warningf("[reset] Failed to unmount mounted directories in %s: %s\n", kubeadmconstants.KubeletRunDirectory, string(umountOutputBytes))
104+
klog.Warningf("[reset] Failed to unmount mounted directories in %s \n", kubeadmconstants.KubeletRunDirectory)
105+
return "", err
110106
}
111107
return absoluteKubeletRunDirectory, nil
112108
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// +build !linux
2+
3+
/*
4+
Copyright 2019 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package phases
20+
21+
import (
22+
"k8s.io/klog"
23+
)
24+
25+
// unmountKubeletDirectory is a NOOP on all but linux.
26+
func unmountKubeletDirectory(absoluteKubeletRunDirectory string) error {
27+
klog.Warning("Cannot unmount filesystems on current OS, all mounted file systems will need to be manually unmounted")
28+
return nil
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// +build linux
2+
3+
/*
4+
Copyright 2019 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package phases
20+
21+
import (
22+
"io/ioutil"
23+
"strings"
24+
"syscall"
25+
26+
"k8s.io/klog"
27+
)
28+
29+
// unmountKubeletDirectory unmounts all paths that contain KubeletRunDirectory
30+
func unmountKubeletDirectory(absoluteKubeletRunDirectory string) error {
31+
raw, err := ioutil.ReadFile("/proc/mounts")
32+
if err != nil {
33+
return err
34+
}
35+
mounts := strings.Split(string(raw), "\n")
36+
for _, mount := range mounts {
37+
m := strings.Split(mount, " ")
38+
if len(m) < 2 || !strings.HasPrefix(m[1], absoluteKubeletRunDirectory) {
39+
continue
40+
}
41+
if err := syscall.Unmount(m[1], 0); err != nil {
42+
klog.Warningf("[reset] Failed to unmount mounted directory in %s: %s", absoluteKubeletRunDirectory, m[1])
43+
}
44+
}
45+
return nil
46+
}

0 commit comments

Comments
 (0)