diff --git a/hack/make-rules/test-cmd-util.sh b/hack/make-rules/test-cmd-util.sh index 54b5757bdda..da2fde186c0 100644 --- a/hack/make-rules/test-cmd-util.sh +++ b/hack/make-rules/test-cmd-util.sh @@ -1106,6 +1106,10 @@ run_kubectl_get_tests() { # Post-condition: The text "No resources found" should be part of the output kube::test::if_has_string "${output_message}" 'No resources found' # Command + output_message=$(kubectl get pods --ignore-not-found 2>&1 "${kube_flags[@]}") + # Post-condition: The text "No resources found" should not be part of the output + kube::test::if_has_not_string "${output_message}" 'No resources found' + # Command output_message=$(kubectl get pods 2>&1 "${kube_flags[@]}" -o wide) # Post-condition: The text "No resources found" should be part of the output kube::test::if_has_string "${output_message}" 'No resources found' diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index 6d8968aa8bc..42b3bcb3d58 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -243,13 +243,10 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ } // print the current object - filteredResourceCount := 0 if !isWatchOnly { if err := printer.PrintObj(obj, out); err != nil { return fmt.Errorf("unable to output the provided object: %v", err) } - filteredResourceCount++ - cmdutil.PrintFilterCount(filteredResourceCount, mapping.Resource, filterOpts) } // print watched changes @@ -259,7 +256,6 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ } first := true - filteredResourceCount = 0 intr := interrupt.New(nil, w.Stop) intr.Run(func() error { _, err := watch.Until(0, w, func(e watch.Event) (bool, error) { @@ -272,8 +268,6 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ if err != nil { return false, err } - filteredResourceCount++ - cmdutil.PrintFilterCount(filteredResourceCount, mapping.Resource, filterOpts) return false, nil }) return err @@ -326,11 +320,6 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ return utilerrors.Reduce(utilerrors.Flatten(utilerrors.NewAggregate(errs))) } - res := "" - if len(infos) > 0 { - res = infos[0].ResourceMapping().Resource - } - var obj runtime.Object if !singleItemImplied || len(infos) > 1 { // we have more than one item, so coerce all items into a list @@ -351,7 +340,7 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ isList := meta.IsListType(obj) if isList { - filteredResourceCount, items, err := cmdutil.FilterResourceList(obj, filterFuncs, filterOpts) + _, items, err := cmdutil.FilterResourceList(obj, filterFuncs, filterOpts) if err != nil { return err } @@ -375,23 +364,17 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ if err := printer.PrintObj(list, out); err != nil { errs = append(errs, err) } - - cmdutil.PrintFilterCount(filteredResourceCount, res, filterOpts) return utilerrors.Reduce(utilerrors.Flatten(utilerrors.NewAggregate(errs))) } - filteredResourceCount := 0 if isFiltered, err := filterFuncs.Filter(obj, filterOpts); !isFiltered { if err != nil { glog.V(2).Infof("Unable to filter resource: %v", err) } else if err := printer.PrintObj(obj, out); err != nil { errs = append(errs, err) } - } else if isFiltered { - filteredResourceCount++ } - cmdutil.PrintFilterCount(filteredResourceCount, res, filterOpts) return utilerrors.Reduce(utilerrors.Flatten(utilerrors.NewAggregate(errs))) } @@ -401,9 +384,6 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ if err != nil { allErrs = append(allErrs, err) } - if len(infos) == 0 && len(allErrs) == 0 && !options.IgnoreNotFound { - outputEmptyListWarning(errOut) - } objs := make([]runtime.Object, len(infos)) for ix := range infos { @@ -426,12 +406,12 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ printer = nil var lastMapping *meta.RESTMapping w := printers.GetNewTabWriter(out) - filteredResourceCount := 0 if resource.MultipleTypesRequested(args) || cmdutil.MustPrintWithKinds(objs, infos, sorter) { showKind = true } + filteredResourceCount := 0 for ix := range objs { var mapping *meta.RESTMapping var original runtime.Object @@ -445,7 +425,6 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ if printer == nil || lastMapping == nil || mapping == nil || mapping.Resource != lastMapping.Resource { if printer != nil { w.Flush() - cmdutil.PrintFilterCount(filteredResourceCount, lastMapping.Resource, filterOpts) } printer, err = f.PrinterForMapping(cmd, mapping, allNamespaces) if err != nil { @@ -517,14 +496,6 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ } } w.Flush() - if printer != nil && lastMapping != nil { - cmdutil.PrintFilterCount(filteredResourceCount, lastMapping.Resource, filterOpts) - } + cmdutil.PrintFilterCount(errOut, len(objs), filteredResourceCount, len(allErrs), "", filterOpts, options.IgnoreNotFound) return utilerrors.NewAggregate(allErrs) } - -// outputEmptyListWarning outputs a warning indicating that no items are available to display -func outputEmptyListWarning(out io.Writer) error { - _, err := fmt.Fprintf(out, "%s\n", "No resources found.") - return err -} diff --git a/pkg/kubectl/cmd/util/helpers.go b/pkg/kubectl/cmd/util/helpers.go index b84612d06f0..aafdcf7a465 100644 --- a/pkg/kubectl/cmd/util/helpers.go +++ b/pkg/kubectl/cmd/util/helpers.go @@ -698,9 +698,26 @@ func FilterResourceList(obj runtime.Object, filterFuncs kubectl.Filters, filterO return filterCount, list, nil } -func PrintFilterCount(hiddenObjNum int, resource string, options *printers.PrintOptions) { - if !options.NoHeaders && !options.ShowAll && hiddenObjNum > 0 { - glog.V(2).Infof(" info: %d completed object(s) was(were) not shown in %s list. Pass --show-all to see all objects.\n\n", hiddenObjNum, resource) +// PrintFilterCount displays informational messages based on the number of resources found, hidden, or +// config flags shown. +func PrintFilterCount(out io.Writer, found, hidden, errors int, resource string, options *printers.PrintOptions, ignoreNotFound bool) { + switch { + case errors > 0 || ignoreNotFound: + // print nothing + case found <= hidden: + if found == 0 { + fmt.Fprintln(out, "No resources found.") + } else { + fmt.Fprintln(out, "No resources found, use --show-all to see completed objects.") + } + case hidden > 0 && !options.ShowAll && !options.NoHeaders: + if glog.V(2) { + if hidden > 1 { + fmt.Fprintf(out, "info: %d objects not shown, use --show-all to see completed objects.\n", hidden) + } else { + fmt.Fprintf(out, "info: 1 object not shown, use --show-all to see completed objects.\n") + } + } } }