kubectl: Use custom-columns output with maps

心已入冬 提交于 2019-12-30 11:00:16

问题


I want to get the specific value of an annotation into a kubectl custom columns field. I can get all the current annotations on a resource like so:

kubectl get pvc -o custom-columns=NAME:.metadata.name,"ANNOTATIONS":.metadata.annotations -n monitoring

This returns a map:

NAME                                 ANNOTATIONS
prometheus-k8s-db-prometheus-k8s-0   map[pv.kubernetes.io/bind-completed:yes pv.kubernetes.io/bound-by-controller:yes volume.beta.kubernetes.io/storage-provisioner:kubernetes.io/aws-ebs]
prometheus-k8s-db-prometheus-k8s-1   map[pv.kubernetes.io/bind-completed:yes pv.kubernetes.io/bound-by-controller:yes volume.beta.kubernetes.io/storage-provisioner:kubernetes.io/aws-ebs]

And considering kubectl -o custom-columns uses JSONpath to the best of my knowledge, I figured I could do this:

kubectl get pvc -o custom-columns=NAME:.metadata.name,"ANNOTATIONS":".metadata.annotations['pv.kubernetes.io/bind-completed']" -n monitoring

But it seems not. Is there a way to do this?


回答1:


Okay, I figured this out. It's easier than I thought.

Annotations is a standard JSON element when it's returned. The problem is that kubectl's JSONPath parser has problems with dots in elements, so you just have to escape them. Here's an example:

kubectl get pvc -o custom-columns=NAME:.metadata.name,"ANNOTATIONS":".metadata.annotations.pv\.kubernetes\.io/bind-completed" -n monitoring

NAME                                 ANNOTATIONS
prometheus-k8s-db-prometheus-k8s-0   yes
prometheus-k8s-db-prometheus-k8s-1   yes


来源:https://stackoverflow.com/questions/57418535/kubectl-use-custom-columns-output-with-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!