fix apply --prune to check cli specified namespace

Kubernetes-commit: f2518347f3dbe36586367c94aa98d5096f74a742
This commit is contained in:
martinkaburu 2019-11-25 23:21:43 +03:00 committed by Kubernetes Publisher
parent 236748c23b
commit 50f26dd951
2 changed files with 54 additions and 2 deletions

View File

@ -372,6 +372,11 @@ func (o *ApplyOptions) Run() error {
}
}
// Enforce CLI specified namespace on server request.
if o.EnforceNamespace {
o.VisitedNamespaces.Insert(o.Namespace)
}
// Generates the objects using the resource builder if they have not
// already been stored by calling "SetObjects()" in the pre-processor.
infos, err := o.GetObjects()
@ -424,7 +429,6 @@ func (o *ApplyOptions) Run() error {
}
if errors.IsConflict(err) {
err = fmt.Errorf(`%v
Please review the fields above--they currently have other managers. Here
are the ways you can resolve this warning:
* If you intend to manage all of these fields, please re-run the apply
@ -435,7 +439,6 @@ are the ways you can resolve this warning:
* You may co-own fields by updating your manifest to match the existing
value; in this case, you'll become the manager if the other manager(s)
stop managing the field (remove it from their configuration).
See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
}
return err

View File

@ -562,6 +562,55 @@ func TestApplyObject(t *testing.T) {
}
}
func TestApplyPruneObjects(t *testing.T) {
cmdtesting.InitTestErrorHandler(t)
nameRC, currentRC := readAndAnnotateReplicationController(t, filenameRC)
pathRC := "/namespaces/test/replicationcontrollers/" + nameRC
for _, fn := range testingOpenAPISchemaFns {
t.Run("test apply returns correct output", func(t *testing.T) {
tf := cmdtesting.NewTestFactory().WithNamespace("test")
defer tf.Cleanup()
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
switch p, m := req.URL.Path, req.Method; {
case p == pathRC && m == "GET":
bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
case p == pathRC && m == "PATCH":
validatePatchApplication(t, req)
bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC))
return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil
default:
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
return nil, nil
}
}),
}
tf.OpenAPISchemaFunc = fn
tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
ioStreams, _, buf, errBuf := genericclioptions.NewTestIOStreams()
cmd := NewCmdApply("kubectl", tf, ioStreams)
cmd.Flags().Set("filename", filenameRC)
cmd.Flags().Set("prune", "true")
cmd.Flags().Set("namespace", "test")
cmd.Flags().Set("output", "yaml")
cmd.Flags().Set("all", "true")
cmd.Run(cmd, []string{})
if !strings.Contains(buf.String(), "test-rc") {
t.Fatalf("unexpected output: %s\nexpected to contain: %s", buf.String(), "test-rc")
}
if errBuf.String() != "" {
t.Fatalf("unexpected error output: %s", errBuf.String())
}
})
}
}
func TestApplyObjectOutput(t *testing.T) {
cmdtesting.InitTestErrorHandler(t)
nameRC, currentRC := readAndAnnotateReplicationController(t, filenameRC)