configMapGenerator

Generate ConfigMap resources.

Each entry in this list results in the creation of one ConfigMap resource (it’s a generator of n maps).

The example below creates four ConfigMaps:

  • first, with the names and contents of the given files
  • second, with key/value as data using key/value pairs from files
  • third, also with key/value as data, directly specified using literals
  • and a fourth, which sets an annotation and label via options for that single ConfigMap

Each configMapGenerator item accepts a parameter of behavior: [create|replace|merge]. This allows an overlay to modify or replace an existing configMap from the parent.

Also, each entry has an options field, that has the same subfields as the kustomization file’s generatorOptions field.

This options field allows one to add labels and/or annotations to the generated instance, or to individually disable the name suffix hash for that instance. Labels and annotations added here will not be overwritten by the global options associated with the kustomization file generatorOptions field. However, due to how booleans behave, if the global generatorOptions field specifies disableNameSuffixHash: true, this will trump any attempt to locally override it.

  1. apiVersion: kustomize.config.k8s.io/v1beta1
  2. kind: Kustomization
  3. # These labels are added to all configmaps and secrets.
  4. generatorOptions:
  5. labels:
  6. fruit: apple
  7. configMapGenerator:
  8. - name: my-java-server-props
  9. behavior: merge
  10. files:
  11. - application.properties
  12. - more.properties
  13. - name: my-java-server-env-file-vars
  14. envs:
  15. - my-server-env.properties
  16. - more-server-props.env
  17. - name: my-java-server-env-vars
  18. literals:
  19. - JAVA_HOME=/opt/java/jdk
  20. - JAVA_TOOL_OPTIONS=-agentlib:hprof
  21. options:
  22. disableNameSuffixHash: true
  23. labels:
  24. pet: dog
  25. - name: dashboards
  26. files:
  27. - mydashboard.json
  28. options:
  29. annotations:
  30. dashboard: "1"
  31. labels:
  32. app.kubernetes.io/name: "app1"

It is also possible to define a key to set a name different than the filename.

The example below creates a ConfigMap with the name of file as myFileName.ini while the actual filename from which the configmap is created is whatever.ini.

  1. apiVersion: kustomize.config.k8s.io/v1beta1
  2. kind: Kustomization
  3. configMapGenerator:
  4. - name: app-whatever
  5. files:
  6. - myFileName.ini=whatever.ini

ConfigMap from File

ConfigMap Resources may be generated from files - such as a java .properties file. To generate a ConfigMap Resource for a file, add an entry to configMapGenerator with the filename.

Example: Generate a ConfigMap with a data item containing the contents of a file.

The ConfigMaps will have data values populated from the file contents. The contents of each file will appear as a single data item in the ConfigMap keyed by the filename.

The example illustrates how you can create ConfigMaps from File using Generators.

File Input

  1. # kustomization.yaml
  2. apiVersion: kustomize.config.k8s.io/v1beta1
  3. kind: Kustomization
  4. configMapGenerator:
  5. - name: my-application-properties
  6. files:
  7. - application.properties
  1. # application.properties
  2. FOO=Bar

Build Output

  1. apiVersion: v1
  2. data:
  3. application.properties: |-
  4. FOO=Bar
  5. kind: ConfigMap
  6. metadata:
  7. name: my-application-properties-f7mm6mhf59

ConfigMap from Literals

ConfigMap Resources may be generated from literal key-value pairs - such as JAVA_HOME=/opt/java/jdk. To generate a ConfigMap Resource from literal key-value pairs, add an entry to configMapGenerator with a list of literals.

Literal Syntax

  • The key/value are separated by a = sign (left side is the key)
  • The value of each literal will appear as a data item in the ConfigMap keyed by its key.

Example: Create a ConfigMap with 2 data items generated from literals.

The example illustrates how you can create ConfigMaps from Literals using Generators.

File Input

  1. # kustomization.yaml
  2. apiVersion: kustomize.config.k8s.io/v1beta1
  3. kind: Kustomization
  4. configMapGenerator:
  5. - name: my-java-server-env-vars
  6. literals:
  7. - JAVA_HOME=/opt/java/jdk
  8. - JAVA_TOOL_OPTIONS=-agentlib:hprof

Build Output

  1. apiVersion: v1
  2. data:
  3. JAVA_HOME: /opt/java/jdk
  4. JAVA_TOOL_OPTIONS: -agentlib:hprof
  5. kind: ConfigMap
  6. metadata:
  7. name: my-java-server-env-vars-44k658k8gk

ConfigMap from env file

ConfigMap Resources may be generated from key-value pairs much the same as using the literals option but taking the key-value pairs from an environment file. These generally end in .env. To generate a ConfigMap Resource from an environment file, add an entry to configMapGenerator with a single env entry, e.g. env: config.env.

Environment File Syntax

  • The key/value pairs inside of the environment file are separated by a = sign (left side is the key)
  • The value of each line will appear as a data item in the ConfigMap keyed by its key.

Example: Create a ConfigMap with 3 data items generated from an environment file.

File Input

  1. # kustomization.yaml
  2. apiVersion: kustomize.config.k8s.io/v1beta1
  3. kind: Kustomization
  4. configMapGenerator:
  5. - name: tracing-options
  6. env: tracing.env
  1. # tracing.env
  2. ENABLE_TRACING=true
  3. SAMPLER_TYPE=probabilistic
  4. SAMPLER_PARAMETERS=0.1

Build Output

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. # The name has had a suffix applied
  5. name: tracing-options-6bh8gkdf7k
  6. # The data has been populated from each literal pair
  7. data:
  8. ENABLE_TRACING: "true"
  9. SAMPLER_TYPE: "probabilistic"
  10. SAMPLER_PARAMETERS: "0.1"

Overriding Base ConfigMap Values

ConfigMaps Values from Bases may be overridden by adding another generator for the ConfigMap in the Variant and specifying the behavior field. behavior may be one of create (default value), replace (replace the base ConfigMap), or merge (add or update the values the ConfigMap). See Bases and Variantions for more on using Bases. e.g. behavior: "merge"

Propagating the Name Suffix

Letting ConfigMap or Secret know the name of the generated Resource name suffix

Workloads that reference the ConfigMap or Secret will need to know the name of the generated Resource including the suffix, however Apply takes care of this automatically for users. Apply will identify references to generated ConfigMaps and Secrets, and update them.

The generated ConfigMap name will be my-java-server-env-vars with a suffix unique to its contents. Changes to the contents will change the name suffix, resulting in the creation of a new ConfigMap, and transform Workloads to point to this one.

The PodTemplate volume references the ConfigMap by the name specified in the generator (excluding the suffix). Apply will update the name to include the suffix applied to the ConfigMap name.

Input: The kustomization.yaml and deployment.yaml files

  1. # kustomization.yaml
  2. apiVersion: kustomize.config.k8s.io/v1beta1
  3. kind: Kustomization
  4. configMapGenerator:
  5. - name: my-java-server-env-vars
  6. literals:
  7. - JAVA_HOME=/opt/java/jdk
  8. - JAVA_TOOL_OPTIONS=-agentlib:hprof
  9. resources:
  10. - deployment.yaml
  1. # deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: test-deployment
  6. labels:
  7. app: test
  8. spec:
  9. selector:
  10. matchLabels:
  11. app: test
  12. template:
  13. metadata:
  14. labels:
  15. app: test
  16. spec:
  17. containers:
  18. - name: container
  19. image: k8s.gcr.io/busybox
  20. command: [ "/bin/sh", "-c", "ls /etc/config/" ]
  21. volumeMounts:
  22. - name: config-volume
  23. mountPath: /etc/config
  24. volumes:
  25. - name: config-volume
  26. configMap:
  27. name: my-java-server-env-vars

Applied: The Resources that are Applied to the cluster.

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. # The name has been updated to include the suffix
  5. name: my-java-server-env-vars-k44mhd6h5f
  6. data:
  7. JAVA_HOME: /opt/java/jdk
  8. JAVA_TOOL_OPTIONS: -agentlib:hprof
  9. apiVersion: apps/v1
  10. kind: Deployment
  11. metadata:
  12. labels:
  13. app: test
  14. name: test-deployment
  15. spec:
  16. selector:
  17. matchLabels:
  18. app: test
  19. template:
  20. metadata:
  21. labels:
  22. app: test
  23. spec:
  24. containers:
  25. - command:
  26. - /bin/sh
  27. - -c
  28. - ls /etc/config/
  29. image: k8s.gcr.io/busybox
  30. name: container
  31. volumeMounts:
  32. - mountPath: /etc/config
  33. name: config-volume
  34. volumes:
  35. - configMap:
  36. # The name has been updated to include the
  37. # suffix matching the ConfigMap
  38. name: my-java-server-env-vars-k44mhd6h5f
  39. name: config-volume