Merge pull request #52648 from dixudx/refactor_NsenterWriter

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

refactor NsenterWriter to utilize pkg/util/nsenter

**What this PR does / why we need it**:
Per [discussion](https://github.com/kubernetes/kubernetes/pull/51771#discussion_r138824451)
Depend on #51771

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
None
```
pull/6/head
Kubernetes Submit Queue 2018-02-22 03:16:50 -08:00 committed by GitHub
commit b2091090fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 15 deletions

View File

@ -12,7 +12,10 @@ go_library(
"writer.go", "writer.go",
], ],
importpath = "k8s.io/kubernetes/pkg/util/io", 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( filegroup(

View File

@ -21,7 +21,8 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"k8s.io/kubernetes/pkg/util/nsenter"
"github.com/golang/glog" "github.com/golang/glog"
) )
@ -54,25 +55,20 @@ type NsenterWriter struct{}
// WriteFile calls 'nsenter cat - > <the file>' and 'nsenter chmod' to create a // WriteFile calls 'nsenter cat - > <the file>' and 'nsenter chmod' to create a
// file on the host. // file on the host.
func (writer *NsenterWriter) WriteFile(filename string, data []byte, perm os.FileMode) error { func (writer *NsenterWriter) WriteFile(filename string, data []byte, perm os.FileMode) error {
cmd := "nsenter" ne := nsenter.NewNsenter()
baseArgs := []string{ echoArgs := []string{"-c", fmt.Sprintf("cat > %s", filename)}
"--mount=/rootfs/proc/1/ns/mnt", glog.V(5).Infof("nsenter: write data to file %s by nsenter", filename)
"--", command := ne.Exec("sh", echoArgs)
} command.SetStdin(bytes.NewBuffer(data))
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)
outputBytes, err := command.CombinedOutput() outputBytes, err := command.CombinedOutput()
if err != nil { if err != nil {
glog.Errorf("Output from writing to %q: %v", filename, string(outputBytes)) glog.Errorf("Output from writing to %q: %v", filename, string(outputBytes))
return err return err
} }
chmodArgs := append(baseArgs, "chmod", fmt.Sprintf("%o", perm), filename) chmodArgs := []string{fmt.Sprintf("%o", perm), filename}
glog.V(5).Infof("Command to change permissions to file: %v %v", cmd, chmodArgs) glog.V(5).Infof("nsenter: change permissions of file %s to %s", filename, chmodArgs[0])
outputBytes, err = exec.Command(cmd, chmodArgs...).CombinedOutput() outputBytes, err = ne.Exec("chmod", chmodArgs).CombinedOutput()
if err != nil { if err != nil {
glog.Errorf("Output from chmod command: %v", string(outputBytes)) glog.Errorf("Output from chmod command: %v", string(outputBytes))
return err return err