Inject Docker LABEL into Kubernetes Metadata Annotation

11/7/2018

I have a docker image that I'm running in my Kubernetes clusters, the image has a label tag with the repository, thats being added at build time:

docker build . --label "repo=<repo_name>" tag <tag>

I have a kubenetes deployment that looks something like this:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: my-cool-app
  name: my-cool-app
spec:
  replicas: 1
  selector:
    matchLabels:
      run: my-cool-app
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: my-cool-app
    spec:
      containers:
      - image: <imagename>:v1
        name: my-cool-app
        resources: {}

I'm wondering is it possible to extract the docker image label, and add that as a metadata label in my kubernetes spec?

-- user2019182
docker
kubernetes

1 Answer

11/8/2018

It is not possible to extract it, but you can attach this in the deployment you used. For example if you use Helm chart, you can use one variable for metadate and for image. All you need to do, is to update the value.yaml. It might look like:

value.yaml

my-cool-app-tag: v1

my-cool-app.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: my-cool-app
  name: my-cool-app
spec:
  replicas: 1
  selector:
    matchLabels:
      run: my-cool-app
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: my-cool-app
        tag-lable: {{ .value.my-cool-app-tag }}
    spec:
      containers:
      - image: <imagename>:{{ .value.my-cool-app-tag }}
        name: my-cool-app
        resources: {}
-- Nick Rak
Source: StackOverflow