Last active
March 6, 2018 13:13
-
-
Save j18e/2b2c14db0b2df1fed24f42e7dc455e6c to your computer and use it in GitHub Desktop.
yamplate
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
from jinja2 import Environment, meta, StrictUndefined, exceptions | |
from yaml import load_all | |
def get_manifests(template_string, overrides): | |
config = get_config(template_string) | |
for k, v in overrides.items(): | |
config['parms'][k] = v | |
rendered = render_jinja(template_string, config['parms']) | |
results = list(load_all(rendered)) | |
try: | |
'parms' in results[0] | |
results.pop(0) | |
return results | |
except: | |
print('error: template must contain config doc') | |
exit(1) | |
def get_config(template_string): | |
raw_template = Environment().parse(template_string) | |
variables = meta.find_undeclared_variables(raw_template) | |
tmp_values = {v: 'tempvalue' for v in variables} | |
valid_template = render_jinja(template_string, tmp_values) | |
results = list(load_all(valid_template)) | |
return results[0] | |
def render_jinja(string, parms): | |
env = Environment(undefined=StrictUndefined) | |
t = env.from_string(string) | |
try: | |
return t.render(parms) | |
except exceptions.UndefinedError as e: | |
print('template error:', e) | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
call get_manifests with template string and config overrides
use with a string containing multiple '---' separated yaml docs. The first doc contains template default parameters and subsequent ones contain yaml fields with jinja2 {{parm}} parameters.
overrides is a dict object containing any desired parameters to override from the default ones set in the template
use with Kubernetes manifests to have a flatter deployment mechanism than helm