diff options
Diffstat (limited to 'vendor/k8s.io/kubernetes/pkg/util/io/writer.go')
-rw-r--r-- | vendor/k8s.io/kubernetes/pkg/util/io/writer.go | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/vendor/k8s.io/kubernetes/pkg/util/io/writer.go b/vendor/k8s.io/kubernetes/pkg/util/io/writer.go index 086508c90..8d1d9964e 100644 --- a/vendor/k8s.io/kubernetes/pkg/util/io/writer.go +++ b/vendor/k8s.io/kubernetes/pkg/util/io/writer.go @@ -21,13 +21,15 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" + + "k8s.io/kubernetes/pkg/util/nsenter" "github.com/golang/glog" ) // Writer is an interface which allows to write data to a file. type Writer interface { + // WriteFile mimics ioutil.WriteFile. WriteFile(filename string, data []byte, perm os.FileMode) error } @@ -36,41 +38,37 @@ type Writer interface { type StdWriter struct { } +// WriteFile directly calls ioutil.WriteFile. func (writer *StdWriter) WriteFile(filename string, data []byte, perm os.FileMode) error { return ioutil.WriteFile(filename, data, perm) } -// Alternative implementation of Writer interface that allows writing data to file -// using nsenter command. +// NsenterWriter is implementation of Writer interface that allows writing data +// to file using nsenter command. // If a program (e.g. kubelet) runs in a container it may want to write data to // a mounted device. Since in Docker, mount propagation mode is set to private, // it will not see the mounted device in its own namespace. To work around this -// limitaion one has to first enter hosts namespace (by using 'nsenter') and only -// then write data. -type NsenterWriter struct { -} +// limitation one has to first enter hosts namespace (by using 'nsenter') and +// only then write data. +type NsenterWriter struct{} -// TODO: should take a writer, not []byte +// WriteFile calls 'nsenter cat - > <the file>' and 'nsenter chmod' to create a +// file on the host. func (writer *NsenterWriter) WriteFile(filename string, data []byte, perm os.FileMode) error { - cmd := "nsenter" - base_args := []string{ - "--mount=/rootfs/proc/1/ns/mnt", - "--", - } - - echo_args := append(base_args, "sh", "-c", fmt.Sprintf("cat > %s", filename)) - glog.V(5).Infof("Command to write data to file: %v %v", cmd, echo_args) - command := exec.Command(cmd, echo_args...) - 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 } - chmod_args := append(base_args, "chmod", fmt.Sprintf("%o", perm), filename) - glog.V(5).Infof("Command to change permissions to file: %v %v", cmd, chmod_args) - outputBytes, err = exec.Command(cmd, chmod_args...).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 |