diff --git a/hack/test-cmd.sh b/hack/test-cmd.sh index f19497b37b1..040a3f44efa 100755 --- a/hack/test-cmd.sh +++ b/hack/test-cmd.sh @@ -775,8 +775,7 @@ __EOF__ kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'mock:' # Command # kubectl create -f $file "${kube_flags[@]}" # test fails here now - # TODO: test get when PR "Fix get with List #14888" is merged - # kubectl get -f $file "${kube_flags[@]}" + kubectl get -f $file "${kube_flags[@]}" kubectl describe -f $file "${kube_flags[@]}" # Command # TODO: remove --validate=false when PR "Add validate support for list kind #14726" is merged diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index 04d8fb96f07..2b434cb0b02 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -220,7 +220,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string } // use the default printer for each object - return b.Do().Visit(func(r *resource.Info, err error) error { + return b.Flatten().Do().Visit(func(r *resource.Info, err error) error { if err != nil { return err } diff --git a/pkg/kubectl/cmd/get_test.go b/pkg/kubectl/cmd/get_test.go index 0622d00f24b..de21ba41118 100644 --- a/pkg/kubectl/cmd/get_test.go +++ b/pkg/kubectl/cmd/get_test.go @@ -86,29 +86,29 @@ func testData() (*api.PodList, *api.ServiceList, *api.ReplicationControllerList) } func testComponentStatusData() *api.ComponentStatusList { - good := &api.ComponentStatus{ + good := api.ComponentStatus{ Conditions: []api.ComponentCondition{ {Type: api.ComponentHealthy, Status: api.ConditionTrue, Message: "ok", Error: "nil"}, }, + ObjectMeta: api.ObjectMeta{Name: "servergood", Namespace: "test"}, } - good.Name = "servergood" - bad := &api.ComponentStatus{ + bad := api.ComponentStatus{ Conditions: []api.ComponentCondition{ {Type: api.ComponentHealthy, Status: api.ConditionFalse, Message: "", Error: "bad status: 500"}, }, + ObjectMeta: api.ObjectMeta{Name: "serverbad", Namespace: "test"}, } - bad.Name = "serverbad" - unknown := &api.ComponentStatus{ + unknown := api.ComponentStatus{ Conditions: []api.ComponentCondition{ {Type: api.ComponentHealthy, Status: api.ConditionUnknown, Message: "", Error: "fizzbuzz error"}, }, + ObjectMeta: api.ObjectMeta{Name: "serverunknown", Namespace: "test"}, } - unknown.Name = "serverunknown" return &api.ComponentStatusList{ - Items: []api.ComponentStatus{*good, *bad, *unknown}, + Items: []api.ComponentStatus{good, bad, unknown}, } } @@ -314,16 +314,33 @@ func TestGetListObjects(t *testing.T) { cmd.SetOutput(buf) cmd.Run(cmd, []string{"pods"}) - expected := []runtime.Object{pods} + expected, err := extractResourceList([]runtime.Object{pods}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } actual := tf.Printer.(*testPrinter).Objects if !reflect.DeepEqual(expected, actual) { - t.Errorf("unexpected object: %#v %#v", expected, actual) + t.Errorf("unexpected object: expected %#v, got %#v", expected, actual) } if len(buf.String()) == 0 { t.Errorf("unexpected empty output") } } +func extractResourceList(objs []runtime.Object) ([]runtime.Object, error) { + finalObjs := []runtime.Object{} + for _, obj := range objs { + items, err := runtime.ExtractList(obj) + if err != nil { + return nil, err + } + for _, item := range items { + finalObjs = append(finalObjs, item) + } + } + return finalObjs, nil +} + func TestGetAllListObjects(t *testing.T) { pods, _, _ := testData() @@ -341,7 +358,10 @@ func TestGetAllListObjects(t *testing.T) { cmd.Flags().Set("show-all", "true") cmd.Run(cmd, []string{"pods"}) - expected := []runtime.Object{pods} + expected, err := extractResourceList([]runtime.Object{pods}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } actual := tf.Printer.(*testPrinter).Objects if !reflect.DeepEqual(expected, actual) { t.Errorf("unexpected object: %#v %#v", expected, actual) @@ -367,10 +387,13 @@ func TestGetListComponentStatus(t *testing.T) { cmd.SetOutput(buf) cmd.Run(cmd, []string{"componentstatuses"}) - expected := []runtime.Object{statuses} + expected, err := extractResourceList([]runtime.Object{statuses}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } actual := tf.Printer.(*testPrinter).Objects if !reflect.DeepEqual(expected, actual) { - t.Errorf("unexpected object: %#v %#v", expected, actual) + t.Errorf("unexpected object: expected %#v, got %#v", expected, actual) } if len(buf.String()) == 0 { t.Errorf("unexpected empty output") @@ -403,7 +426,10 @@ func TestGetMultipleTypeObjects(t *testing.T) { cmd.SetOutput(buf) cmd.Run(cmd, []string{"pods,services"}) - expected := []runtime.Object{pods, svc} + expected, err := extractResourceList([]runtime.Object{pods, svc}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } actual := tf.Printer.(*testPrinter).Objects if !reflect.DeepEqual(expected, actual) { t.Errorf("unexpected object: %#v", actual) @@ -504,7 +530,10 @@ func TestGetMultipleTypeObjectsWithSelector(t *testing.T) { cmd.Flags().Set("selector", "a=b") cmd.Run(cmd, []string{"pods,services"}) - expected := []runtime.Object{pods, svc} + expected, err := extractResourceList([]runtime.Object{pods, svc}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } actual := tf.Printer.(*testPrinter).Objects if !reflect.DeepEqual(expected, actual) { t.Errorf("unexpected object: %#v", actual)