Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Deployment annotation from a Kubernetes Pod

Tags:

kubernetes

Each Kubernetes deployment gets this annotation:

$ kubectl describe deployment/myapp
Name:                   myapp
Namespace:              default
CreationTimestamp:      Sat, 24 Mar 2018 23:27:42 +0100
Labels:                 app=myapp
Annotations:            deployment.kubernetes.io/revision=5

Is there a way to read that annotation (deployment.kubernetes.io/revision) from a pod that belongs to the deployment?

I tried Downward API, but that only allows to get annotations of the pod itself (not of its deployment).

like image 675
Kir Avatar asked May 01 '18 17:05

Kir


3 Answers

kubectl get pod POD_NAME -o jsonpath='{.metadata.annotations}'
like image 83
StanislavKo Avatar answered Oct 14 '22 07:10

StanislavKo


It has been a long time but here is what I do to get a specific annotation :

kubectl get ing test -o jsonpath='{.metadata.annotations.kubernetes\.io/ingress\.class}'

So for you it would be :

kubectl get deploy myapp -o jsonpath='{.metadata.annotations.deployment\.kubernetes\.io/revision}'

I hope it helps.

like image 38
KrustyHack Avatar answered Oct 14 '22 09:10

KrustyHack


As an alternative, you can leverage both using a kubectl selector to query all pods with label app=myapp and jq to query and format the resulting json to get you the name and annotations for each of the pods

kubectl get po -l app=myapp -o=json | jq '[. | {pod: .items[].metadata}][] | {name: .pod.name, annotations: .pod.annotations}'
like image 43
iamnicoj Avatar answered Oct 14 '22 09:10

iamnicoj