@intentius/chant-lexicon-k8s 0.124.48 → 0.124.50
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.
- package/dist/config-schema.d.ts +9 -0
- package/dist/config-schema.d.ts.map +1 -1
- package/dist/config-schema.js +7 -0
- package/dist/config.d.ts +18 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/crd/crd-sources.d.ts.map +1 -1
- package/dist/crd/crd-sources.js +46 -0
- package/dist/generated/index.d.d.ts +87 -0
- package/dist/generated/index.d.ts +58 -0
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/index.js +58 -0
- package/dist/group-namespace.d.ts.map +1 -1
- package/dist/group-namespace.js +7 -0
- package/dist/integrity.json +4 -4
- package/dist/kustomize/render.d.ts +8 -0
- package/dist/kustomize/render.d.ts.map +1 -1
- package/dist/kustomize/render.js +12 -1
- package/dist/manifest.json +1 -1
- package/dist/meta.json +358 -10
- package/dist/okf/index.md +6 -0
- package/dist/okf/types/ClickHouseCluster.md +14 -0
- package/dist/okf/types/ClickHouseInstallation.md +14 -0
- package/dist/okf/types/ClickHouseInstallationTemplate.md +14 -0
- package/dist/okf/types/ClickHouseKeeperInstallation.md +14 -0
- package/dist/okf/types/ClickHouseOperatorConfiguration.md +14 -0
- package/dist/okf/types/KeeperCluster.md +14 -0
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +16 -0
- package/dist/types/index.d.ts +87 -0
- package/dist/workspace-member.d.ts +35 -0
- package/dist/workspace-member.d.ts.map +1 -0
- package/dist/workspace-member.js +99 -0
- package/package.json +8 -6
- package/src/config-schema.ts +7 -0
- package/src/config.ts +19 -0
- package/src/crd/clickhouse-operators.test.ts +105 -0
- package/src/crd/crd-sources.ts +48 -0
- package/src/generated/index.d.ts +87 -0
- package/src/generated/index.ts +58 -0
- package/src/generated/lexicon-k8s.json +358 -10
- package/src/generated/operations.json +96 -0
- package/src/group-namespace.ts +7 -0
- package/src/kustomize/render.ts +13 -1
- package/src/plugin.ts +17 -0
- package/src/testdata/workspace-members/argo/guestbook.yaml +28 -0
- package/src/testdata/workspace-members/argo/namespace.yaml +4 -0
- package/src/testdata/workspace-members/flux/apps.yaml +12 -0
- package/src/testdata/workspace-members/flux/kustomization.yaml +4 -0
- package/src/workspace-kinds.test.ts +35 -0
- package/src/workspace-member.test.ts +154 -0
- package/src/workspace-member.ts +128 -0
- package/workspace-kinds.json +94 -0
|
@@ -195,6 +195,70 @@
|
|
|
195
195
|
"scope": "Namespaced",
|
|
196
196
|
"verbs": []
|
|
197
197
|
},
|
|
198
|
+
"K8s::Altinity::ClickHouseInstallation": {
|
|
199
|
+
"entityType": "K8s::Altinity::ClickHouseInstallation",
|
|
200
|
+
"apiVersion": "clickhouse.altinity.com/v1",
|
|
201
|
+
"kind": "ClickHouseInstallation",
|
|
202
|
+
"plural": "clickhouseinstallations",
|
|
203
|
+
"scope": "Namespaced",
|
|
204
|
+
"verbs": [
|
|
205
|
+
"delete",
|
|
206
|
+
"get",
|
|
207
|
+
"list",
|
|
208
|
+
"patch",
|
|
209
|
+
"post",
|
|
210
|
+
"put",
|
|
211
|
+
"watch"
|
|
212
|
+
]
|
|
213
|
+
},
|
|
214
|
+
"K8s::Altinity::ClickHouseInstallationTemplate": {
|
|
215
|
+
"entityType": "K8s::Altinity::ClickHouseInstallationTemplate",
|
|
216
|
+
"apiVersion": "clickhouse.altinity.com/v1",
|
|
217
|
+
"kind": "ClickHouseInstallationTemplate",
|
|
218
|
+
"plural": "clickhouseinstallationtemplates",
|
|
219
|
+
"scope": "Namespaced",
|
|
220
|
+
"verbs": [
|
|
221
|
+
"delete",
|
|
222
|
+
"get",
|
|
223
|
+
"list",
|
|
224
|
+
"patch",
|
|
225
|
+
"post",
|
|
226
|
+
"put",
|
|
227
|
+
"watch"
|
|
228
|
+
]
|
|
229
|
+
},
|
|
230
|
+
"K8s::Altinity::ClickHouseKeeperInstallation": {
|
|
231
|
+
"entityType": "K8s::Altinity::ClickHouseKeeperInstallation",
|
|
232
|
+
"apiVersion": "clickhouse-keeper.altinity.com/v1",
|
|
233
|
+
"kind": "ClickHouseKeeperInstallation",
|
|
234
|
+
"plural": "clickhousekeeperinstallations",
|
|
235
|
+
"scope": "Namespaced",
|
|
236
|
+
"verbs": [
|
|
237
|
+
"delete",
|
|
238
|
+
"get",
|
|
239
|
+
"list",
|
|
240
|
+
"patch",
|
|
241
|
+
"post",
|
|
242
|
+
"put",
|
|
243
|
+
"watch"
|
|
244
|
+
]
|
|
245
|
+
},
|
|
246
|
+
"K8s::Altinity::ClickHouseOperatorConfiguration": {
|
|
247
|
+
"entityType": "K8s::Altinity::ClickHouseOperatorConfiguration",
|
|
248
|
+
"apiVersion": "clickhouse.altinity.com/v1",
|
|
249
|
+
"kind": "ClickHouseOperatorConfiguration",
|
|
250
|
+
"plural": "clickhouseoperatorconfigurations",
|
|
251
|
+
"scope": "Namespaced",
|
|
252
|
+
"verbs": [
|
|
253
|
+
"delete",
|
|
254
|
+
"get",
|
|
255
|
+
"list",
|
|
256
|
+
"patch",
|
|
257
|
+
"post",
|
|
258
|
+
"put",
|
|
259
|
+
"watch"
|
|
260
|
+
]
|
|
261
|
+
},
|
|
198
262
|
"K8s::Apiextensions::CustomResourceDefinition": {
|
|
199
263
|
"entityType": "K8s::Apiextensions::CustomResourceDefinition",
|
|
200
264
|
"apiVersion": "apiextensions.k8s.io/v1",
|
|
@@ -689,6 +753,38 @@
|
|
|
689
753
|
"scope": "Namespaced",
|
|
690
754
|
"verbs": []
|
|
691
755
|
},
|
|
756
|
+
"K8s::ClickHouse::ClickHouseCluster": {
|
|
757
|
+
"entityType": "K8s::ClickHouse::ClickHouseCluster",
|
|
758
|
+
"apiVersion": "clickhouse.com/v1alpha1",
|
|
759
|
+
"kind": "ClickHouseCluster",
|
|
760
|
+
"plural": "clickhouseclusters",
|
|
761
|
+
"scope": "Namespaced",
|
|
762
|
+
"verbs": [
|
|
763
|
+
"delete",
|
|
764
|
+
"get",
|
|
765
|
+
"list",
|
|
766
|
+
"patch",
|
|
767
|
+
"post",
|
|
768
|
+
"put",
|
|
769
|
+
"watch"
|
|
770
|
+
]
|
|
771
|
+
},
|
|
772
|
+
"K8s::ClickHouse::KeeperCluster": {
|
|
773
|
+
"entityType": "K8s::ClickHouse::KeeperCluster",
|
|
774
|
+
"apiVersion": "clickhouse.com/v1alpha1",
|
|
775
|
+
"kind": "KeeperCluster",
|
|
776
|
+
"plural": "keeperclusters",
|
|
777
|
+
"scope": "Namespaced",
|
|
778
|
+
"verbs": [
|
|
779
|
+
"delete",
|
|
780
|
+
"get",
|
|
781
|
+
"list",
|
|
782
|
+
"patch",
|
|
783
|
+
"post",
|
|
784
|
+
"put",
|
|
785
|
+
"watch"
|
|
786
|
+
]
|
|
787
|
+
},
|
|
692
788
|
"K8s::Cnpg::Backup": {
|
|
693
789
|
"entityType": "K8s::Cnpg::Backup",
|
|
694
790
|
"apiVersion": "postgresql.cnpg.io/v1",
|
package/src/group-namespace.ts
CHANGED
|
@@ -76,6 +76,13 @@ export const GROUP_NAMESPACE_OVERRIDES: Record<string, string> = {
|
|
|
76
76
|
// product (OpenTelemetry is one word with two capitals) and leaves the
|
|
77
77
|
// operator's kinds as `K8s::Opentelemetry::OpenTelemetryCollector`.
|
|
78
78
|
"opentelemetry.io": "OpenTelemetry",
|
|
79
|
+
// Two ClickHouse operators, kept apart. The first-segment rule would give
|
|
80
|
+
// `Clickhouse` to both clickhouse.com (ClickHouse Inc.) and
|
|
81
|
+
// clickhouse.altinity.com (Altinity). The vendor's takes the product's
|
|
82
|
+
// spelling; Altinity's two groups (the Keeper CRD has its own) share one name.
|
|
83
|
+
"clickhouse.com": "ClickHouse",
|
|
84
|
+
"clickhouse.altinity.com": "Altinity",
|
|
85
|
+
"clickhouse-keeper.altinity.com": "Altinity",
|
|
79
86
|
};
|
|
80
87
|
|
|
81
88
|
/**
|
package/src/kustomize/render.ts
CHANGED
|
@@ -42,6 +42,18 @@ export function renderCommand(dir: string, tool: "kustomize" | "kubectl" = "kust
|
|
|
42
42
|
return tool === "kustomize" ? `kustomize build ${q(dir)}` : `kubectl kustomize ${q(dir)}`;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Neither `kustomize` nor `kubectl` is installed. A class of its own so a
|
|
47
|
+
* caller that can say "not rendered, and why" (the workspace graph reader,
|
|
48
|
+
* #3179) tells it from a render the binary ran and refused.
|
|
49
|
+
*/
|
|
50
|
+
export class KustomizeBinaryMissingError extends Error {
|
|
51
|
+
constructor(message: string) {
|
|
52
|
+
super(message);
|
|
53
|
+
this.name = "KustomizeBinaryMissingError";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
45
57
|
/** Does this error mean "the binary is not installed" (vs "the render failed")? */
|
|
46
58
|
function isMissingBinary(err: unknown): boolean {
|
|
47
59
|
const message = err instanceof Error ? err.message : String(err);
|
|
@@ -67,7 +79,7 @@ export async function renderKustomizeDocuments(
|
|
|
67
79
|
({ stdout } = await run(renderCommand(dir, "kubectl")));
|
|
68
80
|
} catch (err2) {
|
|
69
81
|
if (isMissingBinary(err2)) {
|
|
70
|
-
throw new
|
|
82
|
+
throw new KustomizeBinaryMissingError(
|
|
71
83
|
`Cannot render kustomization "${dir}": neither the \`kustomize\` binary nor \`kubectl\` ` +
|
|
72
84
|
`(whose vendored \`kubectl kustomize\` is the fallback) was found on PATH. ` +
|
|
73
85
|
`Install kustomize or kubectl and retry.`,
|
package/src/plugin.ts
CHANGED
|
@@ -33,6 +33,7 @@ import { LABEL_OWNERSHIP_KEYS } from "@intentius/chant/ownership";
|
|
|
33
33
|
import { k8sConfigSchema } from "./config-schema";
|
|
34
34
|
import type { K8sChantConfig } from "./config";
|
|
35
35
|
import { renderKustomizeRoots } from "./kustomize/root";
|
|
36
|
+
import { lastMemberReport, readK8sMember, setLastMemberReport } from "./workspace-member";
|
|
36
37
|
import { encryptedSecretBuildRoot } from "./sops/encrypted-secret-file";
|
|
37
38
|
import { k8sProjectCodegen } from "./crd/project-codegen";
|
|
38
39
|
import { includeBuildRoot } from "./include/resolve";
|
|
@@ -111,6 +112,16 @@ export const k8sPlugin: LexiconPlugin = {
|
|
|
111
112
|
warnings.push(...rendered.warnings);
|
|
112
113
|
}
|
|
113
114
|
|
|
115
|
+
// A workspace member's reader project (#3179): see ./workspace-member.ts.
|
|
116
|
+
setLastMemberReport(undefined);
|
|
117
|
+
const member = (ctx.config as { k8s?: K8sChantConfig }).k8s?.member;
|
|
118
|
+
if (member) {
|
|
119
|
+
const read = await readK8sMember({ member });
|
|
120
|
+
for (const [name, entity] of read.entities) entities.set(name, entity);
|
|
121
|
+
warnings.push(...read.warnings);
|
|
122
|
+
setLastMemberReport(read.report);
|
|
123
|
+
}
|
|
124
|
+
|
|
114
125
|
const included = await includeBuildRoot(ctx);
|
|
115
126
|
for (const [name, entity] of included.entities) entities.set(name, entity);
|
|
116
127
|
|
|
@@ -121,6 +132,12 @@ export const k8sPlugin: LexiconPlugin = {
|
|
|
121
132
|
return { entities, warnings };
|
|
122
133
|
},
|
|
123
134
|
|
|
135
|
+
// What the workspace member read (#3179): whether a kustomize member rendered, and why not.
|
|
136
|
+
graphMeta() {
|
|
137
|
+
const report = lastMemberReport();
|
|
138
|
+
return report ? { k8sMember: report } : undefined;
|
|
139
|
+
},
|
|
140
|
+
|
|
124
141
|
// K8s YAML has no template interpolation functions like CloudFormation's
|
|
125
142
|
// Fn::Sub or GitLab's !reference. Cross-resource references are handled by
|
|
126
143
|
// the AttrRef system in the serializer (resolves to metadata.name).
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
apiVersion: argoproj.io/v1alpha1
|
|
2
|
+
kind: Application
|
|
3
|
+
metadata:
|
|
4
|
+
name: guestbook
|
|
5
|
+
namespace: argocd
|
|
6
|
+
spec:
|
|
7
|
+
project: default
|
|
8
|
+
source:
|
|
9
|
+
repoURL: https://github.com/example/guestbook
|
|
10
|
+
path: deploy
|
|
11
|
+
targetRevision: main
|
|
12
|
+
destination:
|
|
13
|
+
server: https://kubernetes.default.svc
|
|
14
|
+
namespace: guestbook
|
|
15
|
+
---
|
|
16
|
+
apiVersion: argoproj.io/v1alpha1
|
|
17
|
+
kind: Application
|
|
18
|
+
metadata:
|
|
19
|
+
name: payments
|
|
20
|
+
namespace: argocd
|
|
21
|
+
spec:
|
|
22
|
+
project: default
|
|
23
|
+
source:
|
|
24
|
+
repoURL: https://github.com/example/payments
|
|
25
|
+
path: deploy
|
|
26
|
+
destination:
|
|
27
|
+
server: https://kubernetes.default.svc
|
|
28
|
+
namespace: payments
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The kustomize, argo-app and flux-kustomization member kinds this lexicon
|
|
3
|
+
* supplies to a chant workspace (#3179): data in `workspace-kinds.json`,
|
|
4
|
+
* exported at `./workspace-kinds`.
|
|
5
|
+
*/
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { describe, expect, it } from "vitest";
|
|
8
|
+
import { describeWorkspaceKindConformance } from "@intentius/chant-test-utils";
|
|
9
|
+
import { checkMemberFile, loadMemberFileSchema, readPackageKinds } from "../../../packages/core/src/workspace/kinds";
|
|
10
|
+
|
|
11
|
+
const packageDir = join(import.meta.dirname, "..");
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
const argo = "apiVersion: argoproj.io/v1alpha1\nkind: Application\nmetadata:\n name: guestbook\nspec: {}\n";
|
|
15
|
+
const flux = "apiVersion: kustomize.toolkit.fluxcd.io/v1\nkind: Kustomization\nmetadata:\n name: apps\nspec: {}\n";
|
|
16
|
+
const kustomization = "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\nresources:\n - deploy.yaml\n";
|
|
17
|
+
|
|
18
|
+
describeWorkspaceKindConformance({
|
|
19
|
+
packageDir,
|
|
20
|
+
scenarios: [
|
|
21
|
+
{ name: "an overlay", kind: "kustomize", files: { "kustomization.yaml": kustomization }, claims: true },
|
|
22
|
+
{ name: "an overlay named kustomization.yml", kind: "kustomize", files: { "kustomization.yml": kustomization }, claims: true },
|
|
23
|
+
{ name: "an overlay one level down", kind: "kustomize", files: { "prod/kustomization.yaml": kustomization }, claims: false },
|
|
24
|
+
// A Flux Kustomization (350) outranks kustomize (340) in a directory that has both.
|
|
25
|
+
{ name: "a Flux Kustomization beside a kustomization.yaml", kind: "flux-kustomization", files: { "kustomization.yaml": kustomization, "sync.yaml": flux }, claims: true },
|
|
26
|
+
{ name: "an Argo Application", kind: "argo-app", files: { "app.yaml": argo }, claims: true },
|
|
27
|
+
{ name: "an Argo Application among other documents", kind: "argo-app", files: { "all.yml": "kind: ConfigMap\napiVersion: v1\n---\n" + argo }, claims: true },
|
|
28
|
+
{ name: "an Argo Application beside a kustomization.yaml", kind: "argo-app", files: { "kustomization.yaml": kustomization, "app.yaml": argo }, claims: true },
|
|
29
|
+
{ name: "a Deployment", kind: "argo-app", files: { "deploy.yaml": "apiVersion: apps/v1\nkind: Deployment\n" }, claims: false },
|
|
30
|
+
{ name: "an Application of another group", kind: "argo-app", files: { "app.yaml": "apiVersion: app.k8s.io/v1beta1\nkind: Application\n" }, claims: false },
|
|
31
|
+
{ name: "a Flux Kustomization", kind: "flux-kustomization", files: { "sync.yaml": flux }, claims: true },
|
|
32
|
+
{ name: "kustomize's own kustomization.yaml is not a Flux one", kind: "flux-kustomization", files: { "kustomization.yaml": kustomization }, claims: false },
|
|
33
|
+
{ name: "a manifest that is not YAML", kind: "flux-kustomization", files: { "sync.yaml": "{{ not: [yaml" }, claims: false },
|
|
34
|
+
],
|
|
35
|
+
});
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The graph blocks of the kustomize, argo-app and flux-kustomization member
|
|
3
|
+
* kinds (#3179). Each test builds the reader project the way chant workspace
|
|
4
|
+
* graph does (the kind's graph config, placeholders substituted, written to a
|
|
5
|
+
* chant.config.json) and runs chant graph over it.
|
|
6
|
+
*
|
|
7
|
+
* The kustomize render is a canned runner, so no test needs kustomize or
|
|
8
|
+
* kubectl; the last test uses the real binary when one is installed.
|
|
9
|
+
*/
|
|
10
|
+
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
11
|
+
import { execFileSync } from "node:child_process";
|
|
12
|
+
import { dirname, join } from "node:path";
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
15
|
+
import { tmpdir } from "node:os";
|
|
16
|
+
import type { GraphIR } from "@intentius/chant/graph-ir";
|
|
17
|
+
import type { LexiconPlugin } from "@intentius/chant/lexicon";
|
|
18
|
+
import { runGraph } from "@intentius/chant/cli/handlers/graph";
|
|
19
|
+
import { readPackageKinds, substituteGraphConfig } from "../../../packages/core/src/workspace/kinds";
|
|
20
|
+
import type { KustomizeRunner } from "./kustomize/render";
|
|
21
|
+
import { k8sPlugin } from "./plugin";
|
|
22
|
+
import { readK8sMember, setLastMemberReport, type K8sMemberConfig } from "./workspace-member";
|
|
23
|
+
|
|
24
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
25
|
+
const packageDir = join(here, "..");
|
|
26
|
+
const members = join(here, "testdata", "workspace-members");
|
|
27
|
+
const overlay = join(here, "testdata", "kustomize-root", "overlays", "prod");
|
|
28
|
+
|
|
29
|
+
const RENDERED = `apiVersion: v1
|
|
30
|
+
kind: Service
|
|
31
|
+
metadata:
|
|
32
|
+
name: prod-web
|
|
33
|
+
spec:
|
|
34
|
+
ports:
|
|
35
|
+
- port: 80
|
|
36
|
+
---
|
|
37
|
+
apiVersion: apps/v1
|
|
38
|
+
kind: Deployment
|
|
39
|
+
metadata:
|
|
40
|
+
name: prod-web
|
|
41
|
+
spec:
|
|
42
|
+
replicas: 3
|
|
43
|
+
`;
|
|
44
|
+
|
|
45
|
+
const noBinary: KustomizeRunner = async () => {
|
|
46
|
+
throw new Error("spawn kustomize ENOENT: command not found");
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** The k8s plugin with the member's kustomize render swapped for `run`. */
|
|
50
|
+
function pluginWith(run?: KustomizeRunner): LexiconPlugin {
|
|
51
|
+
if (!run) return k8sPlugin;
|
|
52
|
+
return {
|
|
53
|
+
...k8sPlugin,
|
|
54
|
+
buildRoots: async (ctx) => {
|
|
55
|
+
const member = (ctx.config as { k8s?: { member?: K8sMemberConfig } }).k8s?.member;
|
|
56
|
+
setLastMemberReport(undefined);
|
|
57
|
+
if (!member) return { entities: new Map() };
|
|
58
|
+
const read = await readK8sMember({ member, run });
|
|
59
|
+
setLastMemberReport(read.report);
|
|
60
|
+
return { entities: read.entities, warnings: read.warnings };
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const scratch: string[] = [];
|
|
66
|
+
afterEach(async () => {
|
|
67
|
+
vi.restoreAllMocks();
|
|
68
|
+
await Promise.all(scratch.splice(0).map((d) => rm(d, { recursive: true, force: true })));
|
|
69
|
+
});
|
|
70
|
+
|
|
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 }> {
|
|
73
|
+
const graph = readPackageKinds(packageDir).kinds.find((k) => k.name === kind)!.graph!;
|
|
74
|
+
expect(graph.lexicon).toBe("k8s");
|
|
75
|
+
const config = substituteGraphConfig(graph.config, { member: "m", dir, workspace: dirname(dir) });
|
|
76
|
+
const project = await mkdtemp(join(tmpdir(), "chant-k8s-member-"));
|
|
77
|
+
scratch.push(project);
|
|
78
|
+
await writeFile(join(project, "chant.config.json"), JSON.stringify({ lexicons: ["k8s"], k8s: config, graph: { lint: "report" } }));
|
|
79
|
+
const out: string[] = [];
|
|
80
|
+
const err: string[] = [];
|
|
81
|
+
vi.spyOn(console, "log").mockImplementation((line: unknown) => void out.push(String(line)));
|
|
82
|
+
vi.spyOn(console, "error").mockImplementation((line: unknown) => void err.push(String(line)));
|
|
83
|
+
const cwd = process.cwd();
|
|
84
|
+
process.chdir(project);
|
|
85
|
+
try {
|
|
86
|
+
const exit = await runGraph({ args: { command: "graph", path: ".", format: "ir" } as never, plugins: [plugin], serializers: [] });
|
|
87
|
+
return { ir: exit === 0 ? (JSON.parse(out.join("\n")) as GraphIR) : { nodes: [], edges: [], groups: {} }, err, exit };
|
|
88
|
+
} finally {
|
|
89
|
+
process.chdir(cwd);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const ids = (ir: GraphIR) => ir.nodes.map((n) => n.id).sort();
|
|
94
|
+
|
|
95
|
+
describe("member kinds with a graph block (#3179)", () => {
|
|
96
|
+
test("kustomize: the rendered objects are the member's nodes", async () => {
|
|
97
|
+
const { ir, err, exit } = await graphOf("kustomize", overlay, pluginWith(async () => ({ stdout: RENDERED })));
|
|
98
|
+
expect(exit, err.join("\n")).toBe(0);
|
|
99
|
+
expect(ids(ir)).toEqual(["m/deploymentProdWeb", "m/serviceProdWeb"]);
|
|
100
|
+
expect(ir.meta?.k8sMember).toEqual({ kind: "kustomize", rendered: true, objects: 2 });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("kustomize: with no binary the member says rendered: false and why", async () => {
|
|
104
|
+
const { ir, err, exit } = await graphOf("kustomize", overlay, pluginWith(noBinary));
|
|
105
|
+
expect(exit, err.join("\n")).toBe(0);
|
|
106
|
+
expect(ir.nodes).toEqual([]);
|
|
107
|
+
const report = ir.meta?.k8sMember as { rendered: boolean; reason: string; objects: number };
|
|
108
|
+
expect(report.rendered).toBe(false);
|
|
109
|
+
expect(report.objects).toBe(0);
|
|
110
|
+
expect(report.reason).toMatch(/neither the `kustomize` binary nor `kubectl`/);
|
|
111
|
+
expect(err.join("\n")).toMatch(/was not rendered/);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("kustomize: a render the binary refused still fails", async () => {
|
|
115
|
+
const refused: KustomizeRunner = async () => {
|
|
116
|
+
throw new Error("Error: accumulating resources: missing base");
|
|
117
|
+
};
|
|
118
|
+
const { exit, err } = await graphOf("kustomize", overlay, pluginWith(refused));
|
|
119
|
+
expect(exit).not.toBe(0);
|
|
120
|
+
expect(err.join("\n")).toMatch(/missing base/);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("argo-app: the Applications are nodes and other manifests beside them are left out", async () => {
|
|
124
|
+
const { ir, err, exit } = await graphOf("argo-app", join(members, "argo"), pluginWith());
|
|
125
|
+
expect(exit, err.join("\n")).toBe(0);
|
|
126
|
+
expect(ids(ir)).toEqual(["m/applicationGuestbook", "m/applicationPayments"]);
|
|
127
|
+
expect(ir.meta?.k8sMember).toEqual({ kind: "argo-app", objects: 2 });
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("flux-kustomization: the Flux Kustomization is a node, the kustomize one beside it is not", async () => {
|
|
131
|
+
const { ir, err, exit } = await graphOf("flux-kustomization", join(members, "flux"), pluginWith());
|
|
132
|
+
expect(exit, err.join("\n")).toBe(0);
|
|
133
|
+
expect(ids(ir)).toEqual(["m/kustomizationApps"]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const haveBinary = (() => {
|
|
137
|
+
for (const bin of ["kustomize", "kubectl"]) {
|
|
138
|
+
try {
|
|
139
|
+
execFileSync(bin, ["version", "--client"], { stdio: "ignore" });
|
|
140
|
+
return true;
|
|
141
|
+
} catch {
|
|
142
|
+
/* not installed */
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return false;
|
|
146
|
+
})();
|
|
147
|
+
|
|
148
|
+
test.skipIf(!haveBinary)("kustomize: the real binary renders the overlay", async () => {
|
|
149
|
+
const { ir, err, exit } = await graphOf("kustomize", overlay, pluginWith());
|
|
150
|
+
expect(exit, err.join("\n")).toBe(0);
|
|
151
|
+
expect((ir.meta?.k8sMember as { rendered: boolean }).rendered).toBe(true);
|
|
152
|
+
expect(ir.nodes.length).toBeGreaterThan(0);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a workspace member into the graph (#3179).
|
|
3
|
+
*
|
|
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.
|
|
8
|
+
*
|
|
9
|
+
* - `kustomize` renders the directory through the kustomize runner (`kustomize
|
|
10
|
+
* build`, `kubectl kustomize` fallback). With neither binary installed there
|
|
11
|
+
* is nothing to read: the member contributes no objects and its report says
|
|
12
|
+
* `rendered: false` with the reason. A render the binary ran and refused is
|
|
13
|
+
* not that case and still throws.
|
|
14
|
+
* - `argo-app` and `flux-kustomization` read the directory's YAML files and keep
|
|
15
|
+
* the documents of their kind. No binary is involved.
|
|
16
|
+
*/
|
|
17
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
18
|
+
import { basename, join } from "node:path";
|
|
19
|
+
import { loadAll } from "js-yaml";
|
|
20
|
+
import type { Declarable } from "@intentius/chant/declarable";
|
|
21
|
+
import { defaultKustomizeRunner, KustomizeBinaryMissingError, renderKustomizeDocuments, type KustomizeRunner } from "./kustomize/render";
|
|
22
|
+
import { addRenderedEntity, renderedManifestEntity } from "./kustomize/rendered-entity";
|
|
23
|
+
|
|
24
|
+
export type K8sMemberKind = "kustomize" | "argo-app" | "flux-kustomization";
|
|
25
|
+
|
|
26
|
+
export interface K8sMemberConfig {
|
|
27
|
+
kind: K8sMemberKind;
|
|
28
|
+
dir: string;
|
|
29
|
+
label?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** What one member's read found, carried on the graph's `meta.k8sMember`. */
|
|
33
|
+
export interface K8sMemberReport {
|
|
34
|
+
kind: K8sMemberKind;
|
|
35
|
+
/** Kustomize only: false when the render could not run. */
|
|
36
|
+
rendered?: boolean;
|
|
37
|
+
/** Why nothing was read; set with `rendered: false`. */
|
|
38
|
+
reason?: string;
|
|
39
|
+
/** The objects the member contributed. */
|
|
40
|
+
objects: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
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" },
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
function objectDocuments(text: string): Array<Record<string, unknown>> {
|
|
50
|
+
const out: Array<Record<string, unknown>> = [];
|
|
51
|
+
for (const doc of loadAll(text)) {
|
|
52
|
+
if (doc && typeof doc === "object" && !Array.isArray(doc)) out.push(doc as Record<string, unknown>);
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
|
|
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];
|
|
60
|
+
const files = readdirSync(dir).filter((f) => /\.ya?ml$/.test(f)).sort();
|
|
61
|
+
const docs: Array<Record<string, unknown>> = [];
|
|
62
|
+
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);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return docs;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function readK8sMember(opts: {
|
|
72
|
+
member: K8sMemberConfig;
|
|
73
|
+
/** Injectable renderer (tests); defaults to the real subprocess runner. */
|
|
74
|
+
run?: KustomizeRunner;
|
|
75
|
+
}): Promise<{ entities: Map<string, Declarable>; warnings: string[]; report: K8sMemberReport }> {
|
|
76
|
+
const { member } = opts;
|
|
77
|
+
const label = member.label ?? basename(member.dir);
|
|
78
|
+
const entities = new Map<string, Declarable>();
|
|
79
|
+
const warnings: string[] = [];
|
|
80
|
+
|
|
81
|
+
let documents: Array<Record<string, unknown>>;
|
|
82
|
+
let rendered: boolean | undefined;
|
|
83
|
+
let reason: string | undefined;
|
|
84
|
+
if (member.kind === "kustomize") {
|
|
85
|
+
try {
|
|
86
|
+
documents = await renderKustomizeDocuments(member.dir, opts.run ?? defaultKustomizeRunner);
|
|
87
|
+
rendered = true;
|
|
88
|
+
} catch (err) {
|
|
89
|
+
if (!(err instanceof KustomizeBinaryMissingError)) throw err;
|
|
90
|
+
documents = [];
|
|
91
|
+
rendered = false;
|
|
92
|
+
reason = err.message;
|
|
93
|
+
warnings.push(`kustomize member "${label}" was not rendered: ${err.message}`);
|
|
94
|
+
}
|
|
95
|
+
} else {
|
|
96
|
+
documents = manifestDocuments(member.dir, member.kind);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
for (const doc of documents) {
|
|
100
|
+
const entity = renderedManifestEntity(doc, label);
|
|
101
|
+
if (!entity) {
|
|
102
|
+
warnings.push(`${member.kind} member "${label}" has a document without apiVersion/kind; skipped`);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
addRenderedEntity(entities, label, entity);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
entities,
|
|
110
|
+
warnings,
|
|
111
|
+
report: { kind: member.kind, ...(rendered === undefined ? {} : { rendered }), ...(reason ? { reason } : {}), objects: entities.size },
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let lastReport: K8sMemberReport | undefined;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The report of the read in this process. `graphMeta` receives only the
|
|
119
|
+
* entities, so the hook that read the member leaves its report here for the
|
|
120
|
+
* hook that publishes it; `chant graph` runs the two in order in one process.
|
|
121
|
+
*/
|
|
122
|
+
export function setLastMemberReport(report: K8sMemberReport | undefined): void {
|
|
123
|
+
lastReport = report;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function lastMemberReport(): K8sMemberReport | undefined {
|
|
127
|
+
return lastReport;
|
|
128
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
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.",
|
|
3
|
+
"schema": 1,
|
|
4
|
+
"kinds": [
|
|
5
|
+
{
|
|
6
|
+
"name": "argo-app",
|
|
7
|
+
"description": "an Argo CD Application, a manifest with apiVersion argoproj.io/* and kind Application",
|
|
8
|
+
"precedence": 360,
|
|
9
|
+
"probe": {
|
|
10
|
+
"anyDocument": {
|
|
11
|
+
"in": [
|
|
12
|
+
"*.yaml",
|
|
13
|
+
"*.yml"
|
|
14
|
+
],
|
|
15
|
+
"match": [
|
|
16
|
+
{
|
|
17
|
+
"/apiVersion": [
|
|
18
|
+
"argoproj.io/*"
|
|
19
|
+
],
|
|
20
|
+
"/kind": [
|
|
21
|
+
"Application"
|
|
22
|
+
]
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"graph": {
|
|
28
|
+
"lexicon": "k8s",
|
|
29
|
+
"config": {
|
|
30
|
+
"member": {
|
|
31
|
+
"kind": "argo-app",
|
|
32
|
+
"dir": "{dir}",
|
|
33
|
+
"label": "{member}"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"name": "flux-kustomization",
|
|
40
|
+
"description": "a Flux Kustomization, a manifest with apiVersion kustomize.toolkit.fluxcd.io/* and kind Kustomization",
|
|
41
|
+
"precedence": 350,
|
|
42
|
+
"probe": {
|
|
43
|
+
"anyDocument": {
|
|
44
|
+
"in": [
|
|
45
|
+
"*.yaml",
|
|
46
|
+
"*.yml"
|
|
47
|
+
],
|
|
48
|
+
"match": [
|
|
49
|
+
{
|
|
50
|
+
"/apiVersion": [
|
|
51
|
+
"kustomize.toolkit.fluxcd.io/*"
|
|
52
|
+
],
|
|
53
|
+
"/kind": [
|
|
54
|
+
"Kustomization"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"graph": {
|
|
61
|
+
"lexicon": "k8s",
|
|
62
|
+
"config": {
|
|
63
|
+
"member": {
|
|
64
|
+
"kind": "flux-kustomization",
|
|
65
|
+
"dir": "{dir}",
|
|
66
|
+
"label": "{member}"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
"name": "kustomize",
|
|
73
|
+
"description": "a kustomize base or overlay, with a kustomization.yaml in its directory",
|
|
74
|
+
"precedence": 340,
|
|
75
|
+
"probe": {
|
|
76
|
+
"anyFile": [
|
|
77
|
+
"kustomization.yaml",
|
|
78
|
+
"kustomization.yml",
|
|
79
|
+
"Kustomization"
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
"graph": {
|
|
83
|
+
"lexicon": "k8s",
|
|
84
|
+
"config": {
|
|
85
|
+
"member": {
|
|
86
|
+
"kind": "kustomize",
|
|
87
|
+
"dir": "{dir}",
|
|
88
|
+
"label": "{member}"
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
]
|
|
94
|
+
}
|