英文:
helm : error converting YAML to JSON: yaml: line xx: did not find expected key
问题
我可以帮你翻译这段内容。以下是翻译的结果:
我在我的values.yaml文件中有一个JSON编码的字符串 -
values.yaml
network:
cidrs: "[\"123.123.123.123/32\",\"123.124.125.125/32\"]"
现在,我想将这个值作为字符串列表在我的网络策略出口ipblock中使用。但是我无法将其转换为列表。
目前,我正在按照以下方式实现需求,但失败了 -
error converting YAML to JSON: yaml: line xx : did not find expected key
netpol.yaml
spec:
podSelector:
matchLabels:
name: log-forwarder
policyTypes:
- Egress
egress:
{{- $json := .Values.network.cidrs | fromJson -}}
{{- range $json }}
- to:
- ipBlock:
cidr: {{- . }}
{{- end }}
ports:
- protocol: TCP
port: 443
有什么办法可以将编码的字符串转换为字符串列表并在我的网络策略中使用吗?
英文:
I have a json encoded string in my values.yaml file ->
values.yaml
network:
cidrs : "[\"123.123.123.123/32\",\"123.124.125.125/32\"]"
Now , I want to use this value as a list of string in my network policy egress ipblock. But I'm not able to convert it to list.
Currently, I'm following this to achieve the requirement , but it fails -
> error converting YAML to JSON: yaml: line xx : did not find expected key
netpol.yaml
spec:
podSelector:
matchLabels:
name: log-forwarder
policyTypes:
- Egress
egress:
{{- $json := .Values.network.cidrs | fromJson -}}
{{- range $json }}
- to:
- ipBlock:
cidr: {{- . }}
{{- end }}
ports:
- protocol: TCP
port: 443
Any idea , how to convert the encoded string to list of string and use it in my network policy ?
答案1
得分: 2
使用mustFromJson
而不是fromJson
,最近遇到了相同的问题,使用mustFromJson
解决了问题,我会查看文档,看看是否能找到原因。
编辑:由于某种原因,fromJson
无法处理顶层列表,但mustFromJson
可以,看起来像是一个错误,因为文档中唯一列出的区别是mustFromJson
在JSON无效时返回错误。
英文:
Use mustFromJson
instead of fromJson
, had the same problem recently and that fixed it, gonna have a look in the docs to see if I find out why.
edit: for some reason fromJson
cant handle top level lists, but mustFromJson
can, looks like a bug as the only difference listed in the docs is that mustFromJson
returns an error in case the JSON is invalid.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论