A simple yet complete kubernetes' configmap watcher
npm i -S k8s-configmap
I'm considering that you alread read the official guide on mapping configmap as volumes and it's working. If not, here it is https://kubernetes.io/docs/concepts/configuration/configmap/#using-configmaps-as-files-from-a-pod
I'll use this configmap for this tutorial
The exported class expects the root configmap folder (specified in your deployment configuration as mentioned in the above doc), an array of keys to use and an optional options object
const ConfigMap = require('k8s-configmap');
const CONFIGMAP_ROOT_PATH = '/configmap';
const CONFIGMAP_KEYS = {
GAME_PROPERTIES: 'game.properties',
CONFIG: 'config.json',
};
const configmap = new ConfigMap(CONFIGMAP_NAME, [
ConfigMap.Key(CONFIGMAP_KEYS.GAME_PROPERTIES),
ConfigMap.Key(CONFIGMAP_KEYS.CONFIG, JSON.parse),
], { debug: true });
(async () => {
for (const key of Object.keys(CONFIGMAP_KEYS)) {
const current = await configmap.read(CONFIGMAP_KEYS[key]);
console.log(`initial value from ${key}: [${typeof current}]`, current);
configmap.on(CONFIGMAP_KEYS[key], (updated) => {
console.log(`${key} changed`, updated);
});
}
})();
- add built-in parsers for common files (json, key-values, etc)
Im not sure if im really happy with this code, but it works