kubernetes: Cannot sort multiple kinds with kubectl get

What happened:

I am able to kubectl get two kinds at once by separating them with comma: kubectl get cm,secret

However, I get an error when I try to do the same with sorting: --sort-by='{.metadata.name}'

What you expected to happen:

I expected the same objects returned in sorted order.

How to reproduce it (as minimally and precisely as possible):

  1. Create some objects in a namespace:

    kubectl create ns sort
    kubectl -n sort create cm one
    kubectl -n sort create cm two
    kubectl -n sort create secret generic three
    kubectl -n sort create secret generic four
    
  2. View them together as a table:

    $ kubectl -n sort get cm,secret
    NAME            DATA   AGE
    configmap/one   0      2m31s
    configmap/two   0      2m28s
    
    NAME                         TYPE                                  DATA   AGE
    secret/default-token-cnw58   kubernetes.io/service-account-token   3      3m25s
    secret/three                 Opaque                                0      2m21s
    secret/four                  Opaque                                0      2m17s
    
  3. View them separately with sorting:

    $ kubectl -n sort get cm --sort-by='{.metadata.name}'
    NAME   DATA   AGE
    one    0      58s
    two    0      55s
    
    $ kubectl -n sort get secret --sort-by='{.metadata.name}'
    NAME                  TYPE                                  DATA   AGE
    default-token-cnw58   kubernetes.io/service-account-token   3      116s
    four                  Opaque                                0      48s
    three                 Opaque                                0      52s
    
  4. Get an error when trying to view them together with sorting:

    $ kubectl -n sort get cm,secret --sort-by='{.metadata.name}'
    error: couldn't find any field with path "{.metadata.name}" in the list of objects
    

Environment:

  • Kubernetes version (use kubectl version):
    Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.0", GitCommit:"c2b5237ccd9c0f1d600d3072634ca66cefdf272f", GitTreeState:"clean", BuildDate:"2021-08-04T17:56:19Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"darwin/amd64"}
    Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.3+k3s1", GitCommit:"1d1f220fbee9cdeb5416b76b707dde8c231121f2", GitTreeState:"clean", BuildDate:"2021-07-22T20:52:14Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}
    
  • Install tools: k3d, brew install kubernetes-cli

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (14 by maintainers)

Most upvoted comments

I investigated this issue and could have reproduced it. I realized that when multiple resources are requested, data type is different than only one resource is requested and that’s why this command kubectl -n sort get cm,secret --sort-by={.metadata.name} can not sort with {.metadata.name}.

However, when I tried sorting with this {..metadata.name} json path, it works expected and prints sorted values;

kubectl -n sort get cm,secret --sort-by={..metadata.name}

I’m closing this bug and feel free to open if you think problem exists.

Free to assign to yourself, I’m out of bandwidth honestly.

/assign