Home > Mobile >  How to pass helm values dynamically
How to pass helm values dynamically

Time:10-05

I am trying to access a helm value dynamically though another variable as I am utilizing the range functionality to create multiple deployments. Take for example this section of my deployment file

{{- range $teams := .Values.teams }}
.
.
.
        ports:
        - containerPort: {{ .Values.deployment.backend.($teams.tag).serverPort }}
          protocol: {{ .Values.deployment.backend.($teams.tag).serverProtocol }}
        - containerPort: {{ .Values.deployment.backend.($teams.tag).authPort }}
          protocol: {{ .Values.deployment.backend.($teams.tag).authProtocol }}

.
.
.
---
{{- end }}

with a values.yml file of

teams:
  - name: TeamA
    tag: teamA
  - name: TeamB
    tag: teamB
  - name: TeamC
    tag: teamC
deployment:
  backend:
    teamA:
      serverPort: 10001
      serverProtocol: TCP
      authPort: 10010
      authProtocol: TCP
    teamB:
      serverPort: 9101
      serverProtocol: TCP
      authPort: 9110
      authProtocol: TCP
    teamC:
      serverPort: 9001
      serverProtocol: TCP
      authPort: 9010
      authProtocol: TCP


I am not able to figure out how to pass $teams.tag to be evaluated to return the overall value of containerPort for example.

Any help is appreciated.

Cheers

CodePudding user response:

Ended up using tpl https://helm.sh/docs/howto/charts_tips_and_tricks/#using-the-tpl-function

Open for suggestions if there is better ways

CodePudding user response:

Helm itself provides a number of functions for you to manipulate the value.

Here is one way to handle the your use case with get function.

{{- $ := . -}}
{{- range $teams := .Values.teams }}
.
.
.
        ports:
        - containerPort: {{ (get $.Values.deployment.backend $teams.tag).serverPort }}
          protocol: {{ (get $.Values.deployment.backend $teams.tag).serverProtocol }}
        - containerPort: {{ (get $.Values.deployment.backend $teams.tag).authPort }}
          protocol: {{ (get $.Values.deployment.backend $teams.tag).authProtocol }}

.
.
.
---
{{- end }}

Please be noted that the scope inside the range operator will change. So, you need to preassign the . to $ in order to access the root scope.

You can also refer to this doc, https://helm.sh/docs/chart_template_guide/function_list/, to know more about the funcitons you can use.

  • Related