kubectl icon indicating copy to clipboard operation
kubectl copied to clipboard

Kubectl get unexpected returns "No resources found..." when sorting on nonexistent field

Open brianpursley opened this issue 1 year ago • 6 comments

What happened: When using kubectl get with the --sort-by flag, kubectl said "No resources found" when I tried to sort by a label that did not exist on any pod.

Adding the label to a single pod caused the result to suddenly show all pods (including those without the label).

What you expected to happen: If the field I am sorting on does not exist, the results should still be returned using the default, unsorted output.

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

~ $ kubectl run foo --image=nginx -l team=a
pod/foo created
~ $ kubectl run bar --image=nginx -l team=b
pod/bar created
~ $ kubectl run baz --image=nginx -l team=a
pod/baz created
~ $ kubectl get pod --show-labels
NAME   READY   STATUS    RESTARTS   AGE   LABELS
bar    1/1     Running   0          14s   team=b
baz    1/1     Running   0          9s    team=a
foo    1/1     Running   0          22s   team=a
~ $ kubectl get pod --show-labels --sort-by={.metadata.labels.team}
NAME   READY   STATUS    RESTARTS   AGE   LABELS
baz    1/1     Running   0          22s   team=a
foo    1/1     Running   0          35s   team=a
bar    1/1     Running   0          27s   team=b
~ $ kubectl get pod --show-labels --sort-by={.metadata.labels.something}
No resources found in default namespace.
~ $ kubectl run xyz --image=nginx -l something=123
pod/xyz created
~ $ kubectl get pod --show-labels --sort-by={.metadata.labels.something}
NAME   READY   STATUS    RESTARTS   AGE   LABELS
foo    1/1     Running   0          66s   team=a
baz    1/1     Running   0          53s   team=a
bar    1/1     Running   0          58s   team=b
xyz    1/1     Running   0          2s    something=123

Anything else we need to know?:

Environment:

  • Kubernetes client and server versions (use kubectl version):
Client Version: v1.27.0-alpha.0.457+8f698ebeb2a467-dirty
Kustomize Version: v4.5.7
Server Version: v1.26.0-alpha.0-17-gdaa33b9c17e0cb.dev-1660582499
  • Cloud provider or hardware configuration:
  • OS (e.g: cat /etc/os-release): Ubuntu 22.04.1 LTS

brianpursley avatar Dec 23 '22 19:12 brianpursley

Good catch. I think this might be a server side printing bug https://github.com/kubernetes/kubernetes/blob/8fb423bfabe0d53934cc94c154c7da2dc3ce1332/staging/src/k8s.io/kubectl/pkg/cmd/get/get.go#L154. I wonder is this also happening -ojson, etc.

/triage accepted

ardaguclu avatar Jan 02 '23 12:01 ardaguclu

@ardaguclu --show-labels cannot be used with -o option

error: --show-labels option cannot be used with json printer

murthy95 avatar Jan 04 '23 09:01 murthy95

@brianpursley I want to work on this issue

murthy95 avatar Jan 07 '23 08:01 murthy95

/assign

manav014 avatar Jan 22 '23 11:01 manav014

Can we display a message and then display results if there are no matching labels? If this would be implemented, the output for the non-existing labels would look like this:-

~ $ kubectl get pod --show-labels --sort-by={.metadata.labels.something}
WARNING: The resource with that label could not be found. 
NAME   READY   STATUS    RESTARTS   AGE   LABELS
foo    1/1     Running   0          66s   team=a
baz    1/1     Running   0          53s   team=a
bar    1/1     Running   0          58s   team=b

manav014 avatar Jan 22 '23 15:01 manav014

@manav014 I linked a PR that solves this bug, it just needs a review. :)

philip-peterson avatar Dec 18 '23 07:12 philip-peterson