diff --git a/hack/make-rules/test-cmd-util.sh b/hack/make-rules/test-cmd-util.sh index d6d3b6f7c2a..c0f279b83d3 100755 --- a/hack/make-rules/test-cmd-util.sh +++ b/hack/make-rules/test-cmd-util.sh @@ -694,6 +694,10 @@ run_pod_tests() { ## Patch can modify a local object kubectl patch --local -f pkg/kubectl/validation/testdata/v1/validPod.yaml --patch='{"spec": {"restartPolicy":"Never"}}' -o jsonpath='{.spec.restartPolicy}' | grep -q "Never" + ## Patch fails with error message "not patched" and exit code 1 + output_message=$(! kubectl patch "${kube_flags[@]}" pod valid-pod -p='{"spec":{"replicas":7}}' 2>&1) + kube::test::if_has_string "${output_message}" 'not patched' + ## Patch pod can change image # Command kubectl patch "${kube_flags[@]}" pod valid-pod --record -p='{"spec":{"containers":[{"name": "kubernetes-serve-hostname", "image": "nginx"}]}}' diff --git a/pkg/kubectl/cmd/cmd.go b/pkg/kubectl/cmd/cmd.go index cb85111db26..87d81079e31 100644 --- a/pkg/kubectl/cmd/cmd.go +++ b/pkg/kubectl/cmd/cmd.go @@ -239,7 +239,8 @@ __custom_func() { * services (aka 'svc') * statefulsets * storageclasses - ` + +` ) var ( diff --git a/pkg/kubectl/cmd/patch.go b/pkg/kubectl/cmd/patch.go index 267e0139c00..3d099ac7d42 100644 --- a/pkg/kubectl/cmd/patch.go +++ b/pkg/kubectl/cmd/patch.go @@ -182,6 +182,7 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin if !options.Local { dataChangedMsg := "not patched" + didPatch := false helper := resource.NewHelper(client, mapping) patchedObj, err := helper.Patch(namespace, name, patchType, patchBytes) if err != nil { @@ -212,6 +213,7 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin return err } if !reflect.DeepEqual(oldData, newData) { + didPatch = true dataChangedMsg = "patched" } @@ -228,6 +230,12 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin return err } cmdutil.PrintSuccess(mapper, options.OutputFormat == "name", out, info.Mapping.Resource, info.Name, false, dataChangedMsg) + + // if object was not successfully patched, exit with error code 1 + if !didPatch { + return cmdutil.ErrExit + } + return nil } diff --git a/pkg/kubectl/cmd/patch_test.go b/pkg/kubectl/cmd/patch_test.go index 7bfd7146100..c72d308eec3 100644 --- a/pkg/kubectl/cmd/patch_test.go +++ b/pkg/kubectl/cmd/patch_test.go @@ -38,7 +38,14 @@ func TestPatchObject(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/services/frontend" && (m == "PATCH" || m == "GET"): - return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &svc.Items[0])}, nil + obj := svc.Items[0] + + // ensure patched object reflects successful + // patch edits from the client + if m == "PATCH" { + obj.Spec.Type = "NodePort" + } + return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &obj)}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) return nil, nil @@ -116,18 +123,6 @@ func TestPatchNoop(t *testing.T) { } tf.Namespace = "test" - // No-op - { - buf := bytes.NewBuffer([]byte{}) - cmd := NewCmdPatch(f, buf) - cmd.Flags().Set("namespace", "test") - cmd.Flags().Set("patch", `{}`) - cmd.Run(cmd, []string{"services", "frontend"}) - if buf.String() != "service \"baz\" not patched\n" { - t.Errorf("unexpected output: %s", buf.String()) - } - } - // Patched { patchObject = patchObject.DeepCopy()