helm:将YAML转换为JSON时出错:yaml:第xx行:未找到预期的键

huangapple go评论104阅读模式
英文:

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.

source

huangapple
  • 本文由 发表于 2023年2月14日 00:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定