@intentius/chant-lexicon-k8s 0.101.0 → 0.104.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/actions/apps.d.ts +4 -4
- package/dist/actions/apps.d.ts.map +1 -1
- package/dist/actions/core.d.ts +5 -5
- package/dist/actions/core.d.ts.map +1 -1
- package/dist/actions/networking.d.ts.map +1 -1
- package/dist/codegen/generate-typescript.d.ts +2 -2
- package/dist/codegen/generate-typescript.d.ts.map +1 -1
- package/dist/codegen/generate.d.ts +26 -0
- package/dist/codegen/generate.d.ts.map +1 -1
- package/dist/codegen/naming.d.ts.map +1 -1
- package/dist/codegen/versions.d.ts.map +1 -1
- package/dist/components/capability-plugin.d.ts.map +1 -1
- package/dist/components/kubectl-apply.d.ts +4 -1
- package/dist/components/kubectl-apply.d.ts.map +1 -1
- package/dist/composites/catalog.d.ts.map +1 -1
- package/dist/composites/cockroachdb-cluster.d.ts +2 -2
- package/dist/composites/gke-otel-collector.d.ts.map +1 -1
- package/dist/composites/index.d.ts +1 -0
- package/dist/composites/index.d.ts.map +1 -1
- package/dist/composites/otel-collector-agent.d.ts +19 -1
- package/dist/composites/otel-collector-agent.d.ts.map +1 -1
- package/dist/composites/otel-collector-node.d.ts +88 -0
- package/dist/composites/otel-collector-node.d.ts.map +1 -0
- package/dist/composites/otel-collector-rbac.d.ts.map +1 -1
- package/dist/composites/otel-collector-shape.d.ts +3 -1
- package/dist/composites/otel-collector-shape.d.ts.map +1 -1
- package/dist/composites/otel-collector.d.ts +9 -1
- package/dist/composites/otel-collector.d.ts.map +1 -1
- package/dist/composites/ray-cluster.d.ts +6 -6
- package/dist/composites/ray-job.d.ts +5 -5
- package/dist/composites/ray-service.d.ts +6 -6
- package/dist/crd/crd-sources.d.ts.map +1 -1
- package/dist/crd/loader.d.ts.map +1 -1
- package/dist/crd/oci-chart.d.ts +50 -0
- package/dist/crd/oci-chart.d.ts.map +1 -0
- package/dist/crd/types.d.ts +5 -0
- package/dist/crd/types.d.ts.map +1 -1
- package/dist/effect-receipt-row.d.ts.map +1 -1
- package/dist/generated/index.d.ts +764 -729
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/import/embedded.d.ts +4 -2
- package/dist/import/embedded.d.ts.map +1 -1
- package/dist/import/generator.d.ts.map +1 -1
- package/dist/import/parser.d.ts.map +1 -1
- package/dist/import/testdata/embedded/fixtures.d.ts +6 -0
- package/dist/import/testdata/embedded/fixtures.d.ts.map +1 -1
- package/dist/integrity.json +8 -7
- package/dist/lint/audit-catalog.d.ts.map +1 -1
- package/dist/lint/post-synth/index.d.ts.map +1 -1
- package/dist/lint/post-synth/otel-placement-helpers.d.ts.map +1 -1
- package/dist/lint/post-synth/wk8604.d.ts +3 -2
- package/dist/lint/post-synth/wk8604.d.ts.map +1 -1
- package/dist/lint/post-synth/wk8605.d.ts +23 -0
- package/dist/lint/post-synth/wk8605.d.ts.map +1 -0
- package/dist/manifest.json +1 -1
- package/dist/meta.json +194 -17
- package/dist/okf/index.md +5 -0
- package/dist/okf/rules/WK8605.md +11 -0
- package/dist/okf/types/APIGroup.md +3 -3
- package/dist/okf/types/APIGroupList.md +1 -1
- package/dist/okf/types/APIResourceList.md +1 -1
- package/dist/okf/types/APIService.md +1 -1
- package/dist/okf/types/APIServiceList.md +2 -2
- package/dist/okf/types/APIVersions.md +1 -1
- package/dist/okf/types/BatchJob.md +1 -1
- package/dist/okf/types/CSIDriver.md +1 -1
- package/dist/okf/types/CSIDriverList.md +2 -2
- package/dist/okf/types/CSINode.md +1 -1
- package/dist/okf/types/CSINodeList.md +2 -2
- package/dist/okf/types/CSIStorageCapacity.md +2 -2
- package/dist/okf/types/CSIStorageCapacityList.md +2 -2
- package/dist/okf/types/CertificateSigningRequest.md +1 -1
- package/dist/okf/types/CertificateSigningRequestList.md +2 -2
- package/dist/okf/types/ClusterRole.md +1 -1
- package/dist/okf/types/ClusterRoleBindingList.md +2 -2
- package/dist/okf/types/ClusterRoleList.md +2 -2
- package/dist/okf/types/ClusterTrustBundle.md +1 -1
- package/dist/okf/types/ClusterTrustBundleList.md +2 -2
- package/dist/okf/types/ComponentStatus.md +1 -1
- package/dist/okf/types/ComponentStatusList.md +2 -2
- package/dist/okf/types/ConfigMapList.md +2 -2
- package/dist/okf/types/ControllerRevisionList.md +2 -2
- package/dist/okf/types/CoreEventList.md +2 -2
- package/dist/okf/types/CronJob.md +1 -1
- package/dist/okf/types/CronJobList.md +2 -2
- package/dist/okf/types/CustomResourceDefinition.md +1 -1
- package/dist/okf/types/CustomResourceDefinitionList.md +2 -2
- package/dist/okf/types/DaemonSet.md +1 -1
- package/dist/okf/types/DaemonSetList.md +2 -2
- package/dist/okf/types/DeleteOptions.md +1 -1
- package/dist/okf/types/Deploy.md +1 -1
- package/dist/okf/types/DeploymentList.md +2 -2
- package/dist/okf/types/DeviceClass.md +1 -1
- package/dist/okf/types/DeviceClassList.md +2 -2
- package/dist/okf/types/DeviceTaintRule.md +1 -1
- package/dist/okf/types/DeviceTaintRuleList.md +2 -2
- package/dist/okf/types/EndpointSlice.md +2 -2
- package/dist/okf/types/EndpointSliceList.md +2 -2
- package/dist/okf/types/Endpoints.md +1 -1
- package/dist/okf/types/EndpointsList.md +2 -2
- package/dist/okf/types/Event.md +2 -2
- package/dist/okf/types/EventsEvent.md +2 -2
- package/dist/okf/types/EventsEventList.md +2 -2
- package/dist/okf/types/Eviction.md +1 -1
- package/dist/okf/types/FlowSchema.md +1 -1
- package/dist/okf/types/FlowSchemaList.md +2 -2
- package/dist/okf/types/GrafanaDashboard.md +14 -0
- package/dist/okf/types/GrafanaDatasource.md +14 -0
- package/dist/okf/types/GrafanaFolder.md +14 -0
- package/dist/okf/types/GrafanaLibraryPanel.md +14 -0
- package/dist/okf/types/HorizontalPodAutoscaler.md +1 -1
- package/dist/okf/types/HorizontalPodAutoscalerList.md +2 -2
- package/dist/okf/types/IPAddress.md +1 -1
- package/dist/okf/types/IPAddressList.md +2 -2
- package/dist/okf/types/Ing.md +1 -1
- package/dist/okf/types/IngressClass.md +1 -1
- package/dist/okf/types/IngressClassList.md +2 -2
- package/dist/okf/types/IngressList.md +2 -2
- package/dist/okf/types/JobList.md +2 -2
- package/dist/okf/types/Lease.md +1 -1
- package/dist/okf/types/LeaseCandidate.md +1 -1
- package/dist/okf/types/LeaseCandidateList.md +2 -2
- package/dist/okf/types/LeaseList.md +2 -2
- package/dist/okf/types/LimitRange.md +1 -1
- package/dist/okf/types/LimitRangeList.md +2 -2
- package/dist/okf/types/LocalSubjectAccessReview.md +1 -1
- package/dist/okf/types/MutatingAdmissionPolicy.md +1 -1
- package/dist/okf/types/MutatingAdmissionPolicyBinding.md +1 -1
- package/dist/okf/types/MutatingAdmissionPolicyBindingList.md +2 -2
- package/dist/okf/types/MutatingAdmissionPolicyList.md +2 -2
- package/dist/okf/types/MutatingWebhookConfiguration.md +1 -1
- package/dist/okf/types/MutatingWebhookConfigurationList.md +2 -2
- package/dist/okf/types/Namespace.md +1 -1
- package/dist/okf/types/NamespaceList.md +2 -2
- package/dist/okf/types/NetPol.md +1 -1
- package/dist/okf/types/NetworkPolicyList.md +2 -2
- package/dist/okf/types/Node.md +1 -1
- package/dist/okf/types/NodeList.md +2 -2
- package/dist/okf/types/PersistentVolume.md +1 -1
- package/dist/okf/types/PersistentVolumeClaimList.md +2 -2
- package/dist/okf/types/PersistentVolumeList.md +2 -2
- package/dist/okf/types/PodCertificateRequest.md +1 -1
- package/dist/okf/types/PodCertificateRequestList.md +2 -2
- package/dist/okf/types/PodDisruptionBudgetList.md +2 -2
- package/dist/okf/types/PodList.md +2 -2
- package/dist/okf/types/PodTemplateList.md +2 -2
- package/dist/okf/types/PriorityClassList.md +2 -2
- package/dist/okf/types/PriorityLevelConfiguration.md +1 -1
- package/dist/okf/types/PriorityLevelConfigurationList.md +2 -2
- package/dist/okf/types/ReplicaSet.md +1 -1
- package/dist/okf/types/ReplicaSetList.md +2 -2
- package/dist/okf/types/ReplicationController.md +1 -1
- package/dist/okf/types/ReplicationControllerList.md +2 -2
- package/dist/okf/types/ResourceClaim.md +1 -1
- package/dist/okf/types/ResourceClaimList.md +2 -2
- package/dist/okf/types/ResourceClaimTemplate.md +1 -1
- package/dist/okf/types/ResourceClaimTemplateList.md +2 -2
- package/dist/okf/types/ResourceQuota.md +1 -1
- package/dist/okf/types/ResourceQuotaList.md +2 -2
- package/dist/okf/types/ResourceSlice.md +1 -1
- package/dist/okf/types/ResourceSliceList.md +2 -2
- package/dist/okf/types/RoleBindingList.md +2 -2
- package/dist/okf/types/RoleList.md +2 -2
- package/dist/okf/types/RuntimeClass.md +2 -2
- package/dist/okf/types/RuntimeClassList.md +2 -2
- package/dist/okf/types/Scale.md +1 -1
- package/dist/okf/types/SecretList.md +2 -2
- package/dist/okf/types/SelfSubjectAccessReview.md +1 -1
- package/dist/okf/types/SelfSubjectRulesReview.md +1 -1
- package/dist/okf/types/Service.md +1 -1
- package/dist/okf/types/ServiceAccountList.md +2 -2
- package/dist/okf/types/ServiceCIDR.md +1 -1
- package/dist/okf/types/ServiceCIDRList.md +2 -2
- package/dist/okf/types/ServiceList.md +2 -2
- package/dist/okf/types/StatefulSet.md +1 -1
- package/dist/okf/types/StatefulSetList.md +2 -2
- package/dist/okf/types/Status.md +2 -2
- package/dist/okf/types/StorageClass.md +1 -1
- package/dist/okf/types/StorageClassList.md +2 -2
- package/dist/okf/types/StorageVersionMigration.md +1 -1
- package/dist/okf/types/StorageVersionMigrationList.md +2 -2
- package/dist/okf/types/SubjectAccessReview.md +1 -1
- package/dist/okf/types/TokenRequest.md +1 -1
- package/dist/okf/types/TokenReview.md +1 -1
- package/dist/okf/types/ValidatingAdmissionPolicy.md +1 -1
- package/dist/okf/types/ValidatingAdmissionPolicyBinding.md +1 -1
- package/dist/okf/types/ValidatingAdmissionPolicyBindingList.md +2 -2
- package/dist/okf/types/ValidatingAdmissionPolicyList.md +2 -2
- package/dist/okf/types/ValidatingWebhookConfiguration.md +1 -1
- package/dist/okf/types/ValidatingWebhookConfigurationList.md +2 -2
- package/dist/okf/types/VolumeAttachment.md +1 -1
- package/dist/okf/types/VolumeAttachmentList.md +2 -2
- package/dist/okf/types/VolumeAttributesClassList.md +2 -2
- package/dist/op/activities/argo.d.ts.map +1 -1
- package/dist/op/activities/index.d.ts +3 -3
- package/dist/op/activities/index.d.ts.map +1 -1
- package/dist/op/activities/wait-for-ready.d.ts.map +1 -1
- package/dist/op/builders.d.ts.map +1 -1
- package/dist/rules/wk8602.ts +2 -2
- package/dist/rules/wk8604.ts +3 -2
- package/dist/rules/wk8605.ts +131 -0
- package/dist/serializer.d.ts +0 -3
- package/dist/serializer.d.ts.map +1 -1
- package/dist/skills/chant-k8s.md +1 -1
- package/dist/spec/parse.d.ts +27 -0
- package/dist/spec/parse.d.ts.map +1 -1
- package/dist/telemetry-release.d.ts +15 -0
- package/dist/telemetry-release.d.ts.map +1 -0
- package/dist/types/index.d.ts +5373 -972
- package/dist/variables.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/codegen/declarations.test.ts +118 -0
- package/src/codegen/generate-crd-sources.test.ts +49 -0
- package/src/codegen/generate-typescript.ts +69 -4
- package/src/codegen/generate.ts +66 -18
- package/src/components/kubectl-apply.test.ts +87 -0
- package/src/components/kubectl-apply.ts +17 -1
- package/src/composites/catalog.ts +6 -0
- package/src/composites/gke-otel-collector.ts +6 -1
- package/src/composites/index.ts +1 -0
- package/src/composites/otel-collector-agent.ts +39 -2
- package/src/composites/otel-collector-gateway.test.ts +24 -1
- package/src/composites/otel-collector-gateway.ts +1 -1
- package/src/composites/otel-collector-node.test.ts +193 -0
- package/src/composites/otel-collector-node.ts +211 -0
- package/src/composites/otel-collector-rbac.test.ts +11 -0
- package/src/composites/otel-collector-rbac.ts +6 -4
- package/src/composites/otel-collector-shape.ts +9 -8
- package/src/composites/otel-collector.ts +16 -2
- package/src/crd/crd-sources.ts +28 -0
- package/src/crd/loader.test.ts +9 -1
- package/src/crd/loader.ts +26 -3
- package/src/crd/oci-chart.test.ts +149 -0
- package/src/crd/oci-chart.ts +178 -0
- package/src/crd/types.ts +5 -0
- package/src/generated/index.d.ts +5373 -972
- package/src/generated/index.ts +35 -0
- package/src/generated/lexicon-k8s.json +194 -17
- package/src/generated/operations.json +64 -0
- package/src/import/embedded-live.test.ts +46 -4
- package/src/import/embedded-roundtrip.test.ts +73 -7
- package/src/import/embedded-types.e2e.test.ts +3 -1
- package/src/import/embedded.test.ts +64 -0
- package/src/import/embedded.ts +31 -2
- package/src/import/generator.test.ts +19 -0
- package/src/import/generator.ts +20 -2
- package/src/import/testdata/embedded/README.md +4 -1
- package/src/import/testdata/embedded/alertmanager-configmap.yaml +91 -0
- package/src/import/testdata/embedded/fixtures.ts +18 -0
- package/src/import/testdata/embedded/grafana-operator-dashboard.yaml +363 -0
- package/src/import/testdata/embedded/grafana-v2-dashboard-configmap.yaml +851 -0
- package/src/lint/audit-catalog.ts +1 -0
- package/src/lint/post-synth/index.ts +2 -0
- package/src/lint/post-synth/otel-placement.test.ts +14 -0
- package/src/lint/post-synth/post-synth.test.ts +3 -3
- package/src/lint/post-synth/wk8602.ts +2 -2
- package/src/lint/post-synth/wk8604.ts +3 -2
- package/src/lint/post-synth/wk8605.test.ts +153 -0
- package/src/lint/post-synth/wk8605.ts +131 -0
- package/src/serializer-telemetry.test.ts +146 -0
- package/src/serializer.ts +75 -0
- package/src/skills/chant-k8s.md +1 -1
- package/src/spec/parse.test.ts +118 -0
- package/src/spec/parse.ts +250 -29
- package/src/telemetry-build.test.ts +79 -0
- package/src/telemetry-release.ts +93 -0
|
@@ -50,6 +50,7 @@ export const k8sAuditCatalog: Record<string, RuleMeta> = {
|
|
|
50
50
|
WK8602: auditRule("WK8602", "merge-worthy", "guidance", "Multi-replica tail sampling gateway reached without trace-aware load balancing", "Send traces to the gateway through a loadbalancing exporter with routing_key traceID (k8s resolver, or dns on a headless Service), or run the gateway with one replica. It goes silent when the senders are in another build root (chant #1939).", { category: "correctness" }),
|
|
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
|
+
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" }),
|
|
53
54
|
};
|
|
54
55
|
|
|
55
56
|
// Prior art credits live beside the rules in ./audit-lineage.ts (see core audit/prior-art.ts).
|
|
@@ -44,6 +44,7 @@ import { wk8601 } from "./wk8601";
|
|
|
44
44
|
import { wk8602 } from "./wk8602";
|
|
45
45
|
import { wk8603 } from "./wk8603";
|
|
46
46
|
import { wk8604 } from "./wk8604";
|
|
47
|
+
import { wk8605 } from "./wk8605";
|
|
47
48
|
|
|
48
49
|
export const postSynthChecks: PostSynthCheck[] = [
|
|
49
50
|
argo002,
|
|
@@ -90,4 +91,5 @@ export const postSynthChecks: PostSynthCheck[] = [
|
|
|
90
91
|
wk8602,
|
|
91
92
|
wk8603,
|
|
92
93
|
wk8604,
|
|
94
|
+
wk8605,
|
|
93
95
|
];
|
|
@@ -306,6 +306,20 @@ describe("WK8602: multi-replica tail sampling gateway without trace-aware routin
|
|
|
306
306
|
expect(wk8602.check(withAgent("otlphttp", "{ endpoint: 'http://gw:4318' }"))).toHaveLength(1);
|
|
307
307
|
});
|
|
308
308
|
|
|
309
|
+
test.each([
|
|
310
|
+
["otlp_grpc", "{ endpoint: gw.obs.svc.cluster.local:4317 }"],
|
|
311
|
+
["otlp_http", "{ endpoint: 'http://gw:4318' }"],
|
|
312
|
+
])("flags a hand-written exporter under the collector's newer name %s", (type, body) => {
|
|
313
|
+
expect(wk8602.check(withAgent(type, body))).toHaveLength(1);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test("reads load_balancing as loadbalancing", () => {
|
|
317
|
+
const diags = wk8602.check(withAgent("load_balancing", "{ protocol: { otlp: {} }, resolver: { dns: { hostname: gw.obs.svc } } }"));
|
|
318
|
+
expect(diags).toHaveLength(1);
|
|
319
|
+
expect(diags[0].message).toContain("one virtual IP");
|
|
320
|
+
expect(wk8602.check(withAgent("load_balancing", "{ routing_key: traceID, protocol: { otlp: {} }, resolver: { k8s: { service: gw-headless.obs } } }"))).toEqual([]);
|
|
321
|
+
});
|
|
322
|
+
|
|
309
323
|
test("flags the dns resolver on the ClusterIP Service", () => {
|
|
310
324
|
const diags = wk8602.check(withAgent("loadbalancing", "{ protocol: { otlp: {} }, resolver: { dns: { hostname: gw.obs.svc } } }"));
|
|
311
325
|
expect(diags).toHaveLength(1);
|
|
@@ -70,9 +70,9 @@ function manifestsCtx(...objs: unknown[]): PostSynthContext {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
// ── WK8005: Secrets in env ──────────────────────────────────────────
|
|
73
|
-
// Note:
|
|
74
|
-
// ports, probes) use JSON format
|
|
75
|
-
//
|
|
73
|
+
// Note: tests with nested container properties (env, resources, securityContext,
|
|
74
|
+
// ports, probes) use JSON format, written when core's parseYAML was a line
|
|
75
|
+
// parser that misread them. It is js-yaml now (#3006), so YAML works too.
|
|
76
76
|
|
|
77
77
|
describe("WK8005: Hardcoded secrets in env", () => {
|
|
78
78
|
test("flags hardcoded password in env", () => {
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
import type { PostSynthCheck, PostSynthContext, PostSynthDiagnostic } from "@intentius/chant/lint/post-synth";
|
|
23
|
+
import { canonicalTypeOf } from "@intentius/chant-lexicon-otel/model";
|
|
23
24
|
import { docsToManifests } from "./k8s-helpers";
|
|
24
25
|
import {
|
|
25
26
|
collectorPlacements,
|
|
26
|
-
componentType,
|
|
27
27
|
describePlacement,
|
|
28
28
|
hostOfEndpoint,
|
|
29
29
|
pipelinesOf,
|
|
@@ -47,7 +47,7 @@ function unroutedExporters(sender: CollectorPlacement, services: Map<string, boo
|
|
|
47
47
|
for (const id of pipeline.exporters) {
|
|
48
48
|
if (seen.has(id)) continue;
|
|
49
49
|
seen.add(id);
|
|
50
|
-
const type =
|
|
50
|
+
const type = canonicalTypeOf("exporter", id);
|
|
51
51
|
const cfg = record(sender.config.exporters?.[id]);
|
|
52
52
|
|
|
53
53
|
if (type === "loadbalancing") {
|
|
@@ -7,9 +7,10 @@
|
|
|
7
7
|
* otel lexicon's config checks never see a config that lives in the k8s output
|
|
8
8
|
* (chant #2930). This check runs them there: every ConfigMap data value that
|
|
9
9
|
* parses as a collector config (a `service.pipelines` map) goes through the
|
|
10
|
-
* otel lexicon's `validateCollectorConfig
|
|
10
|
+
* otel lexicon's `validateCollectorConfig`, and through `attributionIssues`
|
|
11
|
+
* (OTEL118) when the build stamps telemetry attribution.
|
|
11
12
|
*
|
|
12
|
-
* Findings keep the otel rule ids (OTEL101-OTEL106, OTEL112-
|
|
13
|
+
* Findings keep the otel rule ids (OTEL101-OTEL106, OTEL112-OTEL127, and any config
|
|
13
14
|
* check the otel lexicon adds later), so `lint.rules` and suppressions name
|
|
14
15
|
* one id wherever the config lives. Each message names the ConfigMap's
|
|
15
16
|
* namespace, name and key. Needs only the k8s lexicon in the project.
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WK8605: a collector config that reads the node, in a workload that
|
|
3
|
+
* doesn't give it the node name or the host mounts (chant #3103).
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, test } from "vitest";
|
|
6
|
+
import { dump } from "js-yaml";
|
|
7
|
+
import { expandComposite } from "@intentius/chant";
|
|
8
|
+
import type { Declarable } from "@intentius/chant/declarable";
|
|
9
|
+
import type { PostSynthContext } from "@intentius/chant/lint/post-synth";
|
|
10
|
+
import { makePostSynthCtx } from "@intentius/chant-test-utils";
|
|
11
|
+
import { DebugExporter, NodeAgent, OtlpReceiver, Pipeline } from "@intentius/chant-lexicon-otel";
|
|
12
|
+
import { k8sSerializer } from "../../serializer";
|
|
13
|
+
import { OtelCollector } from "../../composites/otel-collector";
|
|
14
|
+
import { wk8605 } from "./wk8605";
|
|
15
|
+
|
|
16
|
+
function built(instances: Record<string, unknown>): PostSynthContext {
|
|
17
|
+
const entities = new Map<string, Declarable>();
|
|
18
|
+
for (const [name, instance] of Object.entries(instances)) {
|
|
19
|
+
for (const [k, v] of expandComposite(name, instance as never)) entities.set(k, v);
|
|
20
|
+
}
|
|
21
|
+
const out = k8sSerializer.serialize(entities);
|
|
22
|
+
return makePostSynthCtx("k8s", typeof out === "string" ? out : out.primary);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const yaml = (...docs: object[]) => makePostSynthCtx("k8s", docs.map((d) => dump(d, { lineWidth: -1 })).join("---\n"));
|
|
26
|
+
|
|
27
|
+
const NODE_CONFIG = `
|
|
28
|
+
receivers:
|
|
29
|
+
hostmetrics: { root_path: /hostfs, scrapers: { cpu: {} } }
|
|
30
|
+
filelog: { include: [/var/log/pods/*/*/*.log] }
|
|
31
|
+
processors:
|
|
32
|
+
k8sattributes: { filter: { node_from_env_var: K8S_NODE_NAME } }
|
|
33
|
+
exporters: { debug: {} }
|
|
34
|
+
service:
|
|
35
|
+
pipelines:
|
|
36
|
+
metrics: { receivers: [hostmetrics], processors: [k8sattributes], exporters: [debug] }
|
|
37
|
+
logs: { receivers: [filelog], processors: [k8sattributes], exporters: [debug] }
|
|
38
|
+
`;
|
|
39
|
+
|
|
40
|
+
function daemonSet(pod: { env?: object[]; mounts?: object[]; volumes?: object[] }): object[] {
|
|
41
|
+
return [
|
|
42
|
+
{ apiVersion: "v1", kind: "ConfigMap", metadata: { name: "agent-config", namespace: "obs" }, data: { "config.yaml": NODE_CONFIG } },
|
|
43
|
+
{
|
|
44
|
+
apiVersion: "apps/v1",
|
|
45
|
+
kind: "DaemonSet",
|
|
46
|
+
metadata: { name: "agent", namespace: "obs" },
|
|
47
|
+
spec: {
|
|
48
|
+
selector: { matchLabels: { app: "agent" } },
|
|
49
|
+
template: {
|
|
50
|
+
metadata: { labels: { app: "agent" } },
|
|
51
|
+
spec: {
|
|
52
|
+
containers: [{
|
|
53
|
+
name: "otelcol",
|
|
54
|
+
image: "otel/opentelemetry-collector-contrib:0.130.0",
|
|
55
|
+
args: ["--config=/conf/config.yaml"],
|
|
56
|
+
...(pod.env ? { env: pod.env } : {}),
|
|
57
|
+
volumeMounts: [{ name: "conf", mountPath: "/conf" }, ...(pod.mounts ?? [])],
|
|
58
|
+
}],
|
|
59
|
+
volumes: [{ name: "conf", configMap: { name: "agent-config" } }, ...(pod.volumes ?? [])],
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const NODE_ENV = { name: "K8S_NODE_NAME", valueFrom: { fieldRef: { fieldPath: "spec.nodeName" } } };
|
|
68
|
+
|
|
69
|
+
describe("WK8605: node-reading collector config without the node", () => {
|
|
70
|
+
const gateway = new DebugExporter({});
|
|
71
|
+
|
|
72
|
+
test("OtelCollector running NodeAgent passes: the composite adds what the config reads", () => {
|
|
73
|
+
const agent = NodeAgent({ exporters: [gateway], kubeletStats: true });
|
|
74
|
+
expect(wk8605.check(built({ agent: OtelCollector({ config: Object.values(agent.members) }) }))).toEqual([]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("a config that reads nothing from the node is not checked", () => {
|
|
78
|
+
const rx = new OtlpReceiver({ protocols: { grpc: { endpoint: "0.0.0.0:4317" } } });
|
|
79
|
+
const r = OtelCollector({ config: [new Pipeline({ signal: "traces", receivers: [rx], exporters: [gateway] })] });
|
|
80
|
+
expect(wk8605.check(built({ r }))).toEqual([]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("a hand-written DaemonSet without the variable or mounts is reported, naming each gap", () => {
|
|
84
|
+
const d = wk8605.check(yaml(...daemonSet({})));
|
|
85
|
+
expect(d).toHaveLength(1);
|
|
86
|
+
expect(d[0]).toMatchObject({ checkId: "WK8605", severity: "warning", entity: "agent" });
|
|
87
|
+
expect(d[0].message).toContain("DaemonSet obs/agent");
|
|
88
|
+
expect(d[0].message).toContain("no K8S_NODE_NAME variable (set it from spec.nodeName");
|
|
89
|
+
expect(d[0].message).toContain("hostmetrics reads host / at /hostfs");
|
|
90
|
+
expect(d[0].message).toContain("filelog reads host /var/log/pods at /var/log/pods");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("the collector's newer names (host_metrics, file_log, k8s_attributes) are reported the same", () => {
|
|
94
|
+
const docs = daemonSet({}) as Array<Record<string, any>>;
|
|
95
|
+
docs[0].data["config.yaml"] = NODE_CONFIG.replace(/hostmetrics/g, "host_metrics")
|
|
96
|
+
.replace(/filelog/g, "file_log")
|
|
97
|
+
.replace(/k8sattributes/g, "k8s_attributes");
|
|
98
|
+
const d = wk8605.check(yaml(...docs));
|
|
99
|
+
expect(d).toHaveLength(1);
|
|
100
|
+
expect(d[0].message).toContain("no K8S_NODE_NAME variable (set it from spec.nodeName");
|
|
101
|
+
expect(d[0].message).toContain("host_metrics reads host / at /hostfs");
|
|
102
|
+
expect(d[0].message).toContain("file_log reads host /var/log/pods at /var/log/pods");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("the variable from any source and covering hostPath mounts pass, including partial host root mounts", () => {
|
|
106
|
+
const d = wk8605.check(yaml(...daemonSet({
|
|
107
|
+
env: [{ name: "K8S_NODE_NAME", value: "fixed" }],
|
|
108
|
+
mounts: [
|
|
109
|
+
{ name: "proc", mountPath: "/hostfs/proc", readOnly: true },
|
|
110
|
+
{ name: "varlog", mountPath: "/var/log", readOnly: true },
|
|
111
|
+
],
|
|
112
|
+
volumes: [
|
|
113
|
+
{ name: "proc", hostPath: { path: "/proc" } },
|
|
114
|
+
{ name: "varlog", hostPath: { path: "/var/log" } },
|
|
115
|
+
],
|
|
116
|
+
})));
|
|
117
|
+
expect(d).toEqual([]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("a mount at the right path from the wrong host directory, or from a non-hostPath volume, doesn't count", () => {
|
|
121
|
+
const d = wk8605.check(yaml(...daemonSet({
|
|
122
|
+
env: [NODE_ENV],
|
|
123
|
+
mounts: [
|
|
124
|
+
{ name: "hostfs", mountPath: "/hostfs" },
|
|
125
|
+
{ name: "pods", mountPath: "/var/log/pods" },
|
|
126
|
+
],
|
|
127
|
+
volumes: [
|
|
128
|
+
{ name: "hostfs", emptyDir: {} },
|
|
129
|
+
{ name: "pods", hostPath: { path: "/var/lib/pods" } },
|
|
130
|
+
],
|
|
131
|
+
})));
|
|
132
|
+
expect(d).toHaveLength(1);
|
|
133
|
+
expect(d[0].message).toContain("hostmetrics reads host /");
|
|
134
|
+
expect(d[0].message).toContain("filelog reads host /var/log/pods");
|
|
135
|
+
expect(d[0].message).not.toContain("K8S_NODE_NAME");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("a sidecar's variable doesn't count for the collector container", () => {
|
|
139
|
+
const docs = daemonSet({}) as Array<Record<string, any>>;
|
|
140
|
+
docs[1].spec.template.spec.containers.push({ name: "sidecar", image: "busybox", env: [NODE_ENV] });
|
|
141
|
+
const d = wk8605.check(yaml(...docs));
|
|
142
|
+
expect(d[0].message).toContain("no K8S_NODE_NAME variable");
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("defaults.daemonSet appends containers rather than replacing them, so the composite's additions stay", () => {
|
|
146
|
+
const agent = NodeAgent({ exporters: [gateway] });
|
|
147
|
+
const r = OtelCollector({
|
|
148
|
+
config: Object.values(agent.members),
|
|
149
|
+
defaults: { daemonSet: { spec: { template: { spec: { containers: [{ name: "sidecar", image: "busybox" }] } } } } },
|
|
150
|
+
});
|
|
151
|
+
expect(wk8605.check(built({ r }))).toEqual([]);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WK8605: a collector config reads the node, and its workload doesn't give it the node
|
|
3
|
+
*
|
|
4
|
+
* `k8sattributes` filtering to its node by `filter.node_from_env_var`, and
|
|
5
|
+
* `kubeletstats` addressing the kubelet by `${env:K8S_NODE_NAME}`, need that
|
|
6
|
+
* variable set in the pod; unset, the filter matches no pod and the kubelet
|
|
7
|
+
* address doesn't resolve. `hostmetrics` with `root_path` needs the host root
|
|
8
|
+
* mounted there, and `filelog` needs the directories it reads mounted from
|
|
9
|
+
* the host. None of this fails at startup: the collector runs and reports
|
|
10
|
+
* the container instead of the node, or nothing.
|
|
11
|
+
*
|
|
12
|
+
* What a config needs is worked out by `collectorNodeAccess`, the function
|
|
13
|
+
* `OtelCollector` uses to add it (chant #3103), so the composite's own
|
|
14
|
+
* output passes and this fires on hand-written workloads. Only the
|
|
15
|
+
* containers that pass `--config` count (every container when none does). A variable
|
|
16
|
+
* counts as set whatever its source; a mount counts when a hostPath volume
|
|
17
|
+
* covers the path. Whether the container may read root-owned log files is
|
|
18
|
+
* not checked, since that depends on the runtime. One build root at a time
|
|
19
|
+
* (chant #1939).
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { PostSynthCheck, PostSynthContext, PostSynthDiagnostic } from "@intentius/chant/lint/post-synth";
|
|
23
|
+
import { docsToManifests, extractContainers, extractPodSpec, type K8sContainer, type K8sManifest } from "./k8s-helpers";
|
|
24
|
+
import { collectorPlacements, describePlacement } from "./otel-placement-helpers";
|
|
25
|
+
import { collectorNodeAccess, type NodeHostMount } from "../../composites/otel-collector-node";
|
|
26
|
+
|
|
27
|
+
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
28
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function within(path: string, dir: string): boolean {
|
|
32
|
+
const d = dir.replace(/\/+$/, "");
|
|
33
|
+
return d === "" || path === d || path.startsWith(`${d}/`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The containers that run a collector: those passing `--config`, or every
|
|
38
|
+
* container when none does. A sidecar's variables and mounts don't count.
|
|
39
|
+
*/
|
|
40
|
+
function collectorContainers(workload: K8sManifest): K8sContainer[] {
|
|
41
|
+
const all = extractContainers(workload);
|
|
42
|
+
const withConfig = all.filter((c) =>
|
|
43
|
+
[...(Array.isArray(c.command) ? c.command : []), ...(Array.isArray(c.args) ? c.args : [])].some(
|
|
44
|
+
(a) => typeof a === "string" && (a === "--config" || a.startsWith("--config=")),
|
|
45
|
+
),
|
|
46
|
+
);
|
|
47
|
+
return withConfig.length > 0 ? withConfig : all;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Each container mount backed by a hostPath volume, as [host path, container path]. */
|
|
51
|
+
function hostMounts(workload: K8sManifest, containers: K8sContainer[]): Array<[string, string]> {
|
|
52
|
+
const pod = extractPodSpec(workload);
|
|
53
|
+
const hostPaths = new Map<string, string>();
|
|
54
|
+
for (const v of Array.isArray(pod?.volumes) ? pod.volumes : []) {
|
|
55
|
+
if (isRecord(v) && typeof v.name === "string" && isRecord(v.hostPath) && typeof v.hostPath.path === "string") {
|
|
56
|
+
hostPaths.set(v.name, v.hostPath.path);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const out: Array<[string, string]> = [];
|
|
60
|
+
for (const c of containers) {
|
|
61
|
+
for (const m of Array.isArray(c.volumeMounts) ? c.volumeMounts : []) {
|
|
62
|
+
if (!isRecord(m) || typeof m.name !== "string" || typeof m.mountPath !== "string") continue;
|
|
63
|
+
const host = hostPaths.get(m.name);
|
|
64
|
+
if (host !== undefined) out.push([host.replace(/\/+$/, "") || "/", m.mountPath.replace(/\/+$/, "") || "/"]);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Whether some hostPath mount gives the container the host path a requirement names, at its path. */
|
|
71
|
+
function covered(need: NodeHostMount, mounts: Array<[string, string]>): boolean {
|
|
72
|
+
return mounts.some(([host, path]) => {
|
|
73
|
+
// hostmetrics takes the host root or parts of it anywhere at or under root_path.
|
|
74
|
+
if (need.partial && within(path, need.mountPath)) {
|
|
75
|
+
const rel = path.slice(need.mountPath.length);
|
|
76
|
+
return host === (rel || "/");
|
|
77
|
+
}
|
|
78
|
+
if (!within(need.mountPath, path)) return false;
|
|
79
|
+
const rel = need.mountPath.slice(path === "/" ? 0 : path.length);
|
|
80
|
+
const expected = `${host === "/" ? "" : host}${rel}` || "/";
|
|
81
|
+
return expected === need.hostPath;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function envNames(containers: K8sContainer[]): Set<string> {
|
|
86
|
+
const names = new Set<string>();
|
|
87
|
+
for (const c of containers) {
|
|
88
|
+
for (const e of Array.isArray(c.env) ? c.env : []) {
|
|
89
|
+
if (isRecord(e) && typeof e.name === "string") names.add(e.name);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return names;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export const wk8605: PostSynthCheck = {
|
|
96
|
+
id: "WK8605",
|
|
97
|
+
description:
|
|
98
|
+
"OpenTelemetry Collector config reads the node (k8sattributes filtered to its node, kubeletstats, hostmetrics root_path, filelog) but its workload doesn't set the node name variable or mount the host paths. Sees one build root at a time (chant #1939).",
|
|
99
|
+
|
|
100
|
+
check(ctx: PostSynthContext): PostSynthDiagnostic[] {
|
|
101
|
+
const diagnostics: PostSynthDiagnostic[] = [];
|
|
102
|
+
for (const p of collectorPlacements(docsToManifests(ctx))) {
|
|
103
|
+
const need = collectorNodeAccess(p.config);
|
|
104
|
+
if (need.env.length === 0 && need.mounts.length === 0) continue;
|
|
105
|
+
|
|
106
|
+
const containers = collectorContainers(p.workload);
|
|
107
|
+
const env = envNames(containers);
|
|
108
|
+
const mounts = hostMounts(p.workload, containers);
|
|
109
|
+
const gaps: string[] = [];
|
|
110
|
+
for (const v of need.env) {
|
|
111
|
+
if (!env.has(v.name)) gaps.push(`no ${v.name} variable (set it from ${v.valueFrom.fieldRef.fieldPath} with the downward API)`);
|
|
112
|
+
}
|
|
113
|
+
for (const m of need.mounts) {
|
|
114
|
+
if (!covered(m, mounts)) gaps.push(`${m.component} reads host ${m.hostPath} at ${m.mountPath}, which no hostPath volume mounts`);
|
|
115
|
+
}
|
|
116
|
+
if (gaps.length === 0) continue;
|
|
117
|
+
|
|
118
|
+
diagnostics.push({
|
|
119
|
+
checkId: "WK8605",
|
|
120
|
+
severity: "warning",
|
|
121
|
+
message:
|
|
122
|
+
`${describePlacement(p)} runs a collector config that reads the node (ConfigMap ${p.configMap}, ${p.key}), but: ${gaps.join("; ")}. ` +
|
|
123
|
+
`Without them the collector reports on its own container, or nothing. OtelCollector adds these from the config; ` +
|
|
124
|
+
`in a hand-written workload, add them read-only.`,
|
|
125
|
+
entity: p.name,
|
|
126
|
+
lexicon: "k8s",
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return diagnostics;
|
|
130
|
+
},
|
|
131
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telemetry attribution on k8s workloads (#3059, #2558 D22, ws-060): each
|
|
3
|
+
* container of a Deployment, StatefulSet, DaemonSet, ReplicaSet, Job, CronJob
|
|
4
|
+
* or Pod gets `OTEL_SERVICE_NAME` and `OTEL_RESOURCE_ATTRIBUTES` when the
|
|
5
|
+
* build hands the serializer `context.telemetry`.
|
|
6
|
+
*/
|
|
7
|
+
import { describe, expect, test } from "vitest";
|
|
8
|
+
import { loadAll } from "js-yaml";
|
|
9
|
+
import { DECLARABLE_MARKER } from "@intentius/chant/declarable";
|
|
10
|
+
import { k8sSerializer } from "./serializer";
|
|
11
|
+
|
|
12
|
+
function mockResource(entityType: string, props: Record<string, unknown>): any {
|
|
13
|
+
return { [DECLARABLE_MARKER]: true, lexicon: "k8s", entityType, kind: "resource", props };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type Doc = Record<string, any>;
|
|
17
|
+
|
|
18
|
+
function serialize(entities: Map<string, unknown>, telemetry?: { workspace?: string; member?: string; environment?: string }): string {
|
|
19
|
+
const result = k8sSerializer.serialize(entities as Map<string, any>, [], telemetry ? { telemetry } : undefined);
|
|
20
|
+
return typeof result === "string" ? result : result.primary;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function docs(out: string): Doc[] {
|
|
24
|
+
return loadAll(out) as Doc[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const podSpec = (containers: unknown[]) => ({ containers });
|
|
28
|
+
const RELEASE_ENV = {
|
|
29
|
+
name: "CHANT_RELEASE_ATTRIBUTES",
|
|
30
|
+
valueFrom: { fieldRef: { fieldPath: "metadata.annotations['chant.intentius.io/release-attributes']" } },
|
|
31
|
+
};
|
|
32
|
+
const valueOf = (env: Doc[], name: string) => env.find((e) => e.name === name)?.value;
|
|
33
|
+
const attrs = { workspace: "acme", member: "delivery", environment: "prod" };
|
|
34
|
+
|
|
35
|
+
const workloads: Array<[string, string, (spec: unknown) => Record<string, unknown>, (doc: Doc) => Doc]> = [
|
|
36
|
+
["Deployment", "K8s::Apps::Deployment", (s) => ({ spec: { template: { spec: s } } }), (d) => d.spec.template.spec],
|
|
37
|
+
["StatefulSet", "K8s::Apps::StatefulSet", (s) => ({ spec: { template: { spec: s } } }), (d) => d.spec.template.spec],
|
|
38
|
+
["DaemonSet", "K8s::Apps::DaemonSet", (s) => ({ spec: { template: { spec: s } } }), (d) => d.spec.template.spec],
|
|
39
|
+
["Job", "K8s::Batch::Job", (s) => ({ spec: { template: { spec: s } } }), (d) => d.spec.template.spec],
|
|
40
|
+
["CronJob", "K8s::Batch::CronJob", (s) => ({ spec: { schedule: "* * * * *", jobTemplate: { spec: { template: { spec: s } } } } }), (d) => d.spec.jobTemplate.spec.template.spec],
|
|
41
|
+
["Pod", "K8s::Core::Pod", (s) => ({ spec: s }), (d) => d.spec],
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
describe("telemetry attribution on workloads", () => {
|
|
45
|
+
test.each(workloads)("a %s's containers get OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES", (_kind, type, wrap, pod) => {
|
|
46
|
+
const entities = new Map([["apiWorkload", mockResource(type, { metadata: { name: "api" }, ...wrap(podSpec([{ name: "api", image: "nginx:1" }])) })]]);
|
|
47
|
+
const [doc] = docs(serialize(entities, attrs));
|
|
48
|
+
expect(pod(doc).containers[0].env).toEqual([
|
|
49
|
+
{ name: "OTEL_SERVICE_NAME", value: "api" },
|
|
50
|
+
RELEASE_ENV,
|
|
51
|
+
{
|
|
52
|
+
name: "OTEL_RESOURCE_ATTRIBUTES",
|
|
53
|
+
value: "chant.workspace=acme,chant.member=delivery,chant.decl=apiWorkload,deployment.environment.name=prod$(CHANT_RELEASE_ATTRIBUTES)",
|
|
54
|
+
},
|
|
55
|
+
]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("without a telemetry context the output is the same bytes as with an empty one, and has no OTEL_ variables", () => {
|
|
59
|
+
const entities = new Map([["api", mockResource("K8s::Apps::Deployment", { metadata: { name: "api" }, spec: { template: { spec: podSpec([{ name: "api", image: "nginx:1" }]) } } })]]);
|
|
60
|
+
const plain = serialize(entities);
|
|
61
|
+
expect(plain).toBe(k8sSerializer.serialize(entities as Map<string, any>, [], {}));
|
|
62
|
+
expect(plain).not.toContain("OTEL_");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("a variable the container sets is kept, its own resource attributes keep their keys, and valueFrom is left alone", () => {
|
|
66
|
+
const containers = [
|
|
67
|
+
{
|
|
68
|
+
name: "own",
|
|
69
|
+
image: "x",
|
|
70
|
+
env: [
|
|
71
|
+
{ name: "PORT", value: "80" },
|
|
72
|
+
{ name: "OTEL_SERVICE_NAME", value: "checkout" },
|
|
73
|
+
{ name: "OTEL_RESOURCE_ATTRIBUTES", value: "chant.member=mine,team=pay" },
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
{ name: "ref", image: "y", env: [{ name: "OTEL_RESOURCE_ATTRIBUTES", valueFrom: { configMapKeyRef: { name: "otel", key: "attrs" } } }] },
|
|
77
|
+
];
|
|
78
|
+
const entities = new Map([["api", mockResource("K8s::Apps::Deployment", { metadata: { name: "api" }, spec: { template: { spec: podSpec(containers) } } })]]);
|
|
79
|
+
const [own, ref] = docs(serialize(entities, attrs))[0].spec.template.spec.containers;
|
|
80
|
+
expect(own.env).toEqual([
|
|
81
|
+
{ name: "PORT", value: "80" },
|
|
82
|
+
{ name: "OTEL_SERVICE_NAME", value: "checkout" },
|
|
83
|
+
RELEASE_ENV,
|
|
84
|
+
{ name: "OTEL_RESOURCE_ATTRIBUTES", value: "chant.member=mine,team=pay,chant.workspace=acme,chant.decl=api,deployment.environment.name=prod$(CHANT_RELEASE_ATTRIBUTES)" },
|
|
85
|
+
]);
|
|
86
|
+
expect(ref.env).toEqual([
|
|
87
|
+
{ name: "OTEL_RESOURCE_ATTRIBUTES", valueFrom: { configMapKeyRef: { name: "otel", key: "attrs" } } },
|
|
88
|
+
{ name: "OTEL_SERVICE_NAME", value: "api" },
|
|
89
|
+
]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("an env given as a map keeps its keys and gains the missing ones, without the release reference a map cannot order", () => {
|
|
93
|
+
const containers = [{ name: "api", image: "x", env: { PORT: "80", OTEL_SERVICE_NAME: "checkout" } }];
|
|
94
|
+
const entities = new Map([["api", mockResource("K8s::Core::Pod", { metadata: { name: "api" }, spec: podSpec(containers) })]]);
|
|
95
|
+
const env = docs(serialize(entities, { workspace: "acme" }))[0].spec.containers[0].env;
|
|
96
|
+
expect(env).toEqual({ PORT: "80", OTEL_SERVICE_NAME: "checkout", OTEL_RESOURCE_ATTRIBUTES: "chant.workspace=acme,chant.decl=api" });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("an image pinned by digest gives service.version, per container", () => {
|
|
100
|
+
const containers = [
|
|
101
|
+
{ name: "api", image: "ghcr.io/acme/api@sha256:abc123" },
|
|
102
|
+
{ name: "proxy", image: "envoyproxy/envoy:v1.31" },
|
|
103
|
+
];
|
|
104
|
+
const entities = new Map([["api", mockResource("K8s::Apps::Deployment", { metadata: { name: "api" }, spec: { template: { spec: podSpec(containers) } } })]]);
|
|
105
|
+
const [api, proxy] = docs(serialize(entities, { workspace: "acme" }))[0].spec.template.spec.containers;
|
|
106
|
+
expect(valueOf(api.env, "OTEL_RESOURCE_ATTRIBUTES")).toBe("chant.workspace=acme,chant.decl=api,service.version=sha256%3Aabc123$(CHANT_RELEASE_ATTRIBUTES)");
|
|
107
|
+
expect(valueOf(proxy.env, "OTEL_RESOURCE_ATTRIBUTES")).toBe("chant.workspace=acme,chant.decl=api$(CHANT_RELEASE_ATTRIBUTES)");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("init containers, non-workload kinds and a CRD that reuses a workload kind name are not stamped", () => {
|
|
111
|
+
const entities = new Map<string, unknown>([
|
|
112
|
+
["api", mockResource("K8s::Apps::Deployment", { metadata: { name: "api" }, spec: { template: { spec: { initContainers: [{ name: "init", image: "x" }], containers: [{ name: "api", image: "x" }] } } } })],
|
|
113
|
+
["svc", mockResource("K8s::Core::Service", { metadata: { name: "api" }, spec: { ports: [{ port: 80 }] } })],
|
|
114
|
+
["crd", mockResource("K8s::Apps::Deployment", { apiVersion: "example.com/v1", kind: "Deployment", metadata: { name: "x" }, spec: { template: { spec: podSpec([{ name: "x", image: "x" }]) } } })],
|
|
115
|
+
]);
|
|
116
|
+
const out = docs(serialize(entities, attrs));
|
|
117
|
+
const deployment = out.find((d) => d.apiVersion === "apps/v1")!;
|
|
118
|
+
expect(deployment.spec.template.spec.initContainers[0].env).toBeUndefined();
|
|
119
|
+
expect(deployment.spec.template.spec.containers[0].env).toHaveLength(3);
|
|
120
|
+
expect(JSON.stringify(out.filter((d) => d !== deployment))).not.toContain("OTEL_");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("a workload without metadata.name takes the name the serializer derives for it", () => {
|
|
124
|
+
const entities = new Map([["worker", mockResource("K8s::Batch::Job", { spec: { template: { spec: podSpec([{ name: "w", image: "x" }]) } } })]]);
|
|
125
|
+
const [doc] = docs(serialize(entities, { workspace: "acme" }));
|
|
126
|
+
expect(doc.spec.template.spec.containers[0].env[0]).toEqual({ name: "OTEL_SERVICE_NAME", value: doc.metadata.name });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("the release reference is added once, and the variable is always defined before it", () => {
|
|
130
|
+
const containers = [
|
|
131
|
+
{ name: "a", image: "x", env: [{ name: "OTEL_RESOURCE_ATTRIBUTES", value: "team=pay$(CHANT_RELEASE_ATTRIBUTES)" }] },
|
|
132
|
+
{ name: "b", image: "x", env: [{ name: "CHANT_RELEASE_ATTRIBUTES", value: ",team=ops" }] },
|
|
133
|
+
];
|
|
134
|
+
const entities = new Map([["api", mockResource("K8s::Apps::Deployment", { metadata: { name: "api" }, spec: { template: { spec: podSpec(containers) } } })]]);
|
|
135
|
+
const [a, b] = docs(serialize(entities, { workspace: "acme" }))[0].spec.template.spec.containers;
|
|
136
|
+
expect(valueOf(a.env, "OTEL_RESOURCE_ATTRIBUTES")).toBe("team=pay$(CHANT_RELEASE_ATTRIBUTES),chant.workspace=acme,chant.decl=api");
|
|
137
|
+
// The reference is already there, but the variable still has to be defined before it.
|
|
138
|
+
expect(a.env.map((e: Doc) => e.name)).toEqual(["CHANT_RELEASE_ATTRIBUTES", "OTEL_RESOURCE_ATTRIBUTES", "OTEL_SERVICE_NAME"]);
|
|
139
|
+
// The container's own definition comes first, so the reference reads it.
|
|
140
|
+
expect(b.env).toEqual([
|
|
141
|
+
{ name: "CHANT_RELEASE_ATTRIBUTES", value: ",team=ops" },
|
|
142
|
+
{ name: "OTEL_SERVICE_NAME", value: "api" },
|
|
143
|
+
{ name: "OTEL_RESOURCE_ATTRIBUTES", value: "chant.workspace=acme,chant.decl=api$(CHANT_RELEASE_ATTRIBUTES)" },
|
|
144
|
+
]);
|
|
145
|
+
});
|
|
146
|
+
});
|
package/src/serializer.ts
CHANGED
|
@@ -9,6 +9,8 @@ import { createRequire } from "module";
|
|
|
9
9
|
import type { Declarable } from "@intentius/chant/declarable";
|
|
10
10
|
import { isPropertyDeclarable, isResourceDeclarable } from "@intentius/chant/declarable";
|
|
11
11
|
import type { Serializer, SerializerResult, SerializeContext } from "@intentius/chant/serializer";
|
|
12
|
+
import { mergeResourceAttributes, RELEASE_ATTRIBUTES_VARIABLE, telemetryEnvironment, type TelemetryAttribution } from "@intentius/chant/telemetry-attribution";
|
|
13
|
+
import { podSpecOf, releaseAttributesEnv } from "./telemetry-release";
|
|
12
14
|
import { ownershipEntries, LABEL_OWNERSHIP_KEYS } from "@intentius/chant/ownership";
|
|
13
15
|
import {
|
|
14
16
|
isEffectReceipt,
|
|
@@ -300,6 +302,77 @@ function toYAMLValue(value: unknown, entityNames: Map<Declarable, string>): unkn
|
|
|
300
302
|
/**
|
|
301
303
|
* Kubernetes YAML serializer implementation.
|
|
302
304
|
*/
|
|
305
|
+
// ── Telemetry attribution (#2558, D22, #3059) ──────────────────────
|
|
306
|
+
|
|
307
|
+
/** The digest of an image reference pinned with `@sha256:...`, or undefined. */
|
|
308
|
+
function imageDigest(image: unknown): string | undefined {
|
|
309
|
+
if (typeof image !== "string") return undefined;
|
|
310
|
+
const at = image.lastIndexOf("@");
|
|
311
|
+
return at === -1 ? undefined : image.slice(at + 1);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Stamp `OTEL_SERVICE_NAME` and `OTEL_RESOURCE_ATTRIBUTES` into the `env` of
|
|
316
|
+
* each container of a workload. `service.name` is the workload's
|
|
317
|
+
* `metadata.name`, `chant.decl` its declaration id (`decl`), and an image
|
|
318
|
+
* pinned by digest gives `service.version`. A variable the container already
|
|
319
|
+
* sets is kept: its own `OTEL_SERVICE_NAME` wins, and its own resource
|
|
320
|
+
* attributes keep their keys while the missing ones are appended. One set
|
|
321
|
+
* through `valueFrom` is left alone. Init containers are not stamped.
|
|
322
|
+
*
|
|
323
|
+
* `OTEL_RESOURCE_ATTRIBUTES` ends with `$(CHANT_RELEASE_ATTRIBUTES)`, read
|
|
324
|
+
* from the pod annotation a release deploy sets (#3061, ws-081), so a
|
|
325
|
+
* release adds `service.version` and `vcs.ref.head.revision` without
|
|
326
|
+
* changing this output.
|
|
327
|
+
*/
|
|
328
|
+
function stampWorkloadTelemetry(manifest: Record<string, unknown>, decl: string, telemetry: TelemetryAttribution): void {
|
|
329
|
+
const podSpec = podSpecOf(manifest);
|
|
330
|
+
const containers = podSpec?.containers;
|
|
331
|
+
if (!Array.isArray(containers)) return;
|
|
332
|
+
const service = String((manifest.metadata as Record<string, unknown> | undefined)?.name ?? decl);
|
|
333
|
+
const releaseRef = `$(${RELEASE_ATTRIBUTES_VARIABLE})`;
|
|
334
|
+
for (const container of containers) {
|
|
335
|
+
if (!container || typeof container !== "object") continue;
|
|
336
|
+
const c = container as Record<string, unknown>;
|
|
337
|
+
const version = imageDigest(c.image);
|
|
338
|
+
const vars = telemetryEnvironment(telemetry, { service, decl, ...(version ? { version } : {}) });
|
|
339
|
+
if (c.env !== undefined && !Array.isArray(c.env) && typeof c.env === "object" && c.env !== null) {
|
|
340
|
+
// A map, as some composites and hand-written props give it before the API's list form. A map
|
|
341
|
+
// has no order, so it cannot hold the $(VAR) reference to the release attributes.
|
|
342
|
+
const map: Record<string, unknown> = { ...(c.env as Record<string, unknown>) };
|
|
343
|
+
if (!("OTEL_SERVICE_NAME" in map)) map.OTEL_SERVICE_NAME = vars.OTEL_SERVICE_NAME;
|
|
344
|
+
const own = map.OTEL_RESOURCE_ATTRIBUTES;
|
|
345
|
+
if (own === undefined) map.OTEL_RESOURCE_ATTRIBUTES = vars.OTEL_RESOURCE_ATTRIBUTES;
|
|
346
|
+
else if (typeof own === "string") map.OTEL_RESOURCE_ATTRIBUTES = mergeResourceAttributes(own, vars.OTEL_RESOURCE_ATTRIBUTES);
|
|
347
|
+
c.env = map;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
const env = Array.isArray(c.env) ? [...(c.env as Array<Record<string, unknown>>)] : [];
|
|
351
|
+
const at = (key: string) => env.findIndex((e) => e && typeof e === "object" && e.name === key);
|
|
352
|
+
if (at("OTEL_SERVICE_NAME") === -1) env.push({ name: "OTEL_SERVICE_NAME", value: vars.OTEL_SERVICE_NAME });
|
|
353
|
+
let i = at("OTEL_RESOURCE_ATTRIBUTES");
|
|
354
|
+
if (i === -1) {
|
|
355
|
+
env.push({ name: "OTEL_RESOURCE_ATTRIBUTES", value: vars.OTEL_RESOURCE_ATTRIBUTES });
|
|
356
|
+
i = env.length - 1;
|
|
357
|
+
} else if (typeof env[i]!.value === "string") {
|
|
358
|
+
env[i] = { ...env[i]!, value: mergeResourceAttributes(env[i]!.value as string, vars.OTEL_RESOURCE_ATTRIBUTES) };
|
|
359
|
+
}
|
|
360
|
+
// The release attributes (#3061, ws-081): a $(VAR) reference expands only to a variable defined
|
|
361
|
+
// earlier in the list, so the downward-API entry goes just before OTEL_RESOURCE_ATTRIBUTES. A
|
|
362
|
+
// container that defines the variable itself keeps its own, and gets the reference only when
|
|
363
|
+
// its definition comes first.
|
|
364
|
+
if (typeof env[i]!.value === "string") {
|
|
365
|
+
if (at(RELEASE_ATTRIBUTES_VARIABLE) === -1) {
|
|
366
|
+
env.splice(i, 0, releaseAttributesEnv());
|
|
367
|
+
i++;
|
|
368
|
+
}
|
|
369
|
+
const value = env[i]!.value as string;
|
|
370
|
+
if (at(RELEASE_ATTRIBUTES_VARIABLE) < i && !value.includes(releaseRef)) env[i] = { ...env[i]!, value: `${value}${releaseRef}` };
|
|
371
|
+
}
|
|
372
|
+
c.env = env;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
303
376
|
export const k8sSerializer: Serializer = {
|
|
304
377
|
name: "k8s",
|
|
305
378
|
rulePrefix: "WK8",
|
|
@@ -489,6 +562,8 @@ export const k8sSerializer: Serializer = {
|
|
|
489
562
|
}
|
|
490
563
|
}
|
|
491
564
|
|
|
565
|
+
if (context?.telemetry) stampWorkloadTelemetry(manifest, name, context.telemetry);
|
|
566
|
+
|
|
492
567
|
// Emit as YAML — sort Namespaces first so kubectl apply succeeds
|
|
493
568
|
const yamlDoc = emitK8sManifest(manifest);
|
|
494
569
|
if (gvk.kind === "Namespace") {
|
package/src/skills/chant-k8s.md
CHANGED
|
@@ -171,7 +171,7 @@ Composites are higher-level functions that produce multiple coordinated K8s reso
|
|
|
171
171
|
| Multi-host TLS Ingress (cert-manager) | **SecureIngress** | Ingress + optional Certificate |
|
|
172
172
|
| HPA metrics provider | **MetricsServer** | Deployment + Service + RBAC + APIService |
|
|
173
173
|
| CockroachDB cluster | **CockroachDbCluster** | StatefulSet + Services + PVCs + RBAC + optional CertificateRequests |
|
|
174
|
-
| OpenTelemetry agent on every node | **OtelCollector** | DaemonSet + node-local Service + RBAC + ConfigMap |
|
|
174
|
+
| OpenTelemetry agent on every node | **OtelCollector** | DaemonSet + node-local Service + RBAC + ConfigMap; a node-reading config (otel's `NodeAgent`) also gets the node name variable and read-only host mounts |
|
|
175
175
|
| OpenTelemetry gateway tier | **OtelCollectorGateway** | Deployment + ClusterIP and headless Services + PDB + ConfigMap; point agents at it with `gatewayExporter(gateway, { loadBalance: true })` |
|
|
176
176
|
|
|
177
177
|
### Decision Tree — AWS (EKS) Composites
|