如何展平从 configMapGenerator 生成的 configMap?

How to flatten generated configMap from configMapGenerator?

我正在尝试使用 Kustomize 从包含键值对的文件生成 ConfigMap。我的 kustomization 文件包含以下内容:

configMapGenerator:
    - name: my-config
      files:
          - environment.properties

environment.properties 文件包含以下内容:

FIRST: 1
SECOND: 2
LAST: 3

生成的输出包含以下内容:

apiVersion: v1
data:
  environment.properties: |
    FIRST: 1
    SECOND: 2
    LAST: 3
kind: ConfigMap

如何展平这个数据结构?我宁愿有这个输出:

apiVersion: v1
data:
  FIRST: 1
  SECOND: 2
  LAST: 3
kind: ConfigMap

这不是扁平化。这将删除存在这些 key=value 对的文件名。

如果你想要像你展示的那样简单的平面输出,你应该使用 literals:

configMapGenerator: 
- name: the-map 
  literals: 
    - FIRST: 1
    - SECOND: 2
    - LAST: 3

这在 # Kustomization.yaml Reference

上有解释

files []string

List of files to generate ConfigMap data entries from. Each item should be a path to a local file, e.g. path/to/file.config, and the filename will appear as an entry in the ConfigMap data field with its contents as a value.

literals []string

List of literal ConfigMap data entries. Each item should be a key and literal value, e.g. somekey=somevalue, and the key/value will appear as an entry in the ConfigMap data field.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
# generate a ConfigMap named my-java-server-props-<some-hash> where each file
# in the list appears as a data entry (keyed by base filename).
- name: my-java-server-props
  files:
  - application.properties
  - more.properties
# generate a ConfigMap named my-java-server-env-vars-<some-hash> where each literal
# in the list appears as a data entry (keyed by literal key).
- name: my-java-server-env-vars
  literals:    
  - JAVA_HOME=/opt/java/jdk
  - JAVA_TOOL_OPTIONS=-agentlib:hprof
# generate a ConfigMap named my-system-env-<some-hash> where each key/value pair in the
# env.txt appears as a data entry (separated by \n).
- name: my-system-env
  env: env.txt

编辑:

正如 @Steven Liekens 所指出的,这回答了问题:

env string

Single file to generate ConfigMap data entries from. Should be a path to a local env file, e.g. path/to/file.env, where each line of the file is a key=value pair. Each line will appear as an entry in the ConfigMap data field.