Skip to content

Instantly share code, notes, and snippets.

@abdulhadad
Created December 28, 2023 07:19
Show Gist options
  • Save abdulhadad/6b7f3c19129a036573a30d62197ab90a to your computer and use it in GitHub Desktop.
Save abdulhadad/6b7f3c19129a036573a30d62197ab90a to your computer and use it in GitHub Desktop.
application.yml with configmap data
  • for local development
  • read env config from configmap data section
  • replace application.yml with configmap data
server:
port: ${SERVER_PORT}
database:
host: ${DB_HOST}
name: ${DB_NAME}
username: ${DB_USER}
password_secret: ${DB_PASS_SECRET}
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: 2022-02-18T18:52:05Z
name: game-config
namespace: default
resourceVersion: "516"
uid: b4952dc3-d670-11e5-8cd0-68f728db1985
data:
SERVER_PORT: 8080
DB_HOST: localhost
DB_NAME: management
DB_USER: postgres
DB_PASS_SECRET: /run/secret/postgressecret
'use strict';
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
try {
// Synchronicity used only for the example (it stops the Event Loop).
const data = fs.readFileSync('configmap.yaml', 'utf8');
const parsedData = yaml.load(data);
/*
{
SERVER_PORT: 8080,
DB_HOST: 'localhost',
DB_USER: 'postgres',
DB_PASS_SECRET: '/run/secret/postgressecret'
}
*/
// console.log(parsedData.data);
let template = fs.readFileSync(path.join(__dirname, "application.yml"), 'utf8', (err) => {
if (err) return console.log(err);
});
template = template.replace(/\${([^{}]+)}/g, (keyExpr, key) => {
return parsedData.data[key] || "";
});
console.log(template);
fs.writeFile("application_target.yml", template, 'utf8', (err) => {
if (err) return console.log(err);
});
} catch (e) {
console.log(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment