Merge pull request #54046 from hzxuzhonghu/kubectl-apply

Automatic merge from submit-queue (batch tested with PRs 54326, 54046). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

kubectl apply does not send empty patch request

**What this PR does / why we need it**:
`kubectl apply -f filename` always send patch request to apiserver, whether the file changed or not.

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #54010

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
pull/6/head
Kubernetes Submit Queue 2017-10-31 14:13:20 -07:00 committed by GitHub
commit 7efc6c83f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 0 deletions

View File

@ -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 {
@ -617,6 +623,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
}

View File

@ -598,6 +598,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)
}