我是helm chart的新手,我正在构建helm chart以在kubernetes上部署一个应用程序,作为它的一部分,我创建了一个部署模板,如下所示,
{{- $outer := . -}}
{{- range $index, $service := .Values.myservices}}
{{- with $outer }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ $service.name }}
labels:
{{- include "myhelm.labels" $ | nindent 4 }}
spec:
.
.
.
{{- end }}
{{- end }}
这里我使用了一个模板"myhelm.label ",它在_helpers中定义。TPL如下,
{{/*
Common labels
*/}}
{{- define "myhelm.labels" -}}
helm.sh/chart: {{ include "myhelm" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
我喜欢在值中包含更多的标签。Yaml如下
myservices:
api:
name: "com-api"
labels:
app: "com-api"
selectorLabels:
app: "com-cp"
podAnnotations: {}
container:
image: "com-api"
port: 24000
name: "api"
nodeSelector:
app: "com-cp-api"
affinity: {}
tolerations: {}
ui:
name: "com-ui"
labels:
app: "com-ui"
selectorLabels:
app: "com-ui"
podAnnotations: {}
container:
image: "com-ui"
port: 23000
name: "ui"
nodeSelector:
app: "com-cp-ui"
affinity: {}
tolerations: {}
与"myhelm.label "(通用标签)我还想包含特定于服务的标签,比如$service.labels。请帮帮我,我该怎么做?
我可以一个一个地添加特定的标签,如
labels:
{{- include "dlc-project-service-control-plane.labels" $ | nindent 4 }}
app: {{ $service.labels.app }}
但是,我正在寻找一个解决方案,如果我在$service下有多个标签。以值表示标签。并希望在部署模板中的单个语句中添加所有这些。
如果你已经知道解决方案,请分享代码片段,这很有帮助。
感谢我可以使用toYaml实现它,下面是代码片段,
{{- $outer := . -}}
{{- range $index, $service := .Values.myservices}}
{{- with $outer }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ $service.name }}
labels:
{{- include "myhelm.labels" $ | nindent 4 }}
{{- toYaml $service.labels | nindent 4 }}
spec:
.
.
.
{{- end }}
{{- end }}