@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
@@ -11,7 +11,7 @@ import { afterEach, describe, expect, test, vi } from "vitest";
11
11
  import { execFileSync } from "node:child_process";
12
12
  import { dirname, join } from "node:path";
13
13
  import { fileURLToPath } from "node:url";
14
- import { mkdtemp, rm, writeFile } from "node:fs/promises";
14
+ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
15
15
  import { tmpdir } from "node:os";
16
16
  import type { GraphIR } from "@intentius/chant/graph-ir";
17
17
  import type { LexiconPlugin } from "@intentius/chant/lexicon";
@@ -69,7 +69,7 @@ afterEach(async () => {
69
69
  });
70
70
 
71
71
  /** chant graph over the reader project the kind's graph block describes for a member at `dir`. */
72
- async function graphOf(kind: string, dir: string, plugin: LexiconPlugin): Promise<{ ir: GraphIR; err: string[]; exit: number }> {
72
+ async function graphOf(kind: string, dir: string, plugin: LexiconPlugin, detail?: number): Promise<{ ir: GraphIR; err: string[]; exit: number }> {
73
73
  const graph = readPackageKinds(packageDir).kinds.find((k) => k.name === kind)!.graph!;
74
74
  expect(graph.lexicon).toBe("k8s");
75
75
  const config = substituteGraphConfig(graph.config, { member: "m", dir, workspace: dirname(dir) });
@@ -83,7 +83,7 @@ async function graphOf(kind: string, dir: string, plugin: LexiconPlugin): Promis
83
83
  const cwd = process.cwd();
84
84
  process.chdir(project);
85
85
  try {
86
- const exit = await runGraph({ args: { command: "graph", path: ".", format: "ir" } as never, plugins: [plugin], serializers: [] });
86
+ const exit = await runGraph({ args: { command: "graph", path: ".", format: "ir", ...(detail === undefined ? {} : { detail }) } as never, plugins: [plugin], serializers: [] });
87
87
  return { ir: exit === 0 ? (JSON.parse(out.join("\n")) as GraphIR) : { nodes: [], edges: [], groups: {} }, err, exit };
88
88
  } finally {
89
89
  process.chdir(cwd);
@@ -127,12 +127,78 @@ describe("member kinds with a graph block (#3179)", () => {
127
127
  expect(ir.meta?.k8sMember).toEqual({ kind: "argo-app", objects: 2 });
128
128
  });
129
129
 
130
+ test("argo-app and manifests: a cluster Secret is a node cut down to its name and server, and no other Secret is read as one (#5101)", async () => {
131
+ for (const kind of ["argo-app", "manifests"]) {
132
+ const { ir, err, exit } = await graphOf(kind, join(members, "argo-clusters"), pluginWith(), 3);
133
+ expect(exit, err.join("\n")).toBe(0);
134
+ const attrs = (id: string) => ir.nodes.find((n) => n.id === id)?.attrs;
135
+ expect(attrs("m/secretClusterProd")).toMatchObject({ type: "Opaque", stringData: { name: "prod", server: "https://10.0.0.1" } });
136
+ // A Secret that holds its name and server in base64 `data` is read decoded.
137
+ expect(attrs("m/secretClusterStaging")).toMatchObject({ stringData: { name: "staging", server: "https://10.0.0.2" } });
138
+ const text = JSON.stringify(ir.nodes.filter((n) => n.id.startsWith("m/secretCluster")));
139
+ expect(text).not.toMatch(/not-a-real|bearerToken|config/);
140
+ // A Secret with no cluster label is not an Argo member's object, and a manifests member keeps it whole.
141
+ expect(ids(ir).includes("m/secretDbLogin")).toBe(kind === "manifests");
142
+ }
143
+ });
144
+
130
145
  test("flux-kustomization: the Flux Kustomization is a node, the kustomize one beside it is not", async () => {
131
146
  const { ir, err, exit } = await graphOf("flux-kustomization", join(members, "flux"), pluginWith());
132
147
  expect(exit, err.join("\n")).toBe(0);
133
148
  expect(ids(ir)).toEqual(["m/kustomizationApps"]);
134
149
  });
135
150
 
151
+ test("argo-app: an ApplicationSet and an AppProject are nodes beside the Application (#5096)", async () => {
152
+ const { ir, err, exit } = await graphOf("argo-app", join(members, "argo-set"), pluginWith());
153
+ expect(exit, err.join("\n")).toBe(0);
154
+ expect(ids(ir)).toEqual(["m/appProjectTeamA", "m/applicationGuestbook", "m/applicationSetServices"]);
155
+ expect(ir.meta?.k8sMember).toEqual({ kind: "argo-app", objects: 3 });
156
+ });
157
+
158
+ test("manifests: every document with an apiVersion and a kind is a node, and a custom resource carries its spec (#5096)", async () => {
159
+ const { ir, err, exit } = await graphOf("manifests", join(members, "plain"), pluginWith(), 3);
160
+ expect(exit, err.join("\n")).toBe(0);
161
+ expect(ids(ir)).toEqual(["m/deploymentWeb", "m/externalSecretWebCreds", "m/serviceWeb"]);
162
+ expect(ir.meta?.k8sMember).toEqual({ kind: "manifests", objects: 3 });
163
+ const es = ir.nodes.find((n) => n.id === "m/externalSecretWebCreds")!;
164
+ expect(JSON.stringify(es)).toContain("main-store");
165
+ expect(JSON.stringify(es)).toContain("web/password");
166
+ });
167
+
168
+ test("manifests: a file that does not parse is named in a warning and the other files still print (#5096)", async () => {
169
+ const { ir, err, exit } = await graphOf("manifests", join(members, "templated"), pluginWith());
170
+ expect(exit, err.join("\n")).toBe(0);
171
+ expect(ids(ir)).toEqual(["m/configMapSettings"]);
172
+ expect(err.join("\n")).toMatch(/chart-cm\.yaml holds template syntax/);
173
+ expect(err.join("\n")).toMatch(/block\.yaml is not valid YAML/);
174
+ });
175
+
176
+ test("manifests: the reader returns one warning per file (#5096)", async () => {
177
+ const { warnings, entities } = await readK8sMember({ member: { kind: "manifests", dir: join(members, "templated"), label: "t" } });
178
+ expect(warnings).toHaveLength(2);
179
+ expect(warnings.join("\n")).toContain("chart-cm.yaml");
180
+ expect(warnings.join("\n")).toContain("block.yaml");
181
+ expect([...entities.keys()]).toEqual(["t/configMapSettings"]);
182
+ });
183
+
184
+ test("manifests: a directory a kustomization lists in resources is left to the kustomize member (#5096)", async () => {
185
+ const root = await mkdtemp(join(tmpdir(), "chant-k8s-listed-"));
186
+ scratch.push(root);
187
+ await mkdir(join(root, "base"));
188
+ await mkdir(join(root, "other"));
189
+ await mkdir(join(root, "overlays", "prod"), { recursive: true });
190
+ const cm = "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: c\n";
191
+ await writeFile(join(root, "base", "cm.yaml"), cm);
192
+ await writeFile(join(root, "other", "cm.yaml"), cm);
193
+ await writeFile(join(root, "overlays", "prod", "kustomization.yaml"), "resources:\n - ../../base\n - https://example.com/x.yaml\n");
194
+ const listed = await readK8sMember({ member: { kind: "manifests", dir: join(root, "base"), label: "base", workspace: root } });
195
+ expect([...listed.entities.keys()]).toEqual([]);
196
+ expect(listed.report).toMatchObject({ kind: "manifests", coveredBy: "overlays/prod", objects: 0 });
197
+ const unlisted = await readK8sMember({ member: { kind: "manifests", dir: join(root, "other"), label: "other", workspace: root } });
198
+ expect([...unlisted.entities.keys()]).toEqual(["other/configMapC"]);
199
+ expect(unlisted.report.coveredBy).toBeUndefined();
200
+ });
201
+
136
202
  const haveBinary = (() => {
137
203
  for (const bin of ["kustomize", "kubectl"]) {
138
204
  try {
@@ -1,10 +1,11 @@
1
1
  /**
2
- * Reading a workspace member into the graph (#3179).
2
+ * Reading a workspace member into the graph (#3179, #5096).
3
3
  *
4
4
  * `chant workspace graph` writes a reader project for a member of kind
5
- * `kustomize`, `argo-app` or `flux-kustomization`, with `k8s.member` in its
6
- * config (see `workspace-kinds.json`). The k8s plugin's `buildRoots` hook calls
7
- * {@link readK8sMember}, and the objects it returns are the member's nodes.
5
+ * `kustomize`, `argo-app`, `flux-kustomization` or `manifests`, with
6
+ * `k8s.member` in its config (see `workspace-kinds.json`). The k8s plugin's
7
+ * `buildRoots` hook calls {@link readK8sMember}, and the objects it returns are
8
+ * the member's nodes.
8
9
  *
9
10
  * - `kustomize` renders the directory through the kustomize runner (`kustomize
10
11
  * build`, `kubectl kustomize` fallback). With neither binary installed there
@@ -12,21 +13,52 @@
12
13
  * `rendered: false` with the reason. A render the binary ran and refused is
13
14
  * not that case and still throws.
14
15
  * - `argo-app` and `flux-kustomization` read the directory's YAML files and keep
15
- * the documents of their kind. No binary is involved.
16
+ * the documents of their kinds. No binary is involved.
17
+ * - `manifests` reads the directory's YAML files and keeps every document with
18
+ * a string `apiVersion` and `kind`, whatever the kind, so a custom resource
19
+ * with no lexicon class is a node too. No binary is involved. A directory
20
+ * that a kustomization in the workspace lists in `resources` is not read:
21
+ * the kustomize member has its objects.
22
+ *
23
+ * An Argo CD cluster Secret (a Secret labelled `argocd.argoproj.io/secret-type:
24
+ * cluster`) is kept by the `argo-app` and `manifests` kinds, cut down to its
25
+ * metadata and the `name` and `server` it registers (#5101, ./cluster-secret.ts).
26
+ * Its other data holds the cluster's credentials, which the graph does not carry.
27
+ *
28
+ * A YAML file that does not parse, such as a chart template, is a warning that
29
+ * names the file; the member's other files are still read.
16
30
  */
17
- import { readdirSync, readFileSync } from "node:fs";
18
- import { basename, join } from "node:path";
31
+ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
32
+ import { basename, join, relative, resolve } from "node:path";
19
33
  import { loadAll } from "js-yaml";
20
34
  import type { Declarable } from "@intentius/chant/declarable";
21
- import { defaultKustomizeRunner, KustomizeBinaryMissingError, renderKustomizeDocuments, type KustomizeRunner } from "./kustomize/render";
35
+ import {
36
+ defaultKustomizeRunner,
37
+ KustomizeBinaryMissingError,
38
+ KustomizeHelmDisabledError,
39
+ KustomizeHelmRenderError,
40
+ renderKustomizeDocuments,
41
+ type KustomizeHelmOptions,
42
+ type KustomizeRunner,
43
+ } from "./kustomize/render";
22
44
  import { addRenderedEntity, renderedManifestEntity } from "./kustomize/rendered-entity";
45
+ import { clusterSecretView, isClusterSecretDocument } from "./cluster-secret";
46
+ import { kustomizationDocument, readHelmReferences } from "./helm-references";
23
47
 
24
- export type K8sMemberKind = "kustomize" | "argo-app" | "flux-kustomization";
48
+ export type K8sMemberKind = "kustomize" | "argo-app" | "flux-kustomization" | "manifests";
25
49
 
26
50
  export interface K8sMemberConfig {
27
51
  kind: K8sMemberKind;
28
52
  dir: string;
29
53
  label?: string;
54
+ /** Kustomize only: pass `--enable-helm` (#5112). Also set by `CHANT_K8S_ENABLE_HELM=1` in the environment, which is how `chant workspace graph` is told, since it writes this config itself. */
55
+ enableHelm?: boolean;
56
+ /** Kustomize only: the `helm` binary for `--helm-command`. Environment: `CHANT_K8S_HELM_COMMAND`. */
57
+ helmCommand?: string;
58
+ /** Kustomize only: let kustomize pull a chart that is not in the chart home, which uses the network. Environment: `CHANT_K8S_FETCH_CHARTS=1`. */
59
+ fetchCharts?: boolean;
60
+ /** The workspace root, absolute. `manifests` uses it to find the kustomizations that list its directory, and every kind reads the values files a chart reference names from it (#5100). */
61
+ workspace?: string;
30
62
  }
31
63
 
32
64
  /** What one member's read found, carried on the graph's `meta.k8sMember`. */
@@ -34,16 +66,18 @@ export interface K8sMemberReport {
34
66
  kind: K8sMemberKind;
35
67
  /** Kustomize only: false when the render could not run. */
36
68
  rendered?: boolean;
37
- /** Why nothing was read; set with `rendered: false`. */
69
+ /** Why nothing was read; set with `rendered: false`, or with `coveredBy`. */
38
70
  reason?: string;
71
+ /** `manifests` only: the kustomization directory (workspace-relative) that lists this directory in `resources`, so its objects are read there. */
72
+ coveredBy?: string;
39
73
  /** The objects the member contributed. */
40
74
  objects: number;
41
75
  }
42
76
 
43
- /** The apiVersion group and kind each manifest kind is made of. */
44
- const MANIFEST_KINDS: Record<Exclude<K8sMemberKind, "kustomize">, { group: string; kind: string }> = {
45
- "argo-app": { group: "argoproj.io", kind: "Application" },
46
- "flux-kustomization": { group: "kustomize.toolkit.fluxcd.io", kind: "Kustomization" },
77
+ /** The apiVersion group and kinds each manifest kind is made of. */
78
+ const MANIFEST_KINDS: Record<"argo-app" | "flux-kustomization", { group: string; kinds: string[] }> = {
79
+ "argo-app": { group: "argoproj.io", kinds: ["Application", "ApplicationSet", "AppProject"] },
80
+ "flux-kustomization": { group: "kustomize.toolkit.fluxcd.io", kinds: ["Kustomization"] },
47
81
  };
48
82
 
49
83
  function objectDocuments(text: string): Array<Record<string, unknown>> {
@@ -54,18 +88,99 @@ function objectDocuments(text: string): Array<Record<string, unknown>> {
54
88
  return out;
55
89
  }
56
90
 
57
- /** The documents of `kind` in the YAML files directly in `dir`, in file-name order. */
58
- function manifestDocuments(dir: string, kind: Exclude<K8sMemberKind, "kustomize">): Array<Record<string, unknown>> {
59
- const want = MANIFEST_KINDS[kind];
91
+ /** Whether a mapping anywhere in `value` has a mapping for a key, which js-yaml spells "[object Object]". */
92
+ function hasMappingKey(value: unknown): boolean {
93
+ if (Array.isArray(value)) return value.some(hasMappingKey);
94
+ if (value === null || typeof value !== "object") return false;
95
+ return Object.entries(value).some(([k, v]) => k === "[object Object]" || hasMappingKey(v));
96
+ }
97
+
98
+ /** The documents of a manifest member's YAML files directly in `dir`, in file-name order, and a warning for each file that does not parse. */
99
+ function manifestDocuments(dir: string, kind: Exclude<K8sMemberKind, "kustomize">, label: string): { docs: Array<Record<string, unknown>>; warnings: string[] } {
60
100
  const files = readdirSync(dir).filter((f) => /\.ya?ml$/.test(f)).sort();
61
101
  const docs: Array<Record<string, unknown>> = [];
102
+ const warnings: string[] = [];
62
103
  for (const file of files) {
63
- for (const doc of objectDocuments(readFileSync(join(dir, file), "utf-8"))) {
64
- const apiVersion = doc.apiVersion;
65
- if (typeof apiVersion === "string" && apiVersion.startsWith(`${want.group}/`) && doc.kind === want.kind) docs.push(doc);
104
+ let found: Array<Record<string, unknown>>;
105
+ try {
106
+ found = objectDocuments(readFileSync(join(dir, file), "utf-8"));
107
+ } catch (err) {
108
+ const reason = (err instanceof Error ? err.message : String(err)).split("\n")[0];
109
+ warnings.push(`${kind} member "${label}": ${file} is not valid YAML and was skipped (${reason})`);
110
+ continue;
111
+ }
112
+ // `name: {{ .Values.x }}` is a valid YAML flow mapping, with a mapping for a key, so a template line like it parses. No plain manifest has one.
113
+ if (found.some(hasMappingKey)) {
114
+ warnings.push(`${kind} member "${label}": ${file} holds template syntax ({{ ... }}) and was skipped`);
115
+ continue;
116
+ }
117
+ for (const doc of found) {
118
+ if (typeof doc.apiVersion !== "string" || typeof doc.kind !== "string") continue;
119
+ // A cluster Secret registers a cluster with Argo CD, so the Argo member reads it too, cut down to its name and server.
120
+ if (isClusterSecretDocument(doc)) {
121
+ if (kind === "manifests" || kind === "argo-app") docs.push(clusterSecretView(doc));
122
+ continue;
123
+ }
124
+ if (kind === "manifests") {
125
+ docs.push(doc);
126
+ continue;
127
+ }
128
+ const want = MANIFEST_KINDS[kind];
129
+ if (doc.apiVersion.startsWith(`${want.group}/`) && want.kinds.includes(doc.kind)) docs.push(doc);
66
130
  }
67
131
  }
68
- return docs;
132
+ return { docs, warnings };
133
+ }
134
+
135
+ const SCAN_SKIP = new Set(["node_modules", ".git"]);
136
+ const KUSTOMIZATION_FILES = ["kustomization.yaml", "kustomization.yml", "Kustomization"];
137
+
138
+ /** Directories each kustomization under `root` lists in `resources` (or `bases`), absolute, mapped to the kustomization's directory. One scan per root per process. */
139
+ const listedCache = new Map<string, Map<string, string>>();
140
+
141
+ function listedDirectories(root: string): Map<string, string> {
142
+ const cached = listedCache.get(root);
143
+ if (cached) return cached;
144
+ const listed = new Map<string, string>();
145
+ const walk = (dir: string): void => {
146
+ let entries;
147
+ try {
148
+ entries = readdirSync(dir, { withFileTypes: true });
149
+ } catch {
150
+ return;
151
+ }
152
+ const file = entries.find((e) => e.isFile() && KUSTOMIZATION_FILES.includes(e.name));
153
+ if (file) {
154
+ try {
155
+ const doc = loadAll(readFileSync(join(dir, file.name), "utf-8"))[0] as { resources?: unknown; bases?: unknown } | undefined;
156
+ for (const list of [doc?.resources, doc?.bases]) {
157
+ if (!Array.isArray(list)) continue;
158
+ for (const entry of list) {
159
+ if (typeof entry !== "string" || /^[a-z][a-z0-9+.-]*:/i.test(entry)) continue;
160
+ const target = resolve(dir, entry);
161
+ if (!listed.has(target) && existsSync(target) && statSync(target).isDirectory()) listed.set(target, dir);
162
+ }
163
+ }
164
+ } catch {
165
+ // A kustomization that does not parse lists nothing here; its own member reports it.
166
+ }
167
+ }
168
+ for (const e of entries) if (e.isDirectory() && !SCAN_SKIP.has(e.name)) walk(join(dir, e.name));
169
+ };
170
+ walk(root);
171
+ listedCache.set(root, listed);
172
+ return listed;
173
+ }
174
+
175
+ const truthy = (v: string | undefined): boolean => v !== undefined && /^(1|true|yes)$/i.test(v.trim());
176
+
177
+ /** The Helm settings of a kustomize member: its config, or the environment when the config sets none. */
178
+ export function memberHelmOptions(member: K8sMemberConfig, env: NodeJS.ProcessEnv = process.env): KustomizeHelmOptions {
179
+ return {
180
+ enableHelm: member.enableHelm ?? truthy(env.CHANT_K8S_ENABLE_HELM),
181
+ helmCommand: member.helmCommand ?? (env.CHANT_K8S_HELM_COMMAND || undefined),
182
+ fetchCharts: member.fetchCharts ?? truthy(env.CHANT_K8S_FETCH_CHARTS),
183
+ };
69
184
  }
70
185
 
71
186
  export async function readK8sMember(opts: {
@@ -83,19 +198,45 @@ export async function readK8sMember(opts: {
83
198
  let reason: string | undefined;
84
199
  if (member.kind === "kustomize") {
85
200
  try {
86
- documents = await renderKustomizeDocuments(member.dir, opts.run ?? defaultKustomizeRunner);
201
+ documents = await renderKustomizeDocuments(member.dir, opts.run ?? defaultKustomizeRunner, memberHelmOptions(member));
87
202
  rendered = true;
88
203
  } catch (err) {
89
- if (!(err instanceof KustomizeBinaryMissingError)) throw err;
204
+ // A render Helm kept from happening leaves the member unrendered, not lost: the chart and values nodes read as data below still stand.
205
+ if (!(err instanceof KustomizeBinaryMissingError || err instanceof KustomizeHelmDisabledError || err instanceof KustomizeHelmRenderError)) throw err;
90
206
  documents = [];
91
207
  rendered = false;
92
208
  reason = err.message;
93
209
  warnings.push(`kustomize member "${label}" was not rendered: ${err.message}`);
94
210
  }
95
211
  } else {
96
- documents = manifestDocuments(member.dir, member.kind);
212
+ const listedBy = member.kind === "manifests" && member.workspace ? listedDirectories(resolve(member.workspace)).get(resolve(member.dir)) : undefined;
213
+ if (listedBy) {
214
+ const coveredBy = relative(resolve(member.workspace!), listedBy) || ".";
215
+ return {
216
+ entities,
217
+ warnings,
218
+ report: { kind: member.kind, reason: `listed in resources by the kustomization in ${coveredBy}; its objects are read there`, coveredBy, objects: 0 },
219
+ };
220
+ }
221
+ const read = manifestDocuments(member.dir, member.kind, label);
222
+ documents = read.docs;
223
+ warnings.push(...read.warnings);
97
224
  }
98
225
 
226
+ // Charts and values files, read as data (#5100), whether or not the member rendered.
227
+ if (member.kind === "kustomize") {
228
+ const own = kustomizationDocument(member.dir, label);
229
+ if (own) documents = [own, ...documents];
230
+ }
231
+ documents = readHelmReferences({
232
+ documents,
233
+ ctx: { workspace: resolve(member.workspace ?? member.dir), dir: resolve(member.dir) },
234
+ label,
235
+ entities,
236
+ warnings,
237
+ memberKind: member.kind,
238
+ });
239
+
99
240
  for (const doc of documents) {
100
241
  const entity = renderedManifestEntity(doc, label);
101
242
  if (!entity) {
@@ -1,10 +1,10 @@
1
1
  {
2
- "$comment": "Member kinds this lexicon supplies to a chant workspace (#3179). chant reads this file as data and never imports the lexicon. The Argo and Flux probes read the manifest's apiVersion and kind, so a Flux Kustomization is told apart from the kustomize one that shares its kind name. Each kind's graph block (#3179) reads the member through the k8s lexicon's member reader (src/workspace-member.ts): a kustomize member is rendered, and says rendered: false on meta.k8sMember when neither kustomize nor kubectl is installed; an Argo or Flux member is read from its manifests.",
2
+ "$comment": "Member kinds this lexicon supplies to a chant workspace (#3179). chant reads this file as data and never imports the lexicon. The Argo and Flux probes read the manifest's apiVersion and kind, so a Flux Kustomization is told apart from the kustomize one that shares its kind name. Each kind's graph block (#3179) reads the member through the k8s lexicon's member reader (src/workspace-member.ts): a kustomize member is rendered, and says rendered: false on meta.k8sMember when neither kustomize nor kubectl is installed; an Argo, Flux or manifests member is read from its manifests.",
3
3
  "schema": 1,
4
4
  "kinds": [
5
5
  {
6
6
  "name": "argo-app",
7
- "description": "an Argo CD Application, a manifest with apiVersion argoproj.io/* and kind Application",
7
+ "description": "an Argo CD Application, ApplicationSet or AppProject, a manifest with apiVersion argoproj.io/* and one of those kinds; the member also reads an Argo CD cluster Secret, cut down to its name and server",
8
8
  "precedence": 360,
9
9
  "probe": {
10
10
  "anyDocument": {
@@ -18,7 +18,9 @@
18
18
  "argoproj.io/*"
19
19
  ],
20
20
  "/kind": [
21
- "Application"
21
+ "Application",
22
+ "ApplicationSet",
23
+ "AppProject"
22
24
  ]
23
25
  }
24
26
  ]
@@ -30,7 +32,8 @@
30
32
  "member": {
31
33
  "kind": "argo-app",
32
34
  "dir": "{dir}",
33
- "label": "{member}"
35
+ "label": "{member}",
36
+ "workspace": "{workspace}"
34
37
  }
35
38
  }
36
39
  }
@@ -63,7 +66,8 @@
63
66
  "member": {
64
67
  "kind": "flux-kustomization",
65
68
  "dir": "{dir}",
66
- "label": "{member}"
69
+ "label": "{member}",
70
+ "workspace": "{workspace}"
67
71
  }
68
72
  }
69
73
  }
@@ -85,7 +89,42 @@
85
89
  "member": {
86
90
  "kind": "kustomize",
87
91
  "dir": "{dir}",
88
- "label": "{member}"
92
+ "label": "{member}",
93
+ "workspace": "{workspace}"
94
+ }
95
+ }
96
+ }
97
+ },
98
+ {
99
+ "name": "manifests",
100
+ "description": "a directory of Kubernetes manifests, YAML files with an apiVersion and a kind; every document is read as data, whatever its kind",
101
+ "precedence": 305,
102
+ "probe": {
103
+ "anyDocument": {
104
+ "in": [
105
+ "*.yaml",
106
+ "*.yml"
107
+ ],
108
+ "match": [
109
+ {
110
+ "/apiVersion": [
111
+ "*"
112
+ ],
113
+ "/kind": [
114
+ "*"
115
+ ]
116
+ }
117
+ ]
118
+ }
119
+ },
120
+ "graph": {
121
+ "lexicon": "k8s",
122
+ "config": {
123
+ "member": {
124
+ "kind": "manifests",
125
+ "dir": "{dir}",
126
+ "label": "{member}",
127
+ "workspace": "{workspace}"
89
128
  }
90
129
  }
91
130
  }