简体   繁体   中英

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?

Why don't you use the kubernetes client sdk for Java and a yaml library for editing (SnakeYAML)? Those libraries helped me with the same problem you are trying to solve.

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.

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.

apiVersion: v1
kind: ConfigMap
metadata:
  name: application-conf
data: 
  {{- if .Values.global.applicationConfiguration }}
  application.properties: | 
    {{- .Values.global.applicationConfiguration  | nindent 4 }}
   {{- end }}

This is how we can specify a config map. Add applicationConfiguration in your values.yaml if it specified then only it will write an application.properties externally. It need not have all the properties.

This is best tool to edit and make changes in kubernetes env

k9s: https://github.com/derailed/k9s

You can explore https://kubeval.instrumenta.dev/ to catch any configmap error before deploying

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM