kubectl apply does not send empty patch request
This commit is contained in:
		@@ -336,6 +336,12 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti
 | 
				
			|||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				visitedUids.Insert(string(uid))
 | 
									visitedUids.Insert(string(uid))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if string(patchBytes) == "{}" {
 | 
				
			||||||
 | 
									count++
 | 
				
			||||||
 | 
									cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, false, "unchanged")
 | 
				
			||||||
 | 
									return nil
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		count++
 | 
							count++
 | 
				
			||||||
		if len(output) > 0 && !shortOutput {
 | 
							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)
 | 
						patchedObj, err := p.helper.Patch(namespace, name, patchType, patch)
 | 
				
			||||||
	return patch, patchedObj, err
 | 
						return patch, patchedObj, err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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) {
 | 
					func TestApplyMultipleObjectsAsList(t *testing.T) {
 | 
				
			||||||
	testApplyMultipleObjects(t, true)
 | 
						testApplyMultipleObjects(t, true)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user