Search code examples
kubernetes-helm

Helm Go Templating using Dictionary


I can not find a way to iterate over a range in helm templating. I have the next definition in my values.yaml

Variable dictionary to be consumed

projects:
  - tenants: imc
    namespaces: 
      - name: test-1
        company: inter
        environments:
          - build 
          - dev 
          - stage
          - test
      - name: test-2
        environments:
          - build 
          - dev 
          - stage
          - test
      - name: test-3
        environments:
          - build 
          - dev 
          - stage
          - test

Code snippet


{{- range $key, $value := .Values.tenants }}
{{- range $nkey, $nvalue := .namespaces }}
---
apiVersion: v1
kind: Namespace
metadata:
  annotations:
    parent_project_name: {{ $value.name }}
    company: {{ $value.company }}
    openshift.io/description: ""
    openshift.io/display-name: ""
  labels:
    tenant: {{ $value.tenants }}
  name: {{ $value.name }}-{{ $nvalue }}
spec: {}
status: {}
{{- end }}
{{- end }}

I need help in consuming the variable into the template


Solution

  • May you give the expected output? This is what I guess.

    values.yaml

    projects:
      - tenants: imc
        namespaces: 
          - name: test-1
            company: inter
            environments:
              - build 
              - dev 
              - stage
              - test
          - name: test-2
            environments:
              - build 
              - dev 
              - stage
              - test
          - name: test-3
            environments:
              - build 
              - dev 
              - stage
              - test
    

    templates/ns.yaml

    {{- range $key, $value := .Values.projects }}
    {{- range $nkey, $nvalue := .namespaces }}
    ---
    apiVersion: v1
    kind: Namespace
    metadata:
      annotations:
        parent_project_name: {{ $nvalue.name }}
        company: {{ $nvalue.company }}
        openshift.io/description: ""
        openshift.io/display-name: ""
      labels:
        tenant: {{ $value.tenants }}
      name: {{ $nvalue.name }}
    spec: {}
    status: {}
    {{- end }}
    {{- end }}
    

    cmd

    helm template --debug test .
    

    output

    ---
    apiVersion: v1
    kind: Namespace
    metadata:
      annotations:
        parent_project_name: test-1
        company: inter
        openshift.io/description: ""
        openshift.io/display-name: ""
      labels:
        tenant: imc
      name: test-1
    spec: {}
    status: {}
    ---
    apiVersion: v1
    kind: Namespace
    metadata:
      annotations:
        parent_project_name: test-2
        company: 
        openshift.io/description: ""
        openshift.io/display-name: ""
      labels:
        tenant: imc
      name: test-2
    spec: {}
    status: {}
    ---
    apiVersion: v1
    kind: Namespace
    metadata:
      annotations:
        parent_project_name: test-3
        company: 
        openshift.io/description: ""
        openshift.io/display-name: ""
      labels:
        tenant: imc
      name: test-3
    spec: {}
    status: {}