Score:2

How to load configmap from a properties file using kustomize?

cn flag

I have tried using kustomize to load properties file as a configmap.

For that, I created a sample set as in github link.

With base files:

#kustomize build base
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

With external file:

#kustomize build file
apiVersion: v1
data:
  config: "dbport=156767\r\ndcname=dfsd\r\ndbssl=false\r\nlocktime=300\r\ndomainuser=somedts"
kind: ConfigMap
metadata:
  labels:
    env: dev
    owner: sara
  name: dev-database-configmap
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
    env: dev
    owner: sara
  name: dev-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      env: dev
      owner: sara
  template:
    metadata:
      labels:
        app: nginx
        env: dev
        owner: sara
    spec:
      containers:
      - image: nginx
        name: nginx

If you observe the configmap | is removed and also replaced by \r\n as a single string. How to fix this alignment?

in flag
Well, the `\r` characters aside, those two forms are identical; the `: |` scalar quoting for is just for human consumption -- by the time it gets into the cluster it is of the form `config: "whatever\nwhatever-else\n"` as `yaml2json` will show for both forms, or, of course, how it actually materializes in a Pod
Score:0
in flag

Posting this as community wiki, feel free to edit and expand.


As @mdaniel mentioned in comment:

Well, the \r characters aside, those two forms are identical; the : | scalar quoting for is just for human consumption -- by the time it gets into the cluster it is of the form config: "whatever\nwhatever-else\n" as yaml2json will show for both forms, or, of course, how it actually materializes in a Pod

You can check this by getting the configmap details from kubernetes cluster in json and see that they are stored in the same way (except for additional \r which is mentioned above):

$ kubectl get cm database-configmap -o json
{
    "apiVersion": "v1",
    "data": {
        "config": "dbport=1234\ndcname=sfsdf\ndbssl=false\nlocktime=300\ndomainuser="
    },
    "kind": "ConfigMap",
    ...

and

$ kubectl get cm dev-database-configmap -o json
{
    "apiVersion": "v1",
    "data": {
        "config": "dbport=156767\r\ndcname=dfsd\r\ndbssl=false\r\nlocktime=300\r\ndomainuser=somedts"
    },
    "kind": "ConfigMap",
    ...

There's an answer on StackOverflow which shortly shows the difference between \n , \r and \r\n.

Wytrzymały Wiktor avatar
it flag
Hello @SaraJune. Does this [answer your question](https://stackoverflow.com/help/someone-answers)?
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.