@intentius/chant-lexicon-k8s 0.124.96 → 0.124.98

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 (228) hide show
  1. package/dist/application-paths.d.ts +4 -0
  2. package/dist/application-paths.d.ts.map +1 -0
  3. package/dist/application-paths.js +95 -0
  4. package/dist/application-set.d.ts +46 -0
  5. package/dist/application-set.d.ts.map +1 -0
  6. package/dist/application-set.js +303 -0
  7. package/dist/cluster-secret.d.ts +36 -0
  8. package/dist/cluster-secret.d.ts.map +1 -0
  9. package/dist/cluster-secret.js +55 -0
  10. package/dist/components/kustomize-apply.d.ts +6 -0
  11. package/dist/components/kustomize-apply.d.ts.map +1 -1
  12. package/dist/components/kustomize-apply.js +1 -1
  13. package/dist/config-schema.d.ts +8 -0
  14. package/dist/config-schema.d.ts.map +1 -1
  15. package/dist/config-schema.js +6 -2
  16. package/dist/config.d.ts +26 -3
  17. package/dist/config.d.ts.map +1 -1
  18. package/dist/helm-references.d.ts +113 -0
  19. package/dist/helm-references.d.ts.map +1 -0
  20. package/dist/helm-references.js +389 -0
  21. package/dist/import/generator.d.ts +11 -2
  22. package/dist/import/generator.d.ts.map +1 -1
  23. package/dist/import/generator.js +37 -55
  24. package/dist/import/parser.d.ts.map +1 -1
  25. package/dist/import/parser.js +34 -5
  26. package/dist/import/typed-classes.d.ts +57 -0
  27. package/dist/import/typed-classes.d.ts.map +1 -0
  28. package/dist/import/typed-classes.js +126 -0
  29. package/dist/integrity.json +41 -41
  30. package/dist/kustomize/render.d.ts +41 -2
  31. package/dist/kustomize/render.d.ts.map +1 -1
  32. package/dist/kustomize/render.js +114 -7
  33. package/dist/kustomize/root.d.ts +3 -3
  34. package/dist/kustomize/root.d.ts.map +1 -1
  35. package/dist/kustomize/root.js +6 -1
  36. package/dist/lint/post-synth/argo-helpers.d.ts +3 -0
  37. package/dist/lint/post-synth/argo-helpers.d.ts.map +1 -1
  38. package/dist/lint/post-synth/argo-helpers.js +8 -2
  39. package/dist/lint/post-synth/argo002.d.ts.map +1 -1
  40. package/dist/lint/post-synth/argo002.js +2 -1
  41. package/dist/lint/post-synth/argo003.d.ts.map +1 -1
  42. package/dist/lint/post-synth/argo003.js +2 -1
  43. package/dist/lint/post-synth/argo005.d.ts.map +1 -1
  44. package/dist/lint/post-synth/argo005.js +36 -9
  45. package/dist/lint/post-synth/crd-schema-helpers.d.ts +1 -1
  46. package/dist/lint/post-synth/crd-schema-helpers.d.ts.map +1 -1
  47. package/dist/lint/post-synth/crd-schema-helpers.js +3 -3
  48. package/dist/lint/post-synth/flux002.d.ts.map +1 -1
  49. package/dist/lint/post-synth/flux002.js +2 -1
  50. package/dist/lint/post-synth/flux003.d.ts.map +1 -1
  51. package/dist/lint/post-synth/flux003.js +2 -1
  52. package/dist/lint/post-synth/k8s-helpers.d.ts +32 -0
  53. package/dist/lint/post-synth/k8s-helpers.d.ts.map +1 -1
  54. package/dist/lint/post-synth/k8s-helpers.js +57 -10
  55. package/dist/lint/post-synth/wk8005.d.ts +9 -0
  56. package/dist/lint/post-synth/wk8005.d.ts.map +1 -1
  57. package/dist/lint/post-synth/wk8005.js +53 -3
  58. package/dist/lint/post-synth/wk8006.d.ts.map +1 -1
  59. package/dist/lint/post-synth/wk8006.js +4 -2
  60. package/dist/lint/post-synth/wk8041.d.ts.map +1 -1
  61. package/dist/lint/post-synth/wk8041.js +3 -2
  62. package/dist/lint/post-synth/wk8042.js +2 -2
  63. package/dist/lint/post-synth/wk8101.js +2 -2
  64. package/dist/lint/post-synth/wk8102.d.ts.map +1 -1
  65. package/dist/lint/post-synth/wk8102.js +7 -3
  66. package/dist/lint/post-synth/wk8103.d.ts.map +1 -1
  67. package/dist/lint/post-synth/wk8103.js +3 -2
  68. package/dist/lint/post-synth/wk8104.d.ts.map +1 -1
  69. package/dist/lint/post-synth/wk8104.js +3 -2
  70. package/dist/lint/post-synth/wk8105.d.ts.map +1 -1
  71. package/dist/lint/post-synth/wk8105.js +3 -2
  72. package/dist/lint/post-synth/wk8201.d.ts.map +1 -1
  73. package/dist/lint/post-synth/wk8201.js +3 -2
  74. package/dist/lint/post-synth/wk8202.d.ts.map +1 -1
  75. package/dist/lint/post-synth/wk8202.js +3 -2
  76. package/dist/lint/post-synth/wk8203.d.ts.map +1 -1
  77. package/dist/lint/post-synth/wk8203.js +3 -2
  78. package/dist/lint/post-synth/wk8204.d.ts.map +1 -1
  79. package/dist/lint/post-synth/wk8204.js +5 -2
  80. package/dist/lint/post-synth/wk8205.d.ts.map +1 -1
  81. package/dist/lint/post-synth/wk8205.js +3 -2
  82. package/dist/lint/post-synth/wk8207.js +2 -2
  83. package/dist/lint/post-synth/wk8208.js +2 -2
  84. package/dist/lint/post-synth/wk8209.js +2 -2
  85. package/dist/lint/post-synth/wk8301.d.ts.map +1 -1
  86. package/dist/lint/post-synth/wk8301.js +3 -2
  87. package/dist/lint/post-synth/wk8302.js +2 -2
  88. package/dist/lint/post-synth/wk8303.js +2 -2
  89. package/dist/lint/post-synth/wk8304.js +2 -2
  90. package/dist/lint/post-synth/wk8305.js +2 -2
  91. package/dist/lint/post-synth/wk8306.d.ts.map +1 -1
  92. package/dist/lint/post-synth/wk8306.js +3 -2
  93. package/dist/lint/post-synth/wk8401.js +2 -2
  94. package/dist/lint/post-synth/wk8402.js +2 -2
  95. package/dist/lint/post-synth/wk8403.js +2 -2
  96. package/dist/lint/post-synth/wk8404.d.ts.map +1 -1
  97. package/dist/lint/post-synth/wk8404.js +2 -2
  98. package/dist/lint/post-synth/wk8405.js +2 -2
  99. package/dist/lint/post-synth/wk8406.d.ts.map +1 -1
  100. package/dist/lint/post-synth/wk8406.js +3 -2
  101. package/dist/lint/post-synth/wk8407.js +2 -2
  102. package/dist/lint/post-synth/wk8503.js +2 -2
  103. package/dist/manifest.json +1 -1
  104. package/dist/reference-catalog.d.ts +3 -1
  105. package/dist/reference-catalog.d.ts.map +1 -1
  106. package/dist/reference-catalog.js +283 -15
  107. package/dist/rules/argo-helpers.ts +9 -2
  108. package/dist/rules/argo002.ts +2 -1
  109. package/dist/rules/argo003.ts +2 -1
  110. package/dist/rules/argo005.ts +36 -10
  111. package/dist/rules/crd-schema-helpers.ts +3 -3
  112. package/dist/rules/flux002.ts +2 -1
  113. package/dist/rules/flux003.ts +2 -1
  114. package/dist/rules/k8s-helpers.ts +56 -11
  115. package/dist/rules/wk8005.ts +56 -3
  116. package/dist/rules/wk8006.ts +4 -2
  117. package/dist/rules/wk8041.ts +3 -2
  118. package/dist/rules/wk8042.ts +2 -2
  119. package/dist/rules/wk8101.ts +2 -2
  120. package/dist/rules/wk8102.ts +7 -3
  121. package/dist/rules/wk8103.ts +3 -2
  122. package/dist/rules/wk8104.ts +3 -2
  123. package/dist/rules/wk8105.ts +3 -2
  124. package/dist/rules/wk8201.ts +3 -2
  125. package/dist/rules/wk8202.ts +3 -2
  126. package/dist/rules/wk8203.ts +3 -2
  127. package/dist/rules/wk8204.ts +5 -2
  128. package/dist/rules/wk8205.ts +3 -2
  129. package/dist/rules/wk8207.ts +2 -2
  130. package/dist/rules/wk8208.ts +2 -2
  131. package/dist/rules/wk8209.ts +2 -2
  132. package/dist/rules/wk8301.ts +3 -2
  133. package/dist/rules/wk8302.ts +2 -2
  134. package/dist/rules/wk8303.ts +2 -2
  135. package/dist/rules/wk8304.ts +2 -2
  136. package/dist/rules/wk8305.ts +2 -2
  137. package/dist/rules/wk8306.ts +3 -2
  138. package/dist/rules/wk8401.ts +2 -2
  139. package/dist/rules/wk8402.ts +2 -2
  140. package/dist/rules/wk8403.ts +2 -2
  141. package/dist/rules/wk8404.ts +2 -1
  142. package/dist/rules/wk8405.ts +2 -2
  143. package/dist/rules/wk8406.ts +3 -2
  144. package/dist/rules/wk8407.ts +2 -2
  145. package/dist/rules/wk8503.ts +2 -2
  146. package/dist/workspace-member.d.ts +15 -3
  147. package/dist/workspace-member.d.ts.map +1 -1
  148. package/dist/workspace-member.js +151 -21
  149. package/package.json +5 -5
  150. package/src/application-paths.ts +96 -0
  151. package/src/application-set.test.ts +130 -0
  152. package/src/application-set.ts +336 -0
  153. package/src/cluster-secret.test.ts +117 -0
  154. package/src/cluster-secret.ts +76 -0
  155. package/src/components/kustomize-apply.ts +7 -1
  156. package/src/config-schema.ts +6 -2
  157. package/src/config.ts +24 -4
  158. package/src/helm-references.test.ts +149 -0
  159. package/src/helm-references.ts +425 -0
  160. package/src/import/generator.test.ts +57 -0
  161. package/src/import/generator.ts +35 -61
  162. package/src/import/parser.test.ts +59 -0
  163. package/src/import/parser.ts +38 -5
  164. package/src/import/roundtrip-build.test.ts +91 -0
  165. package/src/import/typed-classes.ts +168 -0
  166. package/src/kustomize/helm.test.ts +183 -0
  167. package/src/kustomize/render.ts +142 -6
  168. package/src/kustomize/root.ts +8 -3
  169. package/src/lint/post-synth/argo-helpers.ts +9 -2
  170. package/src/lint/post-synth/argo002.ts +2 -1
  171. package/src/lint/post-synth/argo003.ts +2 -1
  172. package/src/lint/post-synth/argo005.ts +36 -10
  173. package/src/lint/post-synth/audit-identity.test.ts +65 -0
  174. package/src/lint/post-synth/crd-schema-helpers.ts +3 -3
  175. package/src/lint/post-synth/flux002.ts +2 -1
  176. package/src/lint/post-synth/flux003.ts +2 -1
  177. package/src/lint/post-synth/k8s-helpers.ts +56 -11
  178. package/src/lint/post-synth/post-synth.test.ts +144 -0
  179. package/src/lint/post-synth/wk8005.ts +56 -3
  180. package/src/lint/post-synth/wk8006.ts +4 -2
  181. package/src/lint/post-synth/wk8041.ts +3 -2
  182. package/src/lint/post-synth/wk8042.ts +2 -2
  183. package/src/lint/post-synth/wk8101.ts +2 -2
  184. package/src/lint/post-synth/wk8102.ts +7 -3
  185. package/src/lint/post-synth/wk8103.ts +3 -2
  186. package/src/lint/post-synth/wk8104.ts +3 -2
  187. package/src/lint/post-synth/wk8105.ts +3 -2
  188. package/src/lint/post-synth/wk8201.ts +3 -2
  189. package/src/lint/post-synth/wk8202.ts +3 -2
  190. package/src/lint/post-synth/wk8203.ts +3 -2
  191. package/src/lint/post-synth/wk8204.ts +5 -2
  192. package/src/lint/post-synth/wk8205.ts +3 -2
  193. package/src/lint/post-synth/wk8207.ts +2 -2
  194. package/src/lint/post-synth/wk8208.ts +2 -2
  195. package/src/lint/post-synth/wk8209.ts +2 -2
  196. package/src/lint/post-synth/wk8301.ts +3 -2
  197. package/src/lint/post-synth/wk8302.ts +2 -2
  198. package/src/lint/post-synth/wk8303.ts +2 -2
  199. package/src/lint/post-synth/wk8304.ts +2 -2
  200. package/src/lint/post-synth/wk8305.ts +2 -2
  201. package/src/lint/post-synth/wk8306.ts +3 -2
  202. package/src/lint/post-synth/wk8401.ts +2 -2
  203. package/src/lint/post-synth/wk8402.ts +2 -2
  204. package/src/lint/post-synth/wk8403.ts +2 -2
  205. package/src/lint/post-synth/wk8404.ts +2 -1
  206. package/src/lint/post-synth/wk8405.ts +2 -2
  207. package/src/lint/post-synth/wk8406.ts +3 -2
  208. package/src/lint/post-synth/wk8407.ts +2 -2
  209. package/src/lint/post-synth/wk8503.ts +2 -2
  210. package/src/reference-catalog.test.ts +78 -0
  211. package/src/reference-catalog.ts +297 -16
  212. package/src/testdata/helm-chart-missing/kustomization.yaml +8 -0
  213. package/src/testdata/helm-chart-root/charts/webchart/Chart.yaml +3 -0
  214. package/src/testdata/helm-chart-root/charts/webchart/templates/configmap.yaml +6 -0
  215. package/src/testdata/helm-chart-root/charts/webchart/values.yaml +1 -0
  216. package/src/testdata/helm-chart-root/kustomization.yaml +9 -0
  217. package/src/testdata/helm-chart-root/values.yaml +1 -0
  218. package/src/testdata/workspace-members/argo-clusters/clusters.yaml +34 -0
  219. package/src/testdata/workspace-members/argo-set/all.yaml +43 -0
  220. package/src/testdata/workspace-members/plain/deploy.yaml +27 -0
  221. package/src/testdata/workspace-members/plain/es.yaml +15 -0
  222. package/src/testdata/workspace-members/templated/block.yaml +6 -0
  223. package/src/testdata/workspace-members/templated/chart-cm.yaml +4 -0
  224. package/src/testdata/workspace-members/templated/cm.yaml +6 -0
  225. package/src/workspace-kinds.test.ts +9 -1
  226. package/src/workspace-member.test.ts +69 -3
  227. package/src/workspace-member.ts +165 -24
  228. package/workspace-kinds.json +45 -6
@@ -14,6 +14,7 @@ import {
14
14
  } from "@intentius/chant/import/embedded";
15
15
  import { SECRET_DECLARATION_ENTITY_TYPE } from "@intentius/chant/secret-provenance";
16
16
  import { isMaskedValueRef } from "../index-import";
17
+ import { importClassFor } from "./typed-classes";
17
18
 
18
19
  const IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
19
20
 
@@ -22,54 +23,6 @@ function propertyKey(key: string): string {
22
23
  return IDENT.test(key) ? key : JSON.stringify(key);
23
24
  }
24
25
 
25
- /**
26
- * Map K8s entity types to their constructor class names.
27
- */
28
- const TYPE_TO_CLASS: Record<string, string> = {
29
- // Core
30
- "K8s::Core::Pod": "Pod",
31
- "K8s::Core::Service": "Service",
32
- "K8s::Core::ConfigMap": "ConfigMap",
33
- "K8s::Core::Secret": "Secret",
34
- "K8s::Core::Namespace": "Namespace",
35
- "K8s::Core::ServiceAccount": "ServiceAccount",
36
- "K8s::Core::PersistentVolume": "PersistentVolume",
37
- "K8s::Core::PersistentVolumeClaim": "PersistentVolumeClaim",
38
- "K8s::Core::Endpoints": "Endpoints",
39
- "K8s::Core::LimitRange": "LimitRange",
40
- "K8s::Core::ResourceQuota": "ResourceQuota",
41
-
42
- // Apps
43
- "K8s::Apps::Deployment": "Deployment",
44
- "K8s::Apps::StatefulSet": "StatefulSet",
45
- "K8s::Apps::DaemonSet": "DaemonSet",
46
- "K8s::Apps::ReplicaSet": "ReplicaSet",
47
-
48
- // Batch
49
- "K8s::Batch::Job": "Job",
50
- "K8s::Batch::CronJob": "CronJob",
51
-
52
- // Networking
53
- "K8s::Networking::Ingress": "Ingress",
54
- "K8s::Networking::NetworkPolicy": "NetworkPolicy",
55
- "K8s::Networking::IngressClass": "IngressClass",
56
-
57
- // RBAC
58
- "K8s::Rbac::Role": "Role",
59
- "K8s::Rbac::ClusterRole": "ClusterRole",
60
- "K8s::Rbac::RoleBinding": "RoleBinding",
61
- "K8s::Rbac::ClusterRoleBinding": "ClusterRoleBinding",
62
-
63
- // Autoscaling
64
- "K8s::Autoscaling::HorizontalPodAutoscaler": "HorizontalPodAutoscaler",
65
-
66
- // Policy
67
- "K8s::Policy::PodDisruptionBudget": "PodDisruptionBudget",
68
-
69
- // Storage
70
- "K8s::Storage::StorageClass": "StorageClass",
71
- };
72
-
73
26
  /**
74
27
  * Properties that reference known property entity constructors.
75
28
  * Key: property name found in K8s specs → Value: constructor class name.
@@ -140,12 +93,14 @@ export class K8sGenerator implements TypeScriptGenerator {
140
93
  const lines: string[] = [];
141
94
  this.usesMaskedValue = false;
142
95
  let usesDeclareSecret = false;
96
+ let usesManifest = false;
143
97
 
144
98
  // Collect which constructors are needed
145
99
  const usedConstructors = new Set<string>();
146
100
  for (const resource of ir.resources) {
147
- const cls = this.resolveClass(resource.type);
148
- if (cls) usedConstructors.add(cls);
101
+ const cls = this.resolveClass(resource);
102
+ if (!cls) continue;
103
+ usedConstructors.add(cls);
149
104
 
150
105
  // Check properties for nested constructors
151
106
  this.collectNestedConstructors(resource.properties, usedConstructors, resource.type);
@@ -175,8 +130,15 @@ export class K8sGenerator implements TypeScriptGenerator {
175
130
  lines.push("");
176
131
  continue;
177
132
  }
178
- const cls = this.resolveClass(resource.type);
133
+ const cls = this.resolveClass(resource);
179
134
  if (!cls) {
135
+ const manifest = this.emitManifest(resource);
136
+ if (manifest) {
137
+ usesManifest = true;
138
+ lines.push(`export const ${resource.logicalId} = ${manifest};`);
139
+ lines.push("");
140
+ continue;
141
+ }
180
142
  lines.push(`// TODO: unsupported type ${resource.type} (${resource.logicalId})`);
181
143
  lines.push("");
182
144
  continue;
@@ -195,7 +157,7 @@ export class K8sGenerator implements TypeScriptGenerator {
195
157
  // container's env) is imported too.
196
158
  const code = lines.join("\n").replace(/"(?:[^"\\\n]|\\.)*"/g, '""');
197
159
  for (const m of code.matchAll(/\bnew ([A-Z][A-Za-z0-9_]*)\(/g)) usedConstructors.add(m[1]);
198
- const imports = [...usedConstructors].sort().join(", ");
160
+ const imports = [...(usesManifest ? ["k8sManifest"] : []), ...[...usedConstructors].sort()].join(", ");
199
161
  const head = [
200
162
  ...(usesDeclareSecret ? [`import { declareSecret } from "@intentius/chant";`] : []),
201
163
  ...(imports ? [`import { ${imports} } from "@intentius/chant-lexicon-k8s";`] : []),
@@ -224,17 +186,29 @@ export class K8sGenerator implements TypeScriptGenerator {
224
186
  }
225
187
 
226
188
  /**
227
- * Resolve a K8s type name to a constructor class name.
228
- * Falls back to extracting the last segment of the type.
189
+ * Resolve a resource to a constructor class name, or undefined when the
190
+ * lexicon exports none or the class builds another apiVersion than the
191
+ * resource has (the resource is written as a `k8sManifest`, see
192
+ * `importClassFor`).
229
193
  */
230
- private resolveClass(type: string): string | undefined {
231
- if (TYPE_TO_CLASS[type]) return TYPE_TO_CLASS[type];
232
-
233
- // Fallback: extract Kind from "K8s::Group::Kind"
234
- const parts = type.split("::");
235
- if (parts.length === 3 && parts[0] === "K8s") return parts[2];
194
+ private resolveClass(resource: ResourceIR): string | undefined {
195
+ return importClassFor(resource).cls;
196
+ }
236
197
 
237
- return undefined;
198
+ /**
199
+ * An untyped resource: the whole manifest handed to `k8sManifest`, which
200
+ * the serializer writes as given. Property constructors do not apply, the
201
+ * kind's schema is unknown. Undefined when the apiVersion or kind cannot
202
+ * be found.
203
+ */
204
+ private emitManifest(resource: ResourceIR): string | undefined {
205
+ const meta = resource.metadata ?? {};
206
+ const apiVersion = meta.apiVersion ?? resource.properties.apiVersion;
207
+ const kind = meta.kind ?? resource.type.split("::").pop();
208
+ if (typeof apiVersion !== "string" || typeof kind !== "string") return undefined;
209
+ const rest = { ...resource.properties };
210
+ delete rest.apiVersion;
211
+ return `k8sManifest(${this.emitLiteral({ apiVersion, kind, ...rest }, 0, resource.type)})`;
238
212
  }
239
213
 
240
214
  private collectNestedConstructors(
@@ -27,6 +27,21 @@ spec:
27
27
  expect(r.properties.spec).toEqual({ replicas: 2 });
28
28
  });
29
29
 
30
+ test("an object name with characters an identifier cannot hold still gives an identifier", () => {
31
+ const names: Array<[string, string, string]> = [
32
+ ["ClusterRole", "system:metrics-server", "clusterRoleSystemMetricsServer"],
33
+ ["Role", "cert-manager-cainjector:leaderelection", "roleCertManagerCainjectorLeaderelection"],
34
+ ["ClusterRoleBinding", "system:kube-scheduler", "clusterRoleBindingSystemKubeScheduler"],
35
+ ["ConfigMap", "a/b@c d", "configMapABCD"],
36
+ ];
37
+ for (const [kind, name, id] of names) {
38
+ const ir = parser.parse(`apiVersion: rbac.authorization.k8s.io/v1\nkind: ${kind}\nmetadata:\n name: "${name}"\n`);
39
+ expect(ir.resources[0].logicalId).toBe(id);
40
+ expect(ir.resources[0].logicalId).toMatch(/^[A-Za-z_$][A-Za-z0-9_$]*$/);
41
+ expect(ir.resources[0].metadata?.originalName).toBe(name);
42
+ }
43
+ });
44
+
30
45
  test("multi-doc YAML produces multiple resources", () => {
31
46
  const yaml = `
32
47
  apiVersion: apps/v1
@@ -171,4 +186,48 @@ metadata:
171
186
  const ir = parser.parse(yaml);
172
187
  expect(ir.metadata?.namespaces).toContain("production");
173
188
  });
189
+
190
+ test("one warning per kind the lexicon has no class for (#5070)", () => {
191
+ const doc = (name: string) =>
192
+ `apiVersion: external-secrets.io/v1\nkind: ExternalSecret\nmetadata:\n name: ${name}\n`;
193
+ const ir = parser.parse(`${doc("a")}---\n${doc("b")}---\napiVersion: v1\nkind: ConfigMap\nmetadata:\n name: c\n`);
194
+ expect(ir.warnings).toHaveLength(1);
195
+ expect(ir.warnings![0]).toContain("ExternalSecret");
196
+ expect(ir.warnings![0]).toContain("k8s.crds");
197
+ expect(ir.warnings![0]).toContain("chant generate");
198
+ });
199
+
200
+ test("an object at an apiVersion its class does not build is warned about by name (#5075)", () => {
201
+ const ir = parser.parse(
202
+ "apiVersion: rbac.authorization.k8s.io/v1beta1\nkind: ClusterRole\nmetadata:\n name: old\nrules: []\n",
203
+ );
204
+ expect(ir.warnings).toHaveLength(1);
205
+ expect(ir.warnings![0]).toContain("ClusterRole old");
206
+ expect(ir.warnings![0]).toContain("rbac.authorization.k8s.io/v1beta1");
207
+ expect(ir.warnings![0]).toContain("builds rbac.authorization.k8s.io/v1,");
208
+ });
209
+
210
+ test("an autoscaling/v1 HorizontalPodAutoscaler keeps its class and raises no warning (#5075)", () => {
211
+ const ir = parser.parse("apiVersion: autoscaling/v1\nkind: HorizontalPodAutoscaler\nmetadata:\n name: h\n");
212
+ expect(ir.warnings).toBeUndefined();
213
+ });
214
+
215
+ test("a kind with a class raises no warning", () => {
216
+ expect(parser.parse("apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: c\n").warnings).toBeUndefined();
217
+ });
218
+
219
+ test("a metadata.name that is not a string (a {{name}} placeholder) is a warning, not a crash", () => {
220
+ const ir = parser.parse("apiVersion: v1\nkind: Namespace\nmetadata:\n name: {{name}}\n");
221
+ expect(ir.resources).toHaveLength(1);
222
+ expect(ir.resources[0].logicalId).toBe("namespace");
223
+ expect(ir.warnings).toHaveLength(1);
224
+ expect(ir.warnings![0]).toContain("Namespace (v1): metadata.name is a mapping, not a string");
225
+ expect(ir.warnings![0]).toContain("{{name}}");
226
+ });
227
+
228
+ test("a numeric metadata.name is named in the warning", () => {
229
+ const ir = parser.parse("apiVersion: v1\nkind: Namespace\nmetadata:\n name: 123\n");
230
+ expect(ir.resources[0].logicalId).toBe("namespace");
231
+ expect(ir.warnings![0]).toContain("metadata.name is a number, not a string");
232
+ });
174
233
  });
@@ -11,6 +11,7 @@ import type { EmbeddedContentResolver } from "@intentius/chant/import/embedded";
11
11
  import { parseYAML, splitYAMLDocuments } from "@intentius/chant/yaml";
12
12
  import { namespaceSegmentForGroup } from "../group-namespace";
13
13
  import { delegateEmbedded } from "./embedded";
14
+ import { importClassFor, keptApiVersionWarning, untypedKindWarning } from "./typed-classes";
14
15
 
15
16
  // ── GVK to type name mapping ───────────────────────────────────────
16
17
 
@@ -131,13 +132,16 @@ function apiVersionToGroup(apiVersion: string): string {
131
132
  * Combines kind (lowercased first char) with the object name (PascalCased).
132
133
  *
133
134
  * E.g., kind="Deployment", name="my-app" → "deploymentMyApp"
135
+ * and kind="ClusterRole", name="system:node" gives "clusterRoleSystemNode".
134
136
  */
135
137
  function toLogicalId(kind: string, name: string | undefined): string {
136
138
  const prefix = kind.charAt(0).toLowerCase() + kind.slice(1);
137
- if (!name) return prefix;
139
+ if (typeof name !== "string" || !name) return prefix;
138
140
 
141
+ // Split on every character an identifier cannot hold (`:` in `system:node`,
142
+ // `/`, `@`, spaces), not only the `-_.` of DNS names.
139
143
  const pascalName = name
140
- .split(/[-_.]/)
144
+ .split(/[^A-Za-z0-9]+/)
141
145
  .map((seg) => seg.charAt(0).toUpperCase() + seg.slice(1))
142
146
  .join("");
143
147
  return `${prefix}${pascalName}`;
@@ -160,6 +164,7 @@ export class K8sParser implements TemplateParser {
160
164
  */
161
165
  parse(content: string, context?: ParseContext): TemplateIR {
162
166
  const resources: ResourceIR[] = [];
167
+ const parseWarnings: string[] = [];
163
168
  const namespaces = new Set<string>();
164
169
 
165
170
  // Split into documents on `---` / `...` markers (a trailing `# comment`
@@ -170,7 +175,7 @@ export class K8sParser implements TemplateParser {
170
175
  const doc = parseYAML(docStr);
171
176
  if (!doc || typeof doc !== "object") continue;
172
177
 
173
- const resource = this.parseDocument(doc as Record<string, unknown>, context?.embedded);
178
+ const resource = this.parseDocument(doc as Record<string, unknown>, context?.embedded, parseWarnings);
174
179
  if (resource) {
175
180
  resources.push(resource);
176
181
 
@@ -182,6 +187,23 @@ export class K8sParser implements TemplateParser {
182
187
  }
183
188
  }
184
189
 
190
+ // One warning per kind the lexicon has no class for (#5070), and one per
191
+ // object kept at an apiVersion its class does not build (#5075).
192
+ const warnings: string[] = [...parseWarnings];
193
+ const warned = new Set<string>();
194
+ for (const r of resources) {
195
+ const choice = importClassFor(r);
196
+ if (choice.reason === "other-apiVersion") {
197
+ warnings.push(
198
+ keptApiVersionWarning(String(r.metadata?.kind), r.metadata?.originalName as string | undefined, choice.apiVersion!, choice.builtApiVersion!),
199
+ );
200
+ continue;
201
+ }
202
+ if (choice.cls || warned.has(r.type)) continue;
203
+ warned.add(r.type);
204
+ warnings.push(untypedKindWarning(String(r.metadata?.apiVersion), String(r.metadata?.kind)));
205
+ }
206
+
185
207
  const metadata: Record<string, unknown> = {};
186
208
  if (namespaces.size > 0) {
187
209
  metadata.namespaces = [...namespaces];
@@ -191,17 +213,28 @@ export class K8sParser implements TemplateParser {
191
213
  resources,
192
214
  parameters: [],
193
215
  metadata: Object.keys(metadata).length > 0 ? metadata : undefined,
216
+ ...(warnings.length > 0 ? { warnings } : {}),
194
217
  };
195
218
  }
196
219
 
197
- private parseDocument(doc: Record<string, unknown>, embedded?: EmbeddedContentResolver): ResourceIR | null {
220
+ private parseDocument(doc: Record<string, unknown>, embedded?: EmbeddedContentResolver, warnings: string[] = []): ResourceIR | null {
198
221
  const apiVersion = doc.apiVersion as string | undefined;
199
222
  const kind = doc.kind as string | undefined;
200
223
 
201
224
  if (!apiVersion || !kind) return null;
202
225
 
203
226
  const metadata = (doc.metadata ?? {}) as Record<string, unknown>;
204
- const name = metadata.name as string | undefined;
227
+ // A template placeholder such as `name: {{name}}` is a YAML flow mapping,
228
+ // not a string. The object keeps its properties as written; the logical
229
+ // id comes from the kind alone.
230
+ const rawName = metadata.name;
231
+ if (rawName !== undefined && rawName !== null && typeof rawName !== "string") {
232
+ warnings.push(
233
+ `${kind} (${apiVersion}): metadata.name is ${Array.isArray(rawName) ? "a list" : typeof rawName === "object" ? "a mapping" : `a ${typeof rawName}`}, not a string ` +
234
+ "(a template placeholder such as {{name}}?). The object is imported with the name as written; render the template first for a usable name.",
235
+ );
236
+ }
237
+ const name = typeof rawName === "string" ? rawName : undefined;
205
238
  const type = resolveTypeName(apiVersion, kind);
206
239
  const logicalId = toLogicalId(kind, name);
207
240
 
@@ -44,6 +44,35 @@ describe("k8s import round trip", () => {
44
44
  expect(await roundTrip(yaml)).toEqual(loadAll(yaml));
45
45
  });
46
46
 
47
+ test("RBAC objects named with a colon import to valid identifiers and keep their names", async () => {
48
+ const yaml = [
49
+ "apiVersion: rbac.authorization.k8s.io/v1",
50
+ "kind: ClusterRole",
51
+ "metadata:",
52
+ " name: system:metrics-server",
53
+ "rules:",
54
+ "- apiGroups: ['']",
55
+ " resources: [nodes]",
56
+ " verbs: [get]",
57
+ "---",
58
+ "apiVersion: rbac.authorization.k8s.io/v1",
59
+ "kind: ClusterRoleBinding",
60
+ "metadata:",
61
+ " name: system:metrics-server",
62
+ "roleRef:",
63
+ " apiGroup: rbac.authorization.k8s.io",
64
+ " kind: ClusterRole",
65
+ " name: system:metrics-server",
66
+ "subjects:",
67
+ "- kind: ServiceAccount",
68
+ " name: metrics-server",
69
+ " namespace: kube-system",
70
+ ].join("\n");
71
+ // The serializer orders the documents; the names are what this checks.
72
+ const byKind = (docs: unknown[]) => [...(docs as { kind: string }[])].sort((a, b) => a.kind.localeCompare(b.kind));
73
+ expect(byKind(await roundTrip(yaml))).toEqual(byKind(loadAll(yaml) as unknown[]));
74
+ });
75
+
47
76
  test("an autoscaling/v1 HorizontalPodAutoscaler keeps its apiVersion", async () => {
48
77
  const yaml = [
49
78
  "apiVersion: autoscaling/v1",
@@ -61,6 +90,20 @@ describe("k8s import round trip", () => {
61
90
  expect(await roundTrip(yaml)).toEqual(loadAll(yaml));
62
91
  });
63
92
 
93
+ test("an rbac v1beta1 Role keeps its apiVersion (#5075)", async () => {
94
+ const yaml = [
95
+ "apiVersion: rbac.authorization.k8s.io/v1beta1",
96
+ "kind: Role",
97
+ "metadata:",
98
+ " name: reader",
99
+ "rules:",
100
+ "- apiGroups: ['']",
101
+ " resources: [pods]",
102
+ " verbs: [get]",
103
+ ].join("\n");
104
+ expect(await roundTrip(yaml)).toEqual(loadAll(yaml));
105
+ });
106
+
64
107
  test("an autoscaling/v2 HorizontalPodAutoscaler is generated without an apiVersion property", async () => {
65
108
  const yaml = [
66
109
  "apiVersion: autoscaling/v2",
@@ -130,4 +173,52 @@ describe("k8s import round trip", () => {
130
173
  removeDir(imported.dir);
131
174
  }
132
175
  });
176
+
177
+ test("a kind the lexicon has no class for is written untyped, builds, and comes back (#5070)", async () => {
178
+ const yaml = [
179
+ "apiVersion: external-secrets.io/v1",
180
+ "kind: ExternalSecret",
181
+ "metadata:",
182
+ " name: db",
183
+ " namespace: apps",
184
+ "spec:",
185
+ " refreshInterval: 1h",
186
+ " secretStoreRef:",
187
+ " kind: ClusterSecretStore",
188
+ " name: vault",
189
+ " data:",
190
+ " - secretKey: password",
191
+ " remoteRef:",
192
+ " key: db/password",
193
+ "---",
194
+ "apiVersion: networking.gke.io/v1beta1",
195
+ "kind: ManagedCertificate",
196
+ "metadata:",
197
+ " name: cert",
198
+ "spec:",
199
+ " domains:",
200
+ " - example.org",
201
+ "---",
202
+ "apiVersion: networking.gke.io/v1beta1",
203
+ "kind: ManagedCertificate",
204
+ "metadata:",
205
+ " name: cert-two",
206
+ "spec:",
207
+ " domains:",
208
+ " - example.com",
209
+ ].join("\n");
210
+ const imported = await importManifest(yaml);
211
+ try {
212
+ expect(imported.result.error).toBeUndefined();
213
+ const warned = imported.result.warnings.filter((w) => w.includes("k8s.crds"));
214
+ expect(warned.length).toBe(2);
215
+ expect(warned.join("\n")).toContain("chant generate");
216
+ const result = await build(imported.srcDir, [k8sSerializer]);
217
+ expect(result.errors).toEqual([]);
218
+ const docs = (loadAll(primary(result.outputs.get("k8s"))) as unknown[]).filter((d) => d);
219
+ expect(docs).toEqual(loadAll(yaml).filter((d) => d));
220
+ } finally {
221
+ removeDir(imported.dir);
222
+ }
223
+ });
133
224
  });
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Which `K8s::Group::Kind` types the k8s lexicon exports a class for, so
3
+ * `chant import` writes a class only when the module it imports from has it
4
+ * (#5070), and only when building that class gives back the apiVersion the
5
+ * file has (#5075).
6
+ */
7
+
8
+ import { createRequire } from "module";
9
+ import { moduleSourceUrl } from "@intentius/chant/module-source";
10
+ import type { ResourceIR } from "@intentius/chant/import/parser";
11
+
12
+ // `require` is not defined where this runs as an ES module; the serializer
13
+ // reads the same file the same way.
14
+ const require = createRequire(moduleSourceUrl(import.meta.url));
15
+
16
+ /**
17
+ * Map K8s entity types to their constructor class names.
18
+ */
19
+ export const TYPE_TO_CLASS: Record<string, string> = {
20
+ // Core
21
+ "K8s::Core::Pod": "Pod",
22
+ "K8s::Core::Service": "Service",
23
+ "K8s::Core::ConfigMap": "ConfigMap",
24
+ "K8s::Core::Secret": "Secret",
25
+ "K8s::Core::Namespace": "Namespace",
26
+ "K8s::Core::ServiceAccount": "ServiceAccount",
27
+ "K8s::Core::PersistentVolume": "PersistentVolume",
28
+ "K8s::Core::PersistentVolumeClaim": "PersistentVolumeClaim",
29
+ "K8s::Core::Endpoints": "Endpoints",
30
+ "K8s::Core::LimitRange": "LimitRange",
31
+ "K8s::Core::ResourceQuota": "ResourceQuota",
32
+
33
+ // Apps
34
+ "K8s::Apps::Deployment": "Deployment",
35
+ "K8s::Apps::StatefulSet": "StatefulSet",
36
+ "K8s::Apps::DaemonSet": "DaemonSet",
37
+ "K8s::Apps::ReplicaSet": "ReplicaSet",
38
+
39
+ // Batch
40
+ "K8s::Batch::Job": "Job",
41
+ "K8s::Batch::CronJob": "CronJob",
42
+
43
+ // Networking
44
+ "K8s::Networking::Ingress": "Ingress",
45
+ "K8s::Networking::NetworkPolicy": "NetworkPolicy",
46
+ "K8s::Networking::IngressClass": "IngressClass",
47
+
48
+ // RBAC
49
+ "K8s::Rbac::Role": "Role",
50
+ "K8s::Rbac::ClusterRole": "ClusterRole",
51
+ "K8s::Rbac::RoleBinding": "RoleBinding",
52
+ "K8s::Rbac::ClusterRoleBinding": "ClusterRoleBinding",
53
+
54
+ // Autoscaling
55
+ "K8s::Autoscaling::HorizontalPodAutoscaler": "HorizontalPodAutoscaler",
56
+
57
+ // Policy
58
+ "K8s::Policy::PodDisruptionBudget": "PodDisruptionBudget",
59
+
60
+ // Storage
61
+ "K8s::Storage::StorageClass": "StorageClass",
62
+ };
63
+
64
+
65
+ type LexiconEntry = { resourceType?: string; apiVersion?: string };
66
+
67
+ let cachedEntries: Record<string, LexiconEntry> | null | undefined;
68
+
69
+ /** The packaged `lexicon-k8s.json`, or null when this checkout has not generated it. */
70
+ function lexiconEntries(): Record<string, LexiconEntry> | null {
71
+ if (cachedEntries !== undefined) return cachedEntries;
72
+ try {
73
+ cachedEntries = require("../generated/lexicon-k8s.json") as Record<string, LexiconEntry>;
74
+ } catch {
75
+ cachedEntries = null;
76
+ }
77
+ return cachedEntries;
78
+ }
79
+
80
+ /**
81
+ * The class `@intentius/chant-lexicon-k8s` exports for an entity type, or
82
+ * undefined when it has none (a custom resource whose CRD is not in the
83
+ * lexicon). A class named like the kind counts only when it constructs this
84
+ * type: two groups can share a kind name.
85
+ */
86
+ export function exportedClassFor(type: string): string | undefined {
87
+ const known = TYPE_TO_CLASS[type];
88
+ if (known) return known;
89
+
90
+ const parts = type.split("::");
91
+ if (parts.length !== 3 || parts[0] !== "K8s") return undefined;
92
+ const entries = lexiconEntries();
93
+ return entries?.[parts[2]]?.resourceType === type ? parts[2] : undefined;
94
+ }
95
+
96
+ /**
97
+ * The warning for a kind with no class, once per kind. It names the way to
98
+ * get types: list the CRD under `k8s.crds` and run `chant generate`.
99
+ */
100
+ export function untypedKindWarning(apiVersion: string, kind: string): string {
101
+ return (
102
+ `${kind} (${apiVersion}) has no class in @intentius/chant-lexicon-k8s; written as an untyped k8sManifest. ` +
103
+ "To get a typed class, list its CRD under k8s.crds in chant.config.ts and run chant generate."
104
+ );
105
+ }
106
+
107
+ let cachedBuiltVersions: Map<string, string> | undefined;
108
+
109
+ /**
110
+ * The apiVersion the build writes for a class of this entity type, from the
111
+ * packaged lexicon (the same lookup the serializer makes), or undefined when
112
+ * this checkout has not generated it.
113
+ */
114
+ export function builtApiVersionFor(type: string): string | undefined {
115
+ if (!cachedBuiltVersions) {
116
+ cachedBuiltVersions = new Map();
117
+ for (const entry of Object.values(lexiconEntries() ?? {})) {
118
+ if (entry.resourceType && entry.apiVersion && !cachedBuiltVersions.has(entry.resourceType)) {
119
+ cachedBuiltVersions.set(entry.resourceType, entry.apiVersion);
120
+ }
121
+ }
122
+ }
123
+ return cachedBuiltVersions.get(type);
124
+ }
125
+
126
+ /**
127
+ * How `chant import` writes one parsed object: the one place that decides it.
128
+ * `cls` is set when the object is written as `new <cls>(...)`; otherwise it
129
+ * is an untyped `k8sManifest` and `reason` says why.
130
+ *
131
+ * A class builds with its own apiVersion, so an object whose file says
132
+ * another one (`rbac.authorization.k8s.io/v1beta1` against a class that
133
+ * writes `v1`) would come back from the build at a version nobody wrote, and
134
+ * the fields may not mean the same thing there. Such an object stays
135
+ * untyped at the version in the file, with `builtApiVersion` naming what the
136
+ * class would have written. A document that carries its apiVersion as a
137
+ * property (see `ALIASED_GVKS` in ./parser.ts) builds with it, so it keeps
138
+ * its class.
139
+ */
140
+ export interface ImportClass {
141
+ cls?: string;
142
+ reason?: "no-class" | "other-apiVersion";
143
+ apiVersion?: string;
144
+ builtApiVersion?: string;
145
+ }
146
+
147
+ export function importClassFor(resource: Pick<ResourceIR, "type" | "properties" | "metadata">): ImportClass {
148
+ const cls = exportedClassFor(resource.type);
149
+ const apiVersion = resource.metadata?.apiVersion;
150
+ if (!cls) return { reason: "no-class", ...(typeof apiVersion === "string" ? { apiVersion } : {}) };
151
+ if (typeof apiVersion !== "string") return { cls };
152
+
153
+ const written = resource.properties?.apiVersion ?? builtApiVersionFor(resource.type);
154
+ if (written === undefined || written === apiVersion) return { cls };
155
+ return { reason: "other-apiVersion", apiVersion, builtApiVersion: String(written) };
156
+ }
157
+
158
+ /**
159
+ * The warning for an object kept at its own apiVersion because the lexicon's
160
+ * class is for another one. Names the object, the version in the file and the
161
+ * version the class would have written.
162
+ */
163
+ export function keptApiVersionWarning(kind: string, name: string | undefined, apiVersion: string, builtApiVersion: string): string {
164
+ return (
165
+ `${kind}${name ? ` ${name}` : ""} (${apiVersion}): the class in @intentius/chant-lexicon-k8s builds ${builtApiVersion}, ` +
166
+ "so it is written as an untyped k8sManifest that keeps the apiVersion from the file."
167
+ );
168
+ }