Helm Environment Variables in if else

12/30/2020

When am building the image path, this is how I want to build the image Path, where the docker registry address, I want to fetch it from the configMap.

I can't hard code the registry address in the values.yaml file because for each customer the registry address would be different and I don't want to ask customer to enter this input manually. These helm charts are deployed via argoCD, so fetching registryIP via shell and then invoking the helm command is also not an option.

I tried below code, it isn't working because the env variables will not be available in the context where image path is present.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "helm-guestbook.fullname" . }}
spec:
  template:
    metadata:
      labels:
        app: {{ template "helm-guestbook.name" . }}
        release: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          {{- if eq .Values.isOnPrem "true" }}
          image: {{ printf "%s/%s:%s" $dockerRegistryIP .Values.image.repository .Values.image.tag }}
          {{- else }}
          env:
          - name: DOCKER_REGISTRY_IP
            valueFrom:
              configMapKeyRef:
                name: docker-registry-config
                key: DOCKER_REGISTRY_IP

Any pointers on how can I solve this using helm itself ? Thanks

-- rajiv chodisetti
kubernetes
kubernetes-helm

1 Answer

12/30/2020

Check out the lookup function, https://helm.sh/docs/chart_template_guide/functions_and_pipelines/#using-the-lookup-function

Though this could get very complicated very quickly, so be careful to not overuse it.

-- coderanger
Source: StackOverflow