2

I have been trying to figure out how to consume a ConfigMap created using a ConfigMap generator via Kustomize.

When created using Kustomize generators, the configMaps are named with a special suffix. See here:

https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-a-configmap-from-generator

Question is how can this be referenced?

user3123834
  • 21
  • 1
  • 2

3 Answers3

2

You don't reference it yourself. Kustomize recognizes where the configMap is used in the other resources (like a Deployment) and changes those references to use the name+hash.

The reason for this is so that if you change the configMap, Kustomize generates a new hash and updates the Deployment, causing a rolling restart of the Pods.

If you don't want this behavior, you can add the following to your kustomization.yaml file:

generatorOptions:
  disableNameSuffixHash: true
monachus
  • 566
  • 4
  • 7
1

It is specified there in the doc. When you do kubectl apply -k . a configmap created named game-config-4-m9dm2f92bt. You can check that the ConfigMap was created like this: kubectl get configmap. This ConfigMap will contains a field data where your given datas will belong.

Now as usual you can use this configmap in a pod. Like below:

Ex from k8s:

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        # Define the environment variable
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name: special-config
              # Specify the key associated with the value
              key: special.how
  restartPolicy: Never

You can use ConfigMap as volume also, like this example from k8s doc:

apiVersion: v1
kind: Pod
metadata:
  name: configmap-demo-pod
spec:
  containers:
    - name: demo
      image: alpine
      command: ["sleep", "3600"]
      env:
        # Define the environment variable
        - name: PLAYER_INITIAL_LIVES # Notice that the case is different here
                                     # from the key name in the ConfigMap.
          valueFrom:
            configMapKeyRef:
              name: game-demo           # The ConfigMap this value comes from.
              key: player_initial_lives # The key to fetch.
        - name: UI_PROPERTIES_FILE_NAME
          valueFrom:
            configMapKeyRef:
              name: game-demo
              key: ui_properties_file_name
      volumeMounts:
      - name: config
        mountPath: "/config"
        readOnly: true
  volumes:
    # You set volumes at the Pod level, then mount them into containers inside that Pod
    - name: config
      configMap:
        # Provide the name of the ConfigMap you want to mount.
        name: game-demo
        # An array of keys from the ConfigMap to create as files
        items:
        - key: "game.properties"
          path: "game.properties"
        - key: "user-interface.properties"
          path: "user-interface.properties

You can see k8s official doc

Sahadat Hossain
  • 3,583
  • 2
  • 12
  • 19
  • 1
    this did not appear to work. The ConfigMap is named 'game-config-4-m9dm2f92bt'. But you are referencing a 'game-config'. Are you telling me that your installation found 'game-config-4-m9dm2f92bt' when searching for 'game-config'? – user3123834 Mar 08 '21 at 22:23
  • The question is how is this done programmatically. The code to create uses 'game-config-4'. But 'game-config-4-m9dm2f92bt' gets created. And the code to consume, should also be referencing 'game-config-4', without knowing ahead of time whatever the tag may be added. Is there some qualification that says something like 'game-config-4:latest' or with some time period? – user3123834 Mar 08 '21 at 22:47
1

I was struggling with this too. I could not figure out why kustomize was not updating the configmap name for the volume in the deployment to include the hash. What solved this for me was to add namespace: <namespace> in the kustomization.yaml for both the base and overlay.

  • That was the solution for me, too. I removed `namespace` from all files except from the base `kustomization.file` and it solved the problem. In the base `kustomization.yaml` file, with `configMapGenerator[0].name` set to `my-configmap`, I ended up in my deployment with a translated `configMapKeyRef.name` correctly set to `dev-my-configmap-g75875hhh9` - not erroneously set to `my-configmap`. – Ivan dal Bosco Feb 28 '23 at 14:00