我在
values.yaml
和deployment.yaml
中有以下内容:
values.yaml
:
apps:
test-app1:
deploy: true
containerports:
- 8000
- 8081
test-app2:
deploy: true
containerports: 6000
deployment.yaml
:
{{- if .Values.apps }}
{{- range $key, $value := .Values.apps }}
{{- if $value.deploy }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ $key }}
spec:
progressDeadlineSeconds: 600
selector:
matchLabels:
app: {{ $key }}
replicas: {{ $.Values.replicas | default 1 }}
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
template:
metadata:
labels:
app: {{ $key }}
spec:
imagePullSecrets:
- name: secret1
containers:
- image: {{ $.Values.image.repository }}/{{ $key }}:latest
name: {{ $key }}
imagePullPolicy: IfNotPresent
ports:
{{- range .Values.containerports }}
- containerPort: {{ . | title | quote }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
如果范围内有多个集装箱港口,我该如何迭代?使用上面的模板,我收到的错误为:
<.Values.containerports>: nil pointer evaluating interface {}.containerports
非常感谢任何帮助。
更改 values.yaml 使
containerports
都是数组
apps:
test-app1:
deploy: true
containerports:
- 8000
- 8081
test-app2:
deploy: true
containerports:
- 6000
否则Helm会抛出错误
range can't iterate over 6000
然后将
deployment.yaml中的
ports
块更改为
ports:
{{- range $key, $value := $value.containerports }}
- containerPort: {{ . }}
{{- end }}
您可能还想在资源之间添加分隔符,否则只会创建一个部署(在您的情况下为test-app2)。
整个deployment.yaml应该像这样
{{- if .Values.apps }}
{{- range $key, $value := .Values.apps }}
{{- if $value.deploy }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ $key }}
spec:
progressDeadlineSeconds: 600
selector:
matchLabels:
app: {{ $key }}
replicas: {{ $.Values.replicas | default 1 }}
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
template:
metadata:
labels:
app: {{ $key }}
spec:
containers:
- image: {{ $.Values.image.repository }}/{{ $key }}:latest
name: {{ $key }}
imagePullPolicy: IfNotPresent
ports:
{{- range $key, $value := $value.containerports }}
- containerPort: {{ . }}
{{- end }}
{{- end }}
---
{{- end }}
{{- end }}