diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index f390385b095..4adfb65742c 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -248,6 +248,23 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string sorting, err := cmd.Flags().GetString("sort-by") var sorter *kubectl.RuntimeSort if err == nil && len(sorting) > 0 && len(objs) > 1 { + clientConfig, err := f.ClientConfig() + if err != nil { + return err + } + + version, err := cmdutil.OutputVersion(cmd, clientConfig.GroupVersion) + if err != nil { + return err + } + + for ix := range infos { + objs[ix], err = infos[ix].Mapping.ConvertToVersion(infos[ix].Object, version.String()) + if err != nil { + return err + } + } + // TODO: questionable if sorter, err = kubectl.SortObjects(f.Decoder(true), objs, sorting); err != nil { return err @@ -262,10 +279,13 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string for ix := range objs { var mapping *meta.RESTMapping + var original runtime.Object if sorter != nil { mapping = infos[sorter.OriginalPosition(ix)].Mapping + original = infos[sorter.OriginalPosition(ix)].Object } else { mapping = infos[ix].Mapping + original = infos[ix].Object } if printer == nil || lastMapping == nil || mapping == nil || mapping.Resource != lastMapping.Resource { printer, err = f.PrinterForMapping(cmd, mapping, allNamespaces) @@ -275,12 +295,12 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string lastMapping = mapping } if _, found := printer.(*kubectl.HumanReadablePrinter); found { - if err := printer.PrintObj(objs[ix], w); err != nil { + if err := printer.PrintObj(original, w); err != nil { return err } continue } - if err := printer.PrintObj(objs[ix], w); err != nil { + if err := printer.PrintObj(original, w); err != nil { return err } } diff --git a/pkg/kubectl/cmd/get_test.go b/pkg/kubectl/cmd/get_test.go index fd06eea3126..be7646bd9c6 100644 --- a/pkg/kubectl/cmd/get_test.go +++ b/pkg/kubectl/cmd/get_test.go @@ -273,6 +273,56 @@ func TestGetObjects(t *testing.T) { } } +func TestGetSortedObjects(t *testing.T) { + pods := &api.PodList{ + ListMeta: unversioned.ListMeta{ + ResourceVersion: "15", + }, + Items: []api.Pod{ + { + ObjectMeta: api.ObjectMeta{Name: "c", Namespace: "test", ResourceVersion: "10"}, + Spec: apitesting.DeepEqualSafePodSpec(), + }, + { + ObjectMeta: api.ObjectMeta{Name: "b", Namespace: "test", ResourceVersion: "11"}, + Spec: apitesting.DeepEqualSafePodSpec(), + }, + { + ObjectMeta: api.ObjectMeta{Name: "a", Namespace: "test", ResourceVersion: "9"}, + Spec: apitesting.DeepEqualSafePodSpec(), + }, + }, + } + + f, tf, codec := NewAPIFactory() + tf.Printer = &testPrinter{} + tf.Client = &fake.RESTClient{ + Codec: codec, + Resp: &http.Response{StatusCode: 200, Body: objBody(codec, pods)}, + } + tf.Namespace = "test" + tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &unversioned.GroupVersion{Version: "v1"}}} + + buf := bytes.NewBuffer([]byte{}) + cmd := NewCmdGet(f, buf) + cmd.SetOutput(buf) + + // sorting with metedata.name + cmd.Flags().Set("sort-by", ".metadata.name") + cmd.Run(cmd, []string{"pods"}) + + // expect sorted: a,b,c + expected := []runtime.Object{&pods.Items[2], &pods.Items[1], &pods.Items[0]} + actual := tf.Printer.(*testPrinter).Objects + if !reflect.DeepEqual(expected, actual) { + t.Errorf("unexpected object: %#v", actual) + } + if len(buf.String()) == 0 { + t.Errorf("unexpected empty output") + } + +} + func TestGetObjectsIdentifiedByFile(t *testing.T) { pods, _, _ := testData()