From 818f011a2e7e1a05923f06c57ef6089e3a048fba Mon Sep 17 00:00:00 2001 From: Riccardo Piccoli Date: Thu, 13 Feb 2020 17:28:06 +0100 Subject: [PATCH] Change not found output when getting non namespaced resources Signed-off-by: Riccardo Piccoli --- staging/src/k8s.io/kubectl/pkg/cmd/get/get.go | 4 ++- .../k8s.io/kubectl/pkg/cmd/get/get_test.go | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/get/get.go b/staging/src/k8s.io/kubectl/pkg/cmd/get/get.go index ab157bd7674..d50edfc48e6 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/get/get.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/get/get.go @@ -529,6 +529,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e separatorWriter := &separatorWriterWrapper{Delegate: trackingWriter} w := printers.GetNewTabWriter(separatorWriter) + allResourcesNamespaced := !o.AllNamespaces for ix := range objs { var mapping *meta.RESTMapping var info *resource.Info @@ -540,6 +541,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e mapping = info.Mapping } + allResourcesNamespaced = allResourcesNamespaced && info.Namespaced() printWithNamespace := o.AllNamespaces if mapping != nil && mapping.Scope.Name() == meta.RESTScopeNameRoot { @@ -583,7 +585,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e w.Flush() if trackingWriter.Written == 0 && !o.IgnoreNotFound && len(allErrs) == 0 { // if we wrote no output, and had no errors, and are not ignoring NotFound, be sure we output something - if !o.AllNamespaces { + if allResourcesNamespaced { fmt.Fprintln(o.ErrOut, fmt.Sprintf("No resources found in %s namespace.", o.Namespace)) } else { fmt.Fprintln(o.ErrOut, fmt.Sprintf("No resources found")) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go index 7b3b1b974dc..e3025d17e6f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go @@ -591,6 +591,32 @@ func TestNoBlankLinesForGetAll(t *testing.T) { } } +func TestNotFoundMessageForGetNonNamespacedResources(t *testing.T) { + tf := cmdtesting.NewTestFactory().WithNamespace("test") + defer tf.Cleanup() + + codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...) + tf.UnstructuredClient = &fake.RESTClient{ + NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer, + Resp: &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: emptyTableObjBody(codec)}, + } + + streams, _, buf, errbuf := genericclioptions.NewTestIOStreams() + cmd := NewCmdGet("kubectl", tf, streams) + cmd.SetOutput(buf) + cmd.Run(cmd, []string{"persistentvolumes"}) + + expected := `` + if e, a := expected, buf.String(); e != a { + t.Errorf("expected\n%v\ngot\n%v", e, a) + } + expectedErr := `No resources found +` + if e, a := expectedErr, errbuf.String(); e != a { + t.Errorf("expectedErr\n%v\ngot\n%v", e, a) + } +} + func TestGetObjectsShowLabels(t *testing.T) { pods, _, _ := cmdtesting.TestData()