Skip to content

Instantly share code, notes, and snippets.

@Laxman-SM
Forked from lwolf/env2secret.py
Created October 30, 2017 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Laxman-SM/4300e006317812c71c933fb403b08e20 to your computer and use it in GitHub Desktop.
Save Laxman-SM/4300e006317812c71c933fb403b08e20 to your computer and use it in GitHub Desktop.
Script to convert docker-compose compatible environment files into kubernetes secrets
"""
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