使用Helm模板文件有条件地设置在整个模板文件中使用的变量。

huangapple go评论93阅读模式
英文:

With helm template file conditionally set a variables used throughout template file

问题

我想在我的Helm模板文件的顶部有条件地设置一个名为'go'的变量,然后在模板文件的其他地方使用它。以下是我尝试但不起作用的方法:

在模板文件的顶部,我进行如下操作:

{{- if eq $.Values.kafka.create true -}}
{{- $kafka_port := 9094 -}}
{{- else -}}
{{- $kafka_port := $.Values.kafka.port -}} 
{{- end }}

但是,当我尝试执行"helm install"时,我在模板文件的其他地方使用$kafka_port时出现解析错误。具体的错误是:

错误:解析错误 (tombolo/templates/tom-node-launch.yaml:89):未定义变量"$kafka_port"

英文:

I want to conditionally set a 'go' variable at the top of my helm template file then use it later in the template file. Here is what I tried that doesn't work:

At the top of my template file, I do the following:

{{- if eq $.Values.kafka.create true -}}
{{- $kafka_port := 9094 -}}
{{- else -}}
{{- $kafka_port := $.Values.kafka.port -}} 
{{- end }}

But when I attempt to do "helm install" I get a parse error where I use $kafka_port later in the template file. The specific error is:

Error: parse error at (tombolo/templates/tom-node-launch.yaml:89): undefined variable "$kafka_port"

答案1

得分: 2

在外部代码块中定义变量:

{{- $kafka_port := 9094 -}}
{{- if eq $.Values.kafka.create false -}}
{{- $kafka_port = $.Values.kafka.port -}} 
{{- end }}

在外部代码块中定义变量:

{{- $kafka_port := 9094 -}}
{{- if eq $.Values.kafka.create false -}}
{{- $kafka_port = $.Values.kafka.port -}} 
{{- end }}
英文:

Define the variable in the outer block:

{{- $kafka_port := 9094 -}}
{{- if eq $.Values.kafka.create false -}}
{{- $kafka_port = $.Values.kafka.port -}} 
{{- end }}

答案2

得分: 0

你的示例非常简短,对于你的用例可能是可以的。

但作为一般规则:当你开始在模板中编写代码时,这也可能是你应该在控制器部分(即Go代码)中编写代码的一个指示:

type KafkaCfg struct {
	port   int
	create bool
}

const kafkaCreationPort = 9094

func (c KafkaCfg) Port() int {
	if c.create {
		return kafkaCreationPort
	}
	return c.port
}


func someWhereElse() {
    var data struct {
	    Values struct {
		    Kafka KafkaCfg
        }
    }

    data.Values.Kafka.create = true
    helmTemplate.Execute(w, data)
}

// 在你的模板中:

{{ .Values.Kafka.Port }} {{ /* <- 将调用 .Port() 方法 */ }}

链接:https://play.golang.org/p/6pNHY0kcegp

英文:

Your example is pretty short and may well be ok for your use case.

But as a general rule : when you start writing code in your templates, it could also be an indication that you should write code in the controller part (the go code) :

type KafkaCfg struct {
	port   int
	create bool
}

const kafkaCreationPort = 9094

func (c KafkaCfg) Port() int {
	if c.create {
		return kafkaCreationPort
	}
	return c.port
}


func someWhereElse() {
    var data struct {
	    Values struct {
		    Kafka KafkaCfg
        }
    }

    data.Values.Kafka.create = true
    helmTemplate.Execute(w, data)
}

// in your template :

{{ .Values.Kafka.Port }} {{ /* &lt;- will call the .Port() method */ }}

https://play.golang.org/p/6pNHY0kcegp

答案3

得分: 0

Go的text/template语言(与Go编程语言本身不同)没有全局变量。

你可以编写一个辅助模板,通常放在templates/_helpers.tpl中,生成这个片段:

{{- define "kafka.port" -}}
{{- if .Values.kafka.create -}}
9094
{{- else -}}
{{- .Values.kafka.port -}}
{{- end -}}

当调用这个模板时,它会输出端口号;它的单个参数应该是标准的顶级Helm对象

- name: KAFKA_PORT
  value: {{ include "kafka.port" . | quote }}

或者,如果在特定上下文中需要将其作为变量使用,你可以在那里包含它(使用Helm include扩展将调用模板的输出捕获为字符串);但它将仅限于特定的模板或文件。

{{- $kafkaPort := include "kafka.port" . }}
- name: KAFKA_PORT
  value: {{ quote $kafkaPort }}
- name: KAFKA_BROKER_URL
  value: kafka://{{ include "kafka.name" . }}:{{ $kafkaPort }}
英文:

The Go text/template language (as distinct from the Go programming language itself) doesn't have global variables.

The thing you can do is to write a helper template, typically in templates/_helpers.tpl, that generates this fragment:

{{- define &quot;kafka.port&quot; -}}
{{- if .Values.kafka.create -}}
9094
{{- else -}}
{{- .Values.kafka.port -}}
{{- end -}}

This template outputs the port number when called; its single parameter should be the standard top-level Helm object.

- name: KAFKA_PORT
  value: {{ include &quot;kafka.port&quot; . | quote }}

Or, if in a specific context, you happen to need it as a variable, you can include it there (using the Helm include extension to capture the called template's output as a string); but it will be limited to its specific template or file.

{{- $kafkaPort := include &quot;kafka.port&quot; . }}
- name: KAFKA_PORT
  value: {{ quote $kafkaPort }}
- name: KAFKA_BROKER_URL
  value: kafka://{{ include &quot;kafka.name&quot; . }}:{{ $kafkaPort }}

huangapple
  • 本文由 发表于 2021年6月18日 02:58:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/68025124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定