简体   繁体   中英

How can I get data from values.yaml by a specific name? Helm templating question

If I have a values.yaml that looks something like this:

imageName:
  portInfo:
    numberOfPorts: 11
    startingPort: 7980
  env:
    - name: "MIN_PORT"
      value: 7980
    - name: "MAX_PORT"
      value: 7990

Right now I have a function in _helpers.tpl that takes the .Values.portInfo.numberOfPorts and .Values.portInfo.startingPort and will loop through for each port value something like this into deployments.yaml:

- containerPort: 7980
  protocol: TCP
- containerPort: 7981
...

This is what that function looks like:

{{- define "ports.list" -}}
{{- $dbPorts := (.Values.issdbImage.portInfo.numberOfPorts | int) }}
{{- $startingPort := (.Values.issdbImage.portInfo.startingPort | int) }}

{{- range $i := until $dbPorts }}
    - containerPort: {{ add $startingPort $i }}
      protocol: TCP
{{- end }}
{{- end}}

What I want to do instead is use the function to instead grab the values under MIN_PORT and MAX_PORT and do the same thing.

Is this possible? And, if so, I'd appreciate suggestions on how this can be accomplished.

Thanks!

That env: data structure will be hard to traverse from Helm template code. The values you show can easily be computed, and instead of trying to inject a complete environment-variable block in Helm values, it might be easier to construct the environment variables in your template.

# templates/deployment.yaml -- NOT values.yaml
env:
{{- $p := .Values.imageName.portInfo }}
  - name: MIN_PORT
    value: {{ $p.startingPort }}
  - name: MAX_PORT
    value: {{ add $p.startingPort (sub $p.numberOfPorts 1) }}

If you really absolutely can't change the values format, this is possible . Write a helper function to get a value from the .Values.env list:

{{/* Get a value from an association list, like a container env:
     array.  Each item of the alist is a dictionary with keys
     `name:` and `value:`.  Call this template with a list of two
     items, the alist itself and the key to look up; outputs the
     value as a string (an empty string if not found, all values
     concatenated together if there are duplicates). */}}
{{- define "alist.get" -}}
{{- $alist := index . 0 -}}
{{- $needle := index . 1 -}}
{{- range $k, $v := $alist -}}
{{- if eq $k $needle -}}
{{- $v -}}
{{- end -}}
{{- end -}}
{{- end -}}

Then in your generator template, you can call this with .Values.env , using the Helm-specific include function to invoke a template and get its result as a string, and then atoi to convert that string to a number.

{{- define "ports.list" -}}
{{- $startingPort := include "alist.get" (list .Values.env "MIN_PORT") | atoi }}
{{- $endingPort := include "alist.get" (list .Values.env "MAX_PORT") | atoi }}
{{- range untilStep $startingPort (add $endingPort 1) 1 -}}
- containerPort: {{ . }}
  protocol: TCP
{{ end -}}
{{- end -}}

This approach is both more complex and more fragile than directly specifying the configuration parameters in values.yaml . Helm doesn't have great support for unit-testing complex templates (I've rigged up a test framework using helm template and shunit2 in the past) and there's some risk of it going wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM