helmfile icon indicating copy to clipboard operation
helmfile copied to clipboard

Can't get templates to work

Open giulioprovasi opened this issue 5 years ago • 2 comments

Hey everybody,

I'm having issue with templating as specified here, here a basic sample:

helmfile.yaml

{{ readFile "domain.yaml" }}
environments:
  master:
    values:
      - clusterDomain: *clusterDomain
  staging:
    values:
      - clusterDomain: *clusterDomain

domain.yaml

templates:
  clusterDomain: &clusterDomain
      {{ get (dict "master" "http://master.com" "staging" "http://example.com") .Environment.Name }}

And when running helmfile --debug -e master -f helmfile.yaml template I get the following :

processing file "helmfile.yaml" in directory "."
first-pass rendering starting for "helmfile.yaml.part.0": inherited=&{master map[] map[]}, overrode=<nil>
first-pass uses: &{master map[] map[]}
first-pass rendering output of "helmfile.yaml.part.0":
 0: 
 1: environments:
 2:   master:
 3:     values:
 4:       - clusterDomain: *clusterDomain
 5:   staging:
 6:     values:
 7:       - clusterDomain: *clusterDomain
 8: 

could not deduce `environment:` block, configuring only .Environment.Name. error: failed to read helmfile.yaml.part.0: reading document at index 1: yaml: unknown anchor 'clusterDomain' referenced
error in first-pass rendering: result of "helmfile.yaml.part.0":
 0: 
 1: environments:
 2:   master:
 3:     values:
 4:       - clusterDomain: *clusterDomain
 5:   staging:
 6:     values:
 7:       - clusterDomain: *clusterDomain
 8: 

first-pass produced: &{master map[] map[]}
first-pass rendering result of "helmfile.yaml.part.0": {master map[] map[]}
second-pass rendering result of "helmfile.yaml.part.0":
 0: templates:
 1:   clusterDomain: &clusterDomain
 2:       {{ get (dict "master" "http://master.com" "staging" "http://example.com") .Environment.Name }}
 3: 
 4: environments:
 5:   master:
 6:     values:
 7:       - clusterDomain: *clusterDomain
 8:   staging:
 9:     values:
10:       - clusterDomain: *clusterDomain
11: 

err: failed to read helmfile.yaml: reading document at index 1: yaml: invalid map key: map[interface {}]interface {}{"get (dict \"master\" \"http://master.com\" \"staging\" \"http://example.com\") .Environment.Name":interface {}(nil)}
in ./helmfile.yaml: failed to read helmfile.yaml: reading document at index 1: yaml: invalid map key: map[interface {}]interface {}{"get (dict \"master\" \"http://master.com\" \"staging\" \"http://example.com\") .Environment.Name":interface {}(nil)}

I am using helmfile version v0.129.0

Can someone point me to the right direction please ?

Cheers, Giulio

giulioprovasi avatar Oct 01 '20 04:10 giulioprovasi

Answer from post helped me.

templates:

templates:
  external-chart: &external-chart
    missingFileHandler: Debug
    installedTemplate: '{{`{{ .Values | getOrNil .Release.Name | getOrNil "enabled" | default false }}`}}'
    version: '{{`{{ .Values | getOrNil .Release.Name | getOrNil "version" | default "*" }}`}}'
    values:
      - "./environment/_shared/helm/{{`{{ .Release.Name }}`}}.yaml"
      - "./environment/_shared/helm/{{`{{ .Release.Name }}`}}.yaml.gotmpl"
      - "./environment/{{`{{ .Environment.Name }}`}}/helm/{{`{{ .Release.Name }}`}}.yaml"
      - "./environment/{{`{{ .Environment.Name }}`}}/helm/{{`{{ .Release.Name }}`}}.yaml.gotmpl"
    secrets:
      {{- if eq false (hasKey .Values "noSecrets") }}
      - "./environment/{{`{{ .Environment.Name }}`}}/helm/{{`{{ .Release.Name }}`}}-secrets.yaml"
      {{- end }}

helmfile:

---
{{ tpl (readFile "./helmfile/templates.yaml") . }}

releases:
- name: static
  chart: "./environment/{{`{{ .Environment.Name }}`}}/static"
  installedTemplate: '{{`{{ .Values | getOrNil .Release.Name | getOrNil "enabled" | default false  }}`}}'

- name: kube-state-metrics
  <<: *local-versioned-chart

- name: twilio
  <<: *local-chart

kuzaxak avatar Feb 03 '21 11:02 kuzaxak

Answer from post helped me.

templates:

templates:
  external-chart: &external-chart
    missingFileHandler: Debug
    installedTemplate: '{{`{{ .Values | getOrNil .Release.Name | getOrNil "enabled" | default false }}`}}'
    version: '{{`{{ .Values | getOrNil .Release.Name | getOrNil "version" | default "*" }}`}}'
    values:
      - "./environment/_shared/helm/{{`{{ .Release.Name }}`}}.yaml"
      - "./environment/_shared/helm/{{`{{ .Release.Name }}`}}.yaml.gotmpl"
      - "./environment/{{`{{ .Environment.Name }}`}}/helm/{{`{{ .Release.Name }}`}}.yaml"
      - "./environment/{{`{{ .Environment.Name }}`}}/helm/{{`{{ .Release.Name }}`}}.yaml.gotmpl"
    secrets:
      {{- if eq false (hasKey .Values "noSecrets") }}
      - "./environment/{{`{{ .Environment.Name }}`}}/helm/{{`{{ .Release.Name }}`}}-secrets.yaml"
      {{- end }}

helmfile:

---
{{ tpl (readFile "./helmfile/templates.yaml") . }}

releases:
- name: static
  chart: "./environment/{{`{{ .Environment.Name }}`}}/static"
  installedTemplate: '{{`{{ .Values | getOrNil .Release.Name | getOrNil "enabled" | default false  }}`}}'

- name: kube-state-metrics
  <<: *local-versioned-chart

- name: twilio
  <<: *local-chart

Thank you - this strategy helped me to reuse an anchor defined in a template across multiple helmfiles.

talonx avatar Jul 28 '22 11:07 talonx