序列输出在使用地图对象时添加换行符
Output to a sequence add a newline when working with map objects
我想在我的 values.yaml 文件中使用定义的范围创建多个服务定义。
values.yaml
services:
type: ClusterIP
ports:
- name: https
port: 443
protocol: TCP
targetPort: 8443
- name: http
port: 80
protocol: TCP
targetPort: 8080
创建多个定义工作正常,但端口部分在开始序列时总是有一个换行符。
模板services.yaml
{{- $top := . -}}
{{- range $key, $val := .Values.services.ports -}}
apiVersion: v1
kind: Service
metadata:
name: {{ printf "%s-%s" ($.Values.fullnameOverride) ($val.name) }}
labels:
app: {{ printf "%s-%s" ($.Values.fullnameOverride) ($val.name) }}
spec:
type: {{ $.Values.services.type }}
ports:
- {{ toYaml $val | trim | nindent 4 }}
selector:
app: {{ $.Values.fullnameOverride }}
---
{{- end }}
但是当我执行上面的模板时,我得到以下输出:
---
# Source: arichnettools/templates/services.yaml
apiVersion: v1
kind: Service
metadata:
name: arichnettools-https
labels:
app: arichnettools-https
spec:
type: ClusterIP
ports:
-
name: https
port: 443
protocol: TCP
targetPort: 8443
selector:
app: arichnettools
---
# Source: arichnettools/templates/services.yaml
apiVersion: v1
kind: Service
metadata:
name: arichnettools-http
labels:
app: arichnettools-http
spec:
type: ClusterIP
ports:
-
name: http
port: 80
protocol: TCP
targetPort: 8080
selector:
app: arichnettools
---
我不知道如何在 ports:
之后去掉换行符 我不确定这是否会在部署期间给我一个错误,但这让我发疯。我也尝试过 indent
但到目前为止运气不好。
ports:
之后的这一新行不应导致任何错误,但您只需对模板进行一次修改即可轻松摆脱它。
而不是:
ports:
- {{ toYaml $val | trim | nindent 4 }}
尝试使用:
ports:
- {{ toYaml $val | indent 4 | trim }}
我们可以看到修改后效果如预期:
$ cat templates/services.yaml
{{- $top := . -}}
{{- range $key, $val := .Values.services.ports -}}
apiVersion: v1
kind: Service
metadata:
name: {{ printf "%s-%s" ($.Values.fullnameOverride) ($val.name) }}
labels:
app: {{ printf "%s-%s" ($.Values.fullnameOverride) ($val.name) }}
spec:
type: {{ $.Values.services.type }}
ports:
- {{ toYaml $val | indent 4 | trim }}
selector:
app: {{ $.Values.fullnameOverride }}
---
{{- end }}
$ helm template test .
---
# Source: mychart/templates/services.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-test-https
labels:
app: svc-test-https
spec:
type: ClusterIP
ports:
- name: https
port: 443
protocol: TCP
targetPort: 8443
selector:
app: svc-test
---
# Source: mychart/templates/services.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-test-http
labels:
app: svc-test-http
spec:
type: ClusterIP
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
selector:
app: svc-test
我想在我的 values.yaml 文件中使用定义的范围创建多个服务定义。
values.yaml
services:
type: ClusterIP
ports:
- name: https
port: 443
protocol: TCP
targetPort: 8443
- name: http
port: 80
protocol: TCP
targetPort: 8080
创建多个定义工作正常,但端口部分在开始序列时总是有一个换行符。
模板services.yaml
{{- $top := . -}}
{{- range $key, $val := .Values.services.ports -}}
apiVersion: v1
kind: Service
metadata:
name: {{ printf "%s-%s" ($.Values.fullnameOverride) ($val.name) }}
labels:
app: {{ printf "%s-%s" ($.Values.fullnameOverride) ($val.name) }}
spec:
type: {{ $.Values.services.type }}
ports:
- {{ toYaml $val | trim | nindent 4 }}
selector:
app: {{ $.Values.fullnameOverride }}
---
{{- end }}
但是当我执行上面的模板时,我得到以下输出:
---
# Source: arichnettools/templates/services.yaml
apiVersion: v1
kind: Service
metadata:
name: arichnettools-https
labels:
app: arichnettools-https
spec:
type: ClusterIP
ports:
-
name: https
port: 443
protocol: TCP
targetPort: 8443
selector:
app: arichnettools
---
# Source: arichnettools/templates/services.yaml
apiVersion: v1
kind: Service
metadata:
name: arichnettools-http
labels:
app: arichnettools-http
spec:
type: ClusterIP
ports:
-
name: http
port: 80
protocol: TCP
targetPort: 8080
selector:
app: arichnettools
---
我不知道如何在 ports:
之后去掉换行符 我不确定这是否会在部署期间给我一个错误,但这让我发疯。我也尝试过 indent
但到目前为止运气不好。
ports:
之后的这一新行不应导致任何错误,但您只需对模板进行一次修改即可轻松摆脱它。
而不是:
ports:
- {{ toYaml $val | trim | nindent 4 }}
尝试使用:
ports:
- {{ toYaml $val | indent 4 | trim }}
我们可以看到修改后效果如预期:
$ cat templates/services.yaml
{{- $top := . -}}
{{- range $key, $val := .Values.services.ports -}}
apiVersion: v1
kind: Service
metadata:
name: {{ printf "%s-%s" ($.Values.fullnameOverride) ($val.name) }}
labels:
app: {{ printf "%s-%s" ($.Values.fullnameOverride) ($val.name) }}
spec:
type: {{ $.Values.services.type }}
ports:
- {{ toYaml $val | indent 4 | trim }}
selector:
app: {{ $.Values.fullnameOverride }}
---
{{- end }}
$ helm template test .
---
# Source: mychart/templates/services.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-test-https
labels:
app: svc-test-https
spec:
type: ClusterIP
ports:
- name: https
port: 443
protocol: TCP
targetPort: 8443
selector:
app: svc-test
---
# Source: mychart/templates/services.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-test-http
labels:
app: svc-test-http
spec:
type: ClusterIP
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
selector:
app: svc-test