Data is empty when accessing config file in k8s configmap with Helm

10/1/2018

I am trying to use a configmap in my deployment with helm charts. Now seems like files can be accessed with Helm according to the docs here: https://github.com/helm/helm/blob/master/docs/chart_template_guide/accessing_files.md

This is my deployment:

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: "{{ template "service.fullname" . }}"
  labels:        
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: "{{ template "service.fullname" . }}"
    spec:
      containers:
      - name: "{{ .Chart.Name }}"
        image: "{{ .Values.registryHost }}/{{ .Values.userNamespace }}/{{ .Values.projectName }}/{{ .Values.serviceName }}:{{.Chart.Version}}"
        volumeMounts:
        - name: {{ .Values.configmapName}}configmap-volume
          mountPath: /app/config
        ports:
        - containerPort: 80
          name: http            
        livenessProbe:
          httpGet:
            path: /health
            port: http
          initialDelaySeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /health
            port: http
          initialDelaySeconds: 10
          timeoutSeconds: 5            
      volumes:
        - name: {{ .Values.configmapName}}configmap-volume
          configMap:            
            name: "{{ .Values.configmapName}}-configmap"

My configmap is accessing a config file. Here's the configmap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "{{ .Values.configmapName}}-configmap"
  labels:
    app: "{{ .Values.configmapName}}"
data:
  {{ .Files.Get "files/{{ .Values.configmapName}}-config.json" | indent 2}}

The charts directory looks like this:

files/
--runtime-config.json
templates/
--configmap.yaml
--deployment.yaml
--ingress.yaml
--service.yaml
chart.value
vaues.yaml

And this is how my runtime-confi.json file looks like:

{
    "GameModeConfiguration": {
        "command": "xx",
        "modeId": 10,
        "sessionId": 11            
    }
}

The problem is, when I install my chart (even with a dry-run mode), the data for my configmap is empty. It doesn't add the data from the config file into my configmap declaration. This is how it looks like when I do a dry-run:

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: "runtime-configmap"
  labels:
    app: "runtime"
data:
---

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: "whimsical-otter-runtime-service"
  labels:        
    chart: "runtime-service-unknown/version"
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: "whimsical-otter-runtime-service"
    spec:
      containers:
      - name: "runtime-service"
        image: "gcr.io/xxx-dev/xxx/runtime_service:unknown/version"
        volumeMounts:
        - name: runtimeconfigmap-volume
          mountPath: /app/config
        ports:
        - containerPort: 80
          name: http
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 100m
            memory: 100Mi

        livenessProbe:
          httpGet:
            path: /health
            port: http
          initialDelaySeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /health
            port: http
          initialDelaySeconds: 10
          timeoutSeconds: 5

      volumes:
        - name: runtimeconfigmap-volume
          configMap:            
            name: "runtime-configmap"
---

What am I doing wrong that I don't get data?

-- Ben
configmap
kubernetes
kubernetes-helm

2 Answers

10/1/2018

The replacement of the variable within the string does not work:

{{ .Files.Get "files/{{ .Values.configmapName}}-config.json" | indent 2}}

But you can gerenate a string using the printf function like this:

{{ .Files.Get (printf "files/%s-config.json" .Values.configmapName) | indent 2 }}
-- adebasi
Source: StackOverflow

10/1/2018

Apart from the syntax problem pointed by @adebasi, you still need to set this code inside a key to get a valid configmap yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "{{ .Values.configmapName}}-configmap"
  labels:
    app: "{{ .Values.configmapName}}"
data:
  my-file: |
    {{ .Files.Get (printf "files/%s-config.json" .Values.configmapName) | indent 4}}

Or you can use the handy configmap helper:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "{{ .Values.configmapName}}-configmap"
  labels:
    app: "{{ .Values.configmapName}}"
data:
{{ (.Files.Glob "files/*").AsConfig | indent 2 }}
-- Ignacio Millán
Source: StackOverflow