Skip to content

Commit bac7ab3

Browse files
committed
refactor: replace string YAML in generate.ts with template files
Move all remaining string-based YAML construction out of generate.ts into template files under templates/k8s/: - skaffold.yaml — main skaffold config skeleton with placeholders for per-function profiles, rawYaml lists, and port forwards - skaffold-profile.yaml — per-function profile template - functions-configmap.yaml — ConfigMap template for function registry generate.ts now uses readTemplate() + renderTemplate() to load and populate these files, same pattern as the function templates. No more string array concatenation for YAML output.
1 parent 6fb8a5c commit bac7ab3

4 files changed

Lines changed: 180 additions & 123 deletions

File tree

scripts/generate.ts

Lines changed: 40 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -183,25 +183,32 @@ interface FunctionInfo {
183183
port: number;
184184
}
185185

186+
const K8S_TEMPLATES_DIR: string = path.resolve(TEMPLATES_DIR, 'k8s');
187+
188+
function readTemplate(name: string): string {
189+
return fs.readFileSync(path.join(K8S_TEMPLATES_DIR, name), 'utf-8');
190+
}
191+
192+
function renderTemplate(template: string, vars: Record<string, string>): string {
193+
let result = template;
194+
for (const [key, value] of Object.entries(vars)) {
195+
result = result.replace(new RegExp(`\\{\\{${key}\\}\\}`, 'g'), value);
196+
}
197+
return result;
198+
}
199+
186200
function generateFunctionsConfigMap(fns: FunctionInfo[], perFunction?: FunctionInfo): void {
187201
const targetFns = perFunction ? [perFunction] : fns;
188-
const jobsSupported = targetFns.map((fn) => fn.name).join(',');
189202
const gatewayMap: Record<string, string> = {};
190203
for (const fn of targetFns) {
191204
gatewayMap[fn.name] = `http://${fn.name}.${K8S_NAMESPACE}.svc.cluster.local`;
192205
}
193206

194-
const yaml = [
195-
'# AUTO-GENERATED by scripts/generate.ts — do not edit manually.',
196-
'apiVersion: v1',
197-
'kind: ConfigMap',
198-
'metadata:',
199-
' name: functions-registry',
200-
'data:',
201-
` JOBS_SUPPORTED: "${jobsSupported}"`,
202-
` INTERNAL_GATEWAY_DEVELOPMENT_MAP: '${JSON.stringify(gatewayMap)}'`,
203-
'',
204-
].join('\n');
207+
const template = readTemplate('functions-configmap.yaml');
208+
const yaml = renderTemplate(template, {
209+
jobs_supported: targetFns.map((fn) => fn.name).join(','),
210+
gateway_map: JSON.stringify(gatewayMap),
211+
});
205212

206213
if (perFunction) {
207214
const dir = path.join(GENERATED_DIR, perFunction.dir, 'k8s');
@@ -213,70 +220,24 @@ function generateFunctionsConfigMap(fns: FunctionInfo[], perFunction?: FunctionI
213220
}
214221

215222
function generateSkaffoldYaml(fns: FunctionInfo[]): void {
216-
const buildBlock = [
217-
' build:',
218-
' artifacts:',
219-
' - image: constructive-functions',
220-
' context: .',
221-
' docker:',
222-
' dockerfile: Dockerfile.dev',
223-
' sync:',
224-
' manual:',
225-
" - src: 'functions/**/*.ts'",
226-
' dest: /usr/src/app',
227-
' local:',
228-
' push: false',
229-
].join('\n');
230-
231-
const sharedPortForwards = [
232-
' - resourceType: service',
233-
' resourceName: knative-job-service',
234-
` namespace: ${K8S_NAMESPACE}`,
235-
' port: 8080',
236-
' localPort: 8080',
237-
' - resourceType: service',
238-
' resourceName: postgres',
239-
` namespace: ${K8S_NAMESPACE}`,
240-
' port: 5432',
241-
' localPort: 5432',
242-
' - resourceType: service',
243-
' resourceName: constructive-server',
244-
` namespace: ${K8S_NAMESPACE}`,
245-
' port: 3000',
246-
' localPort: 3002',
247-
].join('\n');
248-
249-
// Per-function profiles
250-
const perFnProfiles: string[] = [];
251-
for (const fn of fns) {
252-
perFnProfiles.push([
253-
` - name: ${fn.name}`,
254-
buildBlock,
255-
' manifests:',
256-
' kustomize:',
257-
' paths:',
258-
' - k8s/overlays/local-simple',
259-
' rawYaml:',
260-
` - generated/${fn.dir}/k8s/local-deployment.yaml`,
261-
` - generated/${fn.dir}/k8s/functions-configmap.yaml`,
262-
' deploy:',
263-
' kubectl:',
264-
` defaultNamespace: ${K8S_NAMESPACE}`,
265-
' portForward:',
266-
' - resourceType: service',
267-
` resourceName: ${fn.name}`,
268-
` namespace: ${K8S_NAMESPACE}`,
269-
' port: 80',
270-
` localPort: ${fn.port}`,
271-
sharedPortForwards,
272-
].join('\n'));
273-
}
274-
275-
// local-simple: all functions
223+
const profileTemplate = readTemplate('skaffold-profile.yaml');
224+
const skaffoldTemplate = readTemplate('skaffold.yaml');
225+
226+
// Render per-function profiles from template
227+
const perFnProfiles = fns.map((fn) =>
228+
renderTemplate(profileTemplate, {
229+
name: fn.name,
230+
dir: fn.dir,
231+
port: String(fn.port),
232+
namespace: K8S_NAMESPACE,
233+
}).trimEnd()
234+
).join('\n');
235+
236+
// Build dynamic lists for aggregate profiles
276237
const allRawYaml = fns
277238
.map((fn) => ` - generated/${fn.dir}/k8s/local-deployment.yaml`)
278239
.join('\n');
279-
const allFnPortForwards = fns
240+
const allPortForwards = fns
280241
.map((fn) => [
281242
' - resourceType: service',
282243
` resourceName: ${fn.name}`,
@@ -286,56 +247,12 @@ function generateSkaffoldYaml(fns: FunctionInfo[]): void {
286247
].join('\n'))
287248
.join('\n');
288249

289-
const localSimpleProfile = [
290-
' # All functions together.',
291-
' - name: local-simple',
292-
buildBlock,
293-
' manifests:',
294-
' kustomize:',
295-
' paths:',
296-
' - k8s/overlays/local-simple',
297-
' rawYaml:',
298-
allRawYaml,
299-
' - generated/functions-configmap.yaml',
300-
' deploy:',
301-
' kubectl:',
302-
` defaultNamespace: ${K8S_NAMESPACE}`,
303-
' portForward:',
304-
allFnPortForwards,
305-
sharedPortForwards,
306-
].join('\n');
307-
308-
// local (Knative): no rawYaml, uses kustomize overlay
309-
const localKnativeProfile = [
310-
' # Full Knative setup — requires `cd k8s && make operators-knative-only` first.',
311-
' - name: local',
312-
buildBlock,
313-
' manifests:',
314-
' kustomize:',
315-
' paths:',
316-
' - k8s/overlays/local',
317-
' portForward:',
318-
allFnPortForwards,
319-
sharedPortForwards,
320-
].join('\n');
321-
322-
const skaffold = [
323-
'# AUTO-GENERATED by scripts/generate.ts — do not edit manually.',
324-
'# To add a function, create functions/<name>/handler.json and run `pnpm generate`.',
325-
'apiVersion: skaffold/v4beta11',
326-
'kind: Config',
327-
'metadata:',
328-
' name: constructive-functions',
329-
'',
330-
'profiles:',
331-
' # Per-function profiles (infra + single function).',
332-
...perFnProfiles.map((p) => p),
333-
'',
334-
localSimpleProfile,
335-
'',
336-
localKnativeProfile,
337-
'',
338-
].join('\n');
250+
const skaffold = renderTemplate(skaffoldTemplate, {
251+
per_function_profiles: perFnProfiles,
252+
all_raw_yaml: allRawYaml,
253+
all_port_forwards: allPortForwards,
254+
namespace: K8S_NAMESPACE,
255+
});
339256

340257
const skaffoldPath = path.join(ROOT, 'skaffold.yaml');
341258
if (writeIfChanged(skaffoldPath, skaffold)) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# AUTO-GENERATED by scripts/generate.ts — do not edit manually.
2+
apiVersion: v1
3+
kind: ConfigMap
4+
metadata:
5+
name: functions-registry
6+
data:
7+
JOBS_SUPPORTED: "{{jobs_supported}}"
8+
INTERNAL_GATEWAY_DEVELOPMENT_MAP: '{{gateway_map}}'
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
- name: {{name}}
2+
build:
3+
artifacts:
4+
- image: constructive-functions
5+
context: .
6+
docker:
7+
dockerfile: Dockerfile.dev
8+
sync:
9+
manual:
10+
- src: 'functions/**/*.ts'
11+
dest: /usr/src/app
12+
local:
13+
push: false
14+
manifests:
15+
kustomize:
16+
paths:
17+
- k8s/overlays/local-simple
18+
rawYaml:
19+
- generated/{{dir}}/k8s/local-deployment.yaml
20+
- generated/{{dir}}/k8s/functions-configmap.yaml
21+
deploy:
22+
kubectl:
23+
defaultNamespace: {{namespace}}
24+
portForward:
25+
- resourceType: service
26+
resourceName: {{name}}
27+
namespace: {{namespace}}
28+
port: 80
29+
localPort: {{port}}
30+
- resourceType: service
31+
resourceName: knative-job-service
32+
namespace: {{namespace}}
33+
port: 8080
34+
localPort: 8080
35+
- resourceType: service
36+
resourceName: postgres
37+
namespace: {{namespace}}
38+
port: 5432
39+
localPort: 5432
40+
- resourceType: service
41+
resourceName: constructive-server
42+
namespace: {{namespace}}
43+
port: 3000
44+
localPort: 3002

templates/k8s/skaffold.yaml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# AUTO-GENERATED by scripts/generate.ts — do not edit manually.
2+
# To add a function, create functions/<name>/handler.json and run `pnpm generate`.
3+
apiVersion: skaffold/v4beta11
4+
kind: Config
5+
metadata:
6+
name: constructive-functions
7+
8+
profiles:
9+
# Per-function profiles (infra + single function).
10+
{{per_function_profiles}}
11+
12+
# All functions together.
13+
- name: local-simple
14+
build:
15+
artifacts:
16+
- image: constructive-functions
17+
context: .
18+
docker:
19+
dockerfile: Dockerfile.dev
20+
sync:
21+
manual:
22+
- src: 'functions/**/*.ts'
23+
dest: /usr/src/app
24+
local:
25+
push: false
26+
manifests:
27+
kustomize:
28+
paths:
29+
- k8s/overlays/local-simple
30+
rawYaml:
31+
{{all_raw_yaml}}
32+
- generated/functions-configmap.yaml
33+
deploy:
34+
kubectl:
35+
defaultNamespace: {{namespace}}
36+
portForward:
37+
{{all_port_forwards}}
38+
- resourceType: service
39+
resourceName: knative-job-service
40+
namespace: {{namespace}}
41+
port: 8080
42+
localPort: 8080
43+
- resourceType: service
44+
resourceName: postgres
45+
namespace: {{namespace}}
46+
port: 5432
47+
localPort: 5432
48+
- resourceType: service
49+
resourceName: constructive-server
50+
namespace: {{namespace}}
51+
port: 3000
52+
localPort: 3002
53+
54+
# Full Knative setup — requires `cd k8s && make operators-knative-only` first.
55+
- name: local
56+
build:
57+
artifacts:
58+
- image: constructive-functions
59+
context: .
60+
docker:
61+
dockerfile: Dockerfile.dev
62+
sync:
63+
manual:
64+
- src: 'functions/**/*.ts'
65+
dest: /usr/src/app
66+
local:
67+
push: false
68+
manifests:
69+
kustomize:
70+
paths:
71+
- k8s/overlays/local
72+
portForward:
73+
{{all_port_forwards}}
74+
- resourceType: service
75+
resourceName: knative-job-service
76+
namespace: {{namespace}}
77+
port: 8080
78+
localPort: 8080
79+
- resourceType: service
80+
resourceName: postgres
81+
namespace: {{namespace}}
82+
port: 5432
83+
localPort: 5432
84+
- resourceType: service
85+
resourceName: constructive-server
86+
namespace: {{namespace}}
87+
port: 3000
88+
localPort: 3002

0 commit comments

Comments
 (0)