Helm function to set value based on a variable?

2/14/2021

I'm learning Helm to setup my 3 AWS EKS clusters - sandbox, staging, and production.

How can I set up my templates so some values are derived based on which cluster the chart is being installed at? For example, in my myapp/templates/deployment.yaml I may want

apiVersion: apps/v1
kind: Deployment
metadata:
  ...
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}

I may want replicas to be either 1, 2, or 4 depending if I'm installing the chart in my sandbox, staging, or production cluster respectively? I wanna do same trick for cpu and memory requests and limits for my pods for example.

I was thinking of having something like this in my values.yaml file

environments:
  - sandbox
  - staging
  - production
perClusterValues:
  replicas:
    - 1
    - 2
    - 4
  cpu:
    requests:
      - 256m
      - 512m
      - 1024m
    limits:
      - 512m
      - 1024m
      - 2048m
  memory:
    requests:
      - 1024Mi
      - 1024Mi
      - 2048Mi
    limits:
      - 2048Mi
      - 2048Mi
      - 3072Mi

So if I install a helm chart in the sandbox environment, I want to be able to do

$ helm install myapp myapp --set environment=sandbox

apiVersion: apps/v1
kind: Deployment
metadata:
  ...
spec:
  {{- if not .Values.autoscaling.enabled }}
  # In pseudo-code, in my YAML files
  # Get the index value from .Values.environments list
  # based on pass-in environment parameter
  {{ $myIndex = indexOf .Values.environments .Value.environment }}
  replicas: {{ .Values.perClusterValues.replicas $myIndex }}
  {{- end }}

I hope you understand my logic, but what is the correct syntax? Or is this even a good approach?

-- Chris F
kubernetes
kubernetes-helm

1 Answer

2/14/2021

You can use the helm install -f option to pass an extra YAML values file in, and this takes precedence over the chart's own values.yaml file. So using exactly the template structure you already have, you can provide alternate values files

<!-- language: lang-yaml -->
# sandbox.yaml
autoscaling:
  enabled: false
replicaCount: 1
<!-- language: lang-yaml -->
# production.yaml
autoscaling:
  enabled: true
replicaCount: 5

And then when you go to deploy the chart, run it with

<!-- language: lang-sh -->
helm install myapp . -f production.yaml

(You can also helm install --set replicaCount=3 to override specific values, but the --set syntax is finicky and unusual; using a separate YAML file per environment is probably easier. Some tooling might be able to take advantage of JSON files also being valid YAML to write out additional deploy-time customizations.)

-- David Maze
Source: StackOverflow