Skip to content

How to edit configmap configuration in spring boot kubernetes application during runtime

We have application with huge configuration (this is just a part):

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-app
data:
  application.yaml: |-
    config:
      app: MY-APP
      my-custom-map:
        KEY1: value1
        KEY2: value2
        KEY3: value3
        KEY4: value4
      something1: true
      something2: 123
      something3: string123
      something4: null
      subclass:
        anotherMap:
          "[AAA:0.0.1,BBB:CCC]": "DDD:EEEE"
      subclass2:
        something4: AAAA
        anotherMap2:
          0.0.3: 0.0.3

I follow this example to bind configmap with spring boot configuration but there is still some problem for example how to solve null in yaml which spring yaml postprocessor resolve as empty string: issue

second problem is how to handle this configmap. I know I can edit and then use apply but this can lead to some error. Is there some tool which I can use to edit this yaml and make some bash script for editing ? like: ./my-script.sh -function addMyCustomMapValue -args “KEY5:value5” . I tried to explore yq but I think there is some limitation and it is hard to use for some use-case and then kustomize which I think is good for creating configmap but not for editing existing one.

Is there already some good example for this use-case ?

Answer

Option : 1

You can Use the Lens : https://k8slens.dev/kubernetes.html

It’s UI for monitoring and Managing K8s clusters. Using this you can also edit the configmap.

Option : 2

You can manage all the Key value into single YAML file and create configmap from file :

kubectl create configmap some-config 
  --from-file=some-key=some-config.yaml 
  -n some-namespace 
  -o yaml 
  --dry-run | kubectl apply -f - 

Option : 3

Use helm and values.yaml template to create and your chart and apply it further.

Configmap using YAML helm

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

Option : 4

If you are using the configmap as Environment or injecting it to file path you can use the Hashi corp vault also : https://www.vaultproject.io/

Option : 5

As you suggested you can create one Bash script which will export the existing running Configmap to a new YAML file one you are done with editing YAML manually. You can apply the changes to K8s cluster.

#bin/bash
kubectl get configmap <configmap-name>  -o yaml > cofig.yaml

You can also check the : https://github.com/Gallore/yaml_cli might be helpful.