简体   繁体   English

如何通过特定名称从 values.yaml 获取数据? Helm 模板问题

[英]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:如果我有一个如下所示的 values.yaml:

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:现在我在 _helpers.tpl 中有一个函数,它接受 .Values.portInfo.numberOfPorts 和 .Values.portInfo.startingPort 并将循环遍历每个端口值,就像这样进入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.我想要做的是使用该函数来获取 MIN_PORT 和 MAX_PORT 下的值并执行相同的操作。

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. env:数据结构将很难从 Helm 模板代码中遍历。 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.您显示的值可以轻松计算,与尝试在 Helm 值中注入完整的环境变量块不同,在模板中构建环境变量可能更容易。

# 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:编写一个辅助函数来从.Values.env列表中获取一个值:

{{/* 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.然后在您的生成器模板中,您可以使用.Values.env调用它,使用特定于 Helm 的include函数调用模板并将其结果作为字符串获取,然后使用atoi将该字符串转换为数字。

{{- 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 .这种方法比直接在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. Helm 对​​复杂模板的单元测试没有很好的支持(我过去使用helm templateshunit2搭建了一个测试框架)并且存在一些出错的风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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