@intentius/chant-lexicon-k8s 0.106.4 → 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.
- package/dist/codegen/naming.d.ts.map +1 -1
- package/dist/composites/catalog.d.ts.map +1 -1
- package/dist/composites/index.d.ts +2 -0
- package/dist/composites/index.d.ts.map +1 -1
- package/dist/composites/otel-collector-shape.d.ts +2 -0
- package/dist/composites/otel-collector-shape.d.ts.map +1 -1
- package/dist/composites/otel-operator-collector.d.ts +101 -0
- package/dist/composites/otel-operator-collector.d.ts.map +1 -0
- package/dist/config-schema.d.ts +115 -1
- package/dist/config-schema.d.ts.map +1 -1
- package/dist/config.d.ts +58 -3
- package/dist/config.d.ts.map +1 -1
- package/dist/crd/crd-sources.d.ts.map +1 -1
- package/dist/crd/loader.d.ts +4 -0
- package/dist/crd/loader.d.ts.map +1 -1
- package/dist/crd/parser.d.ts +5 -0
- package/dist/crd/parser.d.ts.map +1 -1
- package/dist/crd/project-codegen.d.ts +33 -0
- package/dist/crd/project-codegen.d.ts.map +1 -0
- package/dist/generated/index.d.d.ts +172 -2
- package/dist/generated/index.d.ts +301 -0
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/group-namespace.d.ts.map +1 -1
- package/dist/import/embedded.d.ts +3 -0
- package/dist/import/embedded.d.ts.map +1 -1
- package/dist/include/entity.d.ts +73 -0
- package/dist/include/entity.d.ts.map +1 -0
- package/dist/include/resolve.d.ts +29 -0
- package/dist/include/resolve.d.ts.map +1 -0
- package/dist/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/integrity.json +17 -10
- package/dist/kustomize/rendered-entity.d.ts +15 -1
- package/dist/kustomize/rendered-entity.d.ts.map +1 -1
- package/dist/kustomize/root.d.ts +11 -2
- package/dist/kustomize/root.d.ts.map +1 -1
- package/dist/lint/audit-catalog.d.ts.map +1 -1
- package/dist/lint/post-synth/crd-schema-helpers.d.ts +5 -1
- package/dist/lint/post-synth/crd-schema-helpers.d.ts.map +1 -1
- package/dist/lint/post-synth/index.d.ts.map +1 -1
- package/dist/lint/post-synth/monitoring-selection-helpers.d.ts +55 -0
- package/dist/lint/post-synth/monitoring-selection-helpers.d.ts.map +1 -0
- package/dist/lint/post-synth/otel-placement-helpers.d.ts +16 -0
- package/dist/lint/post-synth/otel-placement-helpers.d.ts.map +1 -1
- package/dist/lint/post-synth/wk8604.d.ts +6 -0
- package/dist/lint/post-synth/wk8604.d.ts.map +1 -1
- package/dist/lint/post-synth/wk8605.d.ts.map +1 -1
- package/dist/lint/post-synth/wk8606.d.ts +17 -0
- package/dist/lint/post-synth/wk8606.d.ts.map +1 -0
- package/dist/lint/post-synth/wk8701.d.ts +17 -0
- package/dist/lint/post-synth/wk8701.d.ts.map +1 -0
- package/dist/lint/post-synth/wk8702.d.ts +17 -0
- package/dist/lint/post-synth/wk8702.d.ts.map +1 -0
- package/dist/lint/post-synth/wk8703.d.ts +17 -0
- package/dist/lint/post-synth/wk8703.d.ts.map +1 -0
- package/dist/lint/post-synth/wk8704.d.ts +21 -0
- package/dist/lint/post-synth/wk8704.d.ts.map +1 -0
- package/dist/lint/post-synth/wk8705.d.ts +18 -0
- package/dist/lint/post-synth/wk8705.d.ts.map +1 -0
- package/dist/manifest.json +1 -1
- package/dist/meta.json +2284 -370
- package/dist/okf/index.md +20 -1
- package/dist/okf/rules/WK8604.md +6 -2
- package/dist/okf/rules/WK8605.md +4 -0
- package/dist/okf/rules/WK8606.md +15 -0
- package/dist/okf/rules/WK8701.md +18 -0
- package/dist/okf/rules/WK8702.md +20 -0
- package/dist/okf/rules/WK8703.md +17 -0
- package/dist/okf/rules/WK8704.md +22 -0
- package/dist/okf/rules/WK8705.md +18 -0
- package/dist/okf/types/Alertmanager.md +18 -0
- package/dist/okf/types/AlertmanagerConfig.md +18 -0
- package/dist/okf/types/ClusterObservability.md +14 -0
- package/dist/okf/types/CronJob.md +1 -0
- package/dist/okf/types/DaemonSet.md +1 -0
- package/dist/okf/types/Instrumentation.md +14 -0
- package/dist/okf/types/MonitoringProbe.md +14 -0
- package/dist/okf/types/Namespace.md +3 -0
- package/dist/okf/types/OpAMPBridge.md +14 -0
- package/dist/okf/types/OpenTelemetryCollector.md +19 -0
- package/dist/okf/types/Pod.md +1 -0
- package/dist/okf/types/PodMonitor.md +20 -0
- package/dist/okf/types/Prometheus.md +21 -0
- package/dist/okf/types/PrometheusAgent.md +18 -0
- package/dist/okf/types/PrometheusRule.md +6 -1
- package/dist/okf/types/ScrapeConfig.md +19 -0
- package/dist/okf/types/Service.md +1 -0
- package/dist/okf/types/ServiceMonitor.md +7 -1
- package/dist/okf/types/StatefulSet.md +1 -0
- package/dist/okf/types/TargetAllocator.md +14 -0
- package/dist/okf/types/ThanosRuler.md +18 -0
- package/dist/patch/json-patch.d.ts +51 -0
- package/dist/patch/json-patch.d.ts.map +1 -0
- package/dist/patch/rendered-patch.d.ts +90 -0
- package/dist/patch/rendered-patch.d.ts.map +1 -0
- package/dist/plugin.d.ts.map +1 -1
- package/dist/project-kinds.d.ts +46 -0
- package/dist/project-kinds.d.ts.map +1 -0
- package/dist/rules/crd-schema-helpers.ts +10 -2
- package/dist/rules/monitoring-selection-helpers.ts +185 -0
- package/dist/rules/otel-placement-helpers.ts +61 -1
- package/dist/rules/wk8601.ts +1 -1
- package/dist/rules/wk8603.ts +1 -1
- package/dist/rules/wk8604.ts +8 -2
- package/dist/rules/wk8605.ts +5 -3
- package/dist/rules/wk8606.ts +27 -0
- package/dist/rules/wk8701.ts +37 -0
- package/dist/rules/wk8702.ts +43 -0
- package/dist/rules/wk8703.ts +37 -0
- package/dist/rules/wk8704.ts +134 -0
- package/dist/rules/wk8705.ts +45 -0
- package/dist/serializer.d.ts.map +1 -1
- package/dist/types/index.d.ts +172 -2
- package/package.json +3 -3
- package/src/codegen/naming.ts +15 -1
- package/src/composites/catalog.ts +105 -0
- package/src/composites/index.ts +2 -0
- package/src/composites/otel-collector-shape.ts +2 -0
- package/src/composites/otel-operator-collector.test.ts +267 -0
- package/src/composites/otel-operator-collector.ts +221 -0
- package/src/config-augmentation.test.ts +28 -0
- package/src/config-schema.ts +47 -1
- package/src/config.ts +44 -3
- package/src/crd/crd-sources.ts +41 -2
- package/src/crd/loader.ts +1 -1
- package/src/crd/parser.ts +9 -5
- package/src/crd/project-codegen.test.ts +239 -0
- package/src/crd/project-codegen.ts +327 -0
- package/src/generated/index.d.ts +172 -2
- package/src/generated/index.ts +303 -2
- package/src/generated/lexicon-k8s.json +2284 -370
- package/src/generated/operations.json +208 -0
- package/src/group-namespace.test.ts +4 -0
- package/src/group-namespace.ts +4 -0
- package/src/import/embedded.ts +26 -0
- package/src/import/operator-collector-roundtrip.test.ts +105 -0
- package/src/include/entity.ts +119 -0
- package/src/include/include.test.ts +345 -0
- package/src/include/resolve.ts +204 -0
- package/src/index.ts +25 -2
- package/src/kustomize/rendered-entity.ts +43 -1
- package/src/kustomize/root.test.ts +39 -0
- package/src/kustomize/root.ts +16 -30
- package/src/lint/audit-catalog.ts +6 -0
- package/src/lint/post-synth/crd-schema-helpers.ts +10 -2
- package/src/lint/post-synth/index.ts +12 -0
- package/src/lint/post-synth/monitoring-selection-helpers.ts +185 -0
- package/src/lint/post-synth/monitoring-selection.test.ts +316 -0
- package/src/lint/post-synth/otel-placement-helpers.ts +61 -1
- package/src/lint/post-synth/wk8601.ts +1 -1
- package/src/lint/post-synth/wk8603.ts +1 -1
- package/src/lint/post-synth/wk8604.ts +8 -2
- package/src/lint/post-synth/wk8605.ts +5 -3
- package/src/lint/post-synth/wk8606.ts +27 -0
- package/src/lint/post-synth/wk8701.ts +37 -0
- package/src/lint/post-synth/wk8702.ts +43 -0
- package/src/lint/post-synth/wk8703.ts +37 -0
- package/src/lint/post-synth/wk8704.ts +134 -0
- package/src/lint/post-synth/wk8705.ts +45 -0
- package/src/patch/json-patch.test.ts +91 -0
- package/src/patch/json-patch.ts +189 -0
- package/src/patch/rendered-patch.test.ts +143 -0
- package/src/patch/rendered-patch.ts +180 -0
- package/src/plugin.ts +16 -2
- package/src/project-kinds.ts +75 -0
- package/src/serializer.ts +10 -0
- package/src/testdata/include/bundle.yaml +45 -0
|
@@ -157,6 +157,45 @@ describe("renderKustomizeRoots", () => {
|
|
|
157
157
|
).rejects.toThrow(/no kustomization file.*kustomization\.yaml/);
|
|
158
158
|
});
|
|
159
159
|
|
|
160
|
+
test("an entry with patches applies them to the rendered documents (#3566)", async () => {
|
|
161
|
+
const { entities } = await renderKustomizeRoots({
|
|
162
|
+
projectRoot: fixtureRoot,
|
|
163
|
+
roots: [
|
|
164
|
+
{
|
|
165
|
+
path: "overlays/prod",
|
|
166
|
+
patches: [
|
|
167
|
+
{ kind: "Deployment", name: "prod-web", merge: { spec: { replicas: 5 } } },
|
|
168
|
+
{
|
|
169
|
+
kind: "Service",
|
|
170
|
+
name: "prod-web",
|
|
171
|
+
jsonPatch: [{ op: "add", path: "/metadata/labels", value: { tier: "edge" } }],
|
|
172
|
+
},
|
|
173
|
+
],
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
run: cannedRunner([]),
|
|
177
|
+
});
|
|
178
|
+
const props = (key: string) => (entities.get(key) as unknown as { props: Record<string, unknown> }).props;
|
|
179
|
+
expect((props("overlays/prod/deploymentProdWeb").spec as { replicas: number }).replicas).toBe(5);
|
|
180
|
+
expect((props("overlays/prod/serviceProdWeb").metadata as { labels: unknown }).labels).toEqual({ tier: "edge" });
|
|
181
|
+
// The provenance annotation is stamped after patching, so it survives.
|
|
182
|
+
expect(
|
|
183
|
+
(props("overlays/prod/serviceProdWeb").metadata as { annotations: Record<string, string> }).annotations[
|
|
184
|
+
KUSTOMIZE_ROOT_ANNOTATION
|
|
185
|
+
],
|
|
186
|
+
).toBe("overlays/prod");
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("a patch that matches no rendered document fails, naming the root", async () => {
|
|
190
|
+
await expect(
|
|
191
|
+
renderKustomizeRoots({
|
|
192
|
+
projectRoot: fixtureRoot,
|
|
193
|
+
roots: [{ path: "overlays/prod", patches: [{ kind: "Deployment", name: "web", merge: {} }] }],
|
|
194
|
+
run: cannedRunner([]),
|
|
195
|
+
}),
|
|
196
|
+
).rejects.toThrow(/kustomize root "overlays\/prod": patch 0 \(kind Deployment, name web\) matched no document/);
|
|
197
|
+
});
|
|
198
|
+
|
|
160
199
|
test("a rendered document without apiVersion/kind is skipped with a warning", async () => {
|
|
161
200
|
const { entities, warnings } = await renderKustomizeRoots({
|
|
162
201
|
projectRoot: fixtureRoot,
|
package/src/kustomize/root.ts
CHANGED
|
@@ -29,7 +29,14 @@ import { existsSync } from "node:fs";
|
|
|
29
29
|
import { isAbsolute, join, relative, resolve } from "node:path";
|
|
30
30
|
import type { Declarable } from "@intentius/chant/declarable";
|
|
31
31
|
import { defaultKustomizeRunner, renderKustomizeDocuments, type KustomizeRunner } from "./render";
|
|
32
|
-
import { renderedManifestEntity } from "./rendered-entity";
|
|
32
|
+
import { addRenderedEntity, renderedManifestEntity } from "./rendered-entity";
|
|
33
|
+
import { applyRenderedPatches, type RenderedPatch } from "../patch/rendered-patch";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* One `k8s.kustomize.roots` entry: a directory, or a directory plus patches
|
|
37
|
+
* applied to its rendered documents (#3566).
|
|
38
|
+
*/
|
|
39
|
+
export type KustomizeRootEntry = string | { path: string; patches?: RenderedPatch[] };
|
|
33
40
|
|
|
34
41
|
const KUSTOMIZATION_FILES = ["kustomization.yaml", "kustomization.yml", "Kustomization"];
|
|
35
42
|
|
|
@@ -38,18 +45,6 @@ export interface KustomizeRootsResult {
|
|
|
38
45
|
warnings: string[];
|
|
39
46
|
}
|
|
40
47
|
|
|
41
|
-
/** `Deployment` + `my-app` → `deploymentMyApp`, the import path's naming. */
|
|
42
|
-
function logicalId(kind: string, name: string | undefined): string {
|
|
43
|
-
const prefix = kind.charAt(0).toLowerCase() + kind.slice(1);
|
|
44
|
-
if (!name) return prefix;
|
|
45
|
-
const pascal = name
|
|
46
|
-
.split(/[^a-zA-Z0-9]+/)
|
|
47
|
-
.filter(Boolean)
|
|
48
|
-
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
49
|
-
.join("");
|
|
50
|
-
return `${prefix}${pascal}`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
48
|
/**
|
|
54
49
|
* Render each configured kustomization root into entities, keyed
|
|
55
50
|
* `<root>/<kindName>` (namespace-qualified, then numbered, on collision).
|
|
@@ -57,8 +52,8 @@ function logicalId(kind: string, name: string | undefined): string {
|
|
|
57
52
|
export async function renderKustomizeRoots(opts: {
|
|
58
53
|
/** Directory the project config was loaded from; relative roots resolve against it. */
|
|
59
54
|
projectRoot: string;
|
|
60
|
-
/** `k8s.kustomize.roots` — kustomization dirs, usually relative. */
|
|
61
|
-
roots: readonly
|
|
55
|
+
/** `k8s.kustomize.roots` — kustomization dirs, usually relative, optionally with patches. */
|
|
56
|
+
roots: readonly KustomizeRootEntry[];
|
|
62
57
|
/** Injectable renderer (tests); defaults to the real subprocess runner. */
|
|
63
58
|
run?: KustomizeRunner;
|
|
64
59
|
}): Promise<KustomizeRootsResult> {
|
|
@@ -66,7 +61,9 @@ export async function renderKustomizeRoots(opts: {
|
|
|
66
61
|
const entities = new Map<string, Declarable>();
|
|
67
62
|
const warnings: string[] = [];
|
|
68
63
|
|
|
69
|
-
for (const
|
|
64
|
+
for (const entry of opts.roots) {
|
|
65
|
+
const root = typeof entry === "string" ? entry : entry.path;
|
|
66
|
+
const patches = typeof entry === "string" ? undefined : entry.patches;
|
|
70
67
|
const rootDir = isAbsolute(root) ? root : resolve(opts.projectRoot, root);
|
|
71
68
|
// The label rendered docs carry as provenance: the declared (relative)
|
|
72
69
|
// form when possible, so it names the overlay the way the config does.
|
|
@@ -82,26 +79,15 @@ export async function renderKustomizeRoots(opts: {
|
|
|
82
79
|
);
|
|
83
80
|
}
|
|
84
81
|
|
|
85
|
-
const
|
|
82
|
+
const rendered = await renderKustomizeDocuments(rootDir, run);
|
|
83
|
+
const documents = applyRenderedPatches(rendered, patches, `kustomize root "${rootLabel}"`);
|
|
86
84
|
for (const doc of documents) {
|
|
87
85
|
const entity = renderedManifestEntity(doc, rootLabel);
|
|
88
86
|
if (!entity) {
|
|
89
87
|
warnings.push(`kustomize root "${rootLabel}" rendered a document without apiVersion/kind — skipped`);
|
|
90
88
|
continue;
|
|
91
89
|
}
|
|
92
|
-
|
|
93
|
-
const kind = entity.props.kind as string;
|
|
94
|
-
const name = typeof metadata.name === "string" ? metadata.name : undefined;
|
|
95
|
-
const namespace = typeof metadata.namespace === "string" ? metadata.namespace : undefined;
|
|
96
|
-
|
|
97
|
-
// Deterministic, human-readable keys. Two documents can share
|
|
98
|
-
// kind+name (different namespaces, or different API groups with the
|
|
99
|
-
// same kind word) — qualify by namespace first, then number.
|
|
100
|
-
const base = `${rootLabel}/${logicalId(kind, name)}`;
|
|
101
|
-
let key = base;
|
|
102
|
-
if (entities.has(key) && namespace) key = `${base}.${namespace}`;
|
|
103
|
-
for (let n = 2; entities.has(key); n++) key = `${base}~${n}`;
|
|
104
|
-
entities.set(key, entity);
|
|
90
|
+
addRenderedEntity(entities, rootLabel, entity);
|
|
105
91
|
}
|
|
106
92
|
}
|
|
107
93
|
|
|
@@ -51,6 +51,12 @@ export const k8sAuditCatalog: Record<string, RuleMeta> = {
|
|
|
51
51
|
WK8603: auditRule("WK8603", "merge-worthy", "guidance", "k8s_cluster receiver runs in every collector copy", "Run the k8s_cluster receiver in a single-replica Deployment, or set its k8s_leader_elector to a k8s_leader_elector extension enabled in service.extensions.", { category: "correctness" }),
|
|
52
52
|
WK8604: auditRule("WK8604", "merge-worthy", "guidance", "Collector config in a ConfigMap fails the otel config checks", "Fix the collector config the ConfigMap carries: each finding keeps its OTEL1xx id and names the ConfigMap and key. Change the declared components and pipelines, not the rendered YAML.", { category: "correctness" }),
|
|
53
53
|
WK8605: auditRule("WK8605", "merge-worthy", "guidance", "Collector config reads the node but its workload lacks the node name or host mounts", "Set the node name variable from spec.nodeName with the downward API and mount the host paths hostmetrics and filelog read, read-only; OtelCollector adds them from the config.", { category: "correctness" }),
|
|
54
|
+
WK8606: auditRule("WK8606", "merge-worthy", "guidance", "PrometheusRule groups fail the prometheus rule-file checks", "Fix the rule group the PrometheusRule carries: each finding keeps its PROM1xx or PROM2xx id and names the PrometheusRule. Change the declared RuleGroup, not the rendered YAML.", { category: "correctness" }),
|
|
55
|
+
WK8701: auditRule("WK8701", "merge-worthy", "guidance", "PrometheusRule selected by no Prometheus or ThanosRuler", "Match the rule's labels and namespace with ruleSelector and ruleNamespaceSelector; null selects nothing, {} selects all.", { category: "correctness" }),
|
|
56
|
+
WK8702: auditRule("WK8702", "merge-worthy", "guidance", "Monitor selected by no Prometheus or PrometheusAgent", "Match the ServiceMonitor, PodMonitor, Probe or ScrapeConfig with the selector and namespace selector for its kind; null selects nothing, {} selects all.", { category: "correctness" }),
|
|
57
|
+
WK8703: auditRule("WK8703", "merge-worthy", "guidance", "AlertmanagerConfig selected by no Alertmanager", "Match the config's labels and namespace with alertmanagerConfigSelector and alertmanagerConfigNamespaceSelector; null selects nothing, {} selects all.", { category: "correctness" }),
|
|
58
|
+
WK8704: auditRule("WK8704", "merge-worthy", "guidance", "Monitor selects no Service or pod, or names a port that is not there", "Make the monitor's selector match the labels on the Service or pod template, and name a port the Service or container declares.", { category: "correctness" }),
|
|
59
|
+
WK8705: auditRule("WK8705", "merge-worthy", "guidance", "Prometheus selects no monitor at all", "Set serviceMonitorSelector, podMonitorSelector, probeSelector and scrapeConfigSelector for the monitor kinds it should scrape; {} selects all in the selected namespaces.", { category: "correctness" }),
|
|
54
60
|
};
|
|
55
61
|
|
|
56
62
|
// Prior art credits live beside the rules in ./audit-lineage.ts (see core audit/prior-art.ts).
|
|
@@ -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
|
-
/**
|
|
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
|
|
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. */
|
|
@@ -45,6 +45,12 @@ import { wk8602 } from "./wk8602";
|
|
|
45
45
|
import { wk8603 } from "./wk8603";
|
|
46
46
|
import { wk8604 } from "./wk8604";
|
|
47
47
|
import { wk8605 } from "./wk8605";
|
|
48
|
+
import { wk8606 } from "./wk8606";
|
|
49
|
+
import { wk8701 } from "./wk8701";
|
|
50
|
+
import { wk8702 } from "./wk8702";
|
|
51
|
+
import { wk8703 } from "./wk8703";
|
|
52
|
+
import { wk8704 } from "./wk8704";
|
|
53
|
+
import { wk8705 } from "./wk8705";
|
|
48
54
|
|
|
49
55
|
export const postSynthChecks: PostSynthCheck[] = [
|
|
50
56
|
argo002,
|
|
@@ -92,4 +98,10 @@ export const postSynthChecks: PostSynthCheck[] = [
|
|
|
92
98
|
wk8603,
|
|
93
99
|
wk8604,
|
|
94
100
|
wk8605,
|
|
101
|
+
wk8606,
|
|
102
|
+
wk8701,
|
|
103
|
+
wk8702,
|
|
104
|
+
wk8703,
|
|
105
|
+
wk8704,
|
|
106
|
+
wk8705,
|
|
95
107
|
];
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared plumbing for the Prometheus Operator selection checks (WK8701-WK8705).
|
|
3
|
+
*
|
|
4
|
+
* An operator accepts a PrometheusRule, monitor or AlertmanagerConfig that no
|
|
5
|
+
* Prometheus or Alertmanager selects and then ignores it without an error.
|
|
6
|
+
* These helpers decide, from the manifests in one build, whether a selector
|
|
7
|
+
* picks an object, following the operator's rules for a label selector:
|
|
8
|
+
*
|
|
9
|
+
* - an object selector that is null (or absent) matches nothing, `{}` matches all;
|
|
10
|
+
* - a namespace selector that is null (or absent) matches only the selecting
|
|
11
|
+
* resource's own namespace, `{}` matches every namespace.
|
|
12
|
+
*
|
|
13
|
+
* Namespace labels come from the Namespace manifests in the build, plus the
|
|
14
|
+
* `kubernetes.io/metadata.name` label every namespace carries. A selector on a
|
|
15
|
+
* label the build cannot know (the namespace is not declared in it) answers
|
|
16
|
+
* "unknown", and the checks stay silent on "unknown".
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { docsToManifests, type K8sManifest } from "./k8s-helpers";
|
|
20
|
+
import type { PostSynthContext } from "@intentius/chant/lint/post-synth";
|
|
21
|
+
|
|
22
|
+
export const MONITORING_GROUP = "monitoring.coreos.com/";
|
|
23
|
+
|
|
24
|
+
export type Match = "yes" | "no" | "unknown";
|
|
25
|
+
|
|
26
|
+
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
27
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A manifest of a monitoring.coreos.com kind, whatever the version. */
|
|
31
|
+
export function ofMonitoringKind(m: K8sManifest, kind: string): boolean {
|
|
32
|
+
return m.kind === kind && typeof m.apiVersion === "string" && m.apiVersion.startsWith(MONITORING_GROUP);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function monitoringKind(manifests: K8sManifest[], kind: string): K8sManifest[] {
|
|
36
|
+
return manifests.filter((m) => ofMonitoringKind(m, kind));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function namespaceOf(m: K8sManifest): string {
|
|
40
|
+
return typeof m.metadata?.namespace === "string" && m.metadata.namespace !== "" ? m.metadata.namespace : "default";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function nameOf(m: K8sManifest): string {
|
|
44
|
+
return typeof m.metadata?.name === "string" ? m.metadata.name : "";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function describe(m: K8sManifest): string {
|
|
48
|
+
return `${m.kind} ${namespaceOf(m)}/${nameOf(m)}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether a label selector matches a label set. Null, undefined and anything
|
|
53
|
+
* that is not an object match nothing; `{}` matches everything.
|
|
54
|
+
* `unknownKeys` are label keys the caller cannot read (a namespace the build
|
|
55
|
+
* does not declare); a requirement on one answers "unknown".
|
|
56
|
+
*/
|
|
57
|
+
export function matchLabelSelector(selector: unknown, labels: Record<string, string>, unknownKeys: ReadonlySet<string> = new Set()): Match {
|
|
58
|
+
if (!isRecord(selector)) return "no";
|
|
59
|
+
let unknown = false;
|
|
60
|
+
const matchLabels = isRecord(selector.matchLabels) ? selector.matchLabels : {};
|
|
61
|
+
for (const [k, v] of Object.entries(matchLabels)) {
|
|
62
|
+
if (unknownKeys.has(k)) {
|
|
63
|
+
unknown = true;
|
|
64
|
+
} else if (labels[k] !== String(v)) {
|
|
65
|
+
return "no";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const exprs = Array.isArray(selector.matchExpressions) ? selector.matchExpressions : [];
|
|
69
|
+
for (const e of exprs) {
|
|
70
|
+
if (!isRecord(e) || typeof e.key !== "string") continue;
|
|
71
|
+
if (unknownKeys.has(e.key)) {
|
|
72
|
+
unknown = true;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const values = Array.isArray(e.values) ? e.values.map(String) : [];
|
|
76
|
+
const has = Object.prototype.hasOwnProperty.call(labels, e.key);
|
|
77
|
+
switch (e.operator) {
|
|
78
|
+
case "In":
|
|
79
|
+
if (!has || !values.includes(labels[e.key])) return "no";
|
|
80
|
+
break;
|
|
81
|
+
case "NotIn":
|
|
82
|
+
if (has && values.includes(labels[e.key])) return "no";
|
|
83
|
+
break;
|
|
84
|
+
case "Exists":
|
|
85
|
+
if (!has) return "no";
|
|
86
|
+
break;
|
|
87
|
+
case "DoesNotExist":
|
|
88
|
+
if (has) return "no";
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return unknown ? "unknown" : "yes";
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function labelsOf(m: K8sManifest): Record<string, string> {
|
|
96
|
+
const out: Record<string, string> = {};
|
|
97
|
+
const labels = m.metadata?.labels;
|
|
98
|
+
if (isRecord(labels)) for (const [k, v] of Object.entries(labels)) out[k] = String(v);
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The namespaces a build declares, with their labels. */
|
|
103
|
+
function declaredNamespaces(manifests: K8sManifest[]): Map<string, Record<string, string>> {
|
|
104
|
+
const out = new Map<string, Record<string, string>>();
|
|
105
|
+
for (const m of manifests) {
|
|
106
|
+
if (m.kind === "Namespace" && (m.apiVersion === "v1" || m.apiVersion === undefined) && nameOf(m)) {
|
|
107
|
+
out.set(nameOf(m), { ...labelsOf(m), "kubernetes.io/metadata.name": nameOf(m) });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Whether a selecting resource's namespace selector picks `targetNamespace`.
|
|
115
|
+
* Null or absent: the selecting resource's own namespace only.
|
|
116
|
+
*/
|
|
117
|
+
export function matchNamespaceSelector(
|
|
118
|
+
selector: unknown,
|
|
119
|
+
ownNamespace: string,
|
|
120
|
+
targetNamespace: string,
|
|
121
|
+
namespaces: Map<string, Record<string, string>>,
|
|
122
|
+
): Match {
|
|
123
|
+
if (!isRecord(selector)) return ownNamespace === targetNamespace ? "yes" : "no";
|
|
124
|
+
const declared = namespaces.get(targetNamespace);
|
|
125
|
+
const labels = declared ?? { "kubernetes.io/metadata.name": targetNamespace };
|
|
126
|
+
// A namespace the build does not declare has labels the build cannot know, except the name label.
|
|
127
|
+
const unknownKeys = new Set<string>();
|
|
128
|
+
if (!declared) {
|
|
129
|
+
const keys = [
|
|
130
|
+
...Object.keys(isRecord(selector.matchLabels) ? selector.matchLabels : {}),
|
|
131
|
+
...(Array.isArray(selector.matchExpressions) ? selector.matchExpressions : []).map((e) => (isRecord(e) ? String(e.key) : "")),
|
|
132
|
+
];
|
|
133
|
+
for (const k of keys) if (k !== "kubernetes.io/metadata.name") unknownKeys.add(k);
|
|
134
|
+
}
|
|
135
|
+
return matchLabelSelector(selector, labels, unknownKeys);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** A selecting resource's reach over one object: its object selector, then its namespace selector. */
|
|
139
|
+
export function selects(
|
|
140
|
+
selector: unknown,
|
|
141
|
+
namespaceSelector: unknown,
|
|
142
|
+
selecting: K8sManifest,
|
|
143
|
+
object: K8sManifest,
|
|
144
|
+
namespaces: Map<string, Record<string, string>>,
|
|
145
|
+
): Match {
|
|
146
|
+
const ns = matchNamespaceSelector(namespaceSelector, namespaceOf(selecting), namespaceOf(object), namespaces);
|
|
147
|
+
if (ns === "no") return "no";
|
|
148
|
+
const obj = matchLabelSelector(selector, labelsOf(object));
|
|
149
|
+
if (obj === "no") return "no";
|
|
150
|
+
return ns === "unknown" || obj === "unknown" ? "unknown" : "yes";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface SelectionContext {
|
|
154
|
+
manifests: K8sManifest[];
|
|
155
|
+
namespaces: Map<string, Record<string, string>>;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function selectionContext(ctx: PostSynthContext): SelectionContext {
|
|
159
|
+
const manifests = docsToManifests(ctx);
|
|
160
|
+
return { manifests, namespaces: declaredNamespaces(manifests) };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Objects of `kinds` that no resource in `selectors` selects, by the named
|
|
165
|
+
* object selector and namespace selector fields of each selector's spec.
|
|
166
|
+
* Silent (empty) when `selectors` is empty, since the stack is often installed
|
|
167
|
+
* separately. A selector that answers "unknown" counts as selecting.
|
|
168
|
+
*/
|
|
169
|
+
export function unselected(
|
|
170
|
+
sc: SelectionContext,
|
|
171
|
+
objects: K8sManifest[],
|
|
172
|
+
selectors: K8sManifest[],
|
|
173
|
+
fields: (selecting: K8sManifest, object: K8sManifest) => [string, string] | null,
|
|
174
|
+
): K8sManifest[] {
|
|
175
|
+
if (selectors.length === 0) return [];
|
|
176
|
+
return objects.filter((object) => {
|
|
177
|
+
for (const sel of selectors) {
|
|
178
|
+
const f = fields(sel, object);
|
|
179
|
+
if (!f) continue;
|
|
180
|
+
const spec = (sel.spec ?? {}) as Record<string, unknown>;
|
|
181
|
+
if (selects(spec[f[0]], spec[f[1]], sel, object, sc.namespaces) !== "no") return false;
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
});
|
|
185
|
+
}
|