Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes - How to define ConfigMap built using a file in a yaml?

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

like image 815
Chillax Avatar asked Nov 22 '18 11:11

Chillax


People also ask

How do I create a ConfigMap in Kubernetes from Yaml file?

The simplest way to create a ConfigMap is to store a bunch of key-value strings in a ConfigMap YAML file and inject them as environment variables into your Pods. After that, you can reference the environment variables in your applications using whatever methods is necessary for your programming language.

How do I pass a file to ConfigMap?

Create ConfigMap from file as is. In this case you will get ConfigMap with filename as a key and filedata as a value. For example, you have file your-file. json with content {key1: value1, key2: value2, keyN: valueN} . Now you can get any information from your /etc/config/your-file.

How do you define a ConfigMap?

A ConfigMap is an API object that lets you store configuration for other objects to use. Unlike most Kubernetes objects that have a spec , a ConfigMap has data and binaryData fields. These fields accept key-value pairs as their values. Both the data field and the binaryData are optional.


1 Answers

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"     } 
like image 60
edbighead Avatar answered Oct 02 '22 07:10

edbighead