3

I have an application.yml (Spring) file, which has almost 70 fields, want to move those fields to ConfigMap. In the process of setup ConfigMap, have realized all the 70 fields has be flatened example : webservice.endpoint.transferfund It's gonna be a painful task to convert all the 70 fields as flat, is there any alternative.

Please suggest.

Below Config is working:

apiVersion: v1
kind: ConfigMap
metadata:
  name: configmapname
  namespace: default
data:
  webservice.endpoint.transferfund: http://www.customer-service.app/api/tf
  webservice.endpoint.getbalance: http://www.customer-service.app/api/balance
  webservice.endpoint.customerinfo: http://www.customer-service.app/api/customerinfo

Below config is not working, tried it as yml format.

apiVersion: v1
kind: ConfigMap
metadata:
  name: configmapname
  namespace: default
data:
  application.yaml: |-
    webservice:
      endpoint:
        transferfund: http://www.customer-service.app/api/tf
        getbalance: http://www.customer-service.app/api/balance
        customerinfo: http://www.customer-service.app/api/customerinfo 

in src/main/resources/application.yml have below fields to access ConfigMap keys:

webservice:
  endpoint:
    transferfund: ${webservice.endpoint.transferfund}
    getbalance: ${webservice.endpoint.getbalance}
    customerinfo: ${webservice.endpoint.customerinfo}

Updated:

ConfigMap Description:

C:\Users\deskktop>kubectl describe configmap configmapname
Name:         configmapname
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
application.yaml:
----
webservice:
  endpoint:
    transferfund: http://www.customer-service.app/api/tf
    getbalance: http://www.customer-service.app/api/balance
    customerinfo: http://www.customer-service.app/api/customerinfo
Events:  <none>

Deployment script: (configMapRef name provided as configmap name as shown above)

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: configmap-sample
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: configmap-sample
    spec:
      containers:
      - name: configmap-sample
        image: <<image>>
        ports:
        - name: http-api
          containerPort: 9000
        envFrom:
        - configMapRef:
            name: configmapname
        resources:
          limits:
            memory: 1Gi
          requests:
            memory: 768Mi
        env:
        - name: JVM_OPTS
          value: "-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=1 -Xms768M"   
Molay
  • 1,154
  • 2
  • 19
  • 42
  • 2
    What does "not working" mean? How is the Config map being consumed? You need to provide some more information such as `kubectl describe configmap/configmapname` and the Pod spec using it. The create from-file option should work if you reference `application.yml` as the ConfigMap key. The former examples are not valid (`data` doesn't take an arbitrary YAML map, just key / value). – Andy Shinn Jul 02 '19 at 19:11
  • Not working I mean, ConfigMap key values are not getting injected to Spring application if i use ConfigMap as yml format or even if i create ConfigMap from-file. Updated the question with required details. let me know if you need any more. I know i am missing something. But unable to figure out. Kindly help. – Molay Jul 03 '19 at 01:43
  • To properly answer this we'd also need to know the full path where `application.yml` should be mounted inside the Pod. You would mount the config as a file instead of env variables per Alex answer. If you provide this information maybe Alex could edit their answer to be more specific. – Andy Shinn Jul 03 '19 at 23:40

3 Answers3

3

A ConfigMap is a dictionary of configuration settings. It consists of key-value pairs of strings. Kubernetes then adds those values to your containers.

In your case you have to make them flat, because Kubernetes will not understand them.

You can read in the documentation about Creating ConfigMap that:

kubectl create configmap <map-name> <data-source>

where is the name you want to assign to the ConfigMap and is the directory, file, or literal value to draw the data from.

The data source corresponds to a key-value pair in the ConfigMap, where

  • key = the file name or the key you provided on the command line, and
  • value = the file contents or the literal value you provided on the command line.

You can use kubectl describe or kubectl get to retrieve information about a ConfigMap.

EDIT

You could create a ConfigMap from a file with defined key.

Define the key to use when creating a ConfigMap from a file

Syntax might look like this:

kubectl create configmap my_configmap --from-file=<my-key-name>=<path-to-file> And the ConfigMap migh look like the following:

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2019-07-03T18:54:22Z
  name: my_configmap
  namespace: default
  resourceVersion: "530"
  selfLink: /api/v1/namespaces/default/configmaps/my_configmap
  uid: 05f8da22-d671-11e5-8cd0-68f728db1985
data:
  <my-key-name>: |
    key=value
    key=value
    key=value
    key=value

Also I was able to find Create Kubernetes ConfigMaps from configuration files.

Functionality

The projector can:

  • Take raw files and stuff them into a ConfigMap
  • Glob files in your config repo, and stuff ALL of them in your configmap
  • Extract fields from your structured data (yaml/json)
  • Create new structured outputs from a subset of a yaml/json source by pulling out some fields and dropping others
  • Translate back and forth between JSON and YAML (convert a YAML source to a JSON output, etc)
  • Support for extracting complex fields like objects+arrays from sources, and not just scalars!
Crou
  • 10,232
  • 2
  • 26
  • 31
  • I think this would be a much better answer that I could upvote if it had an example of a file name as key and a YAML Block Scalar with their example YAML data to illustrate. The docs are still confusing in this regard. – Andy Shinn Jul 03 '19 at 23:42
  • @AndyShinn, you are correct. I've edited the answer and posted example and more information. – Crou Jul 04 '19 at 10:44
  • as shown in my original question, currently i am able to use the key and get value with transferfund: ${webservice.endpoint.transferfund}, but how do i use this key and get the value ? : |, do i need to change it as transferfund: ${application.yml:webservice.endpoint.transferfund} – Molay Jul 08 '19 at 05:39
  • You might look at this answer [How to use key value pair in kubernetes configmaps to mount volume](https://stackoverflow.com/questions/48201065/how-to-use-key-value-pair-in-kubernetes-configmaps-to-mount-volume). – Crou Jul 24 '19 at 15:15
1

You need to mount the ConfigMap as Volume. Otherwise the content would live in environment variables. The example i post here is from https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-volume

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never
alexzimmer96
  • 996
  • 6
  • 14
  • Alex, thanks for your comment, my question is about the ConfigMap - 'data' field, how can i keep all my yml file content under 'data' field. – Molay Jul 03 '19 at 10:18
  • When you imported `configmapname` from file you can use `kubectl edit configmap/configmapname` to see the format. Your key will be `application.yaml` and the value will be the data in a [YAML Block Scalar](https://yaml-multiline.info/). This is indicated in your `kubectl describe configmap configmapname` output. – Andy Shinn Jul 03 '19 at 23:37
0

You mentioned, you're using the application.yaml in context of a Spring project. So if you don't care whether you use .yaml or .property configuration-files, you can just use property-files because configMap generation supports them. It works with the --from-env-file flag:

kubectl create configmap configmapname --from-env-file application.properties

So in your deployment-file you can directly access the keys:

...
env:
  - KEYNAME
    valueFrom:
       configMapKeyRef:
          name: configmapname
          key: KeyInPropertiesFile