@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
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WK8703: an AlertmanagerConfig that no Alertmanager selects
|
|
3
|
+
*
|
|
4
|
+
* An Alertmanager merges the AlertmanagerConfigs whose labels match its `alertmanagerConfigSelector` in the
|
|
5
|
+
* namespaces its `alertmanagerConfigNamespaceSelector` picks. One neither selects is accepted and then
|
|
6
|
+
* never routes an alert.
|
|
7
|
+
*
|
|
8
|
+
* Selection is worked out from the manifests in one build root (chant #1939),
|
|
9
|
+
* by the operator's rules: a null object selector matches nothing, `{}`
|
|
10
|
+
* matches all; a null namespace selector matches only the selecting resource's
|
|
11
|
+
* own namespace, `{}` every namespace. Silent when the build has no Alertmanager, since the
|
|
12
|
+
* stack is often installed separately. A selector on a namespace label the
|
|
13
|
+
* build cannot read (the Namespace is not declared in it) counts as selecting.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { PostSynthCheck, PostSynthContext, PostSynthDiagnostic } from "@intentius/chant/lint/post-synth";
|
|
17
|
+
import { describe, monitoringKind, nameOf, selectionContext, unselected } from "./monitoring-selection-helpers";
|
|
18
|
+
|
|
19
|
+
export const wk8703: PostSynthCheck = {
|
|
20
|
+
id: "WK8703",
|
|
21
|
+
description:
|
|
22
|
+
"An AlertmanagerConfig that no Alertmanager in the build selects through alertmanagerConfigSelector and alertmanagerConfigNamespaceSelector never routes an alert. A null selector matches nothing, {} matches all. Silent when the build has no Alertmanager.",
|
|
23
|
+
|
|
24
|
+
check(ctx: PostSynthContext): PostSynthDiagnostic[] {
|
|
25
|
+
const sc = selectionContext(ctx);
|
|
26
|
+
const configs = monitoringKind(sc.manifests, "AlertmanagerConfig");
|
|
27
|
+
return unselected(sc, configs, monitoringKind(sc.manifests, "Alertmanager"), () => ["alertmanagerConfigSelector", "alertmanagerConfigNamespaceSelector"]).map((c) => ({
|
|
28
|
+
checkId: "WK8703",
|
|
29
|
+
severity: "warning",
|
|
30
|
+
message:
|
|
31
|
+
`${describe(c)} is selected by no Alertmanager in this build, so its routes and receivers are never used. ` +
|
|
32
|
+
`Match its labels (and namespace) with alertmanagerConfigSelector and alertmanagerConfigNamespaceSelector: null selects nothing, {} selects all.`,
|
|
33
|
+
entity: nameOf(c),
|
|
34
|
+
lexicon: "k8s",
|
|
35
|
+
}));
|
|
36
|
+
},
|
|
37
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WK8704: a ServiceMonitor or PodMonitor that selects no Service or pod, or names a port that is not there
|
|
3
|
+
*
|
|
4
|
+
* A ServiceMonitor's `selector` picks Services (in its own namespace, the
|
|
5
|
+
* `namespaceSelector.matchNames` ones, or every namespace with `any: true`),
|
|
6
|
+
* and each `endpoints[].port` names a port on them. A PodMonitor's `selector`
|
|
7
|
+
* picks pods, and each `podMetricsEndpoints[].port` names a container port.
|
|
8
|
+
* When nothing matches, or the port name is on none of the matches, Prometheus
|
|
9
|
+
* scrapes nothing and says nothing. This check needs no Prometheus in the
|
|
10
|
+
* build: it reads the monitor against the Services and workloads declared
|
|
11
|
+
* beside it.
|
|
12
|
+
*
|
|
13
|
+
* Pods come from the pod templates of Deployment, StatefulSet, DaemonSet, Job
|
|
14
|
+
* and CronJob, and from Pods themselves. An empty `selector` ({}) matches
|
|
15
|
+
* everything, as the operator does. Only a port given by name is checked;
|
|
16
|
+
* `targetPort` and `portNumber` are left alone. One build root at a time
|
|
17
|
+
* (chant #1939): a Service or workload installed from another root is not seen.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { PostSynthCheck, PostSynthContext, PostSynthDiagnostic } from "@intentius/chant/lint/post-synth";
|
|
21
|
+
import { extractContainers, type K8sManifest } from "./k8s-helpers";
|
|
22
|
+
import { describe, labelsOf, matchLabelSelector, monitoringKind, nameOf, namespaceOf, selectionContext } from "./monitoring-selection-helpers";
|
|
23
|
+
|
|
24
|
+
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
25
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Whether a monitor looks in this namespace: its own, `matchNames`, or all of them with `any`. */
|
|
29
|
+
function reaches(monitor: K8sManifest, namespace: string): boolean {
|
|
30
|
+
const ns = (monitor.spec as Record<string, unknown> | undefined)?.namespaceSelector;
|
|
31
|
+
if (isRecord(ns)) {
|
|
32
|
+
if (ns.any === true) return true;
|
|
33
|
+
if (Array.isArray(ns.matchNames) && ns.matchNames.length > 0) return ns.matchNames.includes(namespace);
|
|
34
|
+
}
|
|
35
|
+
return namespaceOf(monitor) === namespace;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The labels a pod from this workload carries, and its manifest to read ports from. */
|
|
39
|
+
function podsOf(manifests: K8sManifest[]): Array<{ manifest: K8sManifest; labels: Record<string, string> }> {
|
|
40
|
+
const out: Array<{ manifest: K8sManifest; labels: Record<string, string> }> = [];
|
|
41
|
+
for (const m of manifests) {
|
|
42
|
+
const spec = (m.spec ?? {}) as Record<string, any>;
|
|
43
|
+
if (m.kind === "Pod") {
|
|
44
|
+
out.push({ manifest: m, labels: labelsOf(m) });
|
|
45
|
+
} else if (m.kind === "Deployment" || m.kind === "StatefulSet" || m.kind === "DaemonSet" || m.kind === "Job") {
|
|
46
|
+
out.push({ manifest: m, labels: labelsOf({ metadata: spec.template?.metadata } as K8sManifest) });
|
|
47
|
+
} else if (m.kind === "CronJob") {
|
|
48
|
+
out.push({ manifest: m, labels: labelsOf({ metadata: spec.jobTemplate?.spec?.template?.metadata } as K8sManifest) });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function containerPortNames(workload: K8sManifest): Set<string> {
|
|
55
|
+
const names = new Set<string>();
|
|
56
|
+
for (const c of extractContainers(workload)) {
|
|
57
|
+
for (const p of Array.isArray(c.ports) ? c.ports : []) {
|
|
58
|
+
if (isRecord(p) && typeof p.name === "string") names.add(p.name);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return names;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function servicePortNames(service: K8sManifest): Set<string> {
|
|
65
|
+
const names = new Set<string>();
|
|
66
|
+
const ports = (service.spec as Record<string, unknown> | undefined)?.ports;
|
|
67
|
+
for (const p of Array.isArray(ports) ? ports : []) {
|
|
68
|
+
if (isRecord(p) && typeof p.name === "string") names.add(p.name);
|
|
69
|
+
}
|
|
70
|
+
return names;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const wk8704: PostSynthCheck = {
|
|
74
|
+
id: "WK8704",
|
|
75
|
+
description:
|
|
76
|
+
"A ServiceMonitor whose selector matches no Service in the build, or whose endpoints[].port names no port on the matched Services; the same for a PodMonitor against pod container ports. Needs no Prometheus in the build. Sees one build root at a time (chant #1939).",
|
|
77
|
+
|
|
78
|
+
check(ctx: PostSynthContext): PostSynthDiagnostic[] {
|
|
79
|
+
const { manifests } = selectionContext(ctx);
|
|
80
|
+
const out: PostSynthDiagnostic[] = [];
|
|
81
|
+
const diag = (m: K8sManifest, message: string): PostSynthDiagnostic => ({
|
|
82
|
+
checkId: "WK8704",
|
|
83
|
+
severity: "warning",
|
|
84
|
+
message,
|
|
85
|
+
entity: nameOf(m),
|
|
86
|
+
lexicon: "k8s",
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const services = manifests.filter((m) => m.kind === "Service" && (m.apiVersion === "v1" || m.apiVersion === undefined));
|
|
90
|
+
for (const sm of monitoringKind(manifests, "ServiceMonitor")) {
|
|
91
|
+
const spec = (sm.spec ?? {}) as Record<string, unknown>;
|
|
92
|
+
// A ServiceMonitor's selector is required; treat a missing one like {}.
|
|
93
|
+
const selector = isRecord(spec.selector) ? spec.selector : {};
|
|
94
|
+
const matched = services.filter((s) => reaches(sm, namespaceOf(s)) && matchLabelSelector(selector, labelsOf(s)) !== "no");
|
|
95
|
+
if (matched.length === 0) {
|
|
96
|
+
out.push(diag(sm, `${describe(sm)} has a selector that matches no Service in this build (looking in ${where(sm)}), so Prometheus scrapes nothing for it. Make spec.selector match the labels on the Service, or fix namespaceSelector.`));
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const known = new Set<string>();
|
|
100
|
+
for (const s of matched) for (const n of servicePortNames(s)) known.add(n);
|
|
101
|
+
for (const e of Array.isArray(spec.endpoints) ? spec.endpoints : []) {
|
|
102
|
+
if (!isRecord(e) || typeof e.port !== "string" || known.has(e.port)) continue;
|
|
103
|
+
out.push(diag(sm, `${describe(sm)} scrapes endpoint port "${e.port}", but the Service(s) it selects (${matched.map(nameOf).join(", ")}) declare no port with that name${known.size > 0 ? ` (they have: ${[...known].join(", ")})` : ""}. Name the Service port, or point the endpoint at an existing one.`));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const pods = podsOf(manifests);
|
|
108
|
+
for (const pm of monitoringKind(manifests, "PodMonitor")) {
|
|
109
|
+
const spec = (pm.spec ?? {}) as Record<string, unknown>;
|
|
110
|
+
const selector = isRecord(spec.selector) ? spec.selector : {};
|
|
111
|
+
const matched = pods.filter((p) => reaches(pm, namespaceOf(p.manifest)) && matchLabelSelector(selector, p.labels) !== "no");
|
|
112
|
+
if (matched.length === 0) {
|
|
113
|
+
out.push(diag(pm, `${describe(pm)} has a selector that matches no pod in this build (looking in ${where(pm)}), so Prometheus scrapes nothing for it. Make spec.selector match the pod template labels, or fix namespaceSelector.`));
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const known = new Set<string>();
|
|
117
|
+
for (const p of matched) for (const n of containerPortNames(p.manifest)) known.add(n);
|
|
118
|
+
for (const e of Array.isArray(spec.podMetricsEndpoints) ? spec.podMetricsEndpoints : []) {
|
|
119
|
+
if (!isRecord(e) || typeof e.port !== "string" || known.has(e.port)) continue;
|
|
120
|
+
out.push(diag(pm, `${describe(pm)} scrapes container port "${e.port}", but the pods it selects (${matched.map((p) => nameOf(p.manifest)).join(", ")}) declare no container port with that name${known.size > 0 ? ` (they have: ${[...known].join(", ")})` : ""}. Name the container port, or point the endpoint at an existing one.`));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
function where(m: K8sManifest): string {
|
|
128
|
+
const ns = (m.spec as Record<string, unknown> | undefined)?.namespaceSelector;
|
|
129
|
+
if (isRecord(ns)) {
|
|
130
|
+
if (ns.any === true) return "every namespace";
|
|
131
|
+
if (Array.isArray(ns.matchNames) && ns.matchNames.length > 0) return `namespace ${ns.matchNames.join(", ")}`;
|
|
132
|
+
}
|
|
133
|
+
return `namespace ${namespaceOf(m)}`;
|
|
134
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WK8705: a Prometheus whose four monitor selectors are all null
|
|
3
|
+
*
|
|
4
|
+
* A Prometheus picks the ServiceMonitors, PodMonitors, Probes and
|
|
5
|
+
* ScrapeConfigs to scrape with `serviceMonitorSelector`, `podMonitorSelector`,
|
|
6
|
+
* `probeSelector` and `scrapeConfigSelector`. A null selector (absent counts
|
|
7
|
+
* as null) matches nothing, so a Prometheus with all four null selects no
|
|
8
|
+
* monitor at all and scrapes only what `additionalScrapeConfigs` and its own
|
|
9
|
+
* config hold. `{}` matches every monitor in the selected namespaces. This is
|
|
10
|
+
* the usual way a fresh Prometheus ends up scraping nothing.
|
|
11
|
+
*
|
|
12
|
+
* A Prometheus with `additionalScrapeConfigs` set still reports: that
|
|
13
|
+
* is a deliberate setup the finding names, not one it hides. The message says
|
|
14
|
+
* so, and a suppression is the way to accept it.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { PostSynthCheck, PostSynthContext, PostSynthDiagnostic } from "@intentius/chant/lint/post-synth";
|
|
18
|
+
import { describe, monitoringKind, nameOf, selectionContext } from "./monitoring-selection-helpers";
|
|
19
|
+
|
|
20
|
+
const SELECTORS = ["serviceMonitorSelector", "podMonitorSelector", "probeSelector", "scrapeConfigSelector"] as const;
|
|
21
|
+
|
|
22
|
+
export const wk8705: PostSynthCheck = {
|
|
23
|
+
id: "WK8705",
|
|
24
|
+
description:
|
|
25
|
+
"A Prometheus whose serviceMonitorSelector, podMonitorSelector, probeSelector and scrapeConfigSelector are all null selects no monitor, so the operator scrapes nothing from ServiceMonitors, PodMonitors, Probes or ScrapeConfigs.",
|
|
26
|
+
|
|
27
|
+
check(ctx: PostSynthContext): PostSynthDiagnostic[] {
|
|
28
|
+
const { manifests } = selectionContext(ctx);
|
|
29
|
+
return monitoringKind(manifests, "Prometheus")
|
|
30
|
+
.filter((p) => {
|
|
31
|
+
const spec = (p.spec ?? {}) as Record<string, unknown>;
|
|
32
|
+
return SELECTORS.every((k) => spec[k] === null || spec[k] === undefined);
|
|
33
|
+
})
|
|
34
|
+
.map((p) => ({
|
|
35
|
+
checkId: "WK8705",
|
|
36
|
+
severity: "warning" as const,
|
|
37
|
+
message:
|
|
38
|
+
`${describe(p)} has ${SELECTORS.join(", ")} all null, so it selects no ServiceMonitor, PodMonitor, Probe or ScrapeConfig and scrapes only its own config` +
|
|
39
|
+
`${(p.spec as Record<string, unknown> | undefined)?.additionalScrapeConfigs ? " and additionalScrapeConfigs" : ""}. ` +
|
|
40
|
+
`Set the selector for each monitor kind it should pick up ({} selects all in the selected namespaces).`,
|
|
41
|
+
entity: nameOf(p),
|
|
42
|
+
lexicon: "k8s",
|
|
43
|
+
}));
|
|
44
|
+
},
|
|
45
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { describe, test, expect } from "vitest";
|
|
2
|
+
import { applyJsonPatch, applyMergePatch } from "./json-patch";
|
|
3
|
+
|
|
4
|
+
const doc = () => ({
|
|
5
|
+
metadata: { name: "web", labels: { app: "web" } },
|
|
6
|
+
spec: { replicas: 1, containers: [{ name: "a" }, { name: "b" }] },
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
describe("applyJsonPatch", () => {
|
|
10
|
+
test("add sets a member, inserts into an array, and appends with -", () => {
|
|
11
|
+
const out = applyJsonPatch(doc(), [
|
|
12
|
+
{ op: "add", path: "/metadata/labels/tier", value: "frontend" },
|
|
13
|
+
{ op: "add", path: "/spec/containers/1", value: { name: "mid" } },
|
|
14
|
+
{ op: "add", path: "/spec/containers/-", value: { name: "last" } },
|
|
15
|
+
]);
|
|
16
|
+
expect(out.metadata.labels).toEqual({ app: "web", tier: "frontend" });
|
|
17
|
+
expect(out.spec.containers.map((c) => c.name)).toEqual(["a", "mid", "b", "last"]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("replace changes an existing value and refuses a missing one", () => {
|
|
21
|
+
expect(applyJsonPatch(doc(), [{ op: "replace", path: "/spec/replicas", value: 3 }]).spec.replicas).toBe(3);
|
|
22
|
+
expect(() => applyJsonPatch(doc(), [{ op: "replace", path: "/spec/paused", value: true }])).toThrow(
|
|
23
|
+
/operation 0 \(replace\): path \/spec\/paused does not exist/,
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("remove deletes a member or an array element and refuses a missing path", () => {
|
|
28
|
+
const out = applyJsonPatch(doc(), [
|
|
29
|
+
{ op: "remove", path: "/metadata/labels/app" },
|
|
30
|
+
{ op: "remove", path: "/spec/containers/0" },
|
|
31
|
+
]);
|
|
32
|
+
expect(out.metadata.labels).toEqual({});
|
|
33
|
+
expect(out.spec.containers).toEqual([{ name: "b" }]);
|
|
34
|
+
expect(() => applyJsonPatch(doc(), [{ op: "remove", path: "/spec/containers/5" }])).toThrow(/out of bounds/);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("test passes on a deep-equal value and fails with both values in the message", () => {
|
|
38
|
+
expect(() =>
|
|
39
|
+
applyJsonPatch(doc(), [{ op: "test", path: "/spec/containers/0", value: { name: "a" } }]),
|
|
40
|
+
).not.toThrow();
|
|
41
|
+
expect(() => applyJsonPatch(doc(), [{ op: "test", path: "/spec/replicas", value: 2 }])).toThrow(
|
|
42
|
+
/test failed at \/spec\/replicas: expected 2, found 1/,
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("move and copy", () => {
|
|
47
|
+
const out = applyJsonPatch(doc(), [
|
|
48
|
+
{ op: "copy", from: "/metadata/labels", path: "/spec/selector" },
|
|
49
|
+
{ op: "move", from: "/spec/replicas", path: "/spec/count" },
|
|
50
|
+
]);
|
|
51
|
+
expect(out).toMatchObject({ spec: { selector: { app: "web" }, count: 1 } });
|
|
52
|
+
expect((out.spec as Record<string, unknown>).replicas).toBeUndefined();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("pointer escapes ~0 and ~1 address keys holding ~ and /", () => {
|
|
56
|
+
const out = applyJsonPatch({ metadata: { annotations: {} as Record<string, string> } }, [
|
|
57
|
+
{ op: "add", path: "/metadata/annotations/example.com~1a~0b", value: "x" },
|
|
58
|
+
]);
|
|
59
|
+
expect(out.metadata.annotations).toEqual({ "example.com/a~b": "x" });
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("never mutates its input, even when a later operation fails", () => {
|
|
63
|
+
const input = doc();
|
|
64
|
+
expect(() =>
|
|
65
|
+
applyJsonPatch(input, [
|
|
66
|
+
{ op: "replace", path: "/spec/replicas", value: 9 },
|
|
67
|
+
{ op: "test", path: "/spec/replicas", value: 1 },
|
|
68
|
+
]),
|
|
69
|
+
).toThrow(/operation 1 \(test\)/);
|
|
70
|
+
expect(input).toEqual(doc());
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("applyMergePatch (RFC 7386)", () => {
|
|
75
|
+
test("merges objects, replaces arrays, deletes on null", () => {
|
|
76
|
+
const out = applyMergePatch(doc(), {
|
|
77
|
+
metadata: { labels: { app: null, tier: "frontend" } },
|
|
78
|
+
spec: { containers: [{ name: "only" }] },
|
|
79
|
+
});
|
|
80
|
+
expect(out.metadata.labels).toEqual({ tier: "frontend" });
|
|
81
|
+
expect(out.spec.containers).toEqual([{ name: "only" }]);
|
|
82
|
+
expect(out.spec.replicas).toBe(1);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("creates missing intermediate objects and leaves the input alone", () => {
|
|
86
|
+
const input = doc();
|
|
87
|
+
const out = applyMergePatch(input, { spec: { strategy: { type: "Recreate" } } });
|
|
88
|
+
expect(out).toMatchObject({ spec: { strategy: { type: "Recreate" } } });
|
|
89
|
+
expect(input).toEqual(doc());
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC 6902 JSON Patch and RFC 7386 JSON Merge Patch, for rendered documents.
|
|
3
|
+
*
|
|
4
|
+
* Core carries an RFC 6902 applier for cfn-lint codegen
|
|
5
|
+
* (`packages/core/src/codegen/json-patch.ts`), but it skips `test`, `move`
|
|
6
|
+
* and `copy`, and it works on JSON strings. A patch on a rendered manifest
|
|
7
|
+
* needs `test` most of all: it is how an author pins the value a chart
|
|
8
|
+
* upgrade might change underneath them. So this module is a small, complete
|
|
9
|
+
* applier over plain objects, with no imports, usable from the build path and
|
|
10
|
+
* from the helm lexicon's `HelmRender`.
|
|
11
|
+
*
|
|
12
|
+
* Both appliers return a new document and never mutate their input.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** One RFC 6902 operation. `path` and `from` are RFC 6901 JSON Pointers. */
|
|
16
|
+
export type JsonPatchOperation =
|
|
17
|
+
| { op: "add"; path: string; value: unknown }
|
|
18
|
+
| { op: "remove"; path: string }
|
|
19
|
+
| { op: "replace"; path: string; value: unknown }
|
|
20
|
+
| { op: "move"; from: string; path: string }
|
|
21
|
+
| { op: "copy"; from: string; path: string }
|
|
22
|
+
| { op: "test"; path: string; value: unknown };
|
|
23
|
+
|
|
24
|
+
type Container = Record<string, unknown> | unknown[];
|
|
25
|
+
|
|
26
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
27
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function clone<T>(value: T): T {
|
|
31
|
+
return value === undefined ? value : (JSON.parse(JSON.stringify(value)) as T);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** "" -> [], "/a/b~1c" -> ["a", "b/c"]. */
|
|
35
|
+
function parsePointer(pointer: string): string[] {
|
|
36
|
+
if (pointer === "") return [];
|
|
37
|
+
if (!pointer.startsWith("/")) throw new Error(`"${pointer}" is not a JSON Pointer (it must start with "/")`);
|
|
38
|
+
return pointer
|
|
39
|
+
.slice(1)
|
|
40
|
+
.split("/")
|
|
41
|
+
.map((t) => t.replace(/~1/g, "/").replace(/~0/g, "~"));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function arrayIndex(arr: unknown[], token: string, pointer: string, allowEnd: boolean): number {
|
|
45
|
+
if (!/^(0|[1-9][0-9]*)$/.test(token)) throw new Error(`"${token}" in ${pointer} is not an array index`);
|
|
46
|
+
const idx = Number(token);
|
|
47
|
+
const max = allowEnd ? arr.length : arr.length - 1;
|
|
48
|
+
if (idx > max) throw new Error(`index ${idx} in ${pointer} is out of bounds (array length ${arr.length})`);
|
|
49
|
+
return idx;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function get(root: unknown, pointer: string): unknown {
|
|
53
|
+
let current = root;
|
|
54
|
+
for (const token of parsePointer(pointer)) {
|
|
55
|
+
if (Array.isArray(current)) {
|
|
56
|
+
current = current[arrayIndex(current, token, pointer, false)];
|
|
57
|
+
} else if (isObject(current) && Object.prototype.hasOwnProperty.call(current, token)) {
|
|
58
|
+
current = current[token];
|
|
59
|
+
} else {
|
|
60
|
+
throw new Error(`path ${pointer} does not exist`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return current;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The container a pointer's last token addresses, and that token. */
|
|
67
|
+
function parentOf(root: unknown, pointer: string): { parent: Container; token: string } {
|
|
68
|
+
const tokens = parsePointer(pointer);
|
|
69
|
+
if (tokens.length === 0) throw new Error("the root cannot be addressed as a member");
|
|
70
|
+
const token = tokens.pop()!;
|
|
71
|
+
const parentPointer = tokens.length === 0 ? "" : "/" + tokens.map((t) => t.replace(/~/g, "~0").replace(/\//g, "~1")).join("/");
|
|
72
|
+
const parent = get(root, parentPointer);
|
|
73
|
+
if (!Array.isArray(parent) && !isObject(parent)) {
|
|
74
|
+
throw new Error(`path ${pointer} does not exist (its parent is not an object or array)`);
|
|
75
|
+
}
|
|
76
|
+
return { parent, token };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function add(root: unknown, pointer: string, value: unknown): unknown {
|
|
80
|
+
if (pointer === "") return value;
|
|
81
|
+
const { parent, token } = parentOf(root, pointer);
|
|
82
|
+
if (Array.isArray(parent)) {
|
|
83
|
+
if (token === "-") parent.push(value);
|
|
84
|
+
else parent.splice(arrayIndex(parent, token, pointer, true), 0, value);
|
|
85
|
+
} else {
|
|
86
|
+
parent[token] = value;
|
|
87
|
+
}
|
|
88
|
+
return root;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function remove(root: unknown, pointer: string): unknown {
|
|
92
|
+
if (pointer === "") throw new Error("the document root cannot be removed");
|
|
93
|
+
const { parent, token } = parentOf(root, pointer);
|
|
94
|
+
if (Array.isArray(parent)) {
|
|
95
|
+
parent.splice(arrayIndex(parent, token, pointer, false), 1);
|
|
96
|
+
} else {
|
|
97
|
+
if (!Object.prototype.hasOwnProperty.call(parent, token)) throw new Error(`path ${pointer} does not exist`);
|
|
98
|
+
delete parent[token];
|
|
99
|
+
}
|
|
100
|
+
return root;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function replace(root: unknown, pointer: string, value: unknown): unknown {
|
|
104
|
+
if (pointer === "") return value;
|
|
105
|
+
get(root, pointer); // must exist
|
|
106
|
+
const { parent, token } = parentOf(root, pointer);
|
|
107
|
+
if (Array.isArray(parent)) parent[arrayIndex(parent, token, pointer, false)] = value;
|
|
108
|
+
else parent[token] = value;
|
|
109
|
+
return root;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function deepEqual(a: unknown, b: unknown): boolean {
|
|
113
|
+
if (a === b) return true;
|
|
114
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
115
|
+
return a.length === b.length && a.every((v, i) => deepEqual(v, b[i]));
|
|
116
|
+
}
|
|
117
|
+
if (isObject(a) && isObject(b)) {
|
|
118
|
+
const ka = Object.keys(a);
|
|
119
|
+
const kb = Object.keys(b);
|
|
120
|
+
return ka.length === kb.length && ka.every((k) => Object.prototype.hasOwnProperty.call(b, k) && deepEqual(a[k], b[k]));
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Apply an RFC 6902 patch. Operations apply in order; the first one that
|
|
127
|
+
* fails (a missing path, a failed `test`) throws with its index, and the
|
|
128
|
+
* input is left untouched.
|
|
129
|
+
*/
|
|
130
|
+
export function applyJsonPatch<T>(doc: T, operations: readonly JsonPatchOperation[]): T {
|
|
131
|
+
let root: unknown = clone(doc);
|
|
132
|
+
operations.forEach((operation, i) => {
|
|
133
|
+
try {
|
|
134
|
+
switch (operation.op) {
|
|
135
|
+
case "add":
|
|
136
|
+
root = add(root, operation.path, clone(operation.value));
|
|
137
|
+
break;
|
|
138
|
+
case "remove":
|
|
139
|
+
root = remove(root, operation.path);
|
|
140
|
+
break;
|
|
141
|
+
case "replace":
|
|
142
|
+
root = replace(root, operation.path, clone(operation.value));
|
|
143
|
+
break;
|
|
144
|
+
case "move": {
|
|
145
|
+
if (operation.path.startsWith(operation.from + "/")) {
|
|
146
|
+
throw new Error(`cannot move ${operation.from} into its own child ${operation.path}`);
|
|
147
|
+
}
|
|
148
|
+
const value = get(root, operation.from);
|
|
149
|
+
root = remove(root, operation.from);
|
|
150
|
+
root = add(root, operation.path, value);
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case "copy":
|
|
154
|
+
root = add(root, operation.path, clone(get(root, operation.from)));
|
|
155
|
+
break;
|
|
156
|
+
case "test": {
|
|
157
|
+
const actual = get(root, operation.path);
|
|
158
|
+
if (!deepEqual(actual, operation.value)) {
|
|
159
|
+
throw new Error(
|
|
160
|
+
`test failed at ${operation.path}: expected ${JSON.stringify(operation.value)}, found ${JSON.stringify(actual)}`,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
default:
|
|
166
|
+
throw new Error(`unknown op "${(operation as { op: unknown }).op}"`);
|
|
167
|
+
}
|
|
168
|
+
} catch (err) {
|
|
169
|
+
throw new Error(`operation ${i} (${operation.op}): ${err instanceof Error ? err.message : String(err)}`);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
return root as T;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Apply an RFC 7386 merge patch: objects merge key by key, a `null` value
|
|
177
|
+
* deletes the key, and anything else (arrays included) replaces the target
|
|
178
|
+
* whole.
|
|
179
|
+
*/
|
|
180
|
+
export function applyMergePatch<T>(doc: T, patch: unknown): T {
|
|
181
|
+
if (!isObject(patch)) return clone(patch) as T;
|
|
182
|
+
const target: Record<string, unknown> = isObject(doc) ? clone(doc) : {};
|
|
183
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
184
|
+
if (value === undefined) continue;
|
|
185
|
+
if (value === null) delete target[key];
|
|
186
|
+
else target[key] = applyMergePatch(target[key], value);
|
|
187
|
+
}
|
|
188
|
+
return target as T;
|
|
189
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { describe, test, expect, expectTypeOf } from "vitest";
|
|
2
|
+
import { applyRenderedPatches, renderedPatch, type MergePatchOf, type KindProps, type RenderedPatch } from "./rendered-patch";
|
|
3
|
+
|
|
4
|
+
const docs = (): Array<Record<string, unknown>> => [
|
|
5
|
+
{ apiVersion: "v1", kind: "ConfigMap", metadata: { name: "cfg", namespace: "a" }, data: { level: "info" } },
|
|
6
|
+
{ apiVersion: "v1", kind: "ConfigMap", metadata: { name: "cfg", namespace: "b" }, data: { level: "info" } },
|
|
7
|
+
{ apiVersion: "apps/v1", kind: "Deployment", metadata: { name: "web", namespace: "a" }, spec: { replicas: 1 } },
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
describe("applyRenderedPatches", () => {
|
|
11
|
+
test("no patches returns the documents unchanged", () => {
|
|
12
|
+
expect(applyRenderedPatches(docs(), undefined, "src")).toEqual(docs());
|
|
13
|
+
expect(applyRenderedPatches(docs(), [], "src")).toEqual(docs());
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("a namespaced selector patches only that document", () => {
|
|
17
|
+
const out = applyRenderedPatches(
|
|
18
|
+
docs(),
|
|
19
|
+
[{ kind: "ConfigMap", name: "cfg", namespace: "b", merge: { data: { level: "debug" } } }],
|
|
20
|
+
"src",
|
|
21
|
+
);
|
|
22
|
+
expect(out.map((d) => (d.data as { level?: string } | undefined)?.level)).toEqual(["info", "debug", undefined]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("without a namespace, every document of that kind and name matches", () => {
|
|
26
|
+
const out = applyRenderedPatches(
|
|
27
|
+
docs(),
|
|
28
|
+
[{ kind: "ConfigMap", name: "cfg", jsonPatch: [{ op: "replace", path: "/data/level", value: "warn" }] }],
|
|
29
|
+
"src",
|
|
30
|
+
);
|
|
31
|
+
expect(out.slice(0, 2).map((d) => (d.data as { level: string }).level)).toEqual(["warn", "warn"]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("patches apply in order, each seeing the last one's result", () => {
|
|
35
|
+
const out = applyRenderedPatches(
|
|
36
|
+
docs(),
|
|
37
|
+
[
|
|
38
|
+
{ kind: "Deployment", name: "web", merge: { spec: { replicas: 3 } } },
|
|
39
|
+
{ kind: "Deployment", name: "web", jsonPatch: [{ op: "test", path: "/spec/replicas", value: 3 }] },
|
|
40
|
+
],
|
|
41
|
+
"src",
|
|
42
|
+
);
|
|
43
|
+
expect((out[2].spec as { replicas: number }).replicas).toBe(3);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("a selector matching nothing is an error naming the source and the documents of that kind", () => {
|
|
47
|
+
expect(() =>
|
|
48
|
+
applyRenderedPatches(docs(), [{ kind: "Deployment", name: "api", merge: { spec: { replicas: 2 } } }], 'HelmRender "x"'),
|
|
49
|
+
).toThrow(/HelmRender "x": patch 0 \(kind Deployment, name api\) matched no document\. Deployment documents present: a\/web\./);
|
|
50
|
+
expect(() =>
|
|
51
|
+
applyRenderedPatches(docs(), [{ kind: "Service", name: "web", merge: {} }], "src"),
|
|
52
|
+
).toThrow(/There are no Service documents/);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("apiVersion narrows the selector", () => {
|
|
56
|
+
expect(() =>
|
|
57
|
+
applyRenderedPatches(docs(), [{ kind: "Deployment", name: "web", apiVersion: "apps/v1beta1", merge: {} }], "src"),
|
|
58
|
+
).toThrow(/matched no document/);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("a failed operation names the source and the patch", () => {
|
|
62
|
+
expect(() =>
|
|
63
|
+
applyRenderedPatches(
|
|
64
|
+
docs(),
|
|
65
|
+
[{ kind: "Deployment", name: "web", jsonPatch: [{ op: "test", path: "/spec/replicas", value: 2 }] }],
|
|
66
|
+
"file.yaml",
|
|
67
|
+
),
|
|
68
|
+
).toThrow(/file\.yaml: patch 0 \(kind Deployment, name web\): operation 0 \(test\): test failed/);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("both or neither of jsonPatch and merge is refused", () => {
|
|
72
|
+
const both = { kind: "Deployment", name: "web", jsonPatch: [], merge: {} } as unknown as RenderedPatch;
|
|
73
|
+
const neither = { kind: "Deployment", name: "web" } as unknown as RenderedPatch;
|
|
74
|
+
expect(() => applyRenderedPatches(docs(), [both], "src")).toThrow(/exactly one of jsonPatch and merge/);
|
|
75
|
+
expect(() => applyRenderedPatches(docs(), [neither], "src")).toThrow(/exactly one of jsonPatch and merge/);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("a patch that removes kind is refused", () => {
|
|
79
|
+
expect(() =>
|
|
80
|
+
applyRenderedPatches(docs(), [{ kind: "Deployment", name: "web", jsonPatch: [{ op: "remove", path: "/kind" }] }], "src"),
|
|
81
|
+
).toThrow(/left a document without a string apiVersion and kind/);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("renderedPatch infers the kind and returns the patch unchanged", () => {
|
|
85
|
+
const patch = renderedPatch({ kind: "Deployment", name: "web", merge: { spec: { replicas: 5 } } });
|
|
86
|
+
expect(patch).toEqual({ kind: "Deployment", name: "web", merge: { spec: { replicas: 5 } } });
|
|
87
|
+
const out = applyRenderedPatches(docs(), [patch], "src");
|
|
88
|
+
expect((out[2].spec as { replicas: number }).replicas).toBe(5);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("the input documents are not mutated", () => {
|
|
92
|
+
const input = docs();
|
|
93
|
+
applyRenderedPatches(input, [{ kind: "Deployment", name: "web", merge: { spec: { replicas: 9 } } }], "src");
|
|
94
|
+
expect(input).toEqual(docs());
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe("renderedPatch types merge against the generated props", () => {
|
|
99
|
+
test("a well-typed Deployment merge compiles, with null deleting a field", () => {
|
|
100
|
+
const patch = renderedPatch({
|
|
101
|
+
kind: "Deployment",
|
|
102
|
+
name: "web",
|
|
103
|
+
merge: { spec: { replicas: 3, paused: null, template: { spec: { enableServiceLinks: false } } } },
|
|
104
|
+
});
|
|
105
|
+
expect(patch.kind).toBe("Deployment");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("wrong-typed or unknown fields are rejected by the compiler", () => {
|
|
109
|
+
renderedPatch({
|
|
110
|
+
kind: "Deployment",
|
|
111
|
+
name: "web",
|
|
112
|
+
// @ts-expect-error replicas is a number
|
|
113
|
+
merge: { spec: { replicas: "three" } },
|
|
114
|
+
});
|
|
115
|
+
renderedPatch({
|
|
116
|
+
kind: "Deployment",
|
|
117
|
+
name: "web",
|
|
118
|
+
// @ts-expect-error not a Deployment field
|
|
119
|
+
merge: { spek: { replicas: 3 } },
|
|
120
|
+
});
|
|
121
|
+
renderedPatch({
|
|
122
|
+
kind: "Deployment",
|
|
123
|
+
name: "web",
|
|
124
|
+
merge: {
|
|
125
|
+
spec: {
|
|
126
|
+
template: {
|
|
127
|
+
spec: {
|
|
128
|
+
// @ts-expect-error a container in a replacing array must be complete and well-typed
|
|
129
|
+
containers: [{ name: "c", livenessProbe: { grpc: { port: "x" } } }],
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("a kind with no generated class takes an open record", () => {
|
|
138
|
+
renderedPatch({ kind: "SomeVendorThing", name: "x", merge: { anything: { goes: 1 } } });
|
|
139
|
+
expectTypeOf<KindProps<"SomeVendorThing">>().toEqualTypeOf<Record<string, unknown>>();
|
|
140
|
+
expectTypeOf<{ spec: { replicas: number } }>().toMatchTypeOf<MergePatchOf<KindProps<"Deployment">>>();
|
|
141
|
+
expectTypeOf<{ spec: { replicas: string } }>().not.toMatchTypeOf<MergePatchOf<KindProps<"Deployment">>>();
|
|
142
|
+
});
|
|
143
|
+
});
|