Change not found output when getting non namespaced resources

Signed-off-by: Riccardo Piccoli <riccardo.piccoli@gmail.com>
This commit is contained in:
Riccardo Piccoli 2020-02-13 17:28:06 +01:00
parent 5ea2d69ccd
commit 818f011a2e
2 changed files with 29 additions and 1 deletions

View File

@ -529,6 +529,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
separatorWriter := &separatorWriterWrapper{Delegate: trackingWriter} separatorWriter := &separatorWriterWrapper{Delegate: trackingWriter}
w := printers.GetNewTabWriter(separatorWriter) w := printers.GetNewTabWriter(separatorWriter)
allResourcesNamespaced := !o.AllNamespaces
for ix := range objs { for ix := range objs {
var mapping *meta.RESTMapping var mapping *meta.RESTMapping
var info *resource.Info var info *resource.Info
@ -540,6 +541,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
mapping = info.Mapping mapping = info.Mapping
} }
allResourcesNamespaced = allResourcesNamespaced && info.Namespaced()
printWithNamespace := o.AllNamespaces printWithNamespace := o.AllNamespaces
if mapping != nil && mapping.Scope.Name() == meta.RESTScopeNameRoot { 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() w.Flush()
if trackingWriter.Written == 0 && !o.IgnoreNotFound && len(allErrs) == 0 { 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 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)) fmt.Fprintln(o.ErrOut, fmt.Sprintf("No resources found in %s namespace.", o.Namespace))
} else { } else {
fmt.Fprintln(o.ErrOut, fmt.Sprintf("No resources found")) fmt.Fprintln(o.ErrOut, fmt.Sprintf("No resources found"))

View File

@ -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) { func TestGetObjectsShowLabels(t *testing.T) {
pods, _, _ := cmdtesting.TestData() pods, _, _ := cmdtesting.TestData()