From 53d340f0a124ac890e928f12228987e6906c1f63 Mon Sep 17 00:00:00 2001 From: Di Xu Date: Mon, 18 Sep 2017 18:15:13 +0800 Subject: [PATCH] refactor NsenterWriter to utilize pkg/util/nsenter --- pkg/util/io/BUILD | 5 ++++- pkg/util/io/writer.go | 24 ++++++++++-------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/pkg/util/io/BUILD b/pkg/util/io/BUILD index da3218fb23..93de0dcba4 100644 --- a/pkg/util/io/BUILD +++ b/pkg/util/io/BUILD @@ -12,7 +12,10 @@ go_library( "writer.go", ], importpath = "k8s.io/kubernetes/pkg/util/io", - deps = ["//vendor/github.com/golang/glog:go_default_library"], + deps = [ + "//pkg/util/nsenter:go_default_library", + "//vendor/github.com/golang/glog:go_default_library", + ], ) filegroup( diff --git a/pkg/util/io/writer.go b/pkg/util/io/writer.go index 5d0f1703eb..8d1d9964e0 100644 --- a/pkg/util/io/writer.go +++ b/pkg/util/io/writer.go @@ -21,7 +21,8 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" + + "k8s.io/kubernetes/pkg/util/nsenter" "github.com/golang/glog" ) @@ -54,25 +55,20 @@ type NsenterWriter struct{} // WriteFile calls 'nsenter cat - > ' and 'nsenter chmod' to create a // file on the host. func (writer *NsenterWriter) WriteFile(filename string, data []byte, perm os.FileMode) error { - cmd := "nsenter" - baseArgs := []string{ - "--mount=/rootfs/proc/1/ns/mnt", - "--", - } - - echoArgs := append(baseArgs, "sh", "-c", fmt.Sprintf("cat > %s", filename)) - glog.V(5).Infof("Command to write data to file: %v %v", cmd, echoArgs) - command := exec.Command(cmd, echoArgs...) - command.Stdin = bytes.NewBuffer(data) + ne := nsenter.NewNsenter() + echoArgs := []string{"-c", fmt.Sprintf("cat > %s", filename)} + glog.V(5).Infof("nsenter: write data to file %s by nsenter", filename) + command := ne.Exec("sh", echoArgs) + command.SetStdin(bytes.NewBuffer(data)) outputBytes, err := command.CombinedOutput() if err != nil { glog.Errorf("Output from writing to %q: %v", filename, string(outputBytes)) return err } - chmodArgs := append(baseArgs, "chmod", fmt.Sprintf("%o", perm), filename) - glog.V(5).Infof("Command to change permissions to file: %v %v", cmd, chmodArgs) - outputBytes, err = exec.Command(cmd, chmodArgs...).CombinedOutput() + chmodArgs := []string{fmt.Sprintf("%o", perm), filename} + glog.V(5).Infof("nsenter: change permissions of file %s to %s", filename, chmodArgs[0]) + outputBytes, err = ne.Exec("chmod", chmodArgs).CombinedOutput() if err != nil { glog.Errorf("Output from chmod command: %v", string(outputBytes)) return err