From 50f26dd9518ee1c0a108e9a369b49ac5d8903b43 Mon Sep 17 00:00:00 2001 From: martinkaburu Date: Mon, 25 Nov 2019 23:21:43 +0300 Subject: [PATCH] fix apply --prune to check cli specified namespace Kubernetes-commit: f2518347f3dbe36586367c94aa98d5096f74a742 --- pkg/cmd/apply/apply.go | 7 ++++-- pkg/cmd/apply/apply_test.go | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/apply/apply.go b/pkg/cmd/apply/apply.go index 1bc989f7..103eb244 100644 --- a/pkg/cmd/apply/apply.go +++ b/pkg/cmd/apply/apply.go @@ -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 diff --git a/pkg/cmd/apply/apply_test.go b/pkg/cmd/apply/apply_test.go index eacbfc80..9e814fb9 100644 --- a/pkg/cmd/apply/apply_test.go +++ b/pkg/cmd/apply/apply_test.go @@ -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)