Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helm charts nested loops

Trying to generate deployments for my helm charts by using this template

{{- range .Values.services }}
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myapp-{{ . }}
spec:
  replicas: {{ .replicaCount }}
  template:
    metadata:
      labels:
        app: myapp-{{ . }}
        chart: myapp-{{ $.Values.cluster }}-{{ $.Values.environment }}
    spec:
      containers:
      - name: myapp-{{ . }}
        image: {{ $.Values.containerRegistry }}/myapp-{{ . }}:latest
        ports:
        - containerPort: {{ .targetPort }}
        env:
  {{- with .environmentVariables }}
  {{ indent 10 }}
  {{- end }}
      imagePullSecrets:
       - name: myregistry
{{- end }}

for 2 of my services. In values.yaml I got

environment: dev

cluster: sandbox

ingress:
  enabled: true

containerRegistry: myapp.io

services:
- backend:
    port: 80
    targetPort: 8080
    replicaCount: 1
    environmentVariables:
      - name: SOME_VAR
        value: "hello"
- web:
    port: 80
    targetPort: 8080
    replicaCount: 1
    environmentVariables:
      - name: SOME_VAR
        value: "hello"

... but the output is not being properly formatted

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myapp-map[backend:map[replicaCount:1 targetPort:8080 environmentVariables:[map[name:SOME_VAR value:hello] port:80]]

instead of

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myapp-web
(...)

and another config

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myapp-backend
(...)

what functions can I use or some different data structure? None of the references (i.e. .environmentVariables are working correctly)

like image 494
FRC Avatar asked Jun 05 '18 20:06

FRC


People also ask

What does {{ }} mean in Helm?

The Helm template syntax is based on the Go programming language's text/template package. The braces {{ and }} are the opening and closing brackets to enter and exit template logic.

Does Helm use Jinja?

Setup helm your yaml files: Your yaml files to generate your secrets, deployments, and more! It's here where you can use the Jinja template!

What is TPL in Helm?

The tpl function allows developers to evaluate strings as templates inside a template. This is useful to pass a template string as a value to a chart or render external configuration files. Syntax: {{ tpl TEMPLATE_STRING VALUES }}

What is range in Helm chart?

range is as similar to for/foreach loops in any other programming language. We can use range to iterate through a collection one by one. Let's see an example.


1 Answers

I think you should reconsider the way the data is structured, this would work better:

  services:
  - name: backend
    settings:
      port: 80
      targetPort: 8080
      replicaCount: 1
      environmentVariables:
        - name: SOME_VAR
          value: "hello"

  - name: web
    settings:
      port: 80
      targetPort: 8080
      replicaCount: 1
      environmentVariables:
        - name: SOME_VAR
          value: "hello"

And your Deployment to look like this:

{{- range .Values.services }}
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myapp-{{ .name }}
spec:
  replicas: {{ .settings.replicaCount }}
  template:
    metadata:
      labels:
        app: myapp-{{ .name }}
    spec:
      containers:
      - name: myapp-{{ .name }}
        image: {{ $.Values.containerRegistry }}/myapp-{{ .name }}:latest
        ports:
        - containerPort: {{ .settings.targetPort }}
        env:
  {{- with .settings.environmentVariables }}
  {{ toYaml . | trim | indent 6 }}
  {{- end }}
      imagePullSecrets:
       - name: myregistry
{{- end }}

would actually create two deployments, by adding the --- separator.

like image 77
iomv Avatar answered Oct 05 '22 21:10

iomv