container labels in kubernetes

12/28/2018

I am building my docker image with jenkins using:

docker build --build-arg VCS_REF=$GIT_COMMIT \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
--build-arg BUILD_NUMBER=$BUILD_NUMBER -t $IMAGE_NAME\ 

I was using Docker but I am migrating to k8.

With docker I could access those labels via:

docker inspect --format "{{ index .Config.Labels \"$label\"}}" $container

How can I access those labels with Kubernetes ?

I am aware about adding those labels in .Metadata.labels of my yaml files but I don't like it that much because - it links those information to the deployment and not the container itself
- can be modified anytime
...

kubectl describe pods

Thank you

-- sam
docker
kubernetes

1 Answer

12/28/2018

Kubernetes doesn't expose that data. If it did, it would be part of the PodStatus API object (and its embedded ContainerStatus), which is one part of the Pod data that would get dumped out by kubectl get pod deployment-name-12345-abcde -o yaml.

You might consider encoding some of that data in the Docker image tag; for instance, if the CI system is building a tagged commit then use the source control tag name as the image tag, otherwise use a commit hash or sequence number. Another typical path is to use a deployment manager like Helm as the principal source of truth about deployments, and if you do that there can be a path from your CD system to Helm to Kubernetes that can pass along labels or annotations. You can also often set up software to know its own build date and source control commit ID at build time, and then expose that information via an informational-only API (like an HTTP GET /_version call or some such).

-- David Maze
Source: StackOverflow