90 lines
2.9 KiB
Python
90 lines
2.9 KiB
Python
from argparse import ArgumentParser
|
|
from sys import stderr
|
|
from jinja2 import Template
|
|
from os import path
|
|
from yaml import safe_load
|
|
|
|
def load_config(config_path):
|
|
file = open(config_path, 'r')
|
|
config = safe_load(file.read())
|
|
file.close()
|
|
return config
|
|
|
|
def load_proxy_template(template_path):
|
|
file = open(template_path, 'r')
|
|
template = Template(file.read())
|
|
file.close()
|
|
return template
|
|
|
|
def write_file(filename, content, dry_run):
|
|
if dry_run:
|
|
print(f'### Would generate {filename} ###', file=stderr)
|
|
print(content, file=stderr)
|
|
else:
|
|
with open(filename, 'w') as f:
|
|
f.write(content)
|
|
|
|
kustomization_template = Template('''---
|
|
apiVersion: kustomize.config.k8s.io/v1beta1
|
|
kind: Kustomization
|
|
resources:
|
|
{%- for filename in filenames %}
|
|
- {{ filename }}
|
|
{%- endfor %}
|
|
''')
|
|
|
|
def main(args):
|
|
dry_run = args.dry_run
|
|
config_path = args.config
|
|
template_path = args.template
|
|
output_path = args.output
|
|
|
|
template = load_proxy_template(template_path)
|
|
|
|
config = load_config(config_path)
|
|
|
|
if config is None:
|
|
print(f'Config at {config_path} is invalid', file=stderr)
|
|
exit(1)
|
|
|
|
filenames = []
|
|
for proxy in config['proxies']:
|
|
listen_hosts = proxy.get('listen_hosts', [proxy.get('listen_host', None)])
|
|
content = template.render(proxy, listen_hosts=listen_hosts)
|
|
|
|
generated_filename = path.join(output_path, proxy['service_name']) + ".yaml"
|
|
filenames.append(path.basename(generated_filename))
|
|
write_file(generated_filename, content, dry_run)
|
|
|
|
kustomization_filename = path.join(output_path, 'kustomization.yaml')
|
|
kustomization_content = kustomization_template.render(filenames=filenames)
|
|
write_file(kustomization_filename, kustomization_content, dry_run)
|
|
|
|
if __name__ == '__main__':
|
|
default_config_path = path.join(path.dirname(__file__), 'config', 'config.yaml')
|
|
default_template_path = path.join(path.dirname(__file__), 'templates', 'proxy.yaml.j2')
|
|
default_output_path = path.normpath(path.join(path.dirname(__file__), '..', 'generated'))
|
|
|
|
parser = ArgumentParser(
|
|
prog='External Reverse Proxy Generator',
|
|
description='Generate reverse proxy manifests',
|
|
)
|
|
parser.add_argument(
|
|
'-n',
|
|
'--dry-run',
|
|
action='store_true',
|
|
help='Print generated manifests instead of writing them to disk'
|
|
)
|
|
parser.add_argument(
|
|
'-k',
|
|
'--skip-kustomize',
|
|
action='store_true',
|
|
help='Skip generation of kustomization.yaml file'
|
|
)
|
|
parser.add_argument('-c', '--config', help='Path to config file', default=default_config_path)
|
|
parser.add_argument('-t', '--template', help='Path to proxy template file', default=default_template_path)
|
|
parser.add_argument('-o', '--output', help='Output directory', default=default_output_path)
|
|
|
|
args=parser.parse_args()
|
|
main(args)
|