mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Fix kubectl sort-by metadata issue.
Internal types are not supposed to have json metadata (though in kubernetes they do) as it is true with openshift types. That means sort-by must work on versioned objects for sorting, otherwise it produces "error: metadata is not found" error if it sorts internal types without json metadata. This PR converts internal types objects to versioned objects and sort-by sorts them correctly without medata error, and then it prints corresponding internal objects in sorted order.
This commit is contained in:
parent
4e00333f9b
commit
be06003e6b
@ -248,6 +248,23 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
|
|||||||
sorting, err := cmd.Flags().GetString("sort-by")
|
sorting, err := cmd.Flags().GetString("sort-by")
|
||||||
var sorter *kubectl.RuntimeSort
|
var sorter *kubectl.RuntimeSort
|
||||||
if err == nil && len(sorting) > 0 && len(objs) > 1 {
|
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
|
// TODO: questionable
|
||||||
if sorter, err = kubectl.SortObjects(f.Decoder(true), objs, sorting); err != nil {
|
if sorter, err = kubectl.SortObjects(f.Decoder(true), objs, sorting); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -262,10 +279,13 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
|
|||||||
|
|
||||||
for ix := range objs {
|
for ix := range objs {
|
||||||
var mapping *meta.RESTMapping
|
var mapping *meta.RESTMapping
|
||||||
|
var original runtime.Object
|
||||||
if sorter != nil {
|
if sorter != nil {
|
||||||
mapping = infos[sorter.OriginalPosition(ix)].Mapping
|
mapping = infos[sorter.OriginalPosition(ix)].Mapping
|
||||||
|
original = infos[sorter.OriginalPosition(ix)].Object
|
||||||
} else {
|
} else {
|
||||||
mapping = infos[ix].Mapping
|
mapping = infos[ix].Mapping
|
||||||
|
original = infos[ix].Object
|
||||||
}
|
}
|
||||||
if printer == nil || lastMapping == nil || mapping == nil || mapping.Resource != lastMapping.Resource {
|
if printer == nil || lastMapping == nil || mapping == nil || mapping.Resource != lastMapping.Resource {
|
||||||
printer, err = f.PrinterForMapping(cmd, mapping, allNamespaces)
|
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
|
lastMapping = mapping
|
||||||
}
|
}
|
||||||
if _, found := printer.(*kubectl.HumanReadablePrinter); found {
|
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
|
return err
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := printer.PrintObj(objs[ix], w); err != nil {
|
if err := printer.PrintObj(original, w); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
func TestGetObjectsIdentifiedByFile(t *testing.T) {
|
||||||
pods, _, _ := testData()
|
pods, _, _ := testData()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user