How to make nested variables optional in helm?

Member

by jasen , in category: Third Party Scripts , a month ago

How to make nested variables optional in helm?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 25 days ago

@jasen 

To make nested variables optional in Helm, you can use the defaultValue field in the values.yaml file. This approach allows you to define default values for nested variables, but allows users to override them if needed.


Here's an example of how you can make a nested variable optional using defaultValue:


values.yaml:

1
2
3
4
5
6
myapp:
  database:
    host: localhost
    port: 5432
    username: myuser
    password: mypassword


In the above example, all the variables under database are nested variables.


You can make the nested variables optional by adding the defaultValue field to each of them in the values.yaml file. For example, if you want to make the host variable optional, you can modify the values.yaml file like this:


values.yaml:

1
2
3
4
5
6
7
8
9
myapp:
  database:
    host: localhost
    port: 5432
    username: myuser
    password: mypassword
    options:
      enabled: false
      timeout: 30


Now, the host variable has a default value of localhost, but users can override it by providing a different value in their custom values.yaml file.


In your Helm templates, you can access these nested variables as usual using the . notation. However, don't forget to handle the case where the nested variables are not provided by users. You can use the default function to handle this:


deployment.yaml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  template:
    spec:
      containers:
      - name: myapp
        image: myapp:latest
        env:
        - name: DB_HOST
          value: {{ .Values.myapp.database.host | default "localhost" }}
        - name: DB_PORT
          value: {{ .Values.myapp.database.port | default "5432" }}
        - name: DB_USERNAME
          value: {{ .Values.myapp.database.username | default "myuser" }}
        - name: DB_PASSWORD
          value: {{ .Values.myapp.database.password | default "mypassword" }}
        - name: DB_OPTIONS_ENABLED
          value: {{ .Values.myapp.database.options.enabled | default false }}
        - name: DB_OPTIONS_TIMEOUT
          value: {{ .Values.myapp.database.options.timeout | default 30 }}


In the above example, the defaultValue of localhost and 5432 will be used if the host and port variables are not provided in the values.yaml file. Similarly, the defaultValue of "myuser" and "mypassword" will be used for the username and password variables, respectively, if not provided.


The default function is used to handle the case where options is not provided at all. In such cases, the default value of false will be used for enabled and 30 for timeout.


Note that if a user provides a value for any of the nested variables in their custom values.yaml file, it will override the defaultValue.