3

I want to dynamically generate my template file, here is my case:

{{- $v := (.Files.Get "values-deployment-nginx.yaml") | fromYaml }}
spec:
  {{- range $key, $value := $v.containers }}
  containers:
  - name: {{ $value.name }}
    image: {{ .Values.{{ $value.name }}-image }}:{{ .Values.{{ $value.name }}--tag }}

I want to first get the {{ $value.name }}, it maybe a string like "nginx", then I would like to use the {{ .Values.nginx-image }} to get the right image value in values file.
Is there a way can do this? Thank you very much!


I have many deployment template dynamically generated, but only want to expose the image and tag to values file , so that we can pass different image informations when install chart. Other variables are in (.Files.Get "values-deployment-nginx.yaml") , like this(is also generated dynamically). So when generate the template, I want to match the image and tag in value file.

values file like this:

deployment-nginx-imagerepo: nginx
deployment-nginx-imagetag: latest

values-deployment-nginx.yaml like this(generated by other project):

autoscale: []
containers:
- envs: []
  imagerepository: nginx
  imagetag: latest
  itemid: n79fecd51_6716_fa65_5e64_aeed8ed6ab7e
  name: nginx
  resource:
    maxcpu: ""
    maxmemory: ""
    mincpu: "0.5"
    minmemory: 512m
  volumemounts: []
name: details
replicas: 1
schedulpolicy: []
storages: []
type: deployment

2 Answers 2

6

In the Go text/template language I believe the index function will do this. (.Values is typically a map, and YAML maps and lists convert to Go maps and slices.) (Also remember almost everything in the sprig library is available.)

image: {{ index .Values (printf "%s-image" $value.name) }}:{{ index .Values (printf "%s-tag" $value.name) }}
Sign up to request clarification or add additional context in comments.

Comments

0

Another approach is to define a helper.

Let's assume we're passing in image info in a values file at runtime that defines a dictionary map of versions by target environment (namespace) and service (chart name).

e.g.,

DEV:
  appA: a.b.c/path/to/appA:1.2.3
  appB: a.b.c/path/to/appB:2.2.1
QA:
  appA: a.b.c/path/to/appA:1.2.1
  appB: a.b.c/path/to/appB:1.8.1

This defines namespaces DEV and QA, each with services appA and appB.

In my templates/_helpers.tpl files:

{{/* Look up image spec from the runtime values file. */}}
{{- define "appB.refSpec"                              -}}{{*/ or appA /*}}
{{-   $targetEnv := index .Values .Release.Namespace    }}
{{-   get $targetEnv .Chart.Name                        }}
{{- end                                                 }}

Then in templates/deployment.yaml:

image: "{{ include "appB.refSpec" . }}"

Now when deploying:

helm upgrade -i -n DEV appB myRepo/appB -f v.yml

The refSpec helper will assign $targetEnv the DEV map from .Values, then pass back the appB value from it to the image: field for deployment.

If we maintain that file in a git repo, then every change can go through pull request review and approval, and it's easy to clone at runtime in an automation.

You may not need that much complexity, but it lets you encapsulate it when you do. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.