Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions k8s/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,8 @@ kubectl -n devops get jobs -l app.kubernetes.io/name=devops-cli-job
kubectl -n devops logs -f job/<name>
```

The spans and the log records of devops-cli name the cluster in `k8s.cluster.name` and the namespace in `k8s.namespace.name`. The tracer reads them from the environment values `K8S_CONTEXT` and `POD_NAMESPACE`. With no `K8S_CONTEXT`, it uses the setting `k8s.context`, whose default is `minikube`. So each devops-cli container in `k8s/devops/` sets `K8S_CONTEXT` to the cluster name of the monitoring stack (`cluster.name` in `monitoring/k8s-monitoring-values.yaml`) and `POD_NAMESPACE` to the namespace of its pod, from the Downward API. The runner template `devops-cli-runner` sets the namespace as a plain value, because the admission policy `roadmap-service-jobs` refuses `valueFrom` in a Job of the Service. The check `tests/gate/k8s_checks/telemetry_resource.py` of the `k8s` row of `devops ci` fails when a container does not set both (#4512).

### Roadmap service

`deployment/roadmap-service` runs one replica. The Service acts as GitHub App `devops-cli-service`, which the personal account owns ([ADR 0015](../docs/adr/0015-roadmap-jobs-act-as-a-github-app-on-the-personal-account.md)), on each repository in `service.repos`. Labels and milestones hold the roadmap state ([ADR 0016](../docs/adr/0016-labels-hold-the-roadmap-items-fields.md)). Argo CD self-heals the deployment, so only a change in git changes its replica count. A scale by hand stays only after the stop switch turns off automated sync. The Roadmap service container `service` requests `768Mi` and limits `3Gi` of memory. The raise is a stopgap for a working set that grows 100 to 150 MiB each hour (#4116 holds the analysis, #4125 the flat check), and `RoadmapServiceMemoryNearLimit` fires before a kill.
Expand Down
9 changes: 9 additions & 0 deletions k8s/devops/cronjob-runner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ spec:
# jobs.
- name: OTEL_SERVICE_NAME
value: devops-cli-job
# The spans and the log records name the cluster (`k8s.cluster.name`) and the
# namespace (`k8s.namespace.name`) from these two values. The cluster name is the
# name in k8s/monitoring/k8s-monitoring-values.yaml; without it, they name
# minikube. The admission policy `roadmap-service-jobs` refuses `valueFrom` in a
# Job of the Service, so the namespace is a plain value.
- name: K8S_CONTEXT
value: homelab-k3s
- name: POD_NAMESPACE
value: devops
# `pytest-xdist` starts one worker for each CPU of the node, not of the pod
# (`-n logical`, 280 MB each). On a node with 48 CPUs that is about 13 GB in a pod
# with 2 CPUs (#2806). The value equals the CPU limit; a test checks it. The shell
Expand Down
10 changes: 10 additions & 0 deletions k8s/devops/cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ spec:
# workstation runs (devops-cli) and roadmap-service.
- name: OTEL_SERVICE_NAME
value: devops-cli-job
# The spans and the log records name the cluster (`k8s.cluster.name`) and the
# namespace (`k8s.namespace.name`) from these two values. The cluster name is the
# name in k8s/monitoring/k8s-monitoring-values.yaml; without it, they name
# minikube.
- name: K8S_CONTEXT
value: homelab-k3s
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: tmp
mountPath: /tmp
Expand Down
9 changes: 9 additions & 0 deletions k8s/devops/mcp-server/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ spec:
value: /home/devops/.data
- name: OTEL_SERVICE_NAME
value: devops-mcp-server
# The spans and the log records name the cluster (`k8s.cluster.name`) and the
# namespace (`k8s.namespace.name`) from these two values. The cluster name is the
# name in k8s/monitoring/k8s-monitoring-values.yaml; without it, they name minikube.
- name: K8S_CONTEXT
value: homelab-k3s
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: DO_NOT_TRACK
value: "1"
- name: OPENSPEC_TELEMETRY
Expand Down
9 changes: 9 additions & 0 deletions k8s/devops/roadmap-service/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ spec:
# and the Recreate strategy mean one pod sends at a time, so all pods share one id.
- name: OTEL_RESOURCE_ATTRIBUTES
value: service.instance.id=roadmap-service
# The spans and the log records name the cluster (`k8s.cluster.name`) and the
# namespace (`k8s.namespace.name`) from these two values. The cluster name is the
# name in k8s/monitoring/k8s-monitoring-values.yaml; without it, they name minikube.
- name: K8S_CONTEXT
value: homelab-k3s
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: DO_NOT_TRACK
value: "1"
- name: OPENSPEC_TELEMETRY
Expand Down
9 changes: 9 additions & 0 deletions k8s/devops/workflowtemplate-rag-ingest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ spec:
value: /home/devops/.data
- name: OTEL_SERVICE_NAME
value: devops-cli-rag-ingest
# The spans and the log records name the cluster (`k8s.cluster.name`) and the
# namespace (`k8s.namespace.name`) from these two values. The cluster name is the name
# in k8s/monitoring/k8s-monitoring-values.yaml; without it, they name minikube.
- name: K8S_CONTEXT
value: homelab-k3s
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://otel-collector.otel.svc.cluster.local:4318
- name: DEVOPS_CLI_AI_GATEWAY_CONCURRENCY_EMBEDDING
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema: spec-driven
created: 2026-11-11
skip_specs: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Design

## Decisions

- **Environment values, no code.** The tracer already reads `K8S_CONTEXT` and `POD_NAMESPACE` first. A value in each pod spec corrects the name, and the workstation keeps the setting `k8s.context`.
- **The cluster name of the monitoring stack.** The metrics of Alloy name the cluster `homelab-k3s` (`cluster.name` in `k8s/monitoring/k8s-monitoring-values.yaml`), and the collector writes the same `cluster` label. The spans use the same name, so a query can join the two. The check reads the name from that file, so the two cannot differ.
- **The namespace from the Downward API.** The standard way to give a pod its namespace. The runner template sets the plain value `devops`, because the admission policy `roadmap-service-jobs` refuses `valueFrom` in a Job of the Service. The check holds the plain value equal to the namespace of the CronJob.
- **A check of the render.** A manifest gets no pytest test (docs/TESTING.md). The check of the `k8s` row reads the render that Argo CD applies and lists the 5 devops-cli containers by name, so a new devops-cli container without the values fails it.
- **A test of the names.** The check holds the manifests, and the test holds the tracer to the same two names.

## Rejected alternatives

- **The Downward API in the runner template too.** Rejected: the admission policy refuses `valueFrom` in a Job of the Service, and the check `job_admission_policy` fails on it. To allow `fieldRef` in the policy would weaken a security control for one label.
- **`OTEL_RESOURCE_ATTRIBUTES` with `k8s.cluster.name` and `k8s.namespace.name`.** Rejected: the span resource of devops-cli does not read this variable (`OTelTelemetryClient`), only the metric resource does. #2536 owns the move to the SDK resource.
- **`k8s.context` in the ConfigMap.** Rejected: the setting also names the kubeconfig context of the commands (`src/devops_cli/k8s/api.py`), so a telemetry label would change what the commands connect to. The live ConfigMap is also outside Argo CD, and this item does not change it.
- **A ConfigMap that holds the cluster name.** Rejected: a generated name with a hash needs a name reference in the WorkflowTemplate, which Kustomize does not change, and one literal value per container is simpler. The check holds the 5 values equal.
- **Remove the `minikube` default of `k8s.context`.** Rejected for this item: the default is the target of the workstation commands, not only a telemetry label.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Proposal: #4512

## Why

Issue #4512: the spans and the OTLP log records of the devops-cli pods name the wrong cluster. All 1,705 `roadmap-service` spans of 2026-11-11 03:50Z-04:04Z have `k8s.cluster.name=minikube`, and the runner log records have `k8s_cluster_name=minikube`. The workstation spans have `homelab-k3s`, the cluster name of the monitoring stack. The pod spans also have no `k8s.namespace.name`.

`_detect_k8s_context` in `src/devops_cli/telemetry/tracer.py` reads `K8S_CONTEXT` and `POD_NAMESPACE` from the environment first. No pod spec in `k8s/devops/` sets them, so the tracer uses the setting `k8s.context`, whose default is `minikube`.

## What Changes

- Each devops-cli container in `k8s/devops/` sets `K8S_CONTEXT` to `homelab-k3s`, the cluster name in `k8s/monitoring/k8s-monitoring-values.yaml`, and `POD_NAMESPACE` to the namespace of its pod. The containers are those of Deployment `roadmap-service`, Deployment `mcp-server`, CronJob `devops-cli`, CronJob `devops-cli-runner` (the runner Job template) and WorkflowTemplate `rag-ingest`: 5 containers, counted from the render of `kubectl kustomize k8s/devops`.
- `POD_NAMESPACE` comes from the Downward API (`metadata.namespace`). The runner template is the exception: the admission policy `roadmap-service-jobs` refuses an env entry with `valueFrom` in a Job of the Service, so the template sets the plain value `devops`.
- A new check of the `k8s` row of `devops ci`, `tests/gate/k8s_checks/telemetry_resource.py`, fails when one of the 5 containers does not set both values, or when the render has another devops-cli container.
- A new test in `tests/telemetry/test_telemetry.py` holds the names of the two environment values that the tracer reads.
- `k8s/README.md` (section devops-cli in the cluster) describes the two values.
- The manifests change what the pods send, so the change has a spec delta.
- Not changed: the code of the tracer, the setting `k8s.context`, `service.instance.id` and `k8s.pod.name`.

## Capabilities

### New Capabilities

None.

### Modified Capabilities

- `command-telemetry`: a new requirement, "A devops-cli pod names its cluster and its namespace".

## Impact

- `k8s/devops/roadmap-service/deployment.yaml`, `k8s/devops/mcp-server/deployment.yaml`, `k8s/devops/cronjob.yaml`, `k8s/devops/cronjob-runner.yaml`, `k8s/devops/workflowtemplate-rag-ingest.yaml`, `k8s/README.md`.
- `tests/gate/k8s_checks/telemetry_resource.py`, `tests/telemetry/test_telemetry.py`.
- Argo CD applies the manifests after the merge. The live ConfigMap and the Secrets need no change. The change fits one pull request.

## Feasibility

Read on 2026-11-11.

- `src/devops_cli/telemetry/tracer.py`, `_detect_k8s_context`: `ctx = os.getenv("K8S_CONTEXT") or os.getenv("KUBECONFIG_CONTEXT")` and `ns = os.getenv("K8S_NAMESPACE") or os.getenv("POD_NAMESPACE")`. Only when `K8S_CONTEXT` is not set does it read `k8s.context` from the settings. The span resource takes `k8s.cluster.name` from this function, and each span takes `k8s.namespace.name`. The OTLP log records use the same resource. No other code in `src/` reads these two names (`git grep`), so the values change only the telemetry.
- The study of 2026-11-11 measured the wrong name with TraceQL `{ } | count_over_time() by (resource.service.name, resource.k8s.cluster.name)`.
- `k8s/devops/roadmap-service/admissionpolicy.yaml` holds the rule `c.env.all(e, !has(e.valueFrom))` for a Job of the Service. The check `tests/gate/k8s_checks/job_admission_policy.py` runs the policy against a Job that `build_job` makes from the runner template. With the plain value it passes (`uv run devops ci --only k8s`: 104 cases, 0 failed).
- The name `homelab-k3s` is already in `k8s/monitoring/k8s-monitoring-values.yaml`, `k8s/monitoring/prometheus-values.yaml` and `k8s/otel/values.yaml`. It is a cluster name, not a host name.
- Kubernetes documents `fieldRef` with `metadata.namespace` for container environment values (https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/). Argo Workflows takes a Kubernetes container spec in a template, so the WorkflowTemplate takes the same entry.

## Existing Tool Considered

The Kubernetes Downward API gives the namespace, and no code is built. The standard `OTEL_RESOURCE_ATTRIBUTES` is not used, because the span resource of devops-cli does not read it (only the metric resource does); #2536 moves the spans to the OpenTelemetry SDK. The change deletes no internal code.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## ADDED Requirements

### Requirement: A devops-cli pod names its cluster and its namespace
Each devops-cli container in the render of `k8s/devops` SHALL set `K8S_CONTEXT` to the cluster name in `k8s/monitoring/k8s-monitoring-values.yaml` and `POD_NAMESPACE` to the namespace of its object. The tracer SHALL put the value of `K8S_CONTEXT` in the resource attribute `k8s.cluster.name` and the value of `POD_NAMESPACE` in the span attribute `k8s.namespace.name`.

#### Scenario: The manifests are rendered
- **WHEN** `kubectl kustomize k8s/devops` renders the Deployments, the CronJobs and the WorkflowTemplate
- **THEN** each devops-cli container sets `K8S_CONTEXT` to the cluster name of the monitoring stack, and `POD_NAMESPACE` from the Downward API, or as the plain value `devops` in the runner template

#### Scenario: A pod starts a span
- **WHEN** `K8S_CONTEXT` and `POD_NAMESPACE` are set and a span starts
- **THEN** the resource holds `k8s.cluster.name` with the value of `K8S_CONTEXT`, and the span holds `k8s.namespace.name` with the value of `POD_NAMESPACE`
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Tasks

## 1. The pod specs

- [x] 1.1 Add the check `tests/gate/k8s_checks/telemetry_resource.py` and the test of the two names in `tests/telemetry/test_telemetry.py`, and see the check fail on the old manifests
- Verification: `$ uv run devops ci --only k8s`
- [x] 1.2 Set `K8S_CONTEXT` and `POD_NAMESPACE` in the 5 devops-cli containers of `k8s/devops/`
- Verification: `$ uv run devops ci --only k8s`
- Verification: `$ uv run devops ci test tests/telemetry/test_telemetry.py`

## 2. Documents

- [x] 2.1 Describe the two values in `k8s/README.md` (section devops-cli in the cluster)
- Verification: `$ uv run devops ci docs`

## Workflow follow-up

- Pending a person: after Argo CD syncs the change, run the TraceQL `{ resource.service.name = "roadmap-service" } | count_over_time() by (resource.k8s.cluster.name)` and see `homelab-k3s` for the new spans.
11 changes: 11 additions & 0 deletions openspec/specs/command-telemetry/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,14 @@ The dashboard `k8s/monitoring/dashboards/devops-cli.json` SHALL have the multi-v
#### Scenario: Each query filters by job
- **WHEN** each Prometheus query of the dashboard file is read
- **THEN** each `devops_cli_*` series in the query has a selector that starts with `job=~"$job"`

### Requirement: A devops-cli pod names its cluster and its namespace
Each devops-cli container in the render of `k8s/devops` SHALL set `K8S_CONTEXT` to the cluster name in `k8s/monitoring/k8s-monitoring-values.yaml` and `POD_NAMESPACE` to the namespace of its object. The tracer SHALL put the value of `K8S_CONTEXT` in the resource attribute `k8s.cluster.name` and the value of `POD_NAMESPACE` in the span attribute `k8s.namespace.name`.

#### Scenario: The manifests are rendered
- **WHEN** `kubectl kustomize k8s/devops` renders the Deployments, the CronJobs and the WorkflowTemplate
- **THEN** each devops-cli container sets `K8S_CONTEXT` to the cluster name of the monitoring stack, and `POD_NAMESPACE` from the Downward API, or as the plain value `devops` in the runner template

#### Scenario: A pod starts a span
- **WHEN** `K8S_CONTEXT` and `POD_NAMESPACE` are set and a span starts
- **THEN** the resource holds `k8s.cluster.name` with the value of `K8S_CONTEXT`, and the span holds `k8s.namespace.name` with the value of `POD_NAMESPACE`
85 changes: 85 additions & 0 deletions tests/gate/k8s_checks/telemetry_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""The cluster and the namespace that the devops-cli pods name in their telemetry (#4512).

The tracer names `k8s.cluster.name` and `k8s.namespace.name` from the environment values
`K8S_CONTEXT` and `POD_NAMESPACE` (`_detect_k8s_context` in src/devops_cli/telemetry/tracer.py).
With no `K8S_CONTEXT`, it uses the setting `k8s.context`, whose default is `minikube`, so each
span and log record of a pod named a cluster that does not exist.
"""

from __future__ import annotations

from collections.abc import Iterator
from typing import Any

import yaml

from tests.gate.k8s_render import applied_documents
from tests.support.paths import REPO_ROOT

DEVOPS_DIR = REPO_ROOT / "k8s" / "devops"


# The cluster name of the metrics that Alloy sends. The pods give their spans the same name.
MONITORING_VALUES = REPO_ROOT / "k8s" / "monitoring" / "k8s-monitoring-values.yaml"


DEVOPS_CLI_IMAGE = "ghcr.io/dan-petty/devops-cli/"


NAMESPACE_FROM_DOWNWARD_API = {"fieldRef": {"fieldPath": "metadata.namespace"}}


def _containers(document: dict[str, Any]) -> Iterator[tuple[str, dict[str, Any]]]:
"""The name and the spec of each container of a rendered object that has a pod template."""
spec = document.get("spec", {})
match document["kind"]:
case "Deployment":
pod = spec["template"]["spec"]
case "CronJob":
pod = spec["jobTemplate"]["spec"]["template"]["spec"]
case "WorkflowTemplate":
for template in spec.get("templates", []):
if "container" in template:
yield template["name"], template["container"]
return
case _:
return
for container in pod["containers"]:
yield container["name"], container


def _named_namespace(entry: dict[str, Any] | None, namespace: str) -> str | None:
"""The namespace that a `POD_NAMESPACE` entry gives in an object of `namespace`."""
if entry is None:
return None
if entry.get("valueFrom") == NAMESPACE_FROM_DOWNWARD_API:
return namespace
value: str | None = entry.get("value")
return value


def check_each_devops_cli_container_names_the_cluster_and_its_namespace() -> None:
"""Each devops-cli container sets `K8S_CONTEXT` to the cluster name of the monitoring stack,
and `POD_NAMESPACE` to the namespace of its object: from the Downward API, or as a plain value
where the admission policy `roadmap-service-jobs` refuses `valueFrom` (the runner template)."""
monitoring = yaml.safe_load(MONITORING_VALUES.read_text(encoding="utf-8"))
cluster = monitoring["cluster"]["name"]
named: dict[tuple[str, str, str], tuple[str | None, str | None]] = {}
for document in applied_documents(DEVOPS_DIR):
namespace = document["metadata"].get("namespace", "")
for name, container in _containers(document):
if not str(container.get("image", "")).startswith(DEVOPS_CLI_IMAGE):
continue
env = {entry["name"]: entry for entry in container.get("env", [])}
context = env.get("K8S_CONTEXT", {}).get("value")
named[(document["kind"], document["metadata"]["name"], name)] = (
context,
_named_namespace(env.get("POD_NAMESPACE"), namespace),
)
assert named == {
("CronJob", "devops-cli", "devops-cli"): (cluster, "devops"),
("CronJob", "devops-cli-runner", "runner"): (cluster, "devops"),
("Deployment", "mcp-server", "mcp-server"): (cluster, "devops"),
("Deployment", "roadmap-service", "service"): (cluster, "devops"),
("WorkflowTemplate", "rag-ingest", "ingest"): (cluster, "devops"),
}
Loading
Loading