48

At present I am creating a configmap from the file config.json by executing:

kubectl create configmap jksconfig --from-file=config.json

I would want the ConfigMap to be created as part of the deployment and tried to do this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: jksconfig
data:
  config.json: |-
    {{ .Files.Get "config.json" | indent 4 }}

But doesn't seem to work. What should be going into configmap.yaml so that the same configmap is created?

---UPDATE---

when I do a helm install dry run:

# Source: mychartv2/templates/jks-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: jksconfig
data:
  config.json: |

Note: I am using minikube as my kubernetes cluster

7
  • You showed a file from the chart/templates directory. If you have the chart then why you don't install the chart by helm? And also your deployment.yaml inside helm chart should have properly written. Commented Nov 22, 2018 at 11:47
  • 1
    I am able to use the configmap , I am looking for a way to create the configmap using yaml and not manually using the command kubectl create configmap Commented Nov 22, 2018 at 12:25
  • It seems your configmap file has helm chart template. Why you don't use $ helm install command to create your needs. Helm will replace the template {{ .Files.Get "config.json" | indent 4 }} with the content of file config.json. Commented Nov 22, 2018 at 13:57
  • 1
    Indeed, I am using helm install. But it is not replacing the content as expected with the content of the file config.json nor throwing an error Commented Nov 22, 2018 at 14:20
  • I think I got a soln. But first you need to show the output of $ helm install <chart> --dry-run --debug to see if the content is correctly indented or not Commented Nov 22, 2018 at 14:39

3 Answers 3

70

Your config.json file should be inside your mychart/ directory, not inside mychart/templates

Chart Template Guide

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  config.json: |-
{{ .Files.Get "config.json" | indent 4}}

config.json

{
    "val": "key"
}

helm install --dry-run --debug mychart

[debug] Created tunnel using local port: '52091'     
                                                     
[debug] SERVER: "127.0.0.1:52091"                    
                                                     
...           
                                                     
NAME:   dining-saola                                 
REVISION: 1                                          
RELEASED: Fri Nov 23 15:06:17 2018                   
CHART: mychart-0.1.0                                 
USER-SUPPLIED VALUES:                                
{}                                                   
                                                     
...
                                                     
---                                                  
# Source: mychart/templates/configmap.yaml           
apiVersion: v1                                       
kind: ConfigMap                                      
metadata:                                            
  name: dining-saola-configmap                       
data:                                                
  config.json: |-                                    
    {                                                
        "val": "key"                                 
    }     

EDIT:

But I want it the values in the config.json file to be taken from values.yaml. Is that possible?

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  config.json: |-
    {
{{- range $key, $val := .Values.json }}
{{ $key | quote | indent 6}}: {{ $val | quote }}
{{- end}}
    }

values.yaml

json:
  key1: val1
  key2: val2
  key3: val3

helm install --dry-run --debug mychart

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mangy-hare-configmap
data:
  config.json: |-
    {
      "key1": "val1"
      "key2": "val2"
      "key3": "val3"
    }
Sign up to request clarification or add additional context in comments.

5 Comments

This did work for one of my use cases. So marking as accepted answer. But I want it the values in the config.json file to be taken from values.yaml. Is that possible?
Are you trying to get the values from values.yaml file inside your json configmap?
If so, i've added one more example to my answer.
@edbighead The generated config.json is missing the separating , between the properties...
@edbighead Can we replace config.json with config.yaml having nested maps? If yes how?
15

Here is an example of a ConfigMap that is attached to a Deployment:

ConfigMap:

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: jksconfig
data:
  config.json: |-
{{ .Files.Get "config.json" | indent 4 }}

Deployment:

---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: jksapp
  labels:
    app: jksapp
spec:
  selector:
    matchLabels:
      app: jksapp
  template:
    metadata:
      labels:
        app: jksapp
      containers:
        - name: jksapp
          image: jksapp:1.0.0
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: config #The name(key) value must match pod volumes name(key) value 
              mountPath: /path/to/config.json
      volumes:
        - name: config
          configMap:
            name: jksconfig

3 Comments

I was able to use the ConfigMap in my deployment. All I want is to know how to get the values from a config.json file ( I am creating it manually now) Also if I pasted the file contents instead of {{ .Files.Get "config.json" | indent 4 }} - that works too
You use as you would any file on a path. You paste your file content bellow the line config.json: |-.
I was looking for a way to import the data from the file config.json rather than having to paste the contents in the yaml file
5

Soln 01:

  • insert your config.json file content into a template
  • then use this template into your data against config.json
  • then run $ helm install command

finally,

{{define "config"}}
{
    "a": "A",
    "b": {
        "b1": 1
    }
}
{{end}}

apiVersion: v1
kind: ConfigMap
metadata:
  name: jksconfig
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "my-app"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
data:
  config.json: {{ (include "config" .) | trim | quote }}

1 Comment

This is great. Only guy who really gets it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.