Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count of nodes which do not have a label?

How do I use kubectl to get K8S nodes which do not have any labels? Also , how do I fetch K8S pods which do not have any labels?

like image 405
Att A Avatar asked Dec 13 '22 11:12

Att A


2 Answers

For those who want to find resources without a specific label, no matter its value:

kubectl get ns --selector='!label_name'
like image 168
leoschet Avatar answered Dec 31 '22 17:12

leoschet


You have to leverage kubectl -o flag and go-template output:

kubectl get nodes -o go-template='{{range .items }}{{if .metadata.labels }}{{else}}{{printf "%s\n" .metadata.name}}{{ end }}{{end}}

This command will show only nodes which do not have any labels. The same can be used for pods:

kubectl get pods --all-namespaces -o go-template='{{range .items }}{{if .metadata.labels }}{{else}}{{printf "%s\n" .metadata.name}}{{ end }}{{end}}'
like image 45
Ottovsky Avatar answered Dec 31 '22 17:12

Ottovsky