Helm 从 configmap 中查找值并验证字典键是否存在,然后打印值

问题描述 投票:0回答:1

厘米:

apiVersion: v1
data:
  cluster: cluster1
kind: ConfigMap
metadata:
  name: testmap
  namespace: test

_helper.tpl 内的代码:

{{- define "jupyterBaseUrl" -}}
{{- $placeHolder := .Values.placeHolder }}
{{- if not .Values.placeHolder }}
{{- $ConfigMap := (lookup "v1" "ConfigMap" .Release.Namespace "testmap") }}
{{- if $ConfigMap }}
{{- $placeHolder = index $ConfigMap.data "cluster"}}
{{- range $key, $value := .Values.cluster }}
{{- if hasKey $key $placeHolder }}
{{ printf "%s" $value }}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}

打印deployment.yaml上的值:

value: {{- include jupyterBaseUrl . }}

value.yaml 中的占位符变量。

placeHolder:

values.yaml 中的字典值

cluster:
  cluster1: region1

我正在尝试验证集群名称是否与 configmap 匹配,然后打印区域值,我在此尝试中遇到的错误是:

Error: INSTALLATION FAILED: template: test/templates/deployment.yaml:76:26: executing "test/templates/deployment.yaml" at <"spark.jupyterBaseUrl">: can't give argument to non-function "jupyterBaseUrl"
kubernetes-helm
1个回答
0
投票

上面的代码大致看起来不错,但有一些设置我遗漏了。在下面找到完整的解决方案。

正确的函数是:

{{- define "jupyterBaseUrl" -}}
{{- $clusterNameEmptyString := .Values.clusterNameEmptyString }}
{{- if not .Values.clusterNameEmptyString }}
{{- $ConfigMap := (lookup "v1" "ConfigMap" .Release.Namespace "testmap") }}
{{- if $ConfigMap }}
{{- $clusterNameEmptyString = index $ConfigMap.data "cluster"}}
{{- range $key, $value := .Values.clusterName }}
{{- if eq $key  $clusterNameEmptyString }}
{{- if typeOf $value | eq "string" }}
{{- printf "%s" $value }}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}

© www.soinside.com 2019 - 2024. All rights reserved.