2

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?

Sara June
  • 389
  • 4
  • 15
  • 1
    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 – mdaniel Oct 08 '21 at 01:22

1 Answers1

0

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.

moonkotte
  • 290
  • 1
  • 8