kubernetes: kubectl get nodes with JSONPath doesn't report node.kubernetes.io/unschedulable taint

What happened?

I cordoned a node, then ran the following command to list all nodes that have the node.kubernetes.io/unschedulable taint:

kubectl get nodes -o jsonpath="{.items[?(@.spec.taints[].key=='node.kubernetes.io/unschedulable')].metadata.name}"

the cordoned node was not included in the list, even though describing the node I can see that it does have the taint and the field .spec.unschedulable is set to true.

What did you expect to happen?

I expected to get metadata.name of the node that has been marked as unschedulable instead.

How can we reproduce it (as minimally and precisely as possible)?

Common

  1. Cordon a node.

Test 1

  1. Run the command:
kubectl get nodes -o jsonpath="{.items[?(@.spec.taints[].key=='node.kubernetes.io/unschedulable')].metadata.name}"
  1. The node is not listed.

Test 2

  1. Run the command:
kubectl get nodes -o jsonpath="{.items[?(@.spec.unschedulable)].metadata.name}"
  1. The node’s metadata.name is printed.

Test 3

  1. Run the command:
kubectl get nodes -ojsonpath='{}' |  jq  -r '.items[] | select(try .spec.taints[].key == "node.kubernetes.io/unschedulable") .metadata.name'
  1. The node’s metadata.name is printed.

Anything else we need to know?

THe following also works:

kubectl get nodes -ojson | jq -r '.items[] | select(try .spec.taints[].key == ("node.kubernetes.io/unschedulable")) .metadata.name'

Kubernetes version

$ kubectl version
Client Version: v1.31.0
Kustomize Version: v5.4.2
Server Version: v1.29.3

Cloud provider

Kubeadm cluster

OS version

Client running on macos, Kubernetes API server running on:

$ cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
$ uname -a
Linux master1 6.8.0-41-generic #41-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug  2 23:26:06 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux

Install tools

Container runtime (CRI) and version (if applicable)

Related plugins (CNI, CSI, …) and versions (if applicable)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (10 by maintainers)

Most upvoted comments

Jsonpath support in kubectl has limited capability and currently it may not have the features that are expected just like the one in here.

IMO, for compatibility reasons, I doubt that we can change the invalid empty array selector [] behaviour. but at the very least, maybe we can update document to clarify the behaviour.

WDYT? @liggitt

BTW: I saw there was a KEP https://github.com/kubernetes/enhancements/pull/1887 trying to clarify and enhance JSONPath syntax, just leave a reference here.

I can confirm the situation.

jsonpath expression {.items[?(@.spec.taints[].key=='node.kubernetes.io/unschedulable')].metadata.name} select the first element of taints. so if the taint with node.kubernetes.io/unschedulable key is the first element, the node will be printed.

this situation can be reproduced by a simpler example:

kubectl get node -o jsonpath='{.items[].metadata.name}'

this command will only print the first node in the cluster. I’m not very familiar with jsonpath, so I can not say it’s a bug or by designed.