Kubectl update configMap

4/23/2018

I am using the following command to create a configMap.

kubectl create configmap test --from-file=./application.properties --from-file=./mongo.properties --from-file=./logback.xml 

Now, I have modified a value for a key from mongo.properties which i need to update in kubernetes.

Option1 :-

kubectl edit test

Here, it opens the entire file. But, I want to just update mongo.properties and hence want to see only the mongo.properties. Is there any other way?

Note :- I dont want to have mongo.properties in a separate configMap.

Thanks

-- user1578872
kubernetes

7 Answers

5/15/2020

Here's a neat way to do an in-place update from a script.

The idea is;

  1. export the configmap to YAML (kubectl get cm -o yaml)
  2. use sed to do a command-line replace of an old value with a new value (sed "s|from|to")
  3. push it back to the cluster using kubectl apply

In this worked example, I'm updating a log level variable from 'info' level logging to 'warn' level logging.

So, step 1, read the current config;

$ kubectl get cm common-config -o yaml

apiVersion: v1
data:
  CR_COMMON_LOG_LEVEL: info
kind: ConfigMap

Step 2, you modify it locally with a regular expression search-and-replace, using sed:

$ kubectl get cm common-config -o yaml | \
  sed -e 's|CR_COMMON_LOG_LEVEL: info|CR_COMMON_LOG_LEVEL: warn|'

apiVersion: v1
data:
  CR_COMMON_LOG_LEVEL: warn
kind: ConfigMap

You can see the value has changed. Let's push it back up to the cluster;

Step 3; use kubectl apply -f -, which tells kubectl to read from stdin and apply it to the cluster;

$ kubectl get cm common-config -o yaml | \
  sed -e 's|CR_COMMON_LOG_LEVEL: info|CR_COMMON_LOG_LEVEL: warn|' | \
  kubectl apply -f -

configmap/common-config configured
-- Steve Cooper
Source: StackOverflow

7/19/2018

Another option is actually you can use this command:

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

Refer to Github issue: Support updating config map and secret with --from-file

-- irvifa
Source: StackOverflow

10/4/2019

Now you can. Just throw: kubectl edit configmap <name of the configmap> on your command line. Then you can edit your configuration.

-- Arivaldo
Source: StackOverflow

4/26/2018

No, you can't.

Replace in kubernetes will simply replace everything in that configmap. You can't just update one file or one single property in it.

However, if you check with the client Api, you will find if you create a configmap with lots of files. Then, those files will be stored as a HashMap, where key is file name by default, value is the file content encoded as a string. So you can write your own function based on existing key-value pair in HashMap.

This is what I found so far, if you find there is already existing method to deal with this issue, please let me know :)

FYI, if you want to update just one or few properties, it is possible if you use patch. However, it is a little bit hard to implement.

this and this may help

-- Conan Bryant
Source: StackOverflow

2/25/2019

Here is how you can add/modify/remove files in a configmap with some help from jq:

export configmap to a JSON file:

CM_FILE=$(mktemp -d)/config-map.json
oc get cm <configmap name> -o json > $CM_FILE

DATA_FILES_DIR=$(mktemp -d)
files=$(cat $CM_FILE | jq '.data' | jq -r 'keys[]')
for k in $files; do
    name=".data[\"$k\"]"
    cat $CM_FILE | jq -r $name > $DATA_FILES_DIR/$k;
done

add/modify a file:

echo '<paste file contents here>' > $DATA_FILES_DIR/<file name>.conf

remove a file:

rm <file name>.conf

when done, update the configmap:

kubectl create configmap <configmap name> --from-file $DATA_FILES_DIR -o yaml --dry-run | kubectl apply -f -

delete temporary files and folders:

rm -rf CM_FILE
rm -rf DATA_FILES_DIR
-- Bruce S.
Source: StackOverflow

11/1/2019

kubectl edit configmap -n <namespace> <configMapName> -o yaml

This opens up a vim editor with the configmap in yaml format. Now simply edit it and save it.

-- Sathish
Source: StackOverflow

11/21/2019

Here is a complete shell script to add new file to configmap (or replace existing one) based on @Bruce S. answer https://stackoverflow.com/a/54876249/2862663

#!/bin/bash
# Requires jq to be installed
if [ -z "$1" ]
  then
    echo "usage: update-config-map.sh <config map name> <config file to add>"
    return
fi 
if [ -z "$2" ]
  then
    echo "usage: update-config-map.sh <config map name> <config file to add>"
    return
fi 

CM_FILE=$(mktemp -d)/config-map.json
kubectl get cm $1 -o json > $CM_FILE

DATA_FILES_DIR=$(mktemp -d)
files=$(cat $CM_FILE | jq '.data' | jq -r 'keys[]')
for k in $files; do
    name=".data[\"$k\"]"
    cat $CM_FILE | jq -r $name > $DATA_FILES_DIR/$k;
done

echo cunfigmap: $CM_FILE tempdir: $DATA_FILES_DIR

echo will add file $2 to config

cp $2 $DATA_FILES_DIR

kubectl create configmap $1 --from-file $DATA_FILES_DIR -o yaml --dry-run |  kubectl apply -f -
echo Done
echo removing temp dirs
rm -rf $CM_FILE
rm -rf $DATA_FILES_DIR
-- Dmitry Andrievsky
Source: StackOverflow