Hot to replace data in kubernetes configMap

Viewed 914

I have configMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-app
data:
  application.yaml: |-
    config:
      property1: 1
      property2: 2
      property3: 2            
      my-map-property:
        a: b
        c: d
        e: f
        g: h

now I want to update my map property to:

  my-map-property:
    1: 5
    2: 6
    3: 7
    4: 8

what is the easiest tool to apply this patch to my configMap ?

I know there is yq or kustomize but when I tried example with kustomize

resources:
- my-configMap.yaml
patchesStrategicMerge:
- patch-configMap.yaml

and run: kubectl kustomize ./ then output contains just patch now whole configMap. Is there some better tool how to deal with this problem ?

1 Answers

You can use yq + kustomize configMap generators. The downside is that Kustomize will mutate the config map name every time there is a change so you will have to "garbage collect" all unused config maps manually.

So you would have the following setup:

# application.yaml
config:
  property1: 1
  property2: 2
  property3: 2
  my-map-property:
    a: b
    c: d
    e: f
    g: h

# kustomization.yaml
configMapGenerator:
  - name: my-app
    files:
      - application.yaml

You would then run some yq commands yq e -i '.config.my-map-property.a = 1' application.yaml

and finally kubectl kustomize ./

Wherever you want to use the config, just reference it as my-app, kustomize will propagate and append the unique identifier to all references.

You can alternatively do something like kubectl create configmap my-app --from-file=application.yaml --dry-run=true -o yaml after running the yq command to mutate the values if you don't want to use Kustomize in your workflow. But I personally prefer using Kustomize.

Related