Naxi
Naxi

Reputation: 2026

JSONPath to list all nodes in ready state except the ones which are tainted?

I want to list all nodes which are in ready state except the ones which have any kind of taint on them. How can I achieve this using jsonpath ?

I tried below statement taken from k8s doc but it doesn't print what I want. I am looking for output such as -- node01 node02. There is no master node in the output as it has a taint on it. What kind of taint is not really significant here.

JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
 && kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"

Upvotes: 2

Views: 2679

Answers (2)

Girdhar Singh Rathore
Girdhar Singh Rathore

Reputation: 5605

You can get it using -o jsonpath and awk

$ kubectl get nodes
NAME           STATUS                     ROLES    AGE   VERSION
controlplane   Ready                      master   27m   v1.19.0
node01         Ready,SchedulingDisabled   <none>   26m   v1.19.0
node02         Ready                      <none>   26m   v1.19.0
node03         Ready                      <none>   26m   v1.19.0

controlplane and node01 are ready but have taints NoSchedule

To list all the nodes with name, status Ready=True and taints

$ kubectl get nodes -o jsonpath='{range .items[*]} {.metadata.name} {" "} {.status.conditions[?(@.type=="Ready")].status} {" "} {.spec.taints} {"\n"} {end}' 
 controlplane   True   [{"effect":"NoSchedule","key":"node-role.kubernetes.io/master"}] 
  node01        True   [{"effect":"NoSchedule","key":"node.kubernetes.io/unschedulable","timeAdded":"2021-04-03T12:22:56Z"}] 
  node02        True    
  node03        True  

Using awk to print the Ready Nodes and do not have taints NoSchedule

$ kubectl get nodes -o jsonpath='{range .items[*]} {.metadata.name} {" "} {.status.conditions[?(@.type=="Ready"].status} {" "} {.spec.taints} {"\n"} {end}'  | awk '$2=="True"' | awk '$3  !~/"NoSchedule"/ { print $1}'
node02
node03

Upvotes: 1

acid_fuji
acid_fuji

Reputation: 6853

I have successfully listed my nodes that are ready and not tainted using jq.

Here you have all the nodes:

$ kubectl get nodes

gke-standard-cluster-1-default-pool-9c101360-9lvw   Ready    <none>   31s   v1.13.11-gke.9
gke-standard-cluster-1-default-pool-9c101360-fdhr   Ready    <none>   30s   v1.13.11-gke.9
gke-standard-cluster-1-default-pool-9c101360-gq9c   Ready    <none>   31s   v1.13.11-gke.

Here I have tainted one node:

$ kubectl taint node gke-standard-cluster-1-default-pool-9c101360-9lvw key=value:NoSchedule

node/gke-standard-cluster-1-default-pool-9c101360-9lvw tainted

And finally a command that list the not tainted and ready nodes:

$ kubectl get nodes -o json | jq -r '.items[] | select(.spec.taints|not) | select(.status.conditions[].reason=="KubeletReady" and .status.conditions[].status=="True") | .metadata.name'

gke-standard-cluster-1-default-pool-9c101360-fdhr
gke-standard-cluster-1-default-pool-9c101360-gq9c

Upvotes: 8

Related Questions