1
votes

I'm using Helm for to deploy multiple K8s deployments. In some deployments I need to include extra environment variables, but for the majority of deployment the standard env. variables are enough. I would like to have named template for those deployments that must have extra env. variables.

Can I include a named template only if the named template exist?

Something like this:

{{ range $idx, $svc := .Values.services }}
kind: Deployment
metadata:
  name: {{ $svc.name }}
spec:
  containers:
    - name: {{ $svc.name }}
      env:
        - name: JAVA_OPTS
        - value: {{ $svc.javaOpts }}
# if template_exists (print $svc.name "-env")
{{ include (print $svc.name "-env") . | indent 12 }}
# end
{{- end -}}

It's in pseudo-code. How to do the # if part?

Thank you.

2

2 Answers

0
votes

The easiest way would be to add additional key like templateExists: true to your services and check it with a simple if statement in your deployment:

{{- if $svc.templateExists }}
{{ include (print $svc.name "-env") . | indent 8 }}
{{- end -}}
services:
  svc1:
    templateExists: true
    name: svc1
    javaOpts: "-Xms128m -Xmx512m"
  svc2:
    name: svc2
    javaOpts: "-Xms256m -Xmx512m"

0
votes

Here is a solution that I came up now. I check if there are any files in the chart that matches the pattern [service]-env.yaml and if there are then I include the content of that file in the deployment.

{{- range $path, $_ := $f.Glob "**-env.yaml" }}
{{- if contains $svc.app.name $path }}
{{ $f.Get $path | indent 8 }}
{{- end }}
{{- end }}

That way, for services that require extra env. variables we can include a file in files/service-env.yaml and those variables will be added to the deployment. For services that do not require such variables, it's left empty.