Skip to content

Commit

Permalink
Adds option to choose context
Browse files Browse the repository at this point in the history
  • Loading branch information
a7ul committed Oct 20, 2020
1 parent f1c8ab2 commit 5889b76
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 268 deletions.
101 changes: 51 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,76 +16,77 @@ npm install -g dotenv-from-k8s
## Usage

```s
dotenv-from-k8s 1.4.0 - A commandline cli tool to fetch, merge and convert secrets and config maps in k8s to dot env property file.
dotenv-from-k8s 1.3.0 - A commandline cli tool to fetch, merge and convert secrets and config maps in k8s to dot env property file.
USAGE
USAGE
dotenv-from-k8s
dotenv-from-k8s
OPTIONS
OPTIONS
-i, --input Input configuration file optional default: false
-o, --out Output env file name, defaults to stdout optional default: false
-s, --secret <secret_name> K8s <secret_name> from which you want to generate env file optional
-c, --configmap <config_map> K8s <config_map> from which you want to generate env file optional
-n, --namespace <name_space> K8s <name_space> from which you want to access the secrets and/or config maps optional
-x, --context <context_name> K8s context <context_name> from which you want to access the secrets and/or config maps optional
-i, --input Input configuration file optional default: false
-o, --out Output env file name, defaults to stdout optional default: false
-s, --secret <secret_name> K8s <secret_name> from which you want to generate env file optional
-c, --configmap <config_map> K8s <config_map> from which you want to generate env file optional
-n, --namespace <name_space> K8s <name_space> from which you want to access the secrets and/or config maps optional
MORE INFO
MORE INFO
Basic example:
---------------
dotenv-from-k8s -c api-config -o .env
or
dotenv-from-k8s -c api-config > .env
Basic example:
---------------
dotenv-from-k8s -c api-config -o .env
or
dotenv-from-k8s -c api-config > .env
Advanced example:
----------------
dotenv-from-k8s -s api-secrets -s api-secrets2 -c api-config -c api-config2 -n default > .env
Advanced example:
----------------
dotenv-from-k8s -s api-secrets -s api-secrets2 -c api-config -c api-config2 -n default > .env
Config file example:
--------------------
cat > env-from.yaml <<EOL
Config file example:
--------------------
cat > env-from.yaml <<EOL
namespace: default
envFrom:
- secretRef:
name: app-secrets
- configMapRef:
name: app-config
namespace: default
envFrom:
- secretRef:
name: app-secrets
- configMapRef:
name: app-config
EOL
EOL
dotenv-from-k8s -i env-from.yaml -o .env
dotenv-from-k8s -i env-from.yaml -o .env
Config file example with overrides:
-----------------------------------
cat > env-from.yaml <<EOL
Config file example with overrides:
-----------------------------------
cat > env-from.yaml <<EOL
namespace: default
envFrom:
- secretRef:
name: app-secrets
- configMapRef:
name: app-config
overrides:
HELLO: WORLD
ANOTHER_KEY: ANOTHER_VALUE
namespace: default
envFrom:
- secretRef:
name: app-secrets
- configMapRef:
name: app-config
overrides:
HELLO: WORLD
ANOTHER_KEY: ANOTHER_VALUE
EOL
EOL
dotenv-from-k8s -i env-from.yaml -o .env
dotenv-from-k8s -i env-from.yaml -o .env
GLOBAL OPTIONS
GLOBAL OPTIONS
-h, --help Display help
-V, --version Display version
--no-color Disable colors
--quiet Quiet mode - only displays warn and error messages
-v, --verbose Verbose mode - will also output debug messages
-h, --help Display help
-V, --version Display version
--no-color Disable colors
--quiet Quiet mode - only displays warn and error messages
-v, --verbose Verbose mode - will also output debug messages
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dotenv-from-k8s",
"description": "A commandline cli tool to fetch, merge and convert secrets and config maps in k8s to dot env property file.",
"version": "1.3.0",
"version": "1.4.0",
"main": "dist/cli.js",
"bin": {
"dotenv-from-k8s": "./dist/cli.js"
Expand Down
14 changes: 11 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import prog, { parse } from 'caporal';
import prog from 'caporal';
import fs from 'fs';
import stream, { Readable } from 'stream';
import util from 'util';
Expand Down Expand Up @@ -72,15 +72,19 @@ dotenv-from-k8s -i env-from.yaml -o .env
'K8s <name_space> from which you want to access the secrets and/or config maps',
prog.STRING,
)
.option(
'-x, --context <context_name>',
'K8s context <context_name> from which you want to access the secrets and/or config maps',
prog.STRING,
)
.action(function (args, options, logger) {
async function main(): Promise<void> {
const k8sApi = getK8sApi();

let outStream: NodeJS.WritableStream = process.stdout;
const secrets: string[] = [];
const configMaps: string[] = [];
const overrides: Record<string, string> = {};
let namespace = 'default';
let context;

if (options.input) {
const parsed = await configParser(options.input);
Expand All @@ -101,7 +105,11 @@ dotenv-from-k8s -i env-from.yaml -o .env
if (options.out) {
outStream = fs.createWriteStream(options.out, { encoding: 'utf8' });
}
if (options.context) {
context = options.context;
}

const k8sApi = getK8sApi(context);
const finalConfig = await getAndMergeSecretsAndConfigs(k8sApi, secrets, configMaps, overrides, namespace);
const propertiesFile = convertJsonToPropertiesFile(finalConfig);
const propFileStream = Readable.from(propertiesFile);
Expand Down
2 changes: 1 addition & 1 deletion src/configParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const readFile = util.promisify(fs.readFile);

export async function configParser(inputFile: string): Promise<Result> {
const rawConfig = await readFile(inputFile, { encoding: 'utf8' });
const config: InputConfig = yaml.safeLoad(rawConfig);
const config = yaml.safeLoad(rawConfig) as InputConfig;
const secrets: string[] = [];
const configMaps: string[] = [];
const namespace = config.namespace || 'default';
Expand Down
5 changes: 4 additions & 1 deletion src/k8s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import * as k8s from '@kubernetes/client-node';

import { base64DecodeObjectValues } from './utils';

export function getK8sApi(): k8s.CoreV1Api {
export function getK8sApi(context?: string): k8s.CoreV1Api {
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
if (context) {
kc.setCurrentContext(context);
}
return kc.makeApiClient(k8s.CoreV1Api);
}

Expand Down
Loading

0 comments on commit 5889b76

Please sign in to comment.