From c5c56dcf359f545bd6e602707c3a4e9de84a1524 Mon Sep 17 00:00:00 2001 From: hzxuzhonghu Date: Tue, 17 Oct 2017 15:13:56 +0800 Subject: [PATCH] kubectl apply does not send empty patch request --- pkg/kubectl/cmd/apply.go | 10 +++++ pkg/kubectl/cmd/apply_test.go | 69 +++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/pkg/kubectl/cmd/apply.go b/pkg/kubectl/cmd/apply.go index 3551dea286a..f6ba5b9f824 100644 --- a/pkg/kubectl/cmd/apply.go +++ b/pkg/kubectl/cmd/apply.go @@ -336,6 +336,12 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti } else { visitedUids.Insert(string(uid)) } + + if string(patchBytes) == "{}" { + count++ + cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, false, "unchanged") + return nil + } } count++ if len(output) > 0 && !shortOutput { @@ -609,6 +615,10 @@ func (p *patcher) patchSimple(obj runtime.Object, modified []byte, source, names } } + if string(patch) == "{}" { + return patch, obj, nil + } + patchedObj, err := p.helper.Patch(namespace, name, patchType, patch) return patch, patchedObj, err } diff --git a/pkg/kubectl/cmd/apply_test.go b/pkg/kubectl/cmd/apply_test.go index fc98f462f9b..57d760aa37b 100644 --- a/pkg/kubectl/cmd/apply_test.go +++ b/pkg/kubectl/cmd/apply_test.go @@ -592,6 +592,75 @@ func TestApplyNonExistObject(t *testing.T) { } } +func TestApplyEmptyPatch(t *testing.T) { + initTestErrorHandler(t) + nameRC, _ := readAndAnnotateReplicationController(t, filenameRC) + pathRC := "/namespaces/test/replicationcontrollers" + pathNameRC := pathRC + "/" + nameRC + + verifyPost := false + + var body []byte + + f, tf, _, _ := cmdtesting.NewAPIFactory() + tf.Printer = &testPrinter{} + tf.UnstructuredClient = &fake.RESTClient{ + GroupVersion: schema.GroupVersion{Version: "v1"}, + NegotiatedSerializer: unstructuredSerializer, + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { + switch p, m := req.URL.Path, req.Method; { + case p == "/api/v1/namespaces/test" && m == "GET": + return &http.Response{StatusCode: 404, Header: defaultHeader(), Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil + case p == pathNameRC && m == "GET": + if body == nil { + return &http.Response{StatusCode: 404, Header: defaultHeader(), Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil + } + bodyRC := ioutil.NopCloser(bytes.NewReader(body)) + return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: bodyRC}, nil + case p == pathRC && m == "POST": + body, _ = ioutil.ReadAll(req.Body) + verifyPost = true + bodyRC := ioutil.NopCloser(bytes.NewReader(body)) + return &http.Response{StatusCode: 201, Header: defaultHeader(), Body: bodyRC}, nil + default: + t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) + return nil, nil + } + }), + } + tf.Namespace = "test" + + // 1. apply non exist object + buf := bytes.NewBuffer([]byte{}) + errBuf := bytes.NewBuffer([]byte{}) + + cmd := NewCmdApply("kubectl", f, buf, errBuf) + cmd.Flags().Set("filename", filenameRC) + cmd.Flags().Set("output", "name") + cmd.Run(cmd, []string{}) + + expectRC := "replicationcontroller/" + nameRC + "\n" + if buf.String() != expectRC { + t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expectRC) + } + if !verifyPost { + t.Fatal("No server-side post call detected") + } + + // 2. test apply already exist object, will not send empty patch request + buf = bytes.NewBuffer([]byte{}) + errBuf = bytes.NewBuffer([]byte{}) + + cmd = NewCmdApply("kubectl", f, buf, errBuf) + cmd.Flags().Set("filename", filenameRC) + cmd.Flags().Set("output", "name") + cmd.Run(cmd, []string{}) + + if buf.String() != expectRC { + t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expectRC) + } +} + func TestApplyMultipleObjectsAsList(t *testing.T) { testApplyMultipleObjects(t, true) }