diff --git a/k8s/README.md b/k8s/README.md index a8ece07e7e..d9c9adfda5 100644 --- a/k8s/README.md +++ b/k8s/README.md @@ -1116,6 +1116,7 @@ This repository is public, so it holds no homelab value. Ingress hosts sit under | `service.repos`, `service.github_app.app_id`, `service.github_app.installation_id`, `service.github_app.slug`, `service.poll_interval_seconds` | `config.yaml` | ConfigMap `devops-cli-config`, which no Application owns | `--stack devops` or `all` | | `service.github_requests_per_hour`, only when a source sets it | `config.yaml` or `DEVOPS_CLI_SERVICE_GITHUB_REQUESTS_PER_HOUR` | ConfigMap `devops-cli-config`. With no value set, the ConfigMap holds none, and the Service uses its default of 500 | `--stack devops` or `all` | | Hosts under the configured domain | `--domain`, else `k8s.domain`, `cloudflare.domain`, `DEVOPS_CLI_K8S_DOMAIN` or `DEVOPS_CLI_DOMAIN` | Kustomize patches in `spec.source.kustomize` of the Applications `devops` and `ingress` | `--stack devops` or `all` (both Applications), `--stack infra` or `gitea` (`ingress` only) | +| Container environment values under the configured domain: each `env[].value` of a container or init container of a Deployment that holds the placeholder domain, such as `HOMEPAGE_VAR_DOMAIN` | the same as the hosts | Kustomize patches in `spec.source.kustomize` of the Application that renders the Deployment | the same stacks as the hosts of that Application | To change the hourly GitHub request budget of the Service, run `devops config set service.github_requests_per_hour `, then `devops k8s deploy-stack --stack devops`. The Service shows the budget it runs with in `devops_cli_service_budget_limit_requests{job="roadmap-service"}`, and the alert `RoadmapServiceGitHubRequestsHigh` follows that value. @@ -1131,7 +1132,7 @@ Unlike the ConfigMap, the host patches do not depend on the checkout. deploy-sta The `cluster` Application ignores `spec.source.kustomize` on the two Applications in its diff. It applies the two Applications server-side, because each has the annotation `argocd.argoproj.io/sync-options: ServerSideApply=true`. Argo CD reads that annotation in every sync, automated or by hand, with any sync options. Server-side apply deletes only the fields that its own field manager, `argocd-controller`, set. The host patches belong to the field manager of deploy-stack's `kubectl patch`, and the image digest on `devops` belongs to Image Updater, so a sync of `cluster` keeps them. Do not remove the annotation. Client-side apply deletes each field that the `last-applied-configuration` annotation holds and git does not. The `RespectIgnoreDifferences=true` option of `cluster` writes the patches into that annotation, so a sync by hand without that option deletes them (#1817). The annotation `argocd.argoproj.io/client-side-apply-migration-manager: argocd-controller` on `cluster` moves the fields of the old client-side apply entry to the server-side apply entry in the first server-side sync. Thus a field that git removes from the two Applications also leaves the cluster. -Each patch names one Ingress or IngressRoute and tests the placeholder host at each position before replacing it, so a host that git reorders or removes within that object fails the Application's manifest generation instead of routing to another backend. Kustomize skips a patch whose object no longer exists, and nothing covers an object or host that git adds: run `devops k8s deploy-stack --stack devops` again once such a change reaches the Applications' revision. deploy-stack renders everything before it writes anything, so an unset setting stops it with an error naming the setting and changes nothing in the cluster. An unset service setting stops the dry run too; an unset domain stops only an Argo CD-managed deploy, and the dry run says so. A revision whose Application renders no host under the placeholder is refused rather than given an empty patch list. +Each patch names one object (an Ingress, an IngressRoute, a Mattermost or a Deployment) and tests the placeholder host or environment value at each position before replacing it, so a value that git reorders or removes within that object fails the Application's manifest generation instead of routing to another backend. Kustomize skips a patch whose object no longer exists, and nothing covers an object, host or environment value that git adds: run `devops k8s deploy-stack --stack devops` again once such a change reaches the Applications' revision. deploy-stack renders everything before it writes anything, so an unset setting stops it with an error naming the setting and changes nothing in the cluster. An unset service setting stops the dry run too; an unset domain stops only an Argo CD-managed deploy, and the dry run says so. A revision whose Application renders no host under the placeholder is refused rather than given an empty patch list. These values exist only in the cluster and in `config.yaml`, until Argo CD reads the homelab's private configuration from a repository on the in-cluster Gitea (#1291). A sync keeps the host patches, but these actions remove them: diff --git a/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/.openspec.yaml b/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/.openspec.yaml new file mode 100644 index 0000000000..9d55295e5d --- /dev/null +++ b/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/.openspec.yaml @@ -0,0 +1,3 @@ +schema: spec-driven +created: 2026-11-11 +skip_specs: false diff --git a/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/design.md b/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/design.md new file mode 100644 index 0000000000..0503434c6a --- /dev/null +++ b/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/design.md @@ -0,0 +1,12 @@ +# Design + +## Decisions +- **Only a Deployment, only `env[].value`.** The start page is a Deployment, and its two variables are literal `env[].value` fields. A value from `valueFrom` holds no text to replace. A StatefulSet, a DaemonSet or a CronJob gets the same field when a manifest needs it. +- **Only a value that holds the placeholder.** `_host_fields` returns an environment value only when `holds_template_domain_value` is true. The Argo CD gate reads the same pointers as the allowed places of the placeholder. If `_host_fields` returned each value, the gate would allow the placeholder in no more places, but the list would no longer say which values deploy-stack changes. +- **Init containers before containers.** The pointers come in the order of the fields in the pod spec of the API, so a patch reads in the same order as `kubectl get -o yaml`. +- **Test, then replace.** Each value gets a `test` operation before its `replace`, as each host does. A value that git moves or changes in its object then fails the manifest generation of the Application, and does not put the domain into another variable. + +## Rejected alternatives +- **Kustomize `replacements`.** It copies one field or one delimited segment of a field from a source object. It cannot replace the domain inside `$(MY_POD_IP):3000,start.example.com`, and the source object would have to hold the real domain in git. +- **A ConfigMap that deploy-stack writes, read by `envFrom`.** It adds an object that no Application owns, for each Application that needs the domain. The patch builder already covers this through the Application. +- **Each environment value of every workload kind.** No manifest needs more than a Deployment now. A wider scan changes more objects with no test against a real need. diff --git a/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/proposal.md b/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/proposal.md new file mode 100644 index 0000000000..8578f378f8 --- /dev/null +++ b/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/proposal.md @@ -0,0 +1,26 @@ +# Proposal: #4542 + +## Why +The cluster start page (Homepage) needs the homelab domain in two container environment values: `HOMEPAGE_VAR_DOMAIN`, which each link uses as `https://.{{HOMEPAGE_VAR_DOMAIN}}/`, and `HOMEPAGE_ALLOWED_HOSTS`, the host check. Git holds only the placeholder domain `example.com` (AGENTS.md rule 10). deploy-stack writes the real hosts as Kustomize patches onto the homelab Applications, but its patch builder reads only Ingress and TLS hosts, IngressRoute `match` and the Mattermost host. A container `env[].value` that holds the placeholder thus stays `example.com` in the cluster. Closes #4542. + +## What Changes +- `_host_fields` in `src/devops_cli/k8s/argocd_overrides.py` also returns each `env[].value` of a container or init container of a Deployment that holds the placeholder domain (`holds_template_domain_value`). `host_patches` tests and then replaces each such value, as it does for a host. +- The offline Argo CD gate (`check_argocd_gate`) reads the same fields, so it now allows the placeholder in such a value of a homelab Application. It still refuses the placeholder in each other field of a Deployment. +- `k8s/README.md`, "Homelab Values at Deploy Time", lists the new value and names the Deployment in the paragraph about the patches. + +## Capabilities +- Modified: `gitops-host-overrides`. It adds the requirement "deploy-stack sets the configured domain in container environment values". + +## Impact +- No object in the three homelab Applications holds such a value today. deploy-stack thus writes the same patches as before on `main`. +- The start page Deployment (a later issue) can put the placeholder in its environment values, and deploy-stack sets the real domain there. +- No new dependency, setting or command. + +## Feasibility +- Source read on `origin/main` (5f75d256): `_host_fields` (`src/devops_cli/k8s/argocd_overrides.py:112-137`) reads only the kinds `Ingress`, `IngressRoute` and `Mattermost`. `holds_template_domain_value` (`src/devops_cli/k8s/template.py:69-78`) finds the placeholder and the template variables, and `render_manifest_template` replaces both. `check_argocd_gate` allows the placeholder only at the pointers of `_host_fields` (`src/devops_cli/k8s/argocd_gate.py:169`), so on `main` it refuses a placeholder in an environment value. +- A JSON patch (RFC 6902) `test` and `replace` operation takes any JSON pointer, so `/spec/template/spec/containers//env//value` works as the host pointers do. Kustomize applies these patches by `target` (the gate check `tests/gate/k8s_checks/host_overrides.py` builds them for each homelab Application). +- Same patches on `main`: a scratch script rendered `devops`, `ingress` and `mattermost` with `kubectl kustomize` and compared `host_patches` of `origin/main` with this branch. The output was `devops: objects=1 ops=2 same=True`, `ingress: objects=19 ops=46 same=True`, `mattermost: objects=1 ops=2 same=True`. +- The start page study recorded the Homepage variables from the upstream documents (https://gethomepage.dev/installation/, https://gethomepage.dev/configs/kubernetes/). + +## Existing Tool Considered +Argo CD and Kustomize have no domain variable for a container environment value. Kustomize `replacements` copies one field or one segment of a field, and does not replace a substring in many values. The existing patch builder is thus extended. Cost: about 150,000 tokens and 2 hours to build, about 5,000 tokens a month to keep, about 30,000 tokens to fix. diff --git a/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/specs/gitops-host-overrides/spec.md b/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/specs/gitops-host-overrides/spec.md new file mode 100644 index 0000000000..3bd799b765 --- /dev/null +++ b/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/specs/gitops-host-overrides/spec.md @@ -0,0 +1,12 @@ +## ADDED Requirements + +### Requirement: deploy-stack sets the configured domain in container environment values +For each Deployment in the render of a homelab Application, `host_patches` SHALL test and then replace each `env[].value` of a container or init container that holds the placeholder domain. It SHALL change no other value, and the Argo CD gate SHALL allow the placeholder in such a value only. + +#### Scenario: A Deployment holds the placeholder in three env values +- **WHEN** `host_patches` reads a Deployment with the placeholder in an init container value, two container values, an argument and an annotation, and with a value without it +- **THEN** the patch has a `test` and a `replace` operation for each of the three env values with the placeholder, and none for the argument, the annotation or the other value + +#### Scenario: The Argo CD gate reads such a Deployment +- **WHEN** the Application `devops` renders a Deployment with the placeholder in an env value and in an argument +- **THEN** the gate refuses only the argument diff --git a/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/tasks.md b/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/tasks.md new file mode 100644 index 0000000000..33fd46f706 --- /dev/null +++ b/openspec/changes/archive/2026-11-11-4542-set-the-homelab-domain-in-container/tasks.md @@ -0,0 +1,13 @@ +# Tasks + +## 1. Behavior + +- [x] 1.1 Write the failing tests: a Deployment env value with the placeholder gets a test and a replace operation, a value without it and the placeholder in an annotation or an argument get none, and the Argo CD gate allows the placeholder only in the env value + - Verification: `$ uv run devops ci test tests/k8s/test_k8s_argocd_overrides.py tests/k8s/test_k8s_argocd_gate.py` +- [x] 1.2 Extend `_host_fields` with the env values of the containers and init containers of a Deployment + - Verification: `$ uv run devops ci test tests/k8s/test_k8s_argocd_overrides.py tests/k8s/test_k8s_argocd_gate.py` + +## 2. Documents + +- [x] 2.1 Add the row of the env values to "Homelab Values at Deploy Time" in `k8s/README.md`, and name the Deployment in the paragraph about the patches + - Verification: `$ uv run devops ci docs` diff --git a/openspec/specs/gitops-host-overrides/spec.md b/openspec/specs/gitops-host-overrides/spec.md index 5aeda561bb..e903bb5322 100644 --- a/openspec/specs/gitops-host-overrides/spec.md +++ b/openspec/specs/gitops-host-overrides/spec.md @@ -1,7 +1,7 @@ # gitops-host-overrides Specification ## Purpose -How the deploy-time host overrides on the Argo CD Applications `devops` and `ingress` stay in place through a sync of the app-of-apps `cluster`, and the alert that fires when a live Ingress routes a host under the placeholder domain of git. +Which hosts and container environment values the deploy-time overrides set on the homelab Argo CD Applications, how the overrides stay in place through a sync of the app-of-apps `cluster`, and the alert that fires when a live Ingress routes a host under the placeholder domain of git. ## Requirements @@ -29,3 +29,14 @@ The alert rule `IngressServesPlaceholderHost` in the group `homelab-services` of - **WHEN** a test reads `k8s/monitoring/k8s-monitoring-values.yaml` and the kube-state-metrics capture `tests/fixtures/metrics/kube-state-metrics.yaml` - **THEN** `kube_ingress_path` is in `clusterMetrics.kube-state-metrics.metricsTuning.includeMetrics` - **AND** the capture gives `kube_ingress_path` the labels `host`, `ingress` and `namespace` + +### Requirement: deploy-stack sets the configured domain in container environment values +For each Deployment in the render of a homelab Application, `host_patches` SHALL test and then replace each `env[].value` of a container or init container that holds the placeholder domain. It SHALL change no other value, and the Argo CD gate SHALL allow the placeholder in such a value only. + +#### Scenario: A Deployment holds the placeholder in three env values +- **WHEN** `host_patches` reads a Deployment with the placeholder in an init container value, two container values, an argument and an annotation, and with a value without it +- **THEN** the patch has a `test` and a `replace` operation for each of the three env values with the placeholder, and none for the argument, the annotation or the other value + +#### Scenario: The Argo CD gate reads such a Deployment +- **WHEN** the Application `devops` renders a Deployment with the placeholder in an env value and in an argument +- **THEN** the gate refuses only the argument diff --git a/src/devops_cli/k8s/argocd_overrides.py b/src/devops_cli/k8s/argocd_overrides.py index 2f9d90b2fd..1b16619df6 100644 --- a/src/devops_cli/k8s/argocd_overrides.py +++ b/src/devops_cli/k8s/argocd_overrides.py @@ -1,11 +1,11 @@ """Argo CD Application overrides that carry the homelab's hosts from the active config. -The repository names every Ingress and IngressRoute host under the placeholder domain -(``CONST_K8S_TEMPLATE_DOMAIN_PLACEHOLDER``). ``devops k8s deploy-stack`` builds what each -homelab Application renders at the revision Argo CD builds, and writes the configured domain onto -the Application as Kustomize JSON patches in ``spec.source.kustomize``, which the ``cluster`` -app-of-apps leaves alone (``k8s/argocd/bootstrap/cluster.yaml``). No homelab value enters git -(#1290). +The repository names every Ingress and IngressRoute host, and each Deployment environment value +that holds a host, under the placeholder domain (``CONST_K8S_TEMPLATE_DOMAIN_PLACEHOLDER``). +``devops k8s deploy-stack`` builds what each homelab Application renders at the revision Argo CD +builds, and writes the configured domain onto the Application as Kustomize JSON patches in +``spec.source.kustomize``, which the ``cluster`` app-of-apps leaves alone +(``k8s/argocd/bootstrap/cluster.yaml``). No homelab value enters git (#1290, #4542). """ from __future__ import annotations @@ -25,7 +25,11 @@ CONST_K8S_ARGOCD_FETCH_TIMEOUT_SECONDS, ) from devops_cli.exceptions.k8s import KubernetesContextError -from devops_cli.k8s.template import normalize_domain, render_manifest_template +from devops_cli.k8s.template import ( + holds_template_domain_value, + normalize_domain, + render_manifest_template, +) Run = Callable[..., subprocess.CompletedProcess[str]] """Runs a command as ``devops_cli.commands.k8s.cluster_runtime._run_cmd`` does.""" @@ -109,9 +113,27 @@ def render_at_revision(source: ApplicationSource, workdir: Path, run: Run) -> st return _stdout(run, ["kubectl", "kustomize", str(target)]) +def _env_fields(pod_spec: dict[str, Any]) -> list[tuple[str, str]]: + """The JSON pointer and value of each container ``env[].value`` that holds the placeholder.""" + return [ + (f"/spec/template/spec/{group}/{i}/env/{j}/value", var["value"]) + for group in ("initContainers", "containers") + for i, container in enumerate(pod_spec.get(group) or []) + if isinstance(container, dict) + for j, var in enumerate(container.get("env") or []) + if isinstance(var, dict) + and isinstance(var.get("value"), str) + and holds_template_domain_value(var["value"]) + ] + + def _host_fields(doc: dict[str, Any]) -> list[tuple[str, str]]: - """The JSON pointer and value of every host an Ingress, IngressRoute, or Mattermost routes.""" + """The JSON pointer and value of every host an Ingress, IngressRoute, or Mattermost routes, + and of each environment value of a Deployment's containers that holds the placeholder (#4542). + """ spec = doc.get("spec") or {} + if doc.get("kind") == "Deployment": + return _env_fields((spec.get("template") or {}).get("spec") or {}) if doc.get("kind") == "Ingress": rules = [ (f"/spec/rules/{i}/host", rule["host"]) @@ -149,14 +171,15 @@ def _target(doc: dict[str, Any]) -> dict[str, str]: def host_patches(rendered: str, domain: str) -> list[dict[str, Any]]: - """One Kustomize JSON patch per object in `rendered` that routes a host under the placeholder. - - Each such host is first tested, then replaced by the same host under `domain`, substituted - as ``render_manifest_template`` does for native deploys. A host that git has since reordered - or removed within its object fails the Application's manifest generation instead of routing - to another backend. Kustomize skips a patch whose object no longer exists, and nothing covers - an object or host git adds, so deploy-stack runs again once such a change reaches the - Application's revision. + """One Kustomize JSON patch per object in `rendered` that routes a host under the placeholder, + or whose containers hold the placeholder in an environment value. + + Each such host or value is first tested, then replaced by the same text under `domain`, + substituted as ``render_manifest_template`` does for native deploys. A host or value that git + has since reordered or removed within its object fails the Application's manifest generation + instead of routing to another backend. Kustomize skips a patch whose object no longer exists, + and nothing covers an object, host or value git adds, so deploy-stack runs again once such a + change reaches the Application's revision. """ configured = normalize_domain(domain) patches = [] diff --git a/tests/k8s/test_k8s_argocd_gate.py b/tests/k8s/test_k8s_argocd_gate.py index 1e38bb5329..ffaa69f6c5 100644 --- a/tests/k8s/test_k8s_argocd_gate.py +++ b/tests/k8s/test_k8s_argocd_gate.py @@ -184,6 +184,54 @@ def test_a_placeholder_ingress_host_in_a_non_homelab_application_fails(tmp_path: ) +def test_a_placeholder_in_a_devops_deployment_fails_only_outside_its_env_values( + tmp_path: Path, +) -> None: + """deploy-stack sets the configured domain in an env value of a homelab Deployment, so the + gate allows the placeholder there and refuses it in each other field (#4542).""" + _write_app(tmp_path, "devops", path="k8s/devops") + devops_dir = tmp_path / "k8s" / "devops" + devops_dir.mkdir(parents=True) + (devops_dir / "kustomization.yaml").write_text( + "apiVersion: kustomize.config.k8s.io/v1beta1\n" + "kind: Kustomization\n" + "resources:\n" + " - deployment.yaml\n", + encoding="utf-8", + ) + (devops_dir / "deployment.yaml").write_text( + "apiVersion: apps/v1\n" + "kind: Deployment\n" + "metadata:\n" + " name: start\n" + "spec:\n" + " template:\n" + " spec:\n" + " initContainers:\n" + " - name: wait\n" + " env:\n" + " - name: TARGET\n" + " value: start.example.com\n" + " containers:\n" + " - name: homepage\n" + " args: ['--host', 'start.example.com']\n" + " env:\n" + " - name: HOMEPAGE_VAR_DOMAIN\n" + " value: example.com\n", + encoding="utf-8", + ) + + with pytest.raises(ArgoCDGateError) as exc_info: + check_argocd_gate(tmp_path) + + assert exc_info.value.failures == { + "devops": [ + "Placeholder domain value 'start.example.com' at Deployment/start " + "/spec/template/spec/containers/0/args/1 is not permitted in application 'devops'" + ] + } + + def test_a_placeholder_in_a_helm_value_file_fails(tmp_path: Path) -> None: _write_app( tmp_path, diff --git a/tests/k8s/test_k8s_argocd_overrides.py b/tests/k8s/test_k8s_argocd_overrides.py index 5eba1742c7..69c1938e74 100644 --- a/tests/k8s/test_k8s_argocd_overrides.py +++ b/tests/k8s/test_k8s_argocd_overrides.py @@ -106,6 +106,73 @@ def test_each_placeholder_host_is_checked_then_set_to_the_configured_domain() -> ] +START_PAGE = """\ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: start + namespace: start + annotations: + note: start.example.com +spec: + template: + spec: + initContainers: + - name: wait + env: + - name: TARGET + value: start.example.com + containers: + - name: homepage + args: ["--host", "start.example.com"] + env: + - name: MY_POD_IP + valueFrom: + fieldRef: + fieldPath: status.podIP + - name: HOMEPAGE_VAR_DOMAIN + value: example.com + - name: PORT + value: "3000" + - name: HOMEPAGE_ALLOWED_HOSTS + value: $(MY_POD_IP):3000,start.example.com +""" + + +def test_each_placeholder_env_value_is_checked_then_set_to_the_configured_domain() -> None: + """Only the env values of a Deployment that hold the placeholder change: not a value + without it, and not the placeholder in an annotation or an argument (#4542).""" + containers = "/spec/template/spec/containers/0/env" + init = "/spec/template/spec/initContainers/0/env" + deployment = {"group": "apps", "version": "v1", "kind": "Deployment"} + assert _decoded(host_patches(START_PAGE, DOMAIN)) == [ + ( + {**deployment, "name": "start", "namespace": "start"}, + [ + {"op": "test", "path": f"{init}/0/value", "value": "start.example.com"}, + {"op": "replace", "path": f"{init}/0/value", "value": "start.example.org"}, + {"op": "test", "path": f"{containers}/1/value", "value": "example.com"}, + {"op": "replace", "path": f"{containers}/1/value", "value": "example.org"}, + { + "op": "test", + "path": f"{containers}/3/value", + "value": "$(MY_POD_IP):3000,start.example.com", + }, + { + "op": "replace", + "path": f"{containers}/3/value", + "value": "$(MY_POD_IP):3000,start.example.org", + }, + ], + ) + ] + + +def test_a_deployment_whose_env_values_hold_no_placeholder_gets_no_patch() -> None: + plain = START_PAGE.replace("example.com", "example.net") + assert host_patches(plain, DOMAIN) == [] + + def test_a_domain_that_is_not_a_hostname_is_refused() -> None: with pytest.raises(KubernetesContextError, match="Invalid domain name"): host_patches(RENDERED, "not a domain")