kubernetes / kubernetes/kubectl
Kubectl get unexpected returns "No resources found..." when sorting on nonexistent field
- Dominant language
- Go
- Stars
- 3.3k
- Forks
- 1k
- PR merge metrics
- No merged PRs in 30d
Description
**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
Contributor guide
Assessment
This issue has not been assessed yet.