-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_composition.py
More file actions
222 lines (202 loc) · 7.13 KB
/
Copy pathgenerate_composition.py
File metadata and controls
222 lines (202 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python3
"""Build apis/composition-openforms.yaml from reference/helm-template-*-external-redis.yaml.
Embeds each rendered object as a P&T base and adds patches for:
- Secret stringData from XR parameters
- ConfigMap data keys from XR parameters
- Deployment replica counts
- metadata.namespace (fixed default openforms; strings in ConfigMaps assume this namespace)
Helm release name in the reference MUST be \"demo\" so in-cluster strings (ALLOWED_HOSTS,
nginx proxy_pass, etc.) stay consistent. To use another release name, re-render with
helm template MYREL ... and replace \"demo\" in this script / reference, then re-run.
Usage:
python3 generate_composition.py \\
../reference/helm-template-openforms-1.12.0-external-redis.yaml \\
> ../apis/composition-openforms.generated.yaml
"""
from __future__ import annotations
import argparse
import re
import sys
from pathlib import Path
import yaml
# Must match helm template --name
RELEASE = "demo"
NS = "openforms"
def split_docs(text: str) -> list[str]:
parts = re.split(r"^---\s*$", text, flags=re.MULTILINE)
out: list[str] = []
for p in parts:
p = p.strip()
if not p:
continue
# Strip leading comment line (e.g. # Source: chart/templates/...)
lines = p.splitlines()
if lines and lines[0].startswith("# Source:"):
p = "\n".join(lines[1:]).strip()
if not p:
continue
out.append(p)
return out
def resource_key(doc: dict) -> str:
return f"{doc.get('kind')}/{doc['metadata']['name']}"
def patches_for(doc: dict) -> list[dict]:
k = resource_key(doc)
common_ns = {
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.namespace",
"toFieldPath": "metadata.namespace",
}
out: list[dict] = [common_ns]
if k == "Secret/demo-openforms":
out.extend(
[
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.settingsSecretKey",
"toFieldPath": "stringData.SECRET_KEY",
},
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.settingsDatabasePassword",
"toFieldPath": "stringData.DB_PASSWORD",
},
]
)
elif k == "ConfigMap/demo-openforms":
out.extend(
[
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.settingsBaseUrl",
"toFieldPath": "data.BASE_URL",
},
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.settingsDatabaseHost",
"toFieldPath": "data.DB_HOST",
},
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.settingsDatabaseName",
"toFieldPath": "data.DB_NAME",
},
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.settingsDatabaseUsername",
"toFieldPath": "data.DB_USER",
},
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.settingsCeleryBrokerUrl",
"toFieldPath": "data.CELERY_BROKER_URL",
},
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.settingsCeleryResultBackend",
"toFieldPath": "data.CELERY_RESULT_BACKEND",
},
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.settingsNumProxies",
"toFieldPath": "data.NUM_PROXIES",
},
]
)
elif k == "Deployment/demo-openforms":
out.append(
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.replicaCount",
"toFieldPath": "spec.replicas",
}
)
elif k == "Deployment/demo-openforms-worker":
out.append(
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.workerReplicaCount",
"toFieldPath": "spec.replicas",
}
)
elif k == "PersistentVolumeClaim/demo-openforms":
out.append(
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.persistenceSize",
"toFieldPath": "spec.resources.requests.storage",
}
)
out.append(
{
"type": "FromCompositeFieldPath",
"fromFieldPath": "spec.parameters.persistenceStorageClassName",
"toFieldPath": "spec.storageClassName",
}
)
return out
def build_composition(resources: list[dict]) -> dict:
return {
"apiVersion": "apiextensions.crossplane.io/v1",
"kind": "Composition",
"metadata": {"name": "openforms-kubernetes"},
"spec": {
"compositeTypeRef": {
"apiVersion": "openforms.example.org/v1alpha1",
"kind": "OpenForms",
},
"mode": "Pipeline",
"pipeline": [
{
"step": "openforms-resources",
"functionRef": {"name": "function-patch-and-transform"},
"input": {
"apiVersion": "pt.fn.crossplane.io/v1beta1",
"kind": "Resources",
"resources": resources,
},
},
{
"step": "mark-ready",
"functionRef": {"name": "function-auto-ready"},
},
],
},
}
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("reference", type=Path)
ap.add_argument(
"-o",
"--output",
type=Path,
default=None,
help="Write full Composition YAML (default: stdout)",
)
args = ap.parse_args()
text = args.reference.read_text()
docs_raw = split_docs(text)
resources: list[dict] = []
for raw in docs_raw:
doc = yaml.safe_load(raw)
if not doc or not isinstance(doc, dict):
continue
md = doc.get("metadata") or {}
if not md.get("name"):
continue
rk = resource_key(doc)
safe = rk.replace("/", "-").lower()
resources.append(
{
"name": f"pt-{safe}",
"base": doc,
"patches": patches_for(doc),
}
)
comp = build_composition(resources)
out = yaml.safe_dump(comp, sort_keys=False, default_flow_style=False, width=100)
if args.output:
args.output.write_text(out)
else:
sys.stdout.write(out)
if __name__ == "__main__":
main()