Skip to content

Commit 7013827

Browse files
committed
Stop special-casing tables in kubectl get to print 'No resources found'
1 parent 1c3aded commit 7013827

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

pkg/kubectl/cmd/get/get.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,6 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
487487

488488
objs := make([]runtime.Object, len(infos))
489489
for ix := range infos {
490-
// TODO: remove this and just pass the table objects to the printer opaquely once `info.Object.(*metav1beta1.Table)` checking is removed below
491-
if o.ServerPrint {
492-
table, err := decodeIntoTable(infos[ix].Object)
493-
if err == nil {
494-
infos[ix].Object = table
495-
}
496-
}
497490
objs[ix] = infos[ix].Object
498491
}
499492

@@ -513,8 +506,11 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
513506

514507
var printer printers.ResourcePrinter
515508
var lastMapping *meta.RESTMapping
516-
nonEmptyObjCount := 0
517-
w := utilprinters.GetNewTabWriter(o.Out)
509+
510+
// track if we write any output
511+
trackingWriter := &trackingWriterWrapper{Delegate: o.Out}
512+
513+
w := utilprinters.GetNewTabWriter(trackingWriter)
518514
for ix := range objs {
519515
var mapping *meta.RESTMapping
520516
var info *resource.Info
@@ -526,16 +522,6 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
526522
mapping = info.Mapping
527523
}
528524

529-
// if dealing with a table that has no rows, skip remaining steps
530-
// and avoid printing an unnecessary newline
531-
if table, isTable := info.Object.(*metav1beta1.Table); isTable {
532-
if len(table.Rows) == 0 {
533-
continue
534-
}
535-
}
536-
537-
nonEmptyObjCount++
538-
539525
printWithNamespace := o.AllNamespaces
540526

541527
if mapping != nil && mapping.Scope.Name() == meta.RESTScopeNameRoot {
@@ -582,12 +568,23 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
582568
}
583569
}
584570
w.Flush()
585-
if nonEmptyObjCount == 0 && !o.IgnoreNotFound && len(allErrs) == 0 {
571+
if trackingWriter.Written == 0 && !o.IgnoreNotFound && len(allErrs) == 0 {
572+
// if we wrote no output, and had no errors, and are not ignoring NotFound, be sure we output something
586573
fmt.Fprintln(o.ErrOut, "No resources found.")
587574
}
588575
return utilerrors.NewAggregate(allErrs)
589576
}
590577

578+
type trackingWriterWrapper struct {
579+
Delegate io.Writer
580+
Written int
581+
}
582+
583+
func (t *trackingWriterWrapper) Write(p []byte) (n int, err error) {
584+
t.Written += len(p)
585+
return t.Delegate.Write(p)
586+
}
587+
591588
// raw makes a simple HTTP request to the provided path on the server using the default
592589
// credentials.
593590
func (o *GetOptions) raw(f cmdutil.Factory) error {

0 commit comments

Comments
 (0)