@leitwerk-dev/worker-runners 0.1.9 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/dist/{chunk-6WACBFXN.js → chunk-2P3CWUP3.js} +16 -5
  2. package/dist/chunk-2P3CWUP3.js.map +1 -0
  3. package/dist/chunk-4F7EMXMU.js +119 -0
  4. package/dist/chunk-4F7EMXMU.js.map +1 -0
  5. package/dist/chunk-6UZ2REWV.js +90 -0
  6. package/dist/chunk-6UZ2REWV.js.map +1 -0
  7. package/dist/{chunk-DL3Y3R6E.js → chunk-A3XSYWA5.js} +74 -8
  8. package/dist/chunk-A3XSYWA5.js.map +1 -0
  9. package/dist/{chunk-2TSWMVLZ.js → chunk-BRIIMAZX.js} +158 -39
  10. package/dist/chunk-BRIIMAZX.js.map +1 -0
  11. package/dist/{chunk-IRFVGUXJ.js → chunk-DFPYUJIK.js} +10 -3
  12. package/dist/chunk-DFPYUJIK.js.map +1 -0
  13. package/dist/{chunk-HLTGLGUS.js → chunk-H4JEE3ZT.js} +70 -93
  14. package/dist/chunk-H4JEE3ZT.js.map +1 -0
  15. package/dist/chunk-ISH4647R.js +506 -0
  16. package/dist/chunk-ISH4647R.js.map +1 -0
  17. package/dist/chunk-UGKQOL5E.js +39 -0
  18. package/dist/chunk-UGKQOL5E.js.map +1 -0
  19. package/dist/chunk-V7TTFLGE.js +899 -0
  20. package/dist/chunk-V7TTFLGE.js.map +1 -0
  21. package/dist/chunk-YDYI3BD2.js +315 -0
  22. package/dist/chunk-YDYI3BD2.js.map +1 -0
  23. package/dist/chunk-ZGFD76I6.js +16 -0
  24. package/dist/chunk-ZGFD76I6.js.map +1 -0
  25. package/dist/docker-client-public.d.ts +2 -0
  26. package/dist/docker-client-public.d.ts.map +1 -0
  27. package/dist/docker-client-public.js +7 -0
  28. package/dist/docker-client-public.js.map +1 -0
  29. package/dist/{docker-engine-client-Cpmy2yma.d.ts → docker-engine-client-Btw6ZTXk.d.ts} +48 -34
  30. package/dist/docker-engine-client.d.ts +47 -33
  31. package/dist/docker-engine-client.d.ts.map +1 -1
  32. package/dist/docker-engine-http-client.d.ts +18 -9
  33. package/dist/docker-engine-http-client.d.ts.map +1 -1
  34. package/dist/docker-engine-http-client.js +12 -201
  35. package/dist/docker-engine-http-client.js.map +1 -1
  36. package/dist/docker-public.d.ts +2 -0
  37. package/dist/docker-public.d.ts.map +1 -0
  38. package/dist/docker-public.js +13 -0
  39. package/dist/docker-public.js.map +1 -0
  40. package/dist/docker-worker-runner.d.ts +25 -8
  41. package/dist/docker-worker-runner.d.ts.map +1 -1
  42. package/dist/docker-worker-runner.js +12 -235
  43. package/dist/docker-worker-runner.js.map +1 -1
  44. package/dist/fake-docker-engine-client.d.ts.map +1 -1
  45. package/dist/filesystem-process-state-exporter.d.ts +10 -0
  46. package/dist/filesystem-process-state-exporter.d.ts.map +1 -0
  47. package/dist/helper-process-state-exporter.d.ts +28 -0
  48. package/dist/helper-process-state-exporter.d.ts.map +1 -0
  49. package/dist/index.d.ts +2 -11
  50. package/dist/index.d.ts.map +1 -1
  51. package/dist/index.js +46 -9
  52. package/dist/index.js.map +1 -1
  53. package/dist/kubernetes-api-client-CEe7pHC4.d.ts +453 -0
  54. package/dist/kubernetes-api-client.d.ts +113 -6
  55. package/dist/kubernetes-api-client.d.ts.map +1 -1
  56. package/dist/kubernetes-client-public.d.ts +2 -0
  57. package/dist/kubernetes-client-public.d.ts.map +1 -0
  58. package/dist/kubernetes-client-public.js +9 -0
  59. package/dist/kubernetes-client-public.js.map +1 -0
  60. package/dist/kubernetes-http-client.d.ts +11 -0
  61. package/dist/kubernetes-http-client.d.ts.map +1 -1
  62. package/dist/kubernetes-http-client.js +10 -295
  63. package/dist/kubernetes-http-client.js.map +1 -1
  64. package/dist/kubernetes-manifests.d.ts +189 -83
  65. package/dist/kubernetes-manifests.d.ts.map +1 -1
  66. package/dist/kubernetes-public.d.ts +2 -0
  67. package/dist/kubernetes-public.d.ts.map +1 -0
  68. package/dist/kubernetes-public.js +13 -0
  69. package/dist/kubernetes-public.js.map +1 -0
  70. package/dist/kubernetes-startup-sampler.d.ts +14 -0
  71. package/dist/kubernetes-startup-sampler.d.ts.map +1 -0
  72. package/dist/kubernetes-volume-pool-api.d.ts +114 -0
  73. package/dist/kubernetes-volume-pool-api.d.ts.map +1 -0
  74. package/dist/kubernetes-volume-pool.d.ts +41 -0
  75. package/dist/kubernetes-volume-pool.d.ts.map +1 -0
  76. package/dist/kubernetes-worker-runner.d.ts +46 -6
  77. package/dist/kubernetes-worker-runner.d.ts.map +1 -1
  78. package/dist/kubernetes-worker-runner.js +12 -161
  79. package/dist/kubernetes-worker-runner.js.map +1 -1
  80. package/dist/local-public.d.ts +2 -0
  81. package/dist/local-public.d.ts.map +1 -0
  82. package/dist/local-public.js +11 -0
  83. package/dist/local-public.js.map +1 -0
  84. package/dist/local-worker-runner.d.ts +22 -5
  85. package/dist/local-worker-runner.d.ts.map +1 -1
  86. package/dist/local-worker-runner.js +14 -108
  87. package/dist/local-worker-runner.js.map +1 -1
  88. package/dist/process-volume-retention.d.ts +15 -0
  89. package/dist/process-volume-retention.d.ts.map +1 -1
  90. package/dist/runner-utils.d.ts +8 -1
  91. package/dist/runner-utils.d.ts.map +1 -1
  92. package/dist/session-transfer-helper-cli.d.ts +2 -0
  93. package/dist/session-transfer-helper-cli.d.ts.map +1 -0
  94. package/dist/session-transfer-helper-cli.js +9 -0
  95. package/dist/session-transfer-helper-cli.js.map +1 -0
  96. package/dist/session-transfer-helper.d.ts +7 -0
  97. package/dist/session-transfer-helper.d.ts.map +1 -0
  98. package/dist/session-transfer-helper.js +11 -0
  99. package/dist/session-transfer-helper.js.map +1 -0
  100. package/dist/types-public.d.ts +3 -0
  101. package/dist/types-public.d.ts.map +1 -0
  102. package/dist/types-public.js +7 -0
  103. package/dist/types-public.js.map +1 -0
  104. package/dist/types.d.ts +140 -39
  105. package/dist/types.d.ts.map +1 -1
  106. package/dist/types.js +6 -11
  107. package/dist/types.js.map +1 -1
  108. package/dist/worker-labels.d.ts +36 -2
  109. package/dist/worker-labels.d.ts.map +1 -1
  110. package/package.json +26 -23
  111. package/src/docker-client-public.ts +1 -0
  112. package/src/docker-engine-client.ts +62 -36
  113. package/src/docker-engine-http-client.ts +90 -96
  114. package/src/docker-public.ts +1 -0
  115. package/src/docker-worker-runner.ts +194 -58
  116. package/src/fake-docker-engine-client.ts +13 -41
  117. package/src/filesystem-process-state-exporter.ts +42 -0
  118. package/src/helper-process-state-exporter.ts +149 -0
  119. package/src/index.ts +5 -94
  120. package/src/kubernetes-api-client.ts +179 -13
  121. package/src/kubernetes-client-public.ts +1 -0
  122. package/src/kubernetes-http-client.ts +239 -92
  123. package/src/kubernetes-manifests.ts +469 -345
  124. package/src/kubernetes-public.ts +1 -0
  125. package/src/kubernetes-startup-sampler.ts +191 -0
  126. package/src/kubernetes-volume-pool-api.ts +241 -0
  127. package/src/kubernetes-volume-pool.ts +375 -0
  128. package/src/kubernetes-worker-runner.ts +361 -44
  129. package/src/local-public.ts +1 -0
  130. package/src/local-worker-runner.ts +93 -10
  131. package/src/process-volume-retention.ts +15 -0
  132. package/src/runner-utils.ts +17 -2
  133. package/src/session-transfer-helper-cli.ts +5 -0
  134. package/src/session-transfer-helper.ts +108 -0
  135. package/src/types-public.ts +12 -0
  136. package/src/types.ts +169 -36
  137. package/src/worker-labels.ts +47 -2
  138. package/dist/chunk-2TSWMVLZ.js.map +0 -1
  139. package/dist/chunk-6WACBFXN.js.map +0 -1
  140. package/dist/chunk-DL3Y3R6E.js.map +0 -1
  141. package/dist/chunk-DUFA24LP.js +0 -401
  142. package/dist/chunk-DUFA24LP.js.map +0 -1
  143. package/dist/chunk-HLTGLGUS.js.map +0 -1
  144. package/dist/chunk-IRFVGUXJ.js.map +0 -1
  145. package/dist/chunk-P4DIFRBH.js +0 -187
  146. package/dist/chunk-P4DIFRBH.js.map +0 -1
  147. package/dist/docker-engine-client.js +0 -10
  148. package/dist/docker-engine-client.js.map +0 -1
  149. package/dist/fake-docker-engine-client.js +0 -131
  150. package/dist/fake-docker-engine-client.js.map +0 -1
  151. package/dist/kubernetes-api-client-BcwF2uon.d.ts +0 -286
  152. package/dist/kubernetes-api-client.js +0 -142
  153. package/dist/kubernetes-api-client.js.map +0 -1
  154. package/dist/kubernetes-manifests.js +0 -379
  155. package/dist/kubernetes-manifests.js.map +0 -1
  156. package/dist/process-volume-retention.js +0 -46
  157. package/dist/process-volume-retention.js.map +0 -1
  158. package/dist/runner-utils.js +0 -36
  159. package/dist/runner-utils.js.map +0 -1
  160. package/dist/worker-labels.js +0 -83
  161. package/dist/worker-labels.js.map +0 -1
@@ -1,144 +1,275 @@
1
+ import path from "node:path";
2
+ import type { DockerNetworkConfig } from "@leitwerk-dev/worker-protocol";
1
3
  import type { IsolatedStartWorkerInput, VolumeRef, WorkerExitInfo } from "./types.js";
2
4
  import {
5
+ buildExportHelperLabels,
3
6
  buildProcessResourceLabels,
4
7
  buildWorkerUnitLabels,
5
8
  PROCESS_IMAGE_PULL_SECRET_COMPONENT_VALUE,
6
9
  PROCESS_NAMESPACE_COMPONENT_VALUE,
7
10
  PROCESS_SERVER_CA_COMPONENT_VALUE,
8
11
  PROCESS_VOLUME_COMPONENT_VALUE,
9
- WORKER_LABEL_COMPONENT,
10
- WORKER_LABEL_COMPONENT_VALUE,
11
- WORKER_LABEL_INSTANCE_ID,
12
- WORKER_LABEL_MANAGED_BY,
13
- WORKER_LABEL_MANAGED_BY_VALUE,
14
- WORKER_LABEL_SERVER_EPOCH,
15
- WORKER_LABEL_WORKER_ID,
16
12
  } from "./worker-labels.js";
17
13
 
14
+ /** @internal */
18
15
  export interface KubernetesProcessVolumeSpec {
16
+ /** @internal */
19
17
  storageClassName?: string;
18
+ /** @internal */
20
19
  size: string;
20
+ /** @internal */
21
21
  accessModes: string[];
22
+ /** @internal */
22
23
  mountPath: string;
24
+ /** @internal */
23
25
  namePrefix?: string;
24
26
  }
25
27
 
28
+ /** @internal */
26
29
  export interface KubernetesProcessNamespaceManifest {
30
+ /** @internal */
27
31
  apiVersion: "v1";
32
+ /** @internal */
28
33
  kind: "Namespace";
29
- metadata: { name: string; labels: Record<string, string> };
34
+ /** @internal */
35
+ metadata: {
36
+ /** @internal */
37
+ name: string;
38
+ /** @internal */
39
+ labels: Record<string, string>;
40
+ };
30
41
  }
31
42
 
43
+ /** @internal */
32
44
  export interface KubernetesWorkerServerCaConfigMapSpec {
45
+ /** @internal */
33
46
  name: string;
47
+ /** @internal */
34
48
  key: string;
49
+ /** @internal */
35
50
  mountPath: string;
36
51
  }
37
52
 
53
+ /** @internal */
54
+ export interface KubernetesDockerPodSpecOptions {
55
+ /** @internal */
56
+ runtimeClassName: string;
57
+ /** @internal */
58
+ hostUsers?: boolean;
59
+ /** @internal */
60
+ gvisor?: boolean;
61
+ /** @internal */
62
+ network?: DockerNetworkConfig;
63
+ }
64
+
65
+ /** @internal */
38
66
  export interface KubernetesPodSpecOptions {
67
+ /** @internal */
39
68
  namespace: string;
69
+ /** @internal */
40
70
  workerServiceAccount?: string;
71
+ /** @internal */
41
72
  imagePullSecrets?: string[];
73
+ /** @internal */
42
74
  imagePullPolicy?: string;
75
+ /** @internal */
43
76
  nodeSelector?: Record<string, string>;
77
+ /** @internal */
44
78
  annotations?: Record<string, string>;
79
+ /** @internal */
45
80
  tolerations?: unknown[];
46
- hostAliases?: Array<{ ip: string; hostnames: string[] }>;
81
+ /** @internal */
82
+ hostAliases?: Array<{
83
+ /** @internal */
84
+ ip: string;
85
+ /** @internal */
86
+ hostnames: string[];
87
+ }>;
88
+ /** @internal */
47
89
  serverCaConfigMap?: KubernetesWorkerServerCaConfigMapSpec;
90
+ /** Trusted operator wiring used only when the process declares runtime.docker. @internal */
91
+ docker?: KubernetesDockerPodSpecOptions;
48
92
  }
49
93
 
50
- export interface KubernetesPersistentVolumeClaimManifest {
94
+ /** @internal */
95
+ interface NamespacedManifest<Kind extends string> {
96
+ /** @internal */
51
97
  apiVersion: "v1";
52
- kind: "PersistentVolumeClaim";
53
- metadata: { name: string; namespace: string; labels: Record<string, string> };
98
+ /** @internal */
99
+ kind: Kind;
100
+ /** @internal */
101
+ metadata: {
102
+ /** @internal */
103
+ name: string;
104
+ /** @internal */
105
+ namespace: string;
106
+ /** @internal */
107
+ labels: Record<string, string>;
108
+ };
109
+ }
110
+
111
+ /** @internal */
112
+ export interface KubernetesPersistentVolumeClaimManifest
113
+ extends NamespacedManifest<"PersistentVolumeClaim"> {
114
+ /** @internal */
54
115
  spec: {
116
+ /** @internal */
55
117
  accessModes: string[];
56
- resources: { requests: { storage: string } };
118
+ /** @internal */
119
+ resources: {
120
+ /** @internal */
121
+ requests: {
122
+ /** @internal */
123
+ storage: string;
124
+ };
125
+ };
126
+ /** @internal */
57
127
  storageClassName?: string;
58
128
  };
59
129
  }
60
130
 
61
- export interface KubernetesConfigMapManifest {
62
- apiVersion: "v1";
63
- kind: "ConfigMap";
64
- metadata: { name: string; namespace: string; labels: Record<string, string> };
131
+ /** @internal */
132
+ export interface KubernetesConfigMapManifest extends NamespacedManifest<"ConfigMap"> {
133
+ /** @internal */
65
134
  data: Record<string, string>;
66
135
  }
67
136
 
68
- export interface KubernetesDockerConfigJsonSecretManifest {
69
- apiVersion: "v1";
70
- kind: "Secret";
71
- metadata: { name: string; namespace: string; labels: Record<string, string> };
137
+ /** @internal */
138
+ export interface KubernetesDockerConfigJsonSecretManifest extends NamespacedManifest<"Secret"> {
139
+ /** @internal */
72
140
  type: "kubernetes.io/dockerconfigjson";
73
- data: { ".dockerconfigjson": string };
74
- }
75
-
76
- export interface KubernetesValidatingAdmissionPolicyManifest {
77
- apiVersion: "admissionregistration.k8s.io/v1";
78
- kind: "ValidatingAdmissionPolicy";
79
- metadata: { name: string; labels?: Record<string, string> };
80
- spec: {
81
- matchConditions?: Array<{ name: string; expression: string }>;
82
- matchConstraints: {
83
- resourceRules: Array<{
84
- apiGroups: string[];
85
- apiVersions: string[];
86
- operations: string[];
87
- resources: string[];
88
- }>;
89
- };
90
- validations: Array<{ expression: string; message: string }>;
91
- };
92
- }
93
-
94
- export interface KubernetesValidatingAdmissionPolicyBindingManifest {
95
- apiVersion: "admissionregistration.k8s.io/v1";
96
- kind: "ValidatingAdmissionPolicyBinding";
97
- metadata: { name: string; labels?: Record<string, string> };
98
- spec: {
99
- policyName: string;
100
- validationActions: string[];
141
+ /** @internal */
142
+ data: {
143
+ /** @internal */
144
+ ".dockerconfigjson": string;
101
145
  };
102
146
  }
103
147
 
148
+ /** @internal */
104
149
  export interface KubernetesPodEventSummary {
150
+ /** @internal */
151
+ objectUid?: string;
152
+ /** @internal */
153
+ fieldPath?: string;
154
+ /** @internal */
155
+ firstTimestamp?: string;
156
+ /** @internal */
157
+ eventTime?: string;
158
+ /** @internal */
105
159
  type?: string;
160
+ /** @internal */
106
161
  reason?: string;
162
+ /** @internal */
107
163
  message?: string;
164
+ /** @internal */
108
165
  count?: number;
166
+ /** @internal */
109
167
  lastTimestamp?: string;
110
168
  }
111
169
 
112
- export interface KubernetesPodManifest {
113
- apiVersion: "v1";
114
- kind: "Pod";
115
- metadata: {
116
- name: string;
117
- namespace: string;
118
- labels: Record<string, string>;
170
+ /** @internal */
171
+ export interface KubernetesPodManifest extends NamespacedManifest<"Pod"> {
172
+ /** @internal */
173
+ metadata: NamespacedManifest<"Pod">["metadata"] & {
174
+ /** @internal */
119
175
  annotations?: Record<string, string>;
120
176
  };
177
+ /** @internal */
121
178
  spec: {
179
+ /** @internal */
122
180
  restartPolicy: "Never";
181
+ /** @internal */
182
+ runtimeClassName?: string;
183
+ /** @internal */
184
+ hostUsers?: boolean;
185
+ /** @internal */
123
186
  serviceAccountName?: string;
124
- nodeSelector?: Record<string, string>;
125
- tolerations?: unknown[];
126
- hostAliases?: Array<{ ip: string; hostnames: string[] }>;
127
- imagePullSecrets?: Array<{ name: string }>;
187
+ /** @internal */
188
+ nodeSelector?: KubernetesPodSpecOptions["nodeSelector"];
189
+ /** @internal */
190
+ tolerations?: KubernetesPodSpecOptions["tolerations"];
191
+ /** @internal */
192
+ hostAliases?: KubernetesPodSpecOptions["hostAliases"];
193
+ /** @internal */
194
+ imagePullSecrets?: Array<{
195
+ /** @internal */
196
+ name: string;
197
+ }>;
198
+ /** @internal */
199
+ automountServiceAccountToken?: boolean;
200
+ /** @internal */
128
201
  containers: Array<{
129
- name: "worker";
202
+ /** @internal */
203
+ name: string;
204
+ /** @internal */
205
+ securityContext?: {
206
+ /** @internal */
207
+ privileged: false;
208
+ /** @internal */
209
+ capabilities: {
210
+ /** @internal */
211
+ add: string[];
212
+ };
213
+ };
214
+ /** @internal */
130
215
  image: string;
216
+ /** @internal */
217
+ command?: string[];
218
+ /** @internal */
131
219
  imagePullPolicy?: string;
132
- env: Array<{ name: string; value: string }>;
133
- volumeMounts: Array<{ name: string; mountPath: string; readOnly?: boolean }>;
220
+ /** @internal */
221
+ env: Array<{
222
+ /** @internal */
223
+ name: string;
224
+ /** @internal */
225
+ value: string;
226
+ }>;
227
+ /** @internal */
228
+ volumeMounts: Array<{
229
+ /** @internal */
230
+ name: string;
231
+ /** @internal */
232
+ mountPath: string;
233
+ /** @internal */
234
+ readOnly?: boolean;
235
+ /** @internal */
236
+ subPath?: string;
237
+ }>;
238
+ /** @internal */
134
239
  resources?: {
240
+ /** @internal */
135
241
  requests?: Record<string, string>;
242
+ /** @internal */
136
243
  limits?: Record<string, string>;
137
244
  };
138
245
  }>;
246
+ /** @internal */
139
247
  volumes: Array<
140
- | { name: string; persistentVolumeClaim: { claimName: string } }
141
- | { name: string; configMap: { name: string; items: Array<{ key: string; path: string }> } }
248
+ | {
249
+ /** @internal */
250
+ name: string;
251
+ /** @internal */
252
+ persistentVolumeClaim: {
253
+ /** @internal */
254
+ claimName: string;
255
+ };
256
+ }
257
+ | {
258
+ /** @internal */
259
+ name: string;
260
+ /** @internal */
261
+ configMap: {
262
+ /** @internal */
263
+ name: string;
264
+ /** @internal */
265
+ items: Array<{
266
+ /** @internal */
267
+ key: string;
268
+ /** @internal */
269
+ path: string;
270
+ }>;
271
+ };
272
+ }
142
273
  >;
143
274
  };
144
275
  }
@@ -152,15 +283,12 @@ export const KUBERNETES_WORKER_SERVER_CA_MOUNT_PATH = "/leitwerk/server-ca";
152
283
  export const KUBERNETES_WORKER_SERVER_CA_CERT_PATH = `${KUBERNETES_WORKER_SERVER_CA_MOUNT_PATH}/${KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_KEY}`;
153
284
 
154
285
  function safeKubernetesDnsName(value: string, maxLen = DNS_LABEL_MAX): string {
155
- const cleaned =
156
- value
157
- .toLowerCase()
158
- .replace(/[^a-z0-9-]/g, "-")
159
- .replace(/^-+|-+$/g, "") || "x";
286
+ const cleaned = sanitizeKubernetesNameSegment(value);
160
287
  const trimmed = cleaned.slice(0, maxLen).replace(/-+$/g, "");
161
288
  return trimmed.length > 0 ? trimmed : "x";
162
289
  }
163
290
 
291
+ /** @internal */
164
292
  export function sanitizeKubernetesNameSegment(value: string): string {
165
293
  const cleaned = value
166
294
  .toLowerCase()
@@ -169,12 +297,28 @@ export function sanitizeKubernetesNameSegment(value: string): string {
169
297
  return cleaned.length > 0 ? cleaned : "x";
170
298
  }
171
299
 
300
+ /** @internal */
172
301
  export function kubernetesWorkerPodName(instanceId: string, workerId: string): string {
173
302
  return safeKubernetesDnsName(
174
303
  `leitwerk-worker-${sanitizeKubernetesNameSegment(instanceId)}-${sanitizeKubernetesNameSegment(workerId)}`,
175
304
  );
176
305
  }
177
306
 
307
+ /** @internal */
308
+ export function kubernetesExportHelperPodName(instanceId: string, exportId: string): string {
309
+ const exportSegment = sanitizeKubernetesNameSegment(exportId);
310
+ const suffix = `-${exportSegment}`;
311
+ const instancePrefix = safeKubernetesDnsName(
312
+ `leitwerk-export-${sanitizeKubernetesNameSegment(instanceId)}`,
313
+ Math.max(1, DNS_LABEL_MAX - suffix.length),
314
+ );
315
+ // Keep the random export id at the end of the name. Truncating the full
316
+ // prefix would otherwise make long instance ids produce colliding helper
317
+ // names for different exports.
318
+ return safeKubernetesDnsName(`${instancePrefix}${suffix}`);
319
+ }
320
+
321
+ /** @internal */
178
322
  export function kubernetesProcessPvcName(
179
323
  instanceId: string,
180
324
  namePrefix = "leitwerk-process-",
@@ -182,6 +326,7 @@ export function kubernetesProcessPvcName(
182
326
  return safeKubernetesDnsName(`${namePrefix}${sanitizeKubernetesNameSegment(instanceId)}`);
183
327
  }
184
328
 
329
+ /** @internal */
185
330
  export function kubernetesProcessNamespaceName(
186
331
  instanceId: string,
187
332
  processNamespacePrefix = "leitwerk-process-",
@@ -191,9 +336,13 @@ export function kubernetesProcessNamespaceName(
191
336
  );
192
337
  }
193
338
 
339
+ /** @internal */
194
340
  export function buildKubernetesProcessNamespaceManifest(args: {
341
+ /** @internal */
195
342
  instanceId: string;
343
+ /** @internal */
196
344
  processNamespacePrefix?: string;
345
+ /** @internal */
197
346
  extraLabels?: Record<string, string>;
198
347
  }): KubernetesProcessNamespaceManifest {
199
348
  return {
@@ -209,16 +358,32 @@ export function buildKubernetesProcessNamespaceManifest(args: {
209
358
  };
210
359
  }
211
360
 
361
+ function processResourceMetadata(
362
+ args: { instanceId: string; namespace: string; extraLabels?: Record<string, string> },
363
+ name: string,
364
+ component: Parameters<typeof buildProcessResourceLabels>[0]["component"],
365
+ ): NamespacedManifest<string>["metadata"] {
366
+ return {
367
+ name,
368
+ namespace: args.namespace,
369
+ labels: buildProcessResourceLabels(
370
+ { instanceId: args.instanceId, component },
371
+ args.extraLabels,
372
+ ),
373
+ };
374
+ }
375
+
376
+ /** @internal */
212
377
  export function buildKubernetesProcessPvcManifest(args: {
378
+ /** @internal */
213
379
  instanceId: string;
380
+ /** @internal */
214
381
  namespace: string;
382
+ /** @internal */
215
383
  volume: KubernetesProcessVolumeSpec;
384
+ /** @internal */
216
385
  extraLabels?: Record<string, string>;
217
386
  }): KubernetesPersistentVolumeClaimManifest {
218
- const labels = buildProcessResourceLabels(
219
- { instanceId: args.instanceId, component: PROCESS_VOLUME_COMPONENT_VALUE },
220
- args.extraLabels,
221
- );
222
387
  const spec: KubernetesPersistentVolumeClaimManifest["spec"] = {
223
388
  accessModes: [...args.volume.accessModes],
224
389
  resources: { requests: { storage: args.volume.size } },
@@ -229,11 +394,11 @@ export function buildKubernetesProcessPvcManifest(args: {
229
394
  return {
230
395
  apiVersion: "v1",
231
396
  kind: "PersistentVolumeClaim",
232
- metadata: {
233
- name: kubernetesProcessPvcName(args.instanceId, args.volume.namePrefix),
234
- namespace: args.namespace,
235
- labels,
236
- },
397
+ metadata: processResourceMetadata(
398
+ args,
399
+ kubernetesProcessPvcName(args.instanceId, args.volume.namePrefix),
400
+ PROCESS_VOLUME_COMPONENT_VALUE,
401
+ ),
237
402
  spec,
238
403
  };
239
404
  }
@@ -246,18 +411,14 @@ export function buildKubernetesServerCaConfigMapManifest(args: {
246
411
  key?: string;
247
412
  extraLabels?: Record<string, string>;
248
413
  }): KubernetesConfigMapManifest {
249
- const labels = buildProcessResourceLabels(
250
- { instanceId: args.instanceId, component: PROCESS_SERVER_CA_COMPONENT_VALUE },
251
- args.extraLabels,
252
- );
253
414
  return {
254
415
  apiVersion: "v1",
255
416
  kind: "ConfigMap",
256
- metadata: {
257
- name: args.name ?? KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_NAME,
258
- namespace: args.namespace,
259
- labels,
260
- },
417
+ metadata: processResourceMetadata(
418
+ args,
419
+ args.name ?? KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_NAME,
420
+ PROCESS_SERVER_CA_COMPONENT_VALUE,
421
+ ),
261
422
  data: { [args.key ?? KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_KEY]: args.caPem },
262
423
  };
263
424
  }
@@ -272,17 +433,7 @@ export function buildKubernetesDockerConfigJsonSecretManifest(args: {
272
433
  return {
273
434
  apiVersion: "v1",
274
435
  kind: "Secret",
275
- metadata: {
276
- name: args.name,
277
- namespace: args.namespace,
278
- labels: buildProcessResourceLabels(
279
- {
280
- instanceId: args.instanceId,
281
- component: PROCESS_IMAGE_PULL_SECRET_COMPONENT_VALUE,
282
- },
283
- args.extraLabels,
284
- ),
285
- },
436
+ metadata: processResourceMetadata(args, args.name, PROCESS_IMAGE_PULL_SECRET_COMPONENT_VALUE),
286
437
  type: "kubernetes.io/dockerconfigjson",
287
438
  data: { ".dockerconfigjson": args.dockerConfigJson },
288
439
  };
@@ -297,93 +448,222 @@ function envList(env: Record<string, string>): Array<{ name: string; value: stri
297
448
  function resources(
298
449
  input: IsolatedStartWorkerInput,
299
450
  ): KubernetesPodManifest["spec"]["containers"][number]["resources"] {
300
- const limits: Record<string, string> = {};
301
- if (input.resources?.cpu) limits.cpu = input.resources.cpu;
302
- if (input.resources?.memory) limits.memory = input.resources.memory;
303
- return Object.keys(limits).length > 0 ? { limits } : undefined;
451
+ const result: NonNullable<ReturnType<typeof resources>> = {};
452
+ for (const [kind, values] of [
453
+ ["limits", { cpu: input.resources?.cpu, memory: input.resources?.memory }],
454
+ ["requests", input.resourceRequests],
455
+ ] as const) {
456
+ const entries = Object.entries(values ?? {}).filter(([, value]) => value);
457
+ if (entries.length) result[kind] = Object.fromEntries(entries);
458
+ }
459
+ return Object.keys(result).length ? result : undefined;
304
460
  }
305
461
 
306
- export function buildKubernetesWorkerPodManifest(
307
- input: IsolatedStartWorkerInput,
462
+ function podManifest(
463
+ name: string,
464
+ volume: VolumeRef,
465
+ labels: Record<string, string>,
308
466
  options: KubernetesPodSpecOptions,
467
+ container: KubernetesPodManifest["spec"]["containers"][number],
468
+ spec: Partial<KubernetesPodManifest["spec"]> = {},
309
469
  ): KubernetesPodManifest {
310
- const volume = input.volume;
311
- const labels = buildWorkerUnitLabels({
312
- instanceId: input.instanceId,
313
- workerId: input.workerId,
314
- serverEpoch: input.serverEpoch,
315
- });
316
- const containerResources = resources(input);
317
- const ca = options.serverCaConfigMap;
318
- const caCertPath = ca ? `${ca.mountPath}/${ca.key}` : undefined;
319
- const containerEnv = caCertPath ? { ...input.env, NODE_EXTRA_CA_CERTS: caCertPath } : input.env;
320
- const volumeMounts: KubernetesPodManifest["spec"]["containers"][number]["volumeMounts"] = [
321
- { name: WORKER_VOLUME_NAME, mountPath: volume.mountPath },
322
- ];
323
- if (ca) {
324
- volumeMounts.push({
325
- name: WORKER_SERVER_CA_VOLUME_NAME,
326
- mountPath: ca.mountPath,
327
- readOnly: true,
328
- });
329
- }
330
- const container: KubernetesPodManifest["spec"]["containers"][number] = {
331
- name: "worker",
332
- image: input.image.reference,
333
- env: envList(containerEnv),
334
- volumeMounts,
335
- ...(options.imagePullPolicy ? { imagePullPolicy: options.imagePullPolicy } : {}),
336
- ...(containerResources ? { resources: containerResources } : {}),
337
- };
338
- const hasNodeSelector = options.nodeSelector && Object.keys(options.nodeSelector).length > 0;
339
- const hasTolerations = options.tolerations && options.tolerations.length > 0;
340
- const hasPullSecrets = options.imagePullSecrets && options.imagePullSecrets.length > 0;
341
- const hasAnnotations = options.annotations && Object.keys(options.annotations).length > 0;
342
- const hasHostAliases = options.hostAliases && options.hostAliases.length > 0;
343
470
  return {
344
471
  apiVersion: "v1",
345
472
  kind: "Pod",
346
473
  metadata: {
347
- name: kubernetesWorkerPodName(input.instanceId, input.workerId),
474
+ name,
348
475
  namespace: volume.namespace ?? options.namespace,
349
476
  labels,
350
- ...(hasAnnotations ? { annotations: { ...options.annotations } } : {}),
477
+ ...(options.annotations && Object.keys(options.annotations).length > 0
478
+ ? { annotations: { ...options.annotations } }
479
+ : {}),
351
480
  },
352
481
  spec: {
353
482
  restartPolicy: "Never",
354
- containers: [container],
355
- volumes: [
356
- { name: WORKER_VOLUME_NAME, persistentVolumeClaim: { claimName: volume.id } },
357
- ...(ca
358
- ? [
359
- {
360
- name: WORKER_SERVER_CA_VOLUME_NAME,
361
- configMap: { name: ca.name, items: [{ key: ca.key, path: ca.key }] },
362
- },
363
- ]
364
- : []),
365
- ],
366
483
  ...(options.workerServiceAccount?.trim()
367
484
  ? { serviceAccountName: options.workerServiceAccount }
368
485
  : {}),
369
- ...(hasPullSecrets
370
- ? { imagePullSecrets: options.imagePullSecrets?.map((n) => ({ name: n })) }
486
+ ...(options.imagePullSecrets?.length
487
+ ? { imagePullSecrets: options.imagePullSecrets.map((name) => ({ name })) }
488
+ : {}),
489
+ ...(options.nodeSelector && Object.keys(options.nodeSelector).length > 0
490
+ ? { nodeSelector: { ...options.nodeSelector } }
371
491
  : {}),
372
- ...(hasNodeSelector ? { nodeSelector: { ...options.nodeSelector } } : {}),
373
- ...(hasTolerations ? { tolerations: [...(options.tolerations as unknown[])] } : {}),
374
- ...(hasHostAliases
492
+ ...(options.tolerations?.length ? { tolerations: [...options.tolerations] } : {}),
493
+ ...(options.hostAliases?.length
375
494
  ? {
376
- hostAliases: options.hostAliases?.map(({ ip, hostnames }) => ({
495
+ hostAliases: options.hostAliases.map(({ ip, hostnames }) => ({
377
496
  ip,
378
497
  hostnames: [...hostnames],
379
498
  })),
380
499
  }
381
500
  : {}),
501
+ ...spec,
502
+ containers: [container],
503
+ volumes: processStateVolumes(volume.id, options.serverCaConfigMap),
382
504
  },
383
505
  };
384
506
  }
385
507
 
386
- function formatPodEvent(event: KubernetesPodEventSummary): string | null {
508
+ function processStateVolumes(
509
+ claimName: string,
510
+ ca: KubernetesWorkerServerCaConfigMapSpec | undefined,
511
+ ): KubernetesPodManifest["spec"]["volumes"] {
512
+ return [
513
+ { name: WORKER_VOLUME_NAME, persistentVolumeClaim: { claimName } },
514
+ ...(ca
515
+ ? [
516
+ {
517
+ name: WORKER_SERVER_CA_VOLUME_NAME,
518
+ configMap: { name: ca.name, items: [{ key: ca.key, path: ca.key }] },
519
+ },
520
+ ]
521
+ : []),
522
+ ];
523
+ }
524
+
525
+ function processStateContainerConfig(
526
+ env: Record<string, string>,
527
+ mountPath: string,
528
+ exportOnly: boolean,
529
+ ca: KubernetesWorkerServerCaConfigMapSpec | undefined,
530
+ ): Pick<KubernetesPodManifest["spec"]["containers"][number], "env" | "volumeMounts"> {
531
+ return {
532
+ env: envList(ca ? { ...env, NODE_EXTRA_CA_CERTS: `${ca.mountPath}/${ca.key}` } : env),
533
+ volumeMounts: [
534
+ ...(exportOnly
535
+ ? ["workspace", "tree"].map((subPath) => ({
536
+ name: WORKER_VOLUME_NAME,
537
+ mountPath: path.posix.join(mountPath, subPath),
538
+ subPath,
539
+ readOnly: true,
540
+ }))
541
+ : [{ name: WORKER_VOLUME_NAME, mountPath }]),
542
+ ...(ca
543
+ ? [{ name: WORKER_SERVER_CA_VOLUME_NAME, mountPath: ca.mountPath, readOnly: true }]
544
+ : []),
545
+ ],
546
+ };
547
+ }
548
+
549
+ /** @internal */
550
+ export function buildKubernetesExportHelperPodManifest(
551
+ input: {
552
+ /** @internal */
553
+ instanceId: string;
554
+ /** @internal */
555
+ exportId: string;
556
+ /** @internal */
557
+ image: string;
558
+ /** @internal */
559
+ command: string[];
560
+ /** @internal */
561
+ env: Record<string, string>;
562
+ /** @internal */
563
+ volume: VolumeRef;
564
+ },
565
+ options: KubernetesPodSpecOptions,
566
+ ): KubernetesPodManifest {
567
+ return podManifest(
568
+ kubernetesExportHelperPodName(input.instanceId, input.exportId),
569
+ input.volume,
570
+ buildExportHelperLabels({ instanceId: input.instanceId, exportId: input.exportId }),
571
+ options,
572
+ {
573
+ name: "session-export-helper",
574
+ image: input.image,
575
+ command: [...input.command],
576
+ ...processStateContainerConfig(
577
+ input.env,
578
+ input.volume.mountPath,
579
+ true,
580
+ options.serverCaConfigMap,
581
+ ),
582
+ ...(options.imagePullPolicy ? { imagePullPolicy: options.imagePullPolicy } : {}),
583
+ },
584
+ { automountServiceAccountToken: false },
585
+ );
586
+ }
587
+
588
+ /** @internal */
589
+ export function buildKubernetesWorkerPodManifest(
590
+ input: IsolatedStartWorkerInput,
591
+ options: KubernetesPodSpecOptions,
592
+ ): KubernetesPodManifest {
593
+ const volume = input.volume;
594
+ const labels = buildWorkerUnitLabels({
595
+ instanceId: input.instanceId,
596
+ workerId: input.workerId,
597
+ serverEpoch: input.serverEpoch,
598
+ });
599
+ const containerResources = resources(input);
600
+ const ca = options.serverCaConfigMap;
601
+ const trustedDockerEnv = input.docker
602
+ ? {
603
+ ...input.env,
604
+ DOCKER_HOST: "unix:///var/run/docker.sock",
605
+ LEITWERK_PRIVATE_DOCKER: "1",
606
+ ...(options.docker?.gvisor ? { LEITWERK_DOCKER_GVISOR: "1" } : {}),
607
+ LEITWERK_PROCESS_VOLUME_MOUNT_PATH: volume.mountPath,
608
+ ...(options.docker?.network
609
+ ? { LEITWERK_DOCKER_NETWORK: JSON.stringify(options.docker.network) }
610
+ : {}),
611
+ }
612
+ : input.env;
613
+ const container: KubernetesPodManifest["spec"]["containers"][number] = {
614
+ name: "worker",
615
+ ...(input.docker && options.docker?.gvisor
616
+ ? {
617
+ securityContext: {
618
+ privileged: false as const,
619
+ capabilities: { add: ["SYS_ADMIN", "NET_ADMIN"] },
620
+ },
621
+ }
622
+ : {}),
623
+ image: input.image.reference,
624
+ ...processStateContainerConfig(trustedDockerEnv, volume.mountPath, false, ca),
625
+ ...(options.imagePullPolicy ? { imagePullPolicy: options.imagePullPolicy } : {}),
626
+ ...(containerResources ? { resources: containerResources } : {}),
627
+ };
628
+ return podManifest(
629
+ kubernetesWorkerPodName(input.instanceId, input.workerId),
630
+ volume,
631
+ labels,
632
+ options,
633
+ container,
634
+ input.docker && options.docker
635
+ ? {
636
+ runtimeClassName: options.docker.runtimeClassName,
637
+ ...(options.docker.gvisor ? {} : { hostUsers: options.docker.hostUsers }),
638
+ }
639
+ : {},
640
+ );
641
+ }
642
+
643
+ const KUBERNETES_DIAGNOSTIC_MAX_LENGTH = 2_048;
644
+
645
+ export function redactKubernetesDiagnostic(
646
+ value: string,
647
+ sensitiveValues: readonly string[] = [],
648
+ ): string {
649
+ let redacted = value;
650
+ for (const sensitiveValue of sensitiveValues) {
651
+ if (sensitiveValue.length >= 8) redacted = redacted.replaceAll(sensitiveValue, "<redacted>");
652
+ }
653
+ return redacted;
654
+ }
655
+
656
+ function boundedDiagnostic(value: string, sensitiveValues: readonly string[] = []): string {
657
+ const redacted = redactKubernetesDiagnostic(value, sensitiveValues);
658
+ return redacted.length <= KUBERNETES_DIAGNOSTIC_MAX_LENGTH
659
+ ? redacted
660
+ : `${redacted.slice(0, KUBERNETES_DIAGNOSTIC_MAX_LENGTH - 1)}…`;
661
+ }
662
+
663
+ function formatPodEvent(
664
+ event: KubernetesPodEventSummary,
665
+ sensitiveValues: readonly string[],
666
+ ): string | null {
387
667
  const pieces: string[] = [];
388
668
  if (event.type?.trim()) pieces.push(event.type.trim());
389
669
  if (event.reason?.trim()) pieces.push(event.reason.trim());
@@ -391,31 +671,56 @@ function formatPodEvent(event: KubernetesPodEventSummary): string | null {
391
671
  const prefix = pieces.join(" ");
392
672
  const message = event.message?.trim();
393
673
  if (!prefix && !message) return null;
394
- return message ? `${prefix ? `${prefix}: ` : ""}${message}` : prefix;
674
+ return boundedDiagnostic(
675
+ message ? `${prefix ? `${prefix}: ` : ""}${message}` : prefix,
676
+ sensitiveValues,
677
+ );
395
678
  }
396
679
 
680
+ /** @internal */
397
681
  export function formatKubernetesPodDiagnostics(
398
682
  events: readonly KubernetesPodEventSummary[] | undefined,
399
683
  limit = 3,
684
+ sensitiveValues: readonly string[] = [],
400
685
  ): string | undefined {
401
686
  const formatted = (events ?? [])
402
- .map(formatPodEvent)
687
+ .map((event) => formatPodEvent(event, sensitiveValues))
403
688
  .filter((entry): entry is string => entry !== null)
404
689
  .slice(0, limit);
405
- return formatted.length > 0 ? `Kubernetes events: ${formatted.join(" | ")}` : undefined;
690
+ return formatted.length > 0
691
+ ? boundedDiagnostic(`Kubernetes events: ${formatted.join(" | ")}`, sensitiveValues)
692
+ : undefined;
406
693
  }
407
694
 
695
+ /** @internal */
408
696
  export function mapKubernetesPodExit(args: {
697
+ /** @internal */
409
698
  phase?: string;
699
+ /** @internal */
410
700
  reason?: string;
701
+ /** @internal */
411
702
  exitCode?: number | null;
703
+ /** @internal */
412
704
  signal?: string | null;
705
+ /** @internal */
413
706
  oomKilled?: boolean;
707
+ /** @internal */
708
+ terminationMessage?: string;
709
+ /** @internal */
414
710
  events?: readonly KubernetesPodEventSummary[];
711
+ /** @internal */
712
+ sensitiveValues?: readonly string[];
415
713
  }): WorkerExitInfo {
714
+ const sensitiveValues = args.sensitiveValues ?? [];
416
715
  const baseReason = args.reason ?? args.phase;
417
- const diagnostics = formatKubernetesPodDiagnostics(args.events);
418
- const reason = [baseReason, diagnostics].filter(Boolean).join("; ");
716
+ const startupDiagnostic = args.terminationMessage?.trim()
717
+ ? `Runtime startup: ${boundedDiagnostic(args.terminationMessage.trim(), sensitiveValues)}`
718
+ : undefined;
719
+ const diagnostics = formatKubernetesPodDiagnostics(args.events, 3, sensitiveValues);
720
+ const reason = boundedDiagnostic(
721
+ [baseReason, startupDiagnostic, diagnostics].filter(Boolean).join("; "),
722
+ sensitiveValues,
723
+ );
419
724
  return {
420
725
  exitCode: args.exitCode ?? null,
421
726
  signal: args.signal ?? null,
@@ -424,187 +729,6 @@ export function mapKubernetesPodExit(args: {
424
729
  };
425
730
  }
426
731
 
427
- interface AdmissionRule {
428
- /** "Namespace" = only-namespace, "!Namespace" = everything-but-namespace, or a specific kind */
429
- scope: string;
430
- condition: string;
431
- message: string;
432
- }
433
-
434
- function admissionObjectExpression(): string {
435
- return "(request.operation == 'DELETE' ? oldObject : object)";
436
- }
437
-
438
- function toAdmissionObjectCondition(condition: string): string {
439
- return condition.replaceAll("object.", `${admissionObjectExpression()}.`);
440
- }
441
-
442
- function toCelExpression(rule: AdmissionRule): string {
443
- const obj = admissionObjectExpression();
444
- const condition = toAdmissionObjectCondition(rule.condition);
445
- if (rule.scope === "Namespace") return `${obj}.kind != 'Namespace' || ${condition}`;
446
- if (rule.scope === "!Namespace") return `${obj}.kind == 'Namespace' || ${condition}`;
447
- return `${obj}.kind != '${rule.scope}' || ${condition}`;
448
- }
449
-
450
- export function buildKubernetesAdmissionPolicyManifests(args: {
451
- name: string;
452
- serverNamespace: string;
453
- serverServiceAccountName: string;
454
- processNamespacePrefix: string;
455
- allowedWorkerServiceAccount?: string;
456
- allowedImagePullSecretNames?: string[];
457
- labels?: Record<string, string>;
458
- }): {
459
- policy: KubernetesValidatingAdmissionPolicyManifest;
460
- binding: KubernetesValidatingAdmissionPolicyBindingManifest;
461
- } {
462
- const p = args.processNamespacePrefix;
463
- const sa = args.allowedWorkerServiceAccount?.trim() || "leitwerk-worker";
464
- const M = WORKER_LABEL_MANAGED_BY;
465
- const MV = WORKER_LABEL_MANAGED_BY_VALUE;
466
- const C = WORKER_LABEL_COMPONENT;
467
- const NS = PROCESS_NAMESPACE_COMPONENT_VALUE;
468
- const PV = PROCESS_VOLUME_COMPONENT_VALUE;
469
- const CA = PROCESS_SERVER_CA_COMPONENT_VALUE;
470
- const IPS = PROCESS_IMAGE_PULL_SECRET_COMPONENT_VALUE;
471
- const CV = WORKER_LABEL_COMPONENT_VALUE;
472
- const allowedSecretNames = (args.allowedImagePullSecretNames ?? []).map((name) => `'${name}'`);
473
-
474
- const rules: AdmissionRule[] = [
475
- {
476
- scope: "Namespace",
477
- condition: `object.metadata.name.startsWith('${p}')`,
478
- message: "leitwerk process namespaces must use the configured prefix",
479
- },
480
- {
481
- scope: "Namespace",
482
- condition: `object.metadata.labels['${M}'] == '${MV}'`,
483
- message: "leitwerk process namespaces must carry managed-by label",
484
- },
485
- {
486
- scope: "Namespace",
487
- condition: `object.metadata.labels['${C}'] == '${NS}'`,
488
- message: "leitwerk process namespaces must carry process-namespace component label",
489
- },
490
- {
491
- scope: "!Namespace",
492
- condition: `object.metadata.namespace.startsWith('${p}')`,
493
- message: "leitwerk process resources must be created in process namespaces",
494
- },
495
- {
496
- scope: "!Namespace",
497
- condition: `object.metadata.labels['${M}'] == '${MV}'`,
498
- message: "leitwerk process resources must carry managed-by label",
499
- },
500
- {
501
- scope: "PersistentVolumeClaim",
502
- condition: `object.metadata.labels['${C}'] == '${PV}'`,
503
- message: "leitwerk PVCs must carry process-volume component label",
504
- },
505
- {
506
- scope: "ConfigMap",
507
- condition: `object.metadata.labels['${C}'] == '${CA}'`,
508
- message: "leitwerk server CA ConfigMaps must carry server-ca component label",
509
- },
510
- {
511
- scope: "Pod",
512
- condition: `object.metadata.labels['${C}'] == '${CV}'`,
513
- message: "leitwerk pods must carry worker component label",
514
- },
515
- {
516
- scope: "ServiceAccount",
517
- condition: `object.metadata.labels['${C}'] == 'worker-service-account'`,
518
- message: "leitwerk worker ServiceAccounts must carry worker-service-account component label",
519
- },
520
- {
521
- scope: "Secret",
522
- condition:
523
- allowedSecretNames.length > 0
524
- ? `object.metadata.name in [${allowedSecretNames.join(", ")}]`
525
- : "false",
526
- message: "leitwerk image-pull Secrets must use a configured target name",
527
- },
528
- {
529
- scope: "Secret",
530
- condition: `object.metadata.labels['${C}'] == '${IPS}'`,
531
- message: "leitwerk image-pull Secrets must carry image-pull-secret component label",
532
- },
533
- {
534
- scope: "Secret",
535
- condition: "object.type == 'kubernetes.io/dockerconfigjson'",
536
- message: "leitwerk image-pull Secrets must use dockerconfigjson type",
537
- },
538
- {
539
- scope: "Secret",
540
- condition: "object.data.size() == 1 && '.dockerconfigjson' in object.data",
541
- message: "leitwerk image-pull Secrets may contain only .dockerconfigjson",
542
- },
543
- {
544
- scope: "Pod",
545
- condition: `object.spec.serviceAccountName == '${sa}'`,
546
- message: "leitwerk worker pods must use the configured worker ServiceAccount",
547
- },
548
- {
549
- scope: "Pod",
550
- condition: `'${WORKER_LABEL_INSTANCE_ID}' in object.metadata.labels`,
551
- message: "leitwerk worker pods must carry instance-id label",
552
- },
553
- {
554
- scope: "Pod",
555
- condition: `'${WORKER_LABEL_WORKER_ID}' in object.metadata.labels`,
556
- message: "leitwerk worker pods must carry worker-id label",
557
- },
558
- {
559
- scope: "Pod",
560
- condition: `'${WORKER_LABEL_SERVER_EPOCH}' in object.metadata.labels`,
561
- message: "leitwerk worker pods must carry server-epoch label",
562
- },
563
- ];
564
-
565
- const labels = args.labels;
566
- const meta = { name: args.name, ...(labels ? { labels: { ...labels } } : {}) };
567
- return {
568
- policy: {
569
- apiVersion: "admissionregistration.k8s.io/v1",
570
- kind: "ValidatingAdmissionPolicy",
571
- metadata: meta,
572
- spec: {
573
- matchConditions: [
574
- {
575
- name: "leitwerk-server-service-account",
576
- expression: `request.userInfo.username == 'system:serviceaccount:${args.serverNamespace}:${args.serverServiceAccountName}'`,
577
- },
578
- ],
579
- matchConstraints: {
580
- resourceRules: [
581
- {
582
- apiGroups: [""],
583
- apiVersions: ["v1"],
584
- operations: ["CREATE", "UPDATE", "DELETE"],
585
- resources: [
586
- "namespaces",
587
- "pods",
588
- "persistentvolumeclaims",
589
- "serviceaccounts",
590
- "configmaps",
591
- "secrets",
592
- ],
593
- },
594
- ],
595
- },
596
- validations: rules.map((r) => ({ expression: toCelExpression(r), message: r.message })),
597
- },
598
- },
599
- binding: {
600
- apiVersion: "admissionregistration.k8s.io/v1",
601
- kind: "ValidatingAdmissionPolicyBinding",
602
- metadata: meta,
603
- spec: { policyName: args.name, validationActions: ["Deny"] },
604
- },
605
- };
606
- }
607
-
608
732
  export function volumeRefFromPvc(args: {
609
733
  instanceId: string;
610
734
  pvcName: string;