Kubernetes deployment is missing Kustomize's hash suffixes

5/19/2019

I'm new to Kubernetes. In my project I'm trying to use Kustomize to generate configMaps for my deployment. Kustomize adds a hash after the configMap name, but I can't get it to also change the deployment to use that new configMap name.

Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: env-receiver-deployment
  labels:
    app: env-receiver-app
    project: env-project
spec:
  replicas: 1
  selector:
    matchLabels:
      app: env-receiver-app
  template:
    metadata:
      labels:
        app: env-receiver-app
        project: env-project
    spec:
      containers:
      - name: env-receiver-container
        image: eu.gcr.io/influxdb-241011/env-receiver:latest
        resources: {}
        ports:
        - containerPort: 8080
        envFrom:
        - configMapRef:
            name: env-receiver-config
        args: [ "-port=$(ER_PORT)", "-dbaddr=$(ER_DBADDR)", "-dbuser=$(ER_DBUSER)", "-dbpass=$(ER_DBPASS)" ]

kustomize.yml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: env-receiver-config
  literals:
  - ER_PORT=8080
  - ER_DBADDR=http://localhost:8086
  - ER_DBUSER=writeuser
  - ER_DBPASS=writeuser

Then I run kustomize, apply the deployment and check if it did apply the environment.

$ kubectl apply -k .
configmap/env-receiver-config-258g858mgg created
$ kubectl apply -f k8s/deployment.yml
deployment.apps/env-receiver-deployment unchanged
$ kubectl describe pod env-receiver-deployment-76c678dcf-5r2hl 
Name:               env-receiver-deployment-76c678dcf-5r2hl
[...]
    Environment Variables from:
      env-receiver-config  ConfigMap  Optional: false
    Environment:           <none>
[...]

But it still gets its environment variables from: env-receiver-config, not env-receiver-config-258g858mgg.

My current workaround is to disable the hash suffixes in the kustomize.yml.

generatorOptions:
  disableNameSuffixHash: true

It looks like I'm missing a step to tell the deployment the name of the new configMap. What is it?

-- Laurenz
kubernetes
kustomize

1 Answer

6/5/2019

It looks like the problem come from the fact that you generate the config map through kustomize but the deployment via kubectl directly without using kustomize.

Basically, kustomize will look for all the env-receiver-config in all your resources and replace them by the hash suffixed version.

For it to work, all your resources have to go through kustomize. To do so, you need to add to your kustomization.yml:

resources:
  - yourDeployment.yml

and then just run kubectl apply -k .. It should create both the ConfigMap and the Deployment using the right ConfigMap name

-- ITChap
Source: StackOverflow