Last active
March 22, 2019 18:31
-
-
Save smcelhinney/78afbc4d687b712054b9d1bb9d3ac1d1 to your computer and use it in GitHub Desktop.
A Node.js script for creating Base64 encoded configMaps (secrets) from existing .env file (for Kubernetes)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const yaml = require('yaml'); | |
const envFilePath = path.resolve(__dirname, '.env'); | |
const destYamlPath = path.resolve(__dirname, 'my-secrets.yml'); | |
const data = fs | |
.readFileSync(envFilePath, 'utf-8') | |
.split('\n') | |
.filter(i => !i.startsWith('#')) | |
.reduce((acc, cur) => { | |
const propertyKey = cur.substr(0, cur.indexOf('=')); | |
const propertyValue = cur.substr(cur.indexOf('=') + 1, cur.length); | |
acc[propertyKey] = Buffer.from(propertyValue).toString('base64'); | |
return acc; | |
}, {}); | |
// Create our YAML file. | |
const obj = { | |
apiVersion: 'v1', | |
kind: 'Secret', | |
metadata: { | |
name: 'my-secrets', | |
namespace: 'my-namespace' | |
}, | |
type: 'Opaque', | |
data | |
}; | |
fs.writeFileSync(destYamlPath, yaml.stringify(obj), 'utf-8'); | |
process.exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment