Dynamically create the configmap yaml file

12/6/2019

Background : I have close to 15 *.properties files in different location. I need to create configmap for each properties files.

Currently I am manually creating configmap yaml files using

 kubectl create configmap app-properties --from-file= /path/app.properties.

Mount and everything thing is working fine.

Requirement : As soon we add any new key/value to properties file, it should get reflected in configmap yaml file. Can I dynamically create configmap yaml using some "include files".

-- pythonhmmm
helmfile
kubernetes
kubernetes-helm

1 Answer

12/6/2019

You could watch the properties files for modifications and recreate the ConfigMap whenever they change.

To do so, there are different tools for macOS and Linux.

Linux

On Linux, you can watch files for changes with inotifywait. You could do something along the following lines:

Create the file monitor.sh:

#!/bin/bash

FILE=$1
inotifywait -m -e modify "$FILE" |
  while read; do
    kubectl create configmap "$(basename $FILE)" --from-file="$FILE" --dry-run -o yaml | kubectl apply -f -
  done

Then execute it for each properties file:

./monitor.sh /path/app.properties

This will generate an updated ConfigMap YAML manifest with kubectl create and apply it with kubectl apply every time the /path/app.properties file is modified.

You can install inotifywait with:

sudo apt-get install inotify-tools

macOS

On macOS, you can use fswatch to watch for file modifications.

Create the file monitor.sh:

#!/bin/bash

FILE=$1
fswatch "$FILE" |
  while read; do
    kubectl create configmap "$(basename $FILE)" --from-file="$FILE" --dry-run -o yaml | kubectl apply -f -
  done

Then execute it for each properties file:

./monitor.sh /path/app.properties

This will generate an updated ConfigMap YAML manifest with kubectl create and apply it with kubectl apply every time the /path/app.properties file is modified.

You can install fswatch with:

brew install fswatch

Note

fswatch might also be available on Linux (sudo apt-get install fswatch), in which case you can use the monitor.sh script for macOS on Linux too. However, you might need to use fswatch -o (with the -o option) to ensure only a single output line.

-- weibeld
Source: StackOverflow