From 7410acf4abbf01366f1ae61378591aec1c5d5fba Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Fri, 9 Dec 2016 19:01:27 -0500 Subject: [PATCH] Prevent "patched" output on obj not patched This patch compares an original object against a patched object returned from the server and only announces that the object was successfully patched if the object returned from the server does not equal the original object. --- pkg/kubectl/cmd/patch.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/pkg/kubectl/cmd/patch.go b/pkg/kubectl/cmd/patch.go index 6debf1f8624..6e42d83c733 100644 --- a/pkg/kubectl/cmd/patch.go +++ b/pkg/kubectl/cmd/patch.go @@ -17,11 +17,13 @@ limitations under the License. package cmd import ( + "encoding/json" "fmt" "io" + "reflect" "strings" - "github.com/evanphx/json-patch" + jsonpatch "github.com/evanphx/json-patch" "github.com/spf13/cobra" "k8s.io/kubernetes/pkg/api" @@ -167,8 +169,9 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin } if !options.Local { + dataChangedMsg := "not patched" helper := resource.NewHelper(client, mapping) - _, err := helper.Patch(namespace, name, patchType, patchBytes) + patchedObj, err := helper.Patch(namespace, name, patchType, patchBytes) if err != nil { return err } @@ -184,8 +187,20 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin } count++ + oldData, err := json.Marshal(info.Object) + if err != nil { + return err + } + newData, err := json.Marshal(patchedObj) + if err != nil { + return err + } + if !reflect.DeepEqual(oldData, newData) { + dataChangedMsg = "patched" + } + if options.OutputFormat == "name" || len(options.OutputFormat) == 0 { - cmdutil.PrintSuccess(mapper, options.OutputFormat == "name", out, "", name, false, "patched") + cmdutil.PrintSuccess(mapper, options.OutputFormat == "name", out, "", name, false, dataChangedMsg) } return nil }