使用 helm 表示 k8s ConfigMap 中的 Golang Map 对象

问题描述 投票:0回答:1

我在 golang 中有一个映射变量作为我的配置:

MyConfig map[int64][]int64 `mapstructure:"my-config"`

我尝试在我的 helm 文件中定义此配置,如下所示:

配置图:

my-config: {{ .Values.config.myconfig }}

价值观:

myconfig:
  1: [2]
  2: [1]

但它呈现为:

my-config: map[1:[2] 2:[1]]

我还更改了我的配置结构,如下所示:

应用:

MyConfig []MyConfigItem `mapstructure:"my-config"`

MyConfigItem struct {
    FirstPart  int64   `mapstructure:"first-part"   validate:"required"`
    SecondPart []int64 `mapstructure:"second-part"  validate:"required"`
}

配置图:

my-config: {{ .Values.config.myconfig }}

价值观:

myConfig:
  - first-part: 1
    second-part: 
      - 2
  - first-part: 2
    second-part: 
      - 1

但它呈现为:

my-config: [map[first-part:1 second-part:[2]] map[first-part:2 second-part:[1]]] 
go kubernetes kubernetes-helm configmap
1个回答
0
投票

我已经找到了第二种方式的解决方案。这就是我设置配置映射文件的方式:

my-config: 
{{- range .Values.config.myConfig }}
  - {{ . | toYaml | indent 4 | trim }}
{{- end }}
{{- else }}
  []
{{- end }}

我在这里找到了解决方案:https://stackoverflow.com/a/64830481/1626977

但是在第一种模式上应用此解决方案并没有产生正确的配置。渲染的配置是:

my-config:
  - "1":
    - 2
  - "2":
    - 2

键是字符串,但它们应该是整数。

我也会进行更多搜索以找到第一种模式的正确解决方案;如果我找到任何答案,我会更新答案。

© www.soinside.com 2019 - 2024. All rights reserved.