如何从一个 YAML 文件中解析 PodSpec.spec.imagePullSecrets?

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

How to parse PodSpec.spec.imagePullSecrets from a yaml file?

问题

我想使用Go语言解析以下结构:

---
prjA:
  user1:
    metadata:
      namespace: prj-ns
    spec:
      containers:
        - image: some-contaner:latest
          name: containerssh-client-image
          resources:
            limits:
              ephemeral-storage: 4Gi
            requests:
              ephemeral-storage: 2Gi
      securityContext:
        runAsGroup: 1000
        runAsNonRoot: true
        runAsUser: 1000
      imagePullSecrets:
        - docker-registry-secret

我正在使用sigs.k8s.io/yaml来解析YAML:

var userConfig map[string]map[string]kubernetes.PodConfig
err = yaml.UnmarshalStrict(yamlFile, &userConfig)

其中kubernetes是从github.com/containerssh/kubernetes导入的。一切都正常工作,除了imagePullSecrets,它给出了以下错误:

ERROR unmarshal user config file; error [error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go struct field PodSpec.spec.imagePullSecrets of type v1.LocalObjectReference]

在Go中指定/解析imagePullSecrets的正确方法是什么?

英文:

I want to parse the following structure using go:

---
prjA:
  user1:
    metadata:
      namespace: prj-ns
    spec:
      containers:
        - image: some-contaner:latest
          name: containerssh-client-image
          resources:
            limits:
              ephemeral-storage: 4Gi
            requests:
              ephemeral-storage: 2Gi
      securityContext:
        runAsGroup: 1000
        runAsNonRoot: true
        runAsUser: 1000
      imagePullSecrets:
        - docker-registry-secret

I'm using sigs.k8s.io/yaml to unmarshal YAML:

var userConfig map[string]map[string]kubernetes.PodConfig
err = yaml.UnmarshalStrict(yamlFile, &userConfig)

where kubernetes is imported from github.com/containerssh/kubernetes. Everything works fine - except the immagePullSecrets which gives the following error:

ERROR unmarshal user config file; error [error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go struct field PodSpec.spec.imagePullSecrets of type v1.LocalObjectReference]

What is the correct way to specify / parse an imagePullSecrets in go?

答案1

得分: 3

这是一个关于输入的问题,也许错误信息不够清晰。

imagePullSecrets 必须使用 name 键来指定,例如:

imagePullSecrets:
  - name: docker-registry-secret

我将保留这个问题,因为它可能会帮助其他遇到相同问题的人。

英文:

This is a problem with the input - and maybe not a very clear error message.

The imagePullSecrets must be specified using the key name like:

imagePullSecrets:
  - name: docker-registry-secret

I leave the question as it might help other people who run in the same problem.

huangapple
  • 本文由 发表于 2022年1月20日 22:36:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/70788081.html
匿名

发表评论

匿名网友

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

确定