Skip to content

Instantly share code, notes, and snippets.

@anderson-custodio
Last active May 31, 2019 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anderson-custodio/535a6cc01182e25e69f9302d0b701175 to your computer and use it in GitHub Desktop.
Save anderson-custodio/535a6cc01182e25e69f9302d0b701175 to your computer and use it in GitHub Desktop.
Helm: Choose image conditionally

How to conditionally choose an image in Helm

If you want to choose an image based on a condition, you can do it with the following example:

values.yaml

image:
  repository: my-docker-repo/my-image
  tag: "latest"
  pullPolicy: Always

app:
  db: mongodb://foo:bar@0.0.0.0:1234/mydb

deployment.yaml

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  ...
spec:
  ...
  template:
    ...
    spec:
      imagePullSecrets:
        - name: {{ .Values.app.pullSecret }}
      containers:
        - name: {{ .Chart.Name }}
          {{ if hasPrefix "db2" .Values.app.db }}
          image: "{{ .Values.image.repository }}-custom:{{ .Values.image.tag }}"
          {{ else }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          {{ end }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}

** Notice that I've used the golang function hasPrefix to verify if the image name starts with db2.

Running with default value (mongodb)

When execute with default value, it will generate the default image:

helm install --dry-run --debug my-helm-chart | grep "image: "

Result: image: "my-docker-repo/my-image:latest"

Running with custom value (db2)

When execute with custom value, it will generate the custom image:

helm install \
  --set "app.db=db2://foo:bar@0.0.0.0:1234,0.0.0.1:2345/mydb?foo=bar" \
  --dry-run --debug my-helm-chart | grep "image: "

Result: image: "my-docker-repo/my-image-custom:latest"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment