removed awk from kubeadm reset

removed awk from kubeadm reset in favor of native go lang calls
that are not vulnerable to expantion.
This commit is contained in:
Marek Counts 2019-08-15 22:16:41 +00:00
parent d6035f3e0d
commit 6845c66efb
4 changed files with 80 additions and 7 deletions

View File

@ -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",

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}