diff --git a/hack/make-rules/test-cmd-util.sh b/hack/make-rules/test-cmd-util.sh index aa8ba947c17..bf63a2b12d8 100755 --- a/hack/make-rules/test-cmd-util.sh +++ b/hack/make-rules/test-cmd-util.sh @@ -4218,6 +4218,14 @@ run_kubectl_all_namespace_tests() { # Post-condition: valid-pod doesn't exist kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' + ### Verify flag all-namespaces is ignored for rootScoped resources + # Pre-condition: node exists + kube::test::get_object_assert nodes "{{range.items}}{{$id_field}}:{{end}}" '127.0.0.1:' + # Command + output_message=$(kubectl get nodes --all-namespaces 2>&1) + # Post-condition: output with no NAMESPACE field + kube::test::if_has_not_string "${output_message}" "NAMESPACE" + set +o nounset set +o errexit } @@ -4907,7 +4915,9 @@ runTests() { ############################ if kube::test::if_supports_resource "${pods}" ; then - record_command run_kubectl_all_namespace_tests + if kube::test::if_supports_resource "${nodes}" ; then + record_command run_kubectl_all_namespace_tests + fi fi ################ diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index 5b987741cee..8d6da273879 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -247,7 +247,14 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ } info := infos[0] mapping := info.ResourceMapping() - printer, err := f.PrinterForMapping(cmd, false, nil, mapping, allNamespaces) + + // no need to print namespace for root-scoped resources + printWithNamespace := allNamespaces + if mapping != nil && mapping.Scope.Name() == meta.RESTScopeNameRoot { + printWithNamespace = false + } + + printer, err := f.PrinterForMapping(cmd, false, nil, mapping, printWithNamespace) if err != nil { return err } @@ -497,6 +504,11 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ w.Flush() } + printWithNamespace := allNamespaces + if mapping != nil && mapping.Scope.Name() == meta.RESTScopeNameRoot { + printWithNamespace = false + } + var outputOpts *printers.OutputOptions // if cmd does not specify output format and useOpenAPIPrintColumnFlagLabel flag is true, // then get the default output options for this mapping from OpenAPI schema. @@ -504,7 +516,7 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [ outputOpts, _ = outputOptsForMappingFromOpenAPI(f, mapping) } - printer, err = f.PrinterForMapping(cmd, false, outputOpts, mapping, allNamespaces) + printer, err = f.PrinterForMapping(cmd, false, outputOpts, mapping, printWithNamespace) if err != nil { if !errs.Has(err.Error()) { errs.Insert(err.Error())