How to set environment related values.yaml in Helm subcharts?

11,480

Here is how I would do it (for reference overriding values):

  1. In your child charts (foochart) define the number of replicas as a variable:
    • foochart/values.yaml

...
replicas: 1
...
  • foochart/templates/deployment.yaml

...
spec:
  replicas: {{ .Values.replicas }}
...
  1. Then, in your main chart's values files:

    • values-local.yaml

foochart:
  replicas: 1
  • values-prod.yaml

foochart:
  replicas: 2
Share:
11,480
Tobi
Author by

Tobi

Updated on June 27, 2022

Comments

  • Tobi
    Tobi almost 2 years

    I am currently deploying my applications in a Kubernetes cluster using Helm. Now I also need to be able to modify some parameter in the values.yaml file for different environments.

    For simple charts with only one level this is easy by having different values-local.yaml and values-prod.yaml and add this to the helm install flag, e.g. helm install --values values-local.yaml.

    But if I have a second layer of subcharts, which also need to distinguish the values between multiple environments, I cannot set a custom values.yaml.

    Assuming following structure:

    | chart
       | Chart.yaml
       | values-local.yaml
       | values-prod.yaml
       | charts
          | foo-app
             | Chart.yaml
             | values-local.yaml
             | values-prod.yaml
             | templates
                | deployments.yaml
                | services.yaml
    

    This will not work since Helm is expecting a values.yaml in subcharts.

    My workaround right now is to have an if-else-construct in the subchart/values.yaml and set this in as a global variable in the parent values.yaml.

    *foo-app/values.yaml*
        {{ - if .Values.global.env.local }}
            foo-app:
              replicas: 1
        {{ else if .Values.global.env.dev}}
            foo-app:
              replicas: 2
        {{ end }}
    

    parent/values-local.yaml
    global:
      env:
       local: true
    
    parent/values-prod.yaml
    global:
      env:
       prod: true
    

    But I hope there is a better approach around so I do not need to rely on these custom flags.

    I hope you can help me out on this.

  • Tedinoz
    Tedinoz over 5 years
    It's not clear whether this is an answer to the OP question, or a new question. As an answer, it lacks detail and/or links to documentation or an example. As a question, you can search for similar questions, or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, ask a new question, and include a link to this one to help provide context. See: Ask questions, get answers, no distractions