Skip to content

Commit

Permalink
clusterctl/kind: return stdout and not combined output
Browse files Browse the repository at this point in the history
Ideally only the stdout of kind commands should be captured
and returned, in case kind commands also contain stderr.

Fixes a recent bug where the kind command "get kubeconfig-path"
started printing a deprecation message on stderr.
  • Loading branch information
neolit123 committed Nov 21, 2019
1 parent 755795f commit c231b33
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
21 changes: 16 additions & 5 deletions cmd/clusterctl/clusterdeployer/bootstrap/kind/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package kind

import (
"bytes"
"fmt"
"io/ioutil"
"os/exec"
Expand Down Expand Up @@ -67,14 +68,24 @@ func WithOptions(options []string) *Kind {

var execFunc = func(args ...string) (string, error) {
const executable = "kind"
klog.V(3).Infof("Running: %v %v", executable, args)
joinedArgs := strings.Join(args, " ")
klog.V(3).Infof("Running: %v %v", executable, joinedArgs)

cmd := exec.Command(executable, args...)
cmdOut, err := cmd.CombinedOutput()
klog.V(2).Infof("Ran: %v %v Output: %v", executable, args, string(cmdOut))
var bufErr, bufOut bytes.Buffer
cmd.Stdout = &bufOut
cmd.Stderr = &bufErr

err := cmd.Run()
commandMessage := fmt.Sprintf("%v %v\nStdout:\n%v\nStderr:\n%v",
executable, joinedArgs, bufOut.String(), bufErr.String())

if err != nil {
err = errors.Wrapf(err, "error running command '%v %v'", executable, strings.Join(args, " "))
return "", errors.Wrapf(err, "error running command: %s", commandMessage)
}
return string(cmdOut), err

klog.V(2).Infof("Ran: %s", commandMessage)
return bufOut.String(), err
}

func (k *Kind) Create() error {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/sergi/go-diff v1.0.0
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 // indirect
golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fc
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
Expand Down

0 comments on commit c231b33

Please sign in to comment.