Helm - How to write a file in a Volume using ConfigMap?

5/8/2021

I have defined the values.yaml like the following:

name: custom-streams
image: streams-docker-images
imagePullPolicy: Always
restartPolicy: Always
replicas: 1
port: 8080
nodeSelector:
  nodetype: free
configHocon: |-
  streams {
          monitoring {
            custom {
              uri = ${?URI}
              method = ${?METHOD}
            }
          }
  }

And configmap.yaml like the following:

apiVersion: v1
kind: ConfigMap
metadata:
  name: custom-streams-configmap
data:
  config.hocon: {{ .Values.configHocon | indent 4}}

Lastly, I have defined the deployment.yaml like the following:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.name }}
spec:
  replicas: {{ default 1 .Values.replicas }}
  strategy: {}
  template:
    spec:
      containers:
      - env:
        {{- range $key, $value := .Values.env }}
        - name: {{ $key }}
          value: {{ $value | quote }}
        {{- end }}
        image: {{ .Values.image }}
        name: {{ .Values.name }}
        volumeMounts:
        - name: config-hocon
          mountPath: /config
        ports:
        - containerPort: {{ .Values.port }}
      restartPolicy: {{ .Values.restartPolicy }}
      volumes:
      - name: config-hocon
        configmap:
          name: custom-streams-configmap
          items:
          - key: config.hocon
            path: config.hocon
status: {}

When I run the container via:

helm install --name custom-streams custom-streams -f values.yaml --debug --namespace streaming

Then the pods are running fine, but I cannot see the config.hocon file in the container:

$ kubectl exec -it custom-streams-55b45b7756-fb292 sh -n streaming
/ # ls
...
config
...
/ # cd config/
/config # ls
/config #

I need the config.hocon written in the /config folder. Can anyone let me know what is wrong with the configurations?

-- himanshuIIITian
configmap
kubernetes
kubernetes-helm

1 Answer

5/8/2021

I was able to resolve the issue. The issue was using configmap in place configMap in deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.name }}
spec:
  replicas: {{ default 1 .Values.replicas }}
  strategy: {}
  template:
    spec:
      containers:
      - env:
        {{- range $key, $value := .Values.env }}
        - name: {{ $key }}
          value: {{ $value | quote }}
        {{- end }}
        image: {{ .Values.image }}
        name: {{ .Values.name }}
        volumeMounts:
        - name: config-hocon
          mountPath: /config
        ports:
        - containerPort: {{ .Values.port }}
      restartPolicy: {{ .Values.restartPolicy }}
      volumes:
      - name: config-hocon
        configMap:
          name: custom-streams-configmap
          items:
          - key: config.hocon
            path: config.hocon
status: {}
-- himanshuIIITian
Source: StackOverflow