Created
April 22, 2016 12:30
-
-
Save lwolf/b72419acd29f4fa262b1192102a2429f to your computer and use it in GitHub Desktop.
Script to convert docker-compose compatible environment files into kubernetes secrets
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
""" | |
requires click package: | |
- pip install click | |
Example usage: | |
- python env2secret.py --src=production-ru.env --dst=production-ru-secret.yaml --fmt=yaml --name=my-app-secret | |
""" | |
import base64 | |
import click | |
import json | |
import yaml | |
@click.command() | |
@click.option('--src', type=click.File('rb'), help='File with raw environment values.') | |
@click.option('--dst', type=click.File('wb'), help='Name of the result secret file.') | |
@click.option('--fmt', default='yaml', help='Format of secret file. json/yaml') | |
@click.option('--name', help="Metadata name of the resulting secret") | |
def generate_secret(src, dst, fmt, name): | |
data = {} | |
for line in src.readlines(): | |
splited_line = line.split('=') | |
if len(splited_line) > 1: | |
data[splited_line[0]] = base64.b64encode("".join(splited_line[1:])) | |
result = { | |
"apiVersion": "v1", | |
"kind": "Secret", | |
"metadata": { | |
"name": str(name) | |
}, | |
"data": data | |
} | |
if fmt == 'json': | |
dst.write(json.dumps(result, indent=4)) | |
else: | |
dst.write(yaml.dump(result, default_flow_style=False)) | |
dst.flush() | |
if __name__ == '__main__': | |
generate_secret() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment