Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kubernetes): correct handling of kubectl stderr #61

Merged
merged 1 commit into from
Aug 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/kubernetes/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package kubernetes
import "fmt"

type ErrorNotFound struct {
resource string
name string
kind string
}

func (e ErrorNotFound) Error() string {
return fmt.Sprintf(`error from server (NotFound): secrets "%s" not found`, e.resource)
return fmt.Sprintf(`error from server (NotFound): %s "%s" not found`, e.kind, e.name)
}

type ErrorMissingConfig struct {
Expand Down
8 changes: 6 additions & 2 deletions pkg/kubernetes/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"

"github.com/Masterminds/semver"
Expand Down Expand Up @@ -39,6 +40,7 @@ func (k Kubectl) Version() (client, server semver.Version, err error) {
)
var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return zero, zero, err
}
Expand All @@ -57,6 +59,7 @@ func (k *Kubectl) setupContext() error {
cmd := exec.Command("kubectl", "config", "view", "-o", "json")
cfgJSON := bytes.Buffer{}
cmd.Stdout = &cfgJSON
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
Expand Down Expand Up @@ -115,9 +118,9 @@ func (k Kubectl) Get(namespace, kind, name string) (map[string]interface{}, erro
cmd.Stderr = &serr
if err := cmd.Run(); err != nil {
if strings.HasPrefix(serr.String(), "Error from server (NotFound)") {
return nil, ErrorNotFound{name}
return nil, ErrorNotFound{kind, name}
}
fmt.Println(serr.String())
fmt.Print(serr.String())
return nil, err
}
var obj map[string]interface{}
Expand Down Expand Up @@ -196,6 +199,7 @@ func (k Kubectl) Diff(yaml string) (*string, error) {
cmd := exec.Command("kubectl", argv...)
raw := bytes.Buffer{}
cmd.Stdout = &raw
cmd.Stderr = FilteredErr{regexp.MustCompile(`exit status \d`)}

stdin, err := cmd.StdinPipe()
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions pkg/kubernetes/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
)

func diff(name, is, should string) (string, error) {
Expand Down Expand Up @@ -44,3 +45,16 @@ func diff(name, is, should string) (string, error) {

return out, nil
}

// FilteredErr is a filtered Stderr. If one of the regular expressions match, the current input is discarded.
type FilteredErr []*regexp.Regexp

func (r FilteredErr) Write(p []byte) (n int, err error) {
for _, re := range r {
if re.Match(p) {
// silently discard
return len(p), nil
}
}
return os.Stderr.Write(p)
}