helm:如何在 toYaml 函数后删除换行符
helm: how to remove newline after toYaml function
来自官方文档:
当模板引擎运行时,它会删除 {{ 和 }} 中的内容,但它会保留剩余的 whitespace 原样。可以用特殊字符修改模板声明的花括号语法,以告诉模板引擎选择 whitespace。 {{-(加上破折号和 space)表示白色 space 应该向左切,而 -}} 表示白色 space 应该被消耗掉。
但我尝试了所有变体,但都没有成功。有没有人解决如何将 yaml 放在 yaml 中?我不想使用 range
apiVersion: v1
kind: Pod
metadata:
name: app
labels:
app: app
spec:
containers:
- name: app
image: image
volumeMounts:
- mountPath: test
name: test
resources:
{{ toYaml .Values.pod.resources | indent 6 }}
volumes:
- name: test
emptyDir: {}
当我在没有 -}}
的情况下使用此代码时,它会添加一个换行符:
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 20m
memory: 64Mi
volumes:
- name: test
emptyDir: {}
但是当我使用 -}}
时,它与另一个位置连接。
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 20m
memory: 64Mi
volumes: <- shoud be in indent 2
- name: test
emptyDir: {}
values.yaml 是
pod:
resources:
requests:
cpu: 20m
memory: 64Mi
limits:
cpu: 100m
memory: 128Mi
以下变体是正确的:
{{ toYaml .Values.pod.resources | indent 6 }}
在这里添加换行符不会产生任何问题。
我试过你的 pod.yaml
并收到以下错误:
$ helm install .
Error: release pilfering-pronghorn failed: Pod "app" is invalid: spec.containers[0].volumeMounts[0].mountPath: Invalid value: "test": must be an absolute path
这意味着 volumeMounts
的 mountPath
应该类似于 /mnt
.
因此,以下 pod.yaml
工作得很好,并使用我们在 values.yaml
中定义的确切资源创建了一个 pod:
apiVersion: v1
kind: Pod
metadata:
name: app
labels:
app: app
spec:
containers:
- name: app
image: image
volumeMounts:
- mountPath: /mnt
name: test
resources:
{{ toYaml .Values.pod.resources | indent 6 }}
volumes:
- name: test
emptyDir: {}
@Nickolay,根据 helm 的说法,它 不是 一个有效的 yaml 文件 - 至少 helm barfs 并说:
error converting YAML to JSON: yaml: line 51: did not find expected key
对我来说,第 51 行是空行 space - 后面的内容不应缩进到同一级别
这对我有用:
{{ toYaml .Values.pod.resources | trim | indent 6 }}
{{- toYaml .Values.pod.resources |缩进 6 -}}
这将删除一个新行
来自官方文档: 当模板引擎运行时,它会删除 {{ 和 }} 中的内容,但它会保留剩余的 whitespace 原样。可以用特殊字符修改模板声明的花括号语法,以告诉模板引擎选择 whitespace。 {{-(加上破折号和 space)表示白色 space 应该向左切,而 -}} 表示白色 space 应该被消耗掉。
但我尝试了所有变体,但都没有成功。有没有人解决如何将 yaml 放在 yaml 中?我不想使用 range
apiVersion: v1
kind: Pod
metadata:
name: app
labels:
app: app
spec:
containers:
- name: app
image: image
volumeMounts:
- mountPath: test
name: test
resources:
{{ toYaml .Values.pod.resources | indent 6 }}
volumes:
- name: test
emptyDir: {}
当我在没有 -}}
的情况下使用此代码时,它会添加一个换行符:
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 20m
memory: 64Mi
volumes:
- name: test
emptyDir: {}
但是当我使用 -}}
时,它与另一个位置连接。
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 20m
memory: 64Mi
volumes: <- shoud be in indent 2
- name: test
emptyDir: {}
values.yaml 是
pod:
resources:
requests:
cpu: 20m
memory: 64Mi
limits:
cpu: 100m
memory: 128Mi
以下变体是正确的:
{{ toYaml .Values.pod.resources | indent 6 }}
在这里添加换行符不会产生任何问题。
我试过你的 pod.yaml
并收到以下错误:
$ helm install .
Error: release pilfering-pronghorn failed: Pod "app" is invalid: spec.containers[0].volumeMounts[0].mountPath: Invalid value: "test": must be an absolute path
这意味着 volumeMounts
的 mountPath
应该类似于 /mnt
.
因此,以下 pod.yaml
工作得很好,并使用我们在 values.yaml
中定义的确切资源创建了一个 pod:
apiVersion: v1
kind: Pod
metadata:
name: app
labels:
app: app
spec:
containers:
- name: app
image: image
volumeMounts:
- mountPath: /mnt
name: test
resources:
{{ toYaml .Values.pod.resources | indent 6 }}
volumes:
- name: test
emptyDir: {}
@Nickolay,根据 helm 的说法,它 不是 一个有效的 yaml 文件 - 至少 helm barfs 并说:
error converting YAML to JSON: yaml: line 51: did not find expected key
对我来说,第 51 行是空行 space - 后面的内容不应缩进到同一级别
这对我有用:
{{ toYaml .Values.pod.resources | trim | indent 6 }}
{{- toYaml .Values.pod.resources |缩进 6 -}}
这将删除一个新行