helm variables in .Values

9/14/2018

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
-- yangyang
go-templates
kubernetes-helm

1 Answer

9/16/2018

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) }}
-- David Maze
Source: StackOverflow