kuberneteskustomize

How to add specific label to ConfigMap using Kustomize


I am searching for a way to add a specific label to ConfigMap using Kustomize. Using commonLabels is not an option because it adds a label to every resource. For config map generation, I am using the following:

configMapGenerator:
- name: ${REPOSITORY_NAME}-env
  envs:
  - .env

And I would like to achieve something like this:

configMapGenerator:
- name: ${REPOSITORY_NAME}-env
  labels:
    key: value
  envs:
  - .env

Solution

  • As per this git link you can add specific labels by using patch files in kustomize.

    apiVersion: v1
    data:
      a: "0"
    kind: ConfigMap
    metadata:
      labels:
        apps: a
      name: a-config-map-26kgmbk2md
    

    Then add these in the configMapGenerator

    configMapGenerator:
      - name: a-config-map
        envs:
          - a.properties
    patchesStrategicMerge:
      - patch-a.yaml
    

    This will add the specific label to individual configMaps to the configmap generator.

    Additionally you can also refer to this git issue for additional information.