简体   繁体   English

我可以使用 _helpers.tpl 中的函数在舵图中填写 values.yaml 吗?

[英]Can I use functions from _helpers.tpl to fill out values.yaml in helm charts?

Right now, I have a values.yaml with a section that looks a bit like this:现在,我有一个 values.yaml,其中的部分看起来有点像这样:

...
imageName:
  ports:
    - containerPort: 7980
      name: db0
      protocol: TCP
    - containerPort: 7981
      name: db1
      protocol: TCP
    - containerPort: 7982
      name: db2
      protocol: TCP
    - containerPort: 7983
      name: db3
      protocol: TCP
    - containerPort: 7984
      name: db4
      protocol: TCP
    - containerPort: 7985
      name: db5
      protocol: TCP
    - containerPort: 7986
      name: db6
      protocol: TCP
    - containerPort: 7987
      name: db7
      protocol: TCP
    - containerPort: 7988
      name: db8
      protocol: TCP
    - containerPort: 7989
      name: db9
      protocol: TCP
    - containerPort: 7990
      name: db10
      protocol: TCP
...

I'd like to clean this up by creating a function in _helpers.tpl that will take the min port value (7980) and the max port value (7990) and create the structure for each one in that format.我想通过在 _helpers.tpl 中创建一个函数来清理它,该函数将采用最小端口值 (7980) 和最大端口值 (7990) 并以该格式为每一个创建结构。

I am wondering: Is this possible?我想知道:这可能吗? I am having a lot of trouble with this and using the helpers file in general so if anyone can nudge me in the right direction with how to accomplish this, I would appreciate that too!我在这方面遇到了很多麻烦,并且通常使用帮助文件,所以如果有人能在正确的方向上推动我如何实现这一点,我也将不胜感激!

Thanks :)谢谢 :)

This should be possible.这应该是可能的。 Say you configure your chart with the number of ports and the starting port:假设您使用端口数和起始端口配置图表:

# values.yaml (or a `helm install -f` values file)
numberOfPorts: 11
startingPort: 7980

You can use the until template function to turn that into a list of numbers:您可以使用until模板函数将其转换为数字列表:

{{- $dbs := until .Values.numberOfPorts }}

Now you can use the standard range function to loop over that list.现在您可以使用标准range函数循环遍历该列表。 Inside the loop body the value will be an integer from 0 to numberOfPorts - 1 and you can produce the list item accordingly.在循环体内,该值将是一个从 0 到numberOfPorts - 1的整数,您可以相应地生成列表项。 Also note that range takes over the .另请注意, range接管. special variable, so you'll need to save anything you need from .Values outside the range loop.特殊变量,因此您需要从range循环之外的.Values保存您需要的任何内容。

imageName:
  ports:
{{- $startingPort := .Values.startingPort }}
{{- range $i := until .Values.numberOfPorts }}
    - containerPort: {{ add $startingPort $i }}
      name: db{{ $i }}
      protocol: TCP
{{- end }}

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

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