Score:1

How to load ConfigMap in Kustomize YAML file?

cn flag

My original YAML

base/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - image: nginx
          name: nginx
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: database-configmap
data:
  config: |
    dbport=1234
    dcname=sfsdf
    dbssl=false
    locktime=300
    domainuser=

base/Kustomization.yaml

resources:
  - deployment.yaml

commonLabels:
  owner: sara

From the parent folder of base:

kustomize build base

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
    owner: sara
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      owner: sara
  template:
    metadata:
      labels:
        app: nginx
        owner: sara
    spec:
      containers:
      - image: nginx
        name: nginx

If you observe above, the ConfigMap is being discarded, please suggest how to fix that.

Mikolaj S. avatar
cn flag
I just tried and it's working fine on kustomize `v4.4.0`, which version are you using (`kustomize version` command)?
Sara June avatar
cn flag
kustomize version {Version:kustomize/v4.1.3 GitCommit:0f614e92f72f1b938a9171b964d90b197ca8fb68 BuildDate:2021-05-20T20:52:40Z GoOs:windows GoArch:amd64}
Mikolaj S. avatar
cn flag
Just tried with `v4.1.3` and it's fine too....but two things to notice: on the output I have ConfigMap at the top, above the Deployoment. Maybe you missed it? The second one, I tried to use `Kustomization.yaml` name of file, but I got error `Error: unable to find one of 'kustomization.yaml', 'kustomization.yml' or 'Kustomization' in directory`. Please double check if you have a proper file name.
Sara June avatar
cn flag
Don't know how, after restart of the machine, it is working. Can you please check this question also https://serverfault.com/questions/1079866/how-to-load-configmap-from-a-properties-file-using-kustomize
Score:0
cn flag

In both versions of the Kustomize - the current newest (v.4.4.0) and v4.1.3 used in the question it is working correctly. The author notice that the restart can help:

after restart of the machine, it is working.

Keep in mind about two things:

  • base/Kustomization.yaml name can't be used; you will get an error Error: unable to find one of 'kustomization.yaml', 'kustomization.yml' or 'Kustomization' in directory. There is a need to use proper name.
  • After running command: kustomize build base the ConfigMap will be generated at the top of the output, even if it is defined at the bottom in the resource file. Check below.

Output of the kustomize build base command:

apiVersion: v1
data:
  config: |
    dbport=1234
    dcname=sfsdf
    dbssl=false
    locktime=300
    domainuser=
kind: ConfigMap
metadata:
  labels:
    owner: sara
  name: database-configmap
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
    owner: sara
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      owner: sara
  template:
    metadata:
      labels:
        app: nginx
        owner: sara
    spec:
      containers:
      - image: nginx
        name: nginx
Sara June avatar
cn flag
can you check this also https://serverfault.com/questions/1079866/how-to-load-configmap-from-a-properties-file-using-kustomize
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.