Get Deployment annotation from a Kubernetes Pod

1,600 views
Skip to first unread message

Kir Shatrov

unread,
May 1, 2018, 1:14:37 PM5/1/18
to Kubernetes user discussion and Q&A
Hi all,

I'm looking for some help with exposing annotations.

Each 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 expose annotations of the pod itself (not of its deployment).


David Rosenstrauch

unread,
May 1, 2018, 2:13:25 PM5/1/18
to kubernet...@googlegroups.com
kubectl get deployment myapp -o json

You could then run it through a json parser to strip out what you want.
E.g.:

kubectl get deployment -o json | jq -r '.items[].metadata.annotations' |
less

HTH,

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

David Rosenstrauch

unread,
May 1, 2018, 2:52:47 PM5/1/18
to kubernet...@googlegroups.com
Actually, my bad - I didn't read your question carefully enough. You
wanted to access that annotation, but from inside a pod.

That can be done too. I've run scripts inside a pod that query the
kubernetes api server for information like that. I don't have a code
example for that handy, unfortunately, but it would be similar to my
previous response, and look something like this:

curl https://<kubernetes api>:443/api/... | jq -r
'.items[].metadata.annotations' | less

More details here:
https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/#without-kubectl-proxy

HTH,

DR

Kir Shatrov

unread,
May 1, 2018, 3:27:46 PM5/1/18
to kubernet...@googlegroups.com
Thanks for suggestion. I know there's a REST API or kubectl available inside the pod, but I was wondering if it could be exposed in a different way, perhaps similar to Downward API.

--
You received this message because you are subscribed to the Google Groups "Kubernetes user discussion and Q&A" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kubernetes-users+unsubscribe@googlegroups.com.
To post to this group, send email to kubernetes-users@googlegroups.com.
Visit this group at https://groups.google.com/group/kubernetes-users.
For more options, visit https://groups.google.com/d/optout.

Ahmet Alp Balkan

unread,
May 1, 2018, 4:02:02 PM5/1/18
to kubernet...@googlegroups.com
I'm not entirely sure if a Pod inherits the annotations of its owner (i.e. Deployment). Based on my experimentation it doesn't.

That said, the downward API lets you read fields from Pod's spec.

In this case, a Pod created by Deployment will have an metadata.ownerReferences field (array) referring to the "ReplicaSet" created by the Pod (and the ReplicaSet seems to be inheriting annotations of the Deployment). So, using the REST API/kubectl config passed to the Pod you can probably read the annotations of the RS.

But this whole thing sounds like a hack, I would suggest explicitly passing this information in through an env var or a configmap.

To unsubscribe from this group and stop receiving emails from it, send an email to kubernetes-use...@googlegroups.com.
To post to this group, send email to kubernet...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Kubernetes user discussion and Q&A" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kubernetes-use...@googlegroups.com.
To post to this group, send email to kubernet...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages