Query By Annotation with kubectl

1
2
3
4
5
# service that has annotation, regardless of value
kubectl get service -A -o jsonpath='{.items[?(@.metadata.annotations.prometheus\.io/scrape)].metadata.name}'

# has annotation set to "true"
kubectl get service -A -o jsonpath='{.items[?(@.metadata.annotations.prometheus\.io/scrape=="true")].metadata.name}'

If you wanted to pull multiple values for the object, you can use range on each object found.

1
kubectl get service -A -o jsonpath='{range .items[?(@.metadata.annotations.prometheus\.io/scrape=="true")]}{ .metadata.namespace },{ .metadata.name}{"\n"}{end}'

https://fabianlee.org/2022/06/18/kubernetes-query-by-annotation-with-kubectl/

Kubectl custom columns

https://technekey.com/customizing-the-kubectl-output/

查询集群中 requests 资源

kubectl get nodes --selector=nodeLevel=aiali --selector=specification=efg1.nvga8n --selector=tce.kubernetes.io/rdmaminipod=P11 --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo {}; kubectl --kubeconfig=/home/houminwei/.kube/view-hl.config describe node | grep -A 14 "Allocated" | grep -ve Event -ve Allocated -ve percent -ve --; echo'