@intentius/chant-lexicon-k8s 0.107.1 → 0.108.0

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 (57) hide show
  1. package/dist/config-schema.d.ts +115 -1
  2. package/dist/config-schema.d.ts.map +1 -1
  3. package/dist/config.d.ts +58 -3
  4. package/dist/config.d.ts.map +1 -1
  5. package/dist/crd/loader.d.ts +4 -0
  6. package/dist/crd/loader.d.ts.map +1 -1
  7. package/dist/crd/parser.d.ts +5 -0
  8. package/dist/crd/parser.d.ts.map +1 -1
  9. package/dist/crd/project-codegen.d.ts +33 -0
  10. package/dist/crd/project-codegen.d.ts.map +1 -0
  11. package/dist/include/entity.d.ts +73 -0
  12. package/dist/include/entity.d.ts.map +1 -0
  13. package/dist/include/resolve.d.ts +29 -0
  14. package/dist/include/resolve.d.ts.map +1 -0
  15. package/dist/index.d.ts +7 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/integrity.json +3 -3
  18. package/dist/kustomize/rendered-entity.d.ts +15 -1
  19. package/dist/kustomize/rendered-entity.d.ts.map +1 -1
  20. package/dist/kustomize/root.d.ts +11 -2
  21. package/dist/kustomize/root.d.ts.map +1 -1
  22. package/dist/lint/post-synth/crd-schema-helpers.d.ts +5 -1
  23. package/dist/lint/post-synth/crd-schema-helpers.d.ts.map +1 -1
  24. package/dist/manifest.json +1 -1
  25. package/dist/patch/json-patch.d.ts +51 -0
  26. package/dist/patch/json-patch.d.ts.map +1 -0
  27. package/dist/patch/rendered-patch.d.ts +90 -0
  28. package/dist/patch/rendered-patch.d.ts.map +1 -0
  29. package/dist/plugin.d.ts.map +1 -1
  30. package/dist/project-kinds.d.ts +46 -0
  31. package/dist/project-kinds.d.ts.map +1 -0
  32. package/dist/rules/crd-schema-helpers.ts +10 -2
  33. package/dist/serializer.d.ts.map +1 -1
  34. package/package.json +3 -3
  35. package/src/config-augmentation.test.ts +28 -0
  36. package/src/config-schema.ts +47 -1
  37. package/src/config.ts +44 -3
  38. package/src/crd/loader.ts +1 -1
  39. package/src/crd/parser.ts +9 -5
  40. package/src/crd/project-codegen.test.ts +239 -0
  41. package/src/crd/project-codegen.ts +327 -0
  42. package/src/include/entity.ts +119 -0
  43. package/src/include/include.test.ts +345 -0
  44. package/src/include/resolve.ts +204 -0
  45. package/src/index.ts +23 -0
  46. package/src/kustomize/rendered-entity.ts +43 -1
  47. package/src/kustomize/root.test.ts +39 -0
  48. package/src/kustomize/root.ts +16 -30
  49. package/src/lint/post-synth/crd-schema-helpers.ts +10 -2
  50. package/src/patch/json-patch.test.ts +91 -0
  51. package/src/patch/json-patch.ts +189 -0
  52. package/src/patch/rendered-patch.test.ts +143 -0
  53. package/src/patch/rendered-patch.ts +180 -0
  54. package/src/plugin.ts +16 -2
  55. package/src/project-kinds.ts +75 -0
  56. package/src/serializer.ts +10 -0
  57. package/src/testdata/include/bundle.yaml +45 -0
@@ -0,0 +1,51 @@
1
+ /**
2
+ * RFC 6902 JSON Patch and RFC 7386 JSON Merge Patch, for rendered documents.
3
+ *
4
+ * Core carries an RFC 6902 applier for cfn-lint codegen
5
+ * (`packages/core/src/codegen/json-patch.ts`), but it skips `test`, `move`
6
+ * and `copy`, and it works on JSON strings. A patch on a rendered manifest
7
+ * needs `test` most of all: it is how an author pins the value a chart
8
+ * upgrade might change underneath them. So this module is a small, complete
9
+ * applier over plain objects, with no imports, usable from the build path and
10
+ * from the helm lexicon's `HelmRender`.
11
+ *
12
+ * Both appliers return a new document and never mutate their input.
13
+ */
14
+ /** One RFC 6902 operation. `path` and `from` are RFC 6901 JSON Pointers. */
15
+ export type JsonPatchOperation = {
16
+ op: "add";
17
+ path: string;
18
+ value: unknown;
19
+ } | {
20
+ op: "remove";
21
+ path: string;
22
+ } | {
23
+ op: "replace";
24
+ path: string;
25
+ value: unknown;
26
+ } | {
27
+ op: "move";
28
+ from: string;
29
+ path: string;
30
+ } | {
31
+ op: "copy";
32
+ from: string;
33
+ path: string;
34
+ } | {
35
+ op: "test";
36
+ path: string;
37
+ value: unknown;
38
+ };
39
+ /**
40
+ * Apply an RFC 6902 patch. Operations apply in order; the first one that
41
+ * fails (a missing path, a failed `test`) throws with its index, and the
42
+ * input is left untouched.
43
+ */
44
+ export declare function applyJsonPatch<T>(doc: T, operations: readonly JsonPatchOperation[]): T;
45
+ /**
46
+ * Apply an RFC 7386 merge patch: objects merge key by key, a `null` value
47
+ * deletes the key, and anything else (arrays included) replaces the target
48
+ * whole.
49
+ */
50
+ export declare function applyMergePatch<T>(doc: T, patch: unknown): T;
51
+ //# sourceMappingURL=json-patch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-patch.d.ts","sourceRoot":"","sources":["../../src/patch/json-patch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,4EAA4E;AAC5E,MAAM,MAAM,kBAAkB,GAC1B;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAC3C;IAAE,EAAE,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,EAAE,EAAE,SAAS,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAC/C;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC1C;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC1C;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC;AAuGjD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,UAAU,EAAE,SAAS,kBAAkB,EAAE,GAAG,CAAC,CA2CtF;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,CAS5D"}
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Patching rendered documents (#3566 item 3).
3
+ *
4
+ * A Helm render, a kustomize build root and a `k8sInclude` all hand chant
5
+ * finished manifests. When a chart or an upstream bundle does not expose a
6
+ * field, the alternatives were forking it or running an overlay outside
7
+ * chant. A `RenderedPatch` selects documents by kind and name (namespace and
8
+ * apiVersion optional) and changes them before they become entities, so the
9
+ * patched form is what the serializer emits, what ownership stamping and
10
+ * post-synth checks see, and what `lifecycle diff --live` compares against
11
+ * the cluster.
12
+ *
13
+ * One patch type for all three sources: `HelmRender({ patches })`,
14
+ * `k8sInclude({ patches })`, and `{ path, patches }` entries in
15
+ * `k8s.kustomize.roots`. Each source calls {@link applyRenderedPatches} on
16
+ * its parsed documents.
17
+ *
18
+ * A selector that matches nothing is an error. The common way to get there
19
+ * is a chart upgrade that renamed a resource, and a patch that silently
20
+ * stops applying is the failure this guards against. There is no opt-out:
21
+ * a patch whose target is legitimately absent (a chart value that turns a
22
+ * resource off) belongs behind the same condition in TypeScript that sets
23
+ * the value.
24
+ */
25
+ import type * as Generated from "../generated/index.d";
26
+ import { type JsonPatchOperation } from "./json-patch.js";
27
+ export type { JsonPatchOperation } from "./json-patch.js";
28
+ export { applyJsonPatch, applyMergePatch } from "./json-patch.js";
29
+ /** Which rendered documents a patch applies to. */
30
+ export interface RenderedSelector<K extends string = string> {
31
+ /** The document's `kind`, e.g. `"Deployment"`. */
32
+ kind: K;
33
+ /** The document's `metadata.name`. */
34
+ name: string;
35
+ /** The document's `metadata.namespace`. Omitted, documents in any namespace (or none) match. */
36
+ namespace?: string;
37
+ /** The document's `apiVersion`, for a kind name two API groups share. */
38
+ apiVersion?: string;
39
+ }
40
+ /**
41
+ * The constructor props of the lexicon's generated class named `K`, when
42
+ * there is one; otherwise an open record. Merge patches for known kinds are
43
+ * checked against it.
44
+ */
45
+ export type KindProps<K extends string> = K extends keyof typeof Generated ? (typeof Generated)[K] extends abstract new (props: infer P) => unknown ? P : Record<string, unknown> : Record<string, unknown>;
46
+ /**
47
+ * An RFC 7386 merge patch over `T`: object fields are optional at every
48
+ * depth and may be `null` (which deletes them); arrays replace the target
49
+ * whole, so their elements stay complete.
50
+ */
51
+ export type MergePatchOf<T> = T extends readonly unknown[] ? T : T extends object ? {
52
+ [P in keyof T]?: MergePatchOf<T[P]> | null;
53
+ } : T;
54
+ /** A patch applied as RFC 6902 operations. */
55
+ export interface RenderedJsonPatch<K extends string = string> extends RenderedSelector<K> {
56
+ jsonPatch: JsonPatchOperation[];
57
+ merge?: never;
58
+ }
59
+ /** A patch applied as an RFC 7386 merge. */
60
+ export interface RenderedMergePatch<K extends string = string> extends RenderedSelector<K> {
61
+ merge: MergePatchOf<KindProps<K>>;
62
+ jsonPatch?: never;
63
+ }
64
+ /** One patch on rendered output: a selector plus either `jsonPatch` or `merge`. */
65
+ export type RenderedPatch<K extends string = string> = RenderedJsonPatch<K> | RenderedMergePatch<K>;
66
+ /**
67
+ * Identity helper that infers `kind`, so a `merge` body is checked against
68
+ * that kind's props:
69
+ *
70
+ * ```ts
71
+ * renderedPatch({ kind: "Deployment", name: "web", merge: { spec: { replicas: 3 } } })
72
+ * ```
73
+ *
74
+ * A plain object literal works anywhere a `RenderedPatch` is taken; it just
75
+ * types `merge` as an open record.
76
+ */
77
+ export declare function renderedPatch<K extends string>(patch: RenderedPatch<K>): RenderedPatch;
78
+ /**
79
+ * Apply `patches` in order to `docs` and return the patched documents, in
80
+ * the same order. `source` names where the documents came from (a chart, a
81
+ * file, a URL, an overlay dir) and leads every error message.
82
+ *
83
+ * Each patch applies to every document its selector matches at that point,
84
+ * so a later patch sees an earlier one's changes. Throws when a selector
85
+ * matches nothing, when an operation fails (a `test` included), when a patch
86
+ * carries both or neither of `jsonPatch` and `merge`, and when a patched
87
+ * document no longer has a string `apiVersion` and `kind`.
88
+ */
89
+ export declare function applyRenderedPatches(docs: ReadonlyArray<Record<string, unknown>>, patches: ReadonlyArray<RenderedPatch> | undefined, source: string): Array<Record<string, unknown>>;
90
+ //# sourceMappingURL=rendered-patch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rendered-patch.d.ts","sourceRoot":"","sources":["../../src/patch/rendered-patch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAKH,OAAO,KAAK,KAAK,SAAS,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAmC,KAAK,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAExF,YAAY,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/D,mDAAmD;AACnD,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;IACzD,kDAAkD;IAClD,IAAI,EAAE,CAAC,CAAC;IACR,sCAAsC;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,gGAAgG;IAChG,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,yEAAyE;IACzE,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,MAAM,OAAO,SAAS,GACtE,CAAC,OAAO,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,QAAQ,MAAM,KAAK,EAAE,MAAM,CAAC,KAAK,OAAO,GACpE,CAAC,GACD,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACzB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE5B;;;;GAIG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,SAAS,SAAS,OAAO,EAAE,GACtD,CAAC,GACD,CAAC,SAAS,MAAM,GACd;KAAG,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI;CAAE,GAC9C,CAAC,CAAC;AAER,8CAA8C;AAC9C,MAAM,WAAW,iBAAiB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,gBAAgB,CAAC,CAAC,CAAC;IACvF,SAAS,EAAE,kBAAkB,EAAE,CAAC;IAChC,KAAK,CAAC,EAAE,KAAK,CAAC;CACf;AAED,4CAA4C;AAC5C,MAAM,WAAW,kBAAkB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,gBAAgB,CAAC,CAAC,CAAC;IACxF,KAAK,EAAE,YAAY,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IAClC,SAAS,CAAC,EAAE,KAAK,CAAC;CACnB;AAED,mFAAmF;AACnF,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI,iBAAiB,CAAC,CAAC,CAAC,GAAG,kBAAkB,CAAC,CAAC,CAAC,CAAC;AAEpG;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,aAAa,CAEtF;AAuBD;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EAC5C,OAAO,EAAE,aAAa,CAAC,aAAa,CAAC,GAAG,SAAS,EACjD,MAAM,EAAE,MAAM,GACb,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CA6ChC"}
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,aAAa,EAAqC,MAAM,0BAA0B,CAAC;AA8BjG,eAAO,MAAM,SAAS,EAAE,aAkuBvB,CAAC"}
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,aAAa,EAAqC,MAAM,0BAA0B,CAAC;AAgCjG,eAAO,MAAM,SAAS,EAAE,aA8uBvB,CAAC"}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Kinds a project generated for itself with `chant generate` from `k8s.crds`
3
+ * (./crd/project-codegen.ts), as opposed to the kinds the lexicon ships in
4
+ * `generated/lexicon-k8s.json`.
5
+ *
6
+ * The serializer's GVK lookup and the CRD spec checks (WK8501/WK8502) consult
7
+ * this registry before the packaged JSON. Entries arrive two ways:
8
+ *
9
+ * - The generated module registers its kinds (type and GVK) when it is
10
+ * imported, so a project's own tests that construct and serialize a
11
+ * resource need nothing else.
12
+ * - A build registers every kind, spec schema included, from the committed
13
+ * `kinds.json` beside the module before it serializes (the plugin's
14
+ * `projectCodegen().load`). That covers a build whose discovery imported the
15
+ * module somewhere else, such as the sandboxed child process.
16
+ *
17
+ * The store hangs off `globalThis` under a registered symbol, so two copies
18
+ * of this module (the plugin the CLI loaded and the one a project's import
19
+ * resolved) share it. No Node imports: the post-synth barrel reaches this
20
+ * through ./lint/post-synth/crd-schema-helpers.ts and must stay edge-safe.
21
+ */
22
+ import type { CrdFieldSchema } from "./spec/parse.js";
23
+ /** One project-generated kind. */
24
+ export interface ProjectKind {
25
+ /** The entity type the generated class constructs, e.g. `K8s::Example::Widget`. */
26
+ resourceType: string;
27
+ /** e.g. `example.com/v1`. */
28
+ apiVersion: string;
29
+ /** e.g. `Widget`. */
30
+ kind: string;
31
+ /** The CRD's `spec` field schema, for the spec checks. */
32
+ specSchema?: CrdFieldSchema;
33
+ }
34
+ /**
35
+ * Register project-generated kinds. A later registration of the same type
36
+ * replaces the earlier one, keeping its spec schema when the new entry has
37
+ * none (the module's import-time registration carries no schema).
38
+ */
39
+ export declare function registerProjectKinds(kinds: readonly ProjectKind[]): void;
40
+ /** The project kind an entity type constructs, if a project registered one. */
41
+ export declare function projectKindForType(resourceType: string): ProjectKind | undefined;
42
+ /** The spec schema a project registered for `apiVersion`/`kind`, if any. */
43
+ export declare function projectSpecSchema(apiVersion: string, kind: string): CrdFieldSchema | undefined;
44
+ /** Test seam: forget every registered kind. */
45
+ export declare function clearProjectKinds(): void;
46
+ //# sourceMappingURL=project-kinds.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"project-kinds.d.ts","sourceRoot":"","sources":["../src/project-kinds.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD,kCAAkC;AAClC,MAAM,WAAW,WAAW;IAC1B,mFAAmF;IACnF,YAAY,EAAE,MAAM,CAAC;IACrB,6BAA6B;IAC7B,UAAU,EAAE,MAAM,CAAC;IACnB,qBAAqB;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,0DAA0D;IAC1D,UAAU,CAAC,EAAE,cAAc,CAAC;CAC7B;AAUD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,GAAG,IAAI,CAOxE;AAED,+EAA+E;AAC/E,wBAAgB,kBAAkB,CAAC,YAAY,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS,CAEhF;AAED,4EAA4E;AAC5E,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS,CAK9F;AAED,+CAA+C;AAC/C,wBAAgB,iBAAiB,IAAI,IAAI,CAExC"}
@@ -15,6 +15,7 @@ import { createRequire } from "module";
15
15
  import type { PostSynthContext } from "@intentius/chant/lint/post-synth";
16
16
  import type { CrdFieldSchema } from "../../spec/parse";
17
17
  import { docsToManifests, type K8sManifest } from "./k8s-helpers";
18
+ import { projectSpecSchema } from "../../project-kinds";
18
19
 
19
20
  export type { CrdFieldSchema };
20
21
 
@@ -61,10 +62,17 @@ export function setCrdSchemaRegistry(registry: Map<string, CrdFieldSchema> | nul
61
62
  cachedRegistry = registry;
62
63
  }
63
64
 
64
- /** The schema for a manifest's `apiVersion`/`kind`, if the lexicon ships one. */
65
+ /**
66
+ * The schema for a manifest's `apiVersion`/`kind`: one the project generated
67
+ * from its own CRDs (`chant generate`, ../../project-kinds.ts) first, then one
68
+ * the lexicon ships.
69
+ */
65
70
  export function specSchemaFor(manifest: K8sManifest): CrdFieldSchema | undefined {
66
71
  if (typeof manifest.apiVersion !== "string" || typeof manifest.kind !== "string") return undefined;
67
- return getCrdSchemaRegistry().get(registryKey(manifest.apiVersion, manifest.kind));
72
+ return (
73
+ projectSpecSchema(manifest.apiVersion, manifest.kind) ??
74
+ getCrdSchemaRegistry().get(registryKey(manifest.apiVersion, manifest.kind))
75
+ );
68
76
  }
69
77
 
70
78
  /** Every manifest in the build that has a shipped spec schema, with that schema. */
@@ -1 +1 @@
1
- {"version":3,"file":"serializer.d.ts","sourceRoot":"","sources":["../src/serializer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAE,UAAU,EAAsC,MAAM,6BAA6B,CAAC;AA6WlG,eAAO,MAAM,aAAa,EAAE,UAoO3B,CAAC"}
1
+ {"version":3,"file":"serializer.d.ts","sourceRoot":"","sources":["../src/serializer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAE,UAAU,EAAsC,MAAM,6BAA6B,CAAC;AAoXlG,eAAO,MAAM,aAAa,EAAE,UAuO3B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intentius/chant-lexicon-k8s",
3
- "version": "0.107.1",
3
+ "version": "0.108.0",
4
4
  "description": "Kubernetes lexicon for chant — declarative IaC in TypeScript",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://intentius.io/chant",
@@ -78,7 +78,7 @@
78
78
  "@types/js-yaml": "^4.0.9"
79
79
  },
80
80
  "optionalDependencies": {
81
- "@intentius/chant-k8s-client": "^0.107.1"
81
+ "@intentius/chant-k8s-client": "^0.108.0"
82
82
  },
83
83
  "devDependencies": {
84
84
  "@intentius/chant": "*",
@@ -86,7 +86,7 @@
86
86
  },
87
87
  "peerDependencies": {
88
88
  "zod": "^4.3.6",
89
- "@intentius/chant": "^0.107.1",
89
+ "@intentius/chant": "^0.108.0",
90
90
  "typescript": "^5.9.3"
91
91
  }
92
92
  }
@@ -69,4 +69,32 @@ describe("the k8s config namespace (#1455, #1344)", () => {
69
69
  };
70
70
  expect(k8sConfigSchema.safeParse(value).success).toBe(true);
71
71
  });
72
+
73
+ test("kustomize roots take a path string or { path, patches } (#3566)", () => {
74
+ const value: K8sChantConfig = {
75
+ kustomize: {
76
+ roots: [
77
+ "overlays/prod",
78
+ {
79
+ path: "overlays/edge",
80
+ patches: [
81
+ { kind: "Deployment", name: "web", merge: { spec: { replicas: 2 } } },
82
+ { kind: "Service", name: "web", namespace: "edge", jsonPatch: [{ op: "remove", path: "/spec/externalIPs" }] },
83
+ ],
84
+ },
85
+ ],
86
+ },
87
+ };
88
+ expect(k8sConfigSchema.safeParse(value).success).toBe(true);
89
+ });
90
+
91
+ test("a malformed patch in a kustomize root is rejected", () => {
92
+ const bad = (patch: unknown) =>
93
+ k8sConfigSchema.safeParse({ kustomize: { roots: [{ path: "o", patches: [patch] }] } }).success;
94
+ // Both bodies, neither body, an unknown op, a missing name.
95
+ expect(bad({ kind: "Deployment", name: "web", merge: {}, jsonPatch: [] })).toBe(false);
96
+ expect(bad({ kind: "Deployment", name: "web" })).toBe(false);
97
+ expect(bad({ kind: "Deployment", name: "web", jsonPatch: [{ op: "upsert", path: "/a" }] })).toBe(false);
98
+ expect(bad({ kind: "Deployment", merge: {} })).toBe(false);
99
+ });
72
100
  });
@@ -20,12 +20,57 @@ export const k8sClusterProfileSchema = z.strictObject({
20
20
  context: z.string(),
21
21
  });
22
22
 
23
+ const kinds = z.array(z.string().min(1)).optional();
24
+
25
+ /** A `k8s.crds` entry. Remote sources must carry their pin. */
26
+ export const k8sProjectCrdSourceSchema = z.discriminatedUnion("type", [
27
+ z.strictObject({ type: z.literal("file"), path: z.string().min(1), kinds }),
28
+ z.strictObject({
29
+ type: z.literal("url"),
30
+ url: z.string().url(),
31
+ sha256: z.string().regex(/^(sha256:)?[0-9a-f]{64}$/, "must be the hex sha256 of the URL's content"),
32
+ kinds,
33
+ }),
34
+ z.strictObject({
35
+ type: z.literal("helm"),
36
+ chart: z.string().min(1),
37
+ version: z.string().min(1),
38
+ digest: z.string().optional(),
39
+ chartSubdir: z.string().optional(),
40
+ kinds,
41
+ }),
42
+ ]);
43
+
44
+ const jsonPatchOperationSchema = z.discriminatedUnion("op", [
45
+ z.strictObject({ op: z.literal("add"), path: z.string(), value: z.unknown() }),
46
+ z.strictObject({ op: z.literal("remove"), path: z.string() }),
47
+ z.strictObject({ op: z.literal("replace"), path: z.string(), value: z.unknown() }),
48
+ z.strictObject({ op: z.literal("move"), from: z.string(), path: z.string() }),
49
+ z.strictObject({ op: z.literal("copy"), from: z.string(), path: z.string() }),
50
+ z.strictObject({ op: z.literal("test"), path: z.string(), value: z.unknown() }),
51
+ ]);
52
+
53
+ const renderedSelectorShape = {
54
+ kind: z.string().min(1),
55
+ name: z.string().min(1),
56
+ namespace: z.string().optional(),
57
+ apiVersion: z.string().optional(),
58
+ };
59
+
60
+ /** A `RenderedPatch` (#3566): a selector plus exactly one of `jsonPatch` and `merge`. */
61
+ export const renderedPatchSchema = z.union([
62
+ z.strictObject({ ...renderedSelectorShape, jsonPatch: z.array(jsonPatchOperationSchema), merge: z.never().optional() }),
63
+ z.strictObject({ ...renderedSelectorShape, merge: z.record(z.string(), z.unknown()), jsonPatch: z.never().optional() }),
64
+ ]);
65
+
23
66
  export const k8sConfigSchema = z.strictObject({
24
67
  profiles: z.record(z.string(), k8sClusterProfileSchema).optional(),
25
68
  execCredentialPlugins: z.array(z.string()).optional(),
26
69
  kustomize: z
27
70
  .strictObject({
28
- roots: z.array(z.string()).optional(),
71
+ roots: z
72
+ .array(z.union([z.string(), z.strictObject({ path: z.string(), patches: z.array(renderedPatchSchema).optional() })]))
73
+ .optional(),
29
74
  })
30
75
  .optional(),
31
76
  receipts: z
@@ -33,6 +78,7 @@ export const k8sConfigSchema = z.strictObject({
33
78
  namespace: z.string().optional(),
34
79
  })
35
80
  .optional(),
81
+ crds: z.array(k8sProjectCrdSourceSchema).optional(),
36
82
  });
37
83
 
38
84
  declare module "@intentius/chant/config" {
package/src/config.ts CHANGED
@@ -58,6 +58,7 @@
58
58
  */
59
59
 
60
60
  import type { ChantConfig } from "@intentius/chant/config";
61
+ import type { RenderedPatch } from "./patch/rendered-patch";
61
62
 
62
63
  /** A single environment's cluster binding. */
63
64
  export interface K8sClusterProfile {
@@ -101,15 +102,23 @@ export interface K8sChantConfig {
101
102
  * `chant.config.*`), NOT `sourceDir` — the overlay tree usually lives
102
103
  * beside the typed source, not inside it.
103
104
  *
105
+ * An entry may also be `{ path, patches }`: the patches apply to the
106
+ * rendered documents before they become entities (see `RenderedPatch`).
107
+ *
104
108
  * ```ts
105
109
  * k8s: {
106
- * kustomize: { roots: ["overlays/prod"] },
110
+ * kustomize: {
111
+ * roots: [
112
+ * "overlays/prod",
113
+ * { path: "overlays/edge", patches: [{ kind: "Deployment", name: "web", merge: { spec: { replicas: 2 } } }] },
114
+ * ],
115
+ * },
107
116
  * } satisfies K8sChantConfig
108
117
  * ```
109
118
  */
110
119
  kustomize?: {
111
- /** Kustomization directories to render into the build. */
112
- roots?: string[];
120
+ /** Kustomization directories to render into the build, optionally with patches. */
121
+ roots?: Array<string | { path: string; patches?: RenderedPatch[] }>;
113
122
  };
114
123
 
115
124
  /**
@@ -134,8 +143,40 @@ export interface K8sChantConfig {
134
143
  /** Namespace the receipt ConfigMaps live in. Defaults to `default`. */
135
144
  namespace?: string;
136
145
  };
146
+
147
+ /**
148
+ * CRDs this project generates typed classes from, with `chant generate`.
149
+ *
150
+ * Each source is a CRD file in the repo, a pinned URL, or a pinned Helm
151
+ * chart that ships CRDs; a multi-document file or bundle yields every CRD
152
+ * in it, or only the `kinds` listed. The classes are written to
153
+ * `src/generated/k8s/` (see `codegen.outDir`), committed, and imported like
154
+ * any module. `chant build` fails when a source changed since the classes
155
+ * were generated, and the generated kinds get the same spec checks
156
+ * (WK8501/WK8502) as the CRDs the lexicon ships.
157
+ *
158
+ * Paths are relative to the project root. A URL needs the `sha256` of its
159
+ * content, and a chart its `version`, so generation never depends on the
160
+ * day it ran.
161
+ *
162
+ * ```ts
163
+ * k8s: {
164
+ * crds: [
165
+ * { type: "file", path: "crds/widgets.yaml" },
166
+ * { type: "url", url: "https://example.com/v1.2.0/crds.yaml", sha256: "9f86d0…", kinds: ["Gadget"] },
167
+ * ],
168
+ * } satisfies K8sChantConfig
169
+ * ```
170
+ */
171
+ crds?: K8sProjectCrdSource[];
137
172
  }
138
173
 
174
+ /** One `k8s.crds` entry. `kinds` keeps only the CRDs of those kinds. */
175
+ export type K8sProjectCrdSource =
176
+ | { type: "file"; path: string; kinds?: string[] }
177
+ | { type: "url"; url: string; sha256: string; kinds?: string[] }
178
+ | { type: "helm"; chart: string; version: string; digest?: string; chartSubdir?: string; kinds?: string[] };
179
+
139
180
  declare module "@intentius/chant/config" {
140
181
  interface ChantConfig {
141
182
  k8s?: K8sChantConfig;
package/src/crd/loader.ts CHANGED
@@ -40,7 +40,7 @@ export async function loadMultipleCRDs(sources: CRDSource[]): Promise<K8sParseRe
40
40
  /**
41
41
  * Fetch raw CRD YAML content from a source.
42
42
  */
43
- async function fetchCRDContent(source: CRDSource): Promise<string> {
43
+ export async function fetchCRDContent(source: CRDSource): Promise<string> {
44
44
  switch (source.type) {
45
45
  case "file":
46
46
  return loadFromFile(source);
package/src/crd/parser.ts CHANGED
@@ -46,6 +46,14 @@ export function parseCRD(content: string): K8sParseResult[] {
46
46
  return results;
47
47
  }
48
48
 
49
+ /**
50
+ * The version a CRD's types are generated from: the served storage version,
51
+ * else the first served one.
52
+ */
53
+ export function crdTargetVersion(spec: CRDSpec): CRDSpec["versions"][number] | undefined {
54
+ return spec.versions.find((v) => v.storage && v.served) ?? spec.versions.find((v) => v.served);
55
+ }
56
+
49
57
  /**
50
58
  * Parse a CRD spec into K8sParseResult entries.
51
59
  * Extracts one result per served version with storage version preferred.
@@ -54,11 +62,7 @@ export function parseCRDSpec(spec: CRDSpec): K8sParseResult[] {
54
62
  const results: K8sParseResult[] = [];
55
63
  const groupNs = namespaceSegmentForGroup(spec.group);
56
64
 
57
- // Find the storage version (the canonical version)
58
- const storageVersion = spec.versions.find((v) => v.storage && v.served);
59
- // Fall back to any served version
60
- const targetVersion = storageVersion ?? spec.versions.find((v) => v.served);
61
-
65
+ const targetVersion = crdTargetVersion(spec);
62
66
  if (!targetVersion) return results;
63
67
 
64
68
  const typeName = `K8s::${groupNs}::${spec.names.kind}`;