Rajiv Rai
Rajiv Rai

Reputation: 245

Fetching docker image and tag as key/value pairs from values.yaml in helm k8s

I have a list of docker images which I want to pass as an environment variable to deployment.yaml

values.yaml

contributions_list:
  - image: flogo-aws
    tag: 36
  - image: flogo-awsec2
    tag: 37
  - image: flogo-awskinesis
    tag: 18
  - image: flogo-chargify
    tag: 19

deployment.yaml

apiVersion: batch/v1
kind: Job
metadata:
  name: container-image-extractor
  namespace: local-tibco-tci
  labels:
    app.cloud.tibco.com/name: container-image-extractor
spec:
  backoffLimit: 0
  template:
    metadata:
      labels:
        app.cloud.tibco.com/name: container-image-extractor
    spec:
      nodeSelector:
        kubernetes.io/os: linux
      restartPolicy: Never
      containers:
      - name: container-image-extractor
        image: reldocker.tibco.com/stratosphere/container-image-extractor
        imagePullPolicy: IfNotPresent
        env:
        - name: SOURCE_DOCKER_IMAGE
          value: "<docker_image>:<docker_tag>"    # docker image from which contents to be copied
 

My questions are as follows.

  1. Is this the correct way to pass an array of docker image and tags as an argument to deployment.yaml
  2. How would I replace <docker_image> and <docker_tag> in deployment.yaml from values.yaml and incrementally job should be triggered for each docker image and tag.

Upvotes: 0

Views: 1857

Answers (2)

Fran&#231;ois
Fran&#231;ois

Reputation: 2200

This is how I would do it, creating a job for every image in your list

{{- range .Values.contributions_list }}
apiVersion: batch/v1
kind: Job
metadata:
  name: "container-image-extractor-{{ .image }}-{{ .tag }}"
  namespace: local-tibco-tci
  labels:
    app.cloud.tibco.com/name: container-image-extractor
spec:
  backoffLimit: 0
  template:
    metadata:
      labels:
        app.cloud.tibco.com/name: container-image-extractor
    spec:
      nodeSelector:
        kubernetes.io/os: linux
      restartPolicy: Never
      containers:
      - name: container-image-extractor
        image: reldocker.tibco.com/stratosphere/container-image-extractor
        imagePullPolicy: IfNotPresent
        env:
        - name: SOURCE_DOCKER_IMAGE
          value: "{{ .image }}:{{ .tag }}"    # docker image from which contents to be copied
{{ end }}

If you use a value outside of this contribution list (release name, env, whatever), do not forget to change the scope such {{ $.Values.myjob.limits.cpu | quote }}. The $. is important :)

Edit: If you don't change the name at each iteration of the loop, it will override the configuration every time. With different names, you will have multiple jobs created.

Upvotes: 1

bells17
bells17

Reputation: 380

You need to fix a deployment.yaml as below:

{{- range $contribution := .Values.contributions_list }}
apiVersion: batch/v1
kind: Job
metadata:
  name: container-image-extractor
  namespace: local-tibco-tci
  labels:
    app.cloud.tibco.com/name: container-image-extractor
spec:
  backoffLimit: 0
  template:
    metadata:
      labels:
        app.cloud.tibco.com/name: container-image-extractor
    spec:
      nodeSelector:
        kubernetes.io/os: linux
      restartPolicy: Never
      containers:
      - name: container-image-extractor
        image: reldocker.tibco.com/stratosphere/container-image-extractor
        imagePullPolicy: IfNotPresent
        env:
        - name: SOURCE_DOCKER_IMAGE
          value: "{{ $contribution.image }}:{{ $contribution.tag }}"
{{- end }}

If you want to know helm template syntax, you can see this document

Upvotes: 0

Related Questions