|
| 1 | +#!/usr/bin/env python |
| 2 | + |
1 | 3 | # This script is to help generate any flat yaml files from the ambassador helm chart.
|
2 | 4 | #
|
3 | 5 | # This script takes two arguments:
|
@@ -29,52 +31,27 @@ def get_requirement_key(req):
|
29 | 31 | return '{}.{}.{}'.format(req['kind'], req['name'], ns)
|
30 | 32 |
|
31 | 33 |
|
32 |
| -# ensure that the yaml docs are sorted in the same way as in the requirements. |
33 |
| -# order actually matters here. for example, we need the namespace show up before any |
34 |
| -# namespaced resources. |
35 |
| -# Also this ensures that all the "required" resources make it into the final yaml |
36 |
| -def same_sort(requirements, yaml_docs): |
37 |
| - sorted_resources = [] |
38 |
| - for req in requirements.get('resources'): |
39 |
| - req_key = get_requirement_key(req) |
40 |
| - if req_key not in yaml_docs: |
41 |
| - raise Exception('Resource %s not found in generated yaml' % req_key) |
42 |
| - sorted_resources.append(yaml_docs[req_key]) |
43 |
| - return sorted_resources |
44 |
| - |
45 |
| - |
46 |
| -class RequirementChecker(): |
47 |
| - |
48 |
| - def __init__(self, requirements): |
49 |
| - self.requirements = {} |
50 |
| - for req in requirements: |
51 |
| - key = get_requirement_key(req) |
52 |
| - self.requirements[key] = True |
53 |
| - |
54 |
| - |
55 |
| - def is_required(self, resource): |
56 |
| - key = get_resource_key(resource) |
57 |
| - return key in self.requirements |
58 |
| - |
59 |
| - |
60 | 34 | def main(templated_helm_file, require_file):
|
61 | 35 | yaml = ruamel.yaml.YAML()
|
62 | 36 | yaml.indent(mapping=2)
|
63 | 37 | with open(templated_helm_file, 'r') as f:
|
64 |
| - templated_helm = yaml.load_all(f.read()) |
| 38 | + templated_helm = {} |
| 39 | + for yaml_doc in yaml.load_all(f.read()): |
| 40 | + if yaml_doc is None: |
| 41 | + continue |
| 42 | + templated_helm[get_resource_key(yaml_doc)] = yaml_doc |
65 | 43 | with open(require_file, 'r') as f:
|
66 | 44 | requirements = yaml.load(f.read())
|
67 |
| - checker = RequirementChecker(requirements.get('resources')) |
68 | 45 |
|
69 |
| - new_doc = {} |
70 |
| - for yaml_doc in templated_helm: |
71 |
| - if yaml_doc is None: |
72 |
| - continue |
73 |
| - if checker.is_required(yaml_doc): |
74 |
| - new_doc[get_resource_key(yaml_doc)] = yaml_doc |
75 | 46 | print('# GENERATED FILE: edits made by hand will not be preserved.')
|
76 |
| - print('---') |
77 |
| - yaml.dump_all(same_sort(requirements, new_doc), sys.stdout) |
| 47 | + # Print out required resources in the order they appear in require_file. Order actually matters |
| 48 | + # here, for example, we need the namespace show up before any namespaced resources. |
| 49 | + for requirement in requirements.get('resources'): |
| 50 | + print('---') |
| 51 | + key = get_requirement_key(requirement) |
| 52 | + if key not in templated_helm: |
| 53 | + raise Exception(f'Resource {key} not found in generated yaml (known resources are: {templated_helm.keys()})') |
| 54 | + yaml.dump(templated_helm[key], sys.stdout) |
78 | 55 |
|
79 | 56 |
|
80 | 57 | if __name__ == '__main__':
|
|
0 commit comments