From e31d14cb10917949b21f7f19fec940b51b929259 Mon Sep 17 00:00:00 2001 From: Antoine Pelisse Date: Fri, 12 Oct 2018 10:56:16 -0700 Subject: [PATCH 1/2] kubectl diff: Rename KUBERNETES_EXTERNAL_DIFF to KUBECTL_EXTERNAL_DIFF --- pkg/kubectl/cmd/diff/diff.go | 7 +++---- pkg/kubectl/cmd/diff/diff_test.go | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/kubectl/cmd/diff/diff.go b/pkg/kubectl/cmd/diff/diff.go index 18f8a3e3808..59211f2b1f8 100644 --- a/pkg/kubectl/cmd/diff/diff.go +++ b/pkg/kubectl/cmd/diff/diff.go @@ -26,7 +26,6 @@ import ( "github.com/ghodss/yaml" "github.com/jonboulle/clockwork" "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -50,7 +49,7 @@ var ( Output is always YAML. - KUBERNETES_EXTERNAL_DIFF environment variable can be used to select your own + KUBECTL_EXTERNAL_DIFF environment variable can be used to select your own diff command. By default, the "diff" command available in your path will be run with "-u" (unicode) and "-N" (treat new files as empty) options.`)) diffExample = templates.Examples(i18n.T(` @@ -98,7 +97,7 @@ func NewCmdDiff(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.C } // DiffProgram finds and run the diff program. The value of -// KUBERNETES_EXTERNAL_DIFF environment variable will be used a diff +// KUBECTL_EXTERNAL_DIFF environment variable will be used a diff // program. By default, `diff(1)` will be used. type DiffProgram struct { Exec exec.Interface @@ -107,7 +106,7 @@ type DiffProgram struct { func (d *DiffProgram) getCommand(args ...string) exec.Cmd { diff := "" - if envDiff := os.Getenv("KUBERNETES_EXTERNAL_DIFF"); envDiff != "" { + if envDiff := os.Getenv("KUBECTL_EXTERNAL_DIFF"); envDiff != "" { diff = envDiff } else { diff = "diff" diff --git a/pkg/kubectl/cmd/diff/diff_test.go b/pkg/kubectl/cmd/diff/diff_test.go index 9a88512ee58..2b833e551db 100644 --- a/pkg/kubectl/cmd/diff/diff_test.go +++ b/pkg/kubectl/cmd/diff/diff_test.go @@ -52,7 +52,7 @@ func (f *FakeObject) Live() runtime.Object { } func TestDiffProgram(t *testing.T) { - os.Setenv("KUBERNETES_EXTERNAL_DIFF", "echo") + os.Setenv("KUBECTL_EXTERNAL_DIFF", "echo") streams, _, stdout, _ := genericclioptions.NewTestIOStreams() diff := DiffProgram{ IOStreams: streams, From 572a028148bf6729410cdd7806ef83aedc7ace57 Mon Sep 17 00:00:00 2001 From: Antoine Pelisse Date: Fri, 12 Oct 2018 11:08:14 -0700 Subject: [PATCH 2/2] kubectl diff: Print error when something goes wrong --- pkg/kubectl/cmd/diff/diff.go | 8 ++------ test/cmd/diff.sh | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/pkg/kubectl/cmd/diff/diff.go b/pkg/kubectl/cmd/diff/diff.go index 59211f2b1f8..44f21834162 100644 --- a/pkg/kubectl/cmd/diff/diff.go +++ b/pkg/kubectl/cmd/diff/diff.go @@ -122,8 +122,7 @@ func (d *DiffProgram) getCommand(args ...string) exec.Cmd { // Run runs the detected diff program. `from` and `to` are the directory to diff. func (d *DiffProgram) Run(from, to string) error { - d.getCommand(from, to).Run() // Ignore diff return code - return nil + return d.getCommand(from, to).Run() } // Printer is used to print an object. @@ -396,8 +395,5 @@ func RunDiff(f cmdutil.Factory, diff *DiffProgram, options *DiffOptions) error { return err } - // Error ignore on purpose. diff(1) for example, returns an error if there is any diff. - _ = differ.Run(diff) - - return nil + return differ.Run(diff) } diff --git a/test/cmd/diff.sh b/test/cmd/diff.sh index d52a75bdfce..bc763473773 100755 --- a/test/cmd/diff.sh +++ b/test/cmd/diff.sh @@ -27,12 +27,12 @@ run_kubectl_diff_tests() { kube::log::status "Testing kubectl diff" # Test that it works when the live object doesn't exist - output_message=$(kubectl diff -f hack/testdata/pod.yaml) + output_message=$(! kubectl diff -f hack/testdata/pod.yaml) kube::test::if_has_string "${output_message}" 'test-pod' kubectl apply -f hack/testdata/pod.yaml - output_message=$(kubectl diff -f hack/testdata/pod-changed.yaml) + output_message=$(! kubectl diff -f hack/testdata/pod-changed.yaml) kube::test::if_has_string "${output_message}" 'k8s.gcr.io/pause:3.0' kubectl delete -f hack/testdata/pod.yaml