Share the love

In Helm charts, loops can be written using the Go template language. Helm uses the Go template engine to generate Kubernetes manifests from chart templates.

Here is an example of a loop in a Helm chart that creates multiple Kubernetes deployments:

{{- $replicas := 3 }}
{{- range $i, $replica := until $replicas }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment-{{ $i }}
spec:
  replicas: {{ $replica }}
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-image
{{- end }}

In this example, the range function is used to iterate over a range of numbers (from 0 to 3, in this case) and the until function is used to generate the range of numbers. The $i variable represents the current iteration number, and the $replica variable represents the number of replicas for the current iteration. The {{ $i }} and {{ $replica }} variables are used to create unique names and replica counts for each deployment.

It’s also possible to loop over lists of objects in the template and use the data to generate manifests. Here is an example where we are looping over a list of objects and generating a deployment for each object:

{{- $services := [
    { name: "service1", image: "image1" },
    { name: "service2", image: "image2" },
    { name: "service3", image: "image3" }
]}}
{{- range $service := $services }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $service.name }}
spec:
  replicas: 3
  selector:
    matchLabels:
      app: {{ $service.name }}
  template:
    metadata:
      labels:
        app: {{ $service.name }}
    spec:
      containers:
      - name: {{ $service.name }}
        image: {{ $service.image }}
{{- end }}

In this example, we are looping over a list of objects, where each object has the properties “name” and “image”. We are using these properties to generate unique names and images for each deployment.

It’s important to keep in mind that this is just an example and the specific implementation may vary depending on the application and its requirements. Additionally, it’s important to have a good understanding of the Go template language in order to effectively use loops in Helm charts.

Here is an example of a loop in a Helm chart that creates multiple Kubernetes Ingress rules:

{{- $ingresses := [
    { name: "ingress1", host: "example1.com", path: "/path1" },
    { name: "ingress2", host: "example2.com", path: "/path2" },
    { name: "ingress3", host: "example3.com", path: "/path3" }
]}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  {{- range $ingress := $ingresses }}
  - host: {{ $ingress.host }}
    http:
      paths:
      - path: {{ $ingress.path }}
        pathType: Prefix
        pathRewrite:
          replace: "{{ $ingress.path }}"
          with: "/"
        route:
          action:
            proxy:
              path: /
              service:
                name: my-service
                port: 80
  {{- end }}

In this example, we are using a range function to loop over a list of objects, where each object has the properties “name”, “host” and “path”. We are using these properties to generate unique host and path for each ingress rule.

Helper file in Helm charts

Here is an example of a loop in a Helm chart that creates multiple Kubernetes Services using a helper file:

{{- $services := (include "path/to/services.yaml") }}
{{- range $service := $services.services }}
apiVersion: v1
kind: Service
metadata:
  name: {{ $service.name }}
spec:
  selector:
    app: {{ $service.name }}
  ports:
    - name: http
      port: 80
      targetPort: {{ $service.targetPort }}
{{- end }}

In this example, we are using the include function to include a helper file called services.yaml that contains a list of objects representing our services. The file should look like this:

services:
  - name: service1
    targetPort: 8080
  - name: service2
    targetPort: 8888

We are then using the range function to loop over the services and generate a Service resource for each.