kubernetesconfigmapkustomize

Generate configmap using Kustomization, but the name in `configMapRef` doesn't include the suffix


The project structure of my k8s YAML manifests with kustomization is like this:

.
├── app
│   ├── base
│   │   └── kustomization.yaml
|   |   └── my-configmap.yaml
|   |   └── my-deploy.yaml  
|   |   └── my-svc.yaml  
│   ├── overlay
│       └── kustomization.yaml

In base's kustomization.yaml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - my-configmap.yml
  - my-deploy.yml
  - my-svc.yml
configMapGenerator:
  - name: db
    literals:
      - SQLITE_DB_LOCATION=/tmp/db/my.db

As you can see above, I have declared configMapGenerator with name db.

In base's my-deploy.yaml :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deploy
  namespace: my-app
  labels:
    app: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: foo123/my-app:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9000
        envFrom:
        - configMapRef:
            name: my-configmap
        - configMapRef:
            name: db   # I refer to the generator's ConfigMap here!

As you can see above, under container's envFrom, I have two configMapRef, the 2nd one refers to name db.

Under app/base/ directory, I build kustomization with command kustomize build . | cat, the output YAML shows me:

---
apiVersion: v1
data:
  SQLITE_DB_LOCATION: /tmp/db/my.db
kind: ConfigMap
metadata:
  name: db-mmm78m6dgk  # The generated ConfigMap has suffix in name, that is kustomize's default behaviour.
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: my-app
  name: my-deploy
  namespace: my-app
spec:
  replicas: 2
  template:
    ...
    spec:
      containers:
      - envFrom:
        - configMapRef:
            name: sp-oauth-config
        - configMapRef:
            name: db  # I wished Kustomize will use name `db-mmm78m6dgk` here, but it doesn't...

As you can see above built output, the ConfigMap generated by kustomization is named db-mmm78m6dgk, I fully understand that kustomization by default append suffix to the name.

But I also expect kustomization is able to automatically use the same name when referring the ConfigMap in container's configMapRef field, however, the above output shows me the final referred name is db. WHY?

Why kustomization doesn't use db-mmm78m6dgk for container's configMapRef field but db??


I installed standalone Kustomize tool, version v5.2.1


Solution

  • This is due to the namespace: my-app metadata in the Deployment. Remove it and it should be fine :)

    I don't know why having the namespace is a problem tbh, I went through the same thing a couple of months ago and I found this issue that helped me. You can still specify the namespace inside your kustomization.yaml.