From 2437b59266559103f44ed5e04caa947f2f19931c Mon Sep 17 00:00:00 2001 From: lichuqiang Date: Thu, 12 Oct 2017 19:34:15 +0800 Subject: [PATCH 1/2] fix print format of rootScoped resourced --- pkg/kubectl/cmd/get.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index 79f03b6a231..ca77c858319 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -244,7 +244,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 } @@ -493,6 +500,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. @@ -500,7 +512,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()) From 50ad6fbd0650303eacebf1d757a5228805da7f48 Mon Sep 17 00:00:00 2001 From: lichuqiang Date: Fri, 13 Oct 2017 14:24:02 +0800 Subject: [PATCH 2/2] add test case for querying rootScoped resources in cli --- hack/make-rules/test-cmd-util.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/hack/make-rules/test-cmd-util.sh b/hack/make-rules/test-cmd-util.sh index 4a094995e11..66b8a2b78e3 100755 --- a/hack/make-rules/test-cmd-util.sh +++ b/hack/make-rules/test-cmd-util.sh @@ -4165,6 +4165,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 } @@ -4846,7 +4854,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 ################