diff --git a/pkg/kubectl/cmd/get/get.go b/pkg/kubectl/cmd/get/get.go index 40bebe837f5..ad6554c0ef8 100644 --- a/pkg/kubectl/cmd/get/get.go +++ b/pkg/kubectl/cmd/get/get.go @@ -219,6 +219,9 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri o.IncludeUninitialized = cmdutil.ShouldIncludeUninitialized(cmd, false) + if resource.MultipleTypesRequested(args) { + o.PrintFlags.EnsureWithKind() + } o.ToPrinter = func(mapping *meta.RESTMapping, withNamespace bool) (printers.ResourcePrinterFunc, error) { // make a new copy of current flags / opts before mutating printFlags := o.PrintFlags.Copy() @@ -229,9 +232,7 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri printFlags.UseOpenAPIColumns(apiSchema, mapping) } } - if resource.MultipleTypesRequested(args) { - printFlags.EnsureWithKind(mapping.GroupVersionKind.GroupKind()) - } + printFlags.SetKind(mapping.GroupVersionKind.GroupKind()) } if withNamespace { printFlags.EnsureWithNamespace() diff --git a/pkg/kubectl/cmd/get/get_flags.go b/pkg/kubectl/cmd/get/get_flags.go index 4540100b121..e36321c8c13 100644 --- a/pkg/kubectl/cmd/get/get_flags.go +++ b/pkg/kubectl/cmd/get/get_flags.go @@ -41,6 +41,11 @@ type PrintFlags struct { OutputFormat *string } +// SetKind sets the Kind option of humanreadable flags +func (f *PrintFlags) SetKind(kind schema.GroupKind) { + f.HumanReadableFlags.SetKind(kind) +} + // EnsureWithNamespace ensures that humanreadable flags return // a printer capable of printing with a "namespace" column. func (f *PrintFlags) EnsureWithNamespace() error { @@ -49,8 +54,8 @@ func (f *PrintFlags) EnsureWithNamespace() error { // EnsureWithKind ensures that humanreadable flags return // a printer capable of including resource kinds. -func (f *PrintFlags) EnsureWithKind(kind schema.GroupKind) error { - return f.HumanReadableFlags.EnsureWithKind(kind) +func (f *PrintFlags) EnsureWithKind() error { + return f.HumanReadableFlags.EnsureWithKind() } // Copy returns a copy of PrintFlags for mutation diff --git a/pkg/kubectl/cmd/get/get_test.go b/pkg/kubectl/cmd/get/get_test.go index 05fc905673f..bdea31c0671 100644 --- a/pkg/kubectl/cmd/get/get_test.go +++ b/pkg/kubectl/cmd/get/get_test.go @@ -329,6 +329,60 @@ foo 0/0 0 } } +func TestGetObjectsShowKind(t *testing.T) { + pods, _, _ := testData() + + tf := cmdtesting.NewTestFactory() + defer tf.Cleanup() + codec := legacyscheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...) + + tf.UnstructuredClient = &fake.RESTClient{ + NegotiatedSerializer: unstructuredSerializer, + Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods.Items[0])}, + } + tf.Namespace = "test" + + streams, _, buf, _ := genericclioptions.NewTestIOStreams() + cmd := NewCmdGet("kubectl", tf, streams) + cmd.SetOutput(buf) + cmd.Flags().Set("show-kind", "true") + cmd.Run(cmd, []string{"pods", "foo"}) + + expected := `NAME READY STATUS RESTARTS AGE +pod/foo 0/0 0 +` + if e, a := expected, buf.String(); e != a { + t.Errorf("expected %v, got %v", e, a) + } +} + +func TestGetObjectsShowLabels(t *testing.T) { + pods, _, _ := testData() + + tf := cmdtesting.NewTestFactory() + defer tf.Cleanup() + codec := legacyscheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...) + + tf.UnstructuredClient = &fake.RESTClient{ + NegotiatedSerializer: unstructuredSerializer, + Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods.Items[0])}, + } + tf.Namespace = "test" + + streams, _, buf, _ := genericclioptions.NewTestIOStreams() + cmd := NewCmdGet("kubectl", tf, streams) + cmd.SetOutput(buf) + cmd.Flags().Set("show-labels", "true") + cmd.Run(cmd, []string{"pods", "foo"}) + + expected := `NAME READY STATUS RESTARTS AGE LABELS +foo 0/0 0 +` + if e, a := expected, buf.String(); e != a { + t.Errorf("expected %v, got %v", e, a) + } +} + func TestGetObjectIgnoreNotFound(t *testing.T) { initTestErrorHandler(t) diff --git a/pkg/kubectl/cmd/get/humanreadable_flags.go b/pkg/kubectl/cmd/get/humanreadable_flags.go index 5a185b84524..43c303a5f92 100644 --- a/pkg/kubectl/cmd/get/humanreadable_flags.go +++ b/pkg/kubectl/cmd/get/humanreadable_flags.go @@ -43,13 +43,14 @@ type HumanPrintFlags struct { WithNamespace bool } -// EnsureWithKind sets the provided GroupKind humanreadable value. -// If the kind received is non-empty, the "showKind" humanreadable -// printer option is set to true. -func (f *HumanPrintFlags) EnsureWithKind(kind schema.GroupKind) error { - showKind := !kind.Empty() - +// SetKind sets the Kind option +func (f *HumanPrintFlags) SetKind(kind schema.GroupKind) { f.Kind = kind +} + +// EnsureWithKind sets the "Showkind" humanreadable option to true. +func (f *HumanPrintFlags) EnsureWithKind() error { + showKind := true f.ShowKind = &showKind return nil }