In Helm, can I loop over a set of manifests in my Helm chart?

2 min read 05-10-2024
In Helm, can I loop over a set of manifests in my Helm chart?


Looping Through Manifests in Helm: A Deep Dive

Problem: You're working with a Helm chart and need to deploy multiple instances of a resource, each with slightly different configurations. You're wondering if there's a way to loop through a set of manifests within your chart to automate this process.

Rephrased: Imagine you want to deploy three identical databases, each with a unique name and port. Manually creating three separate templates with slightly different configurations seems tedious. Is there a more efficient way to do this using Helm?

The Solution: Looping Through Manifests

While Helm doesn't offer a direct "loop" functionality, you can achieve similar results by leveraging templates, variables, and the range function.

Example:

Let's say you want to deploy three Redis instances with different names and ports. Here's how you can structure your values.yaml and templates/ files:

values.yaml:

redis:
  instances:
    - name: redis-1
      port: 6379
    - name: redis-2
      port: 6380
    - name: redis-3
      port: 6381

templates/deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:latest
        ports:
        - containerPort: {{ .Values.redis.instances.port }}
        env:
        - name: REDIS_NAME
          value: {{ .Values.redis.instances.name }}

Explanation:

  1. values.yaml: We define a redis section with an instances list. Each instance is an object with name and port properties.
  2. templates/deployment.yaml: We use the range function to iterate through each element in the redis.instances list.
    • Inside the range loop, we access the current instance's name and port using {{ .Values.redis.instances.name }} and {{ .Values.redis.instances.port }} respectively.
    • This dynamically creates separate resources for each Redis instance, configured based on the values provided in values.yaml.

Benefits of this approach:

  • Efficiency: Avoids repeating the same template code multiple times.
  • Flexibility: You can easily add or remove instances by modifying the instances list in your values.yaml file.
  • Customization: You can add more properties to your instances (e.g., password, memory) and use them within the template to tailor each instance's configuration.

Additional Notes:

  • This approach can be extended to other types of resources like services, ingress, etc.
  • You can use with statements in combination with range to conditionally execute parts of the template based on the properties of each instance.
  • Always ensure that your YAML files are properly formatted and indented.

In conclusion, while Helm doesn't provide a direct "loop" keyword, the combination of templates, variables, and the range function allows you to create dynamic and reusable deployments. By mastering this technique, you can streamline the process of managing multiple instances of resources within your Helm charts.