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
3 changes: 2 additions & 1 deletion k8s/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <n>`, 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.

Expand All @@ -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:

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,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.
Original file line number Diff line number Diff line change
@@ -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://<name>.{{HOMEPAGE_VAR_DOMAIN}}/<path>`, 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/<i>/env/<j>/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.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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`
13 changes: 12 additions & 1 deletion openspec/specs/gitops-host-overrides/spec.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
55 changes: 39 additions & 16 deletions src/devops_cli/k8s/argocd_overrides.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."""
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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 = []
Expand Down
Loading