我需要遍历实例列表并为每个实例创建 1 个有状态集。但是,在范围内,我将自己限制在该循环的范围内。我需要访问我的 statefulset 中的一些全局值。
我已经通过将我需要的所有全局对象放在一个 env 变量中来解决它,但是......这看起来很hacky。
在仍然能够引用全局对象的同时循环遍历范围的正确方法是什么?
我的循环示例
{{- $values := .Values -}}
{{- $release := .Release -}}
{{- range .Values.nodes }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ $release.Name }} <-- Global Scope
labels:
.
.
.
env:
- name: IP_ADDRESS
value: {{ .ip_address }} <-- From range scope
.
.
.
{{- end }}
Run Code Online (Sandbox Code Playgroud)
值示例
# Global
image:
repository: ..ecr.....
# Instances
nodes:
- node1:
name: node-1
iP: 1.1.1.1
- node2:
name: node-2
iP: …Run Code Online (Sandbox Code Playgroud) 在传递给模板的数据中,我有两个变量Type,Res.Type我想进行比较以为select字段预选择一个选项。
为了说明我的问题,我创建了这个简化的版本:
package main
import (
"bufio"
"bytes"
"html/template"
"log"
)
type Result struct{ Type string }
func main() {
types := map[string]string{
"FindAllString": "FindAllString",
"FindString": "FindString",
"FindStringSubmatch": "FindStringSubmatch",
}
res := &Result{Type: "findAllString"}
templateString := `
<select name="type">
{{ range $key,$value := .Types }}
{{ if eq $key .Res.Type }}
<option value="{{$key}}" selected>{{$value}}</option>
{{ else }}
<option value="{{$key}}">{{$value}}</option>
{{ end }}
{{ end }}
</select>`
t, err := template.New("index").Parse(templateString)
if err != nil {
panic(err) …Run Code Online (Sandbox Code Playgroud)