Skip to content

Commit 818f011

Browse files
committed
Change not found output when getting non namespaced resources
Signed-off-by: Riccardo Piccoli <[email protected]>
1 parent 5ea2d69 commit 818f011

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/get/get.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
529529
separatorWriter := &separatorWriterWrapper{Delegate: trackingWriter}
530530

531531
w := printers.GetNewTabWriter(separatorWriter)
532+
allResourcesNamespaced := !o.AllNamespaces
532533
for ix := range objs {
533534
var mapping *meta.RESTMapping
534535
var info *resource.Info
@@ -540,6 +541,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
540541
mapping = info.Mapping
541542
}
542543

544+
allResourcesNamespaced = allResourcesNamespaced && info.Namespaced()
543545
printWithNamespace := o.AllNamespaces
544546

545547
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
583585
w.Flush()
584586
if trackingWriter.Written == 0 && !o.IgnoreNotFound && len(allErrs) == 0 {
585587
// if we wrote no output, and had no errors, and are not ignoring NotFound, be sure we output something
586-
if !o.AllNamespaces {
588+
if allResourcesNamespaced {
587589
fmt.Fprintln(o.ErrOut, fmt.Sprintf("No resources found in %s namespace.", o.Namespace))
588590
} else {
589591
fmt.Fprintln(o.ErrOut, fmt.Sprintf("No resources found"))

staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,32 @@ func TestNoBlankLinesForGetAll(t *testing.T) {
591591
}
592592
}
593593

594+
func TestNotFoundMessageForGetNonNamespacedResources(t *testing.T) {
595+
tf := cmdtesting.NewTestFactory().WithNamespace("test")
596+
defer tf.Cleanup()
597+
598+
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
599+
tf.UnstructuredClient = &fake.RESTClient{
600+
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
601+
Resp: &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: emptyTableObjBody(codec)},
602+
}
603+
604+
streams, _, buf, errbuf := genericclioptions.NewTestIOStreams()
605+
cmd := NewCmdGet("kubectl", tf, streams)
606+
cmd.SetOutput(buf)
607+
cmd.Run(cmd, []string{"persistentvolumes"})
608+
609+
expected := ``
610+
if e, a := expected, buf.String(); e != a {
611+
t.Errorf("expected\n%v\ngot\n%v", e, a)
612+
}
613+
expectedErr := `No resources found
614+
`
615+
if e, a := expectedErr, errbuf.String(); e != a {
616+
t.Errorf("expectedErr\n%v\ngot\n%v", e, a)
617+
}
618+
}
619+
594620
func TestGetObjectsShowLabels(t *testing.T) {
595621
pods, _, _ := cmdtesting.TestData()
596622

0 commit comments

Comments
 (0)