my-homelab-configs/docs/cluster-context.md

167 lines
8.7 KiB
Markdown

# Cluster identities and profile-specific state
Jeannie treats `homelab.yml` as the source of declared cluster profiles. Each
profile has one immutable, locally enrolled binding containing:
- The exact HTTPS API endpoint (LAN address and port 6443 by default).
- SHA-256 of the DER-encoded cluster CA certificate.
- The UID of the live `kube-system` namespace.
- The profile name.
The selected inventory control-plane node must also exist with a control-plane
role label. A reused address, replaced CA, or new cluster UID never silently
updates an existing binding. Kubeconfigs must use explicit CA verification and
a single absolute file path. Context aliases are allowed only when they resolve
to the same pinned identity. TLS bypasses and endpoint/CA overrides are refused.
## Enroll the existing cluster once
Run on Debian after pulling the change. Preview enrollment first:
```bash
export LAB_CLUSTER_PROFILE=pimox
./jeannie cluster-context enroll
```
Compare the reported endpoint, CA fingerprint, and namespace UID with the
intended cluster. The preview reads the API and writes no identity. After
review, explicitly enroll and verify:
```bash
./jeannie cluster-context enroll --yes
./jeannie cluster-context check
./jeannie cluster-context status
```
Enrollment requires TLS-verified access to the declared endpoint, an explicit
CA, and the declared control-plane node. This first enrollment is an operator
trust decision, not an independent attestation that a never-before-seen cluster
is correct. Repeating it is idempotent for the same identity. `--yes` cannot
replace a mismatched binding, and the same namespace UID cannot be enrolled
under a second profile. There is no automatic rotation or force bypass.
## Migrate existing state without changing infrastructure
The Kubernetes stacks `cluster`, `platform`, and `apps` use:
```text
$HOMELAB_STATE_DIR/clusters/<profile>/identity.json
$HOMELAB_STATE_DIR/clusters/<profile>/stacks/<stack>/terraform.tfstate
$HOMELAB_STATE_DIR/clusters/<profile>/.terraform/<stack>/
```
`HOMELAB_STATE_DIR` defaults to `~/.local/share/homelab`. Both the local backend
path and `TF_DATA_DIR` are separate per profile and stack. Non-default Terraform
workspaces, arbitrary state pushes, state-file overrides, hidden `TF_CLI_ARGS` overrides, and alternate
import configuration directories are refused for these stacks. The wrapper
serializes each stack's commands in addition to OpenTofu's own backend lock.
OCI foundations, provisioning, and edge are shared infrastructure; their state
locations remain unchanged.
An unassigned legacy state blocks using an empty scoped state. Review each
legacy file before copying it into the enrolled profile:
```bash
./jeannie cluster-context migrate --stack cluster
./jeannie cluster-context migrate --stack platform
./jeannie cluster-context migrate --stack apps
```
Known control-plane or kubeconfig hints that conflict with the profile cause a
refusal. One narrowly audited exception is the root-level
`null_resource.kyverno_helm_recovery` marker when no Kyverno Helm release is
tracked in that state. This create-only helper has no infrastructure identity
and no destroy provisioner. Its historical kubeconfig is reported in
`stale_execution_helpers` and recorded in the adoption ledger. The original
state retains the marker; the scoped copy excludes it so a historical
kubeconfig cannot become an active provisioner target. This increments only
the destination serial and leaves tracked infrastructure unchanged. The
preview shows both serials and resource counts. The exception requires the exact reviewed helper source;
a source change, tracked Kyverno release, other helper, or other stack keeps the
normal refusal. It never rewrites a cluster binding or authorizes access to the
historical cluster. A later apply uses the selected profile's planned helper
configuration and normal identity guards.
The preview reports source, destination, lineage, resource count, and
the verified cluster identity. Namespace/release IDs alone cannot establish
which cluster an old state file belongs to: inspect its ownership before
accepting the copy. After reviewing each preview, repeat the same command with
`--yes`. Do not migrate a file that belongs to the other cluster.
Migration supports complete unencrypted version-4 state snapshots. It preserves
the original file, lineage, and tracked infrastructure. Serial and resources are
copied unchanged except for the explicitly reported orphan-marker quarantine; it never
imports, applies, merges, or overwrites newer scoped state. It refuses active
legacy locks or a source that changes during the operation. A content-hashed
ownership ledger prevents assigning the same legacy snapshot to two profiles.
Once a snapshot belongs to one profile, another profile can initialize its own
independent state without consuming that snapshot. An unexplained change in an
unmigrated snapshot blocks automatic adoption. Stop raw legacy writers during
migration; processes launched outside Jeannie do not honor its operation lock.
State status and both backup collectors include the scoped paths and identity
records. Provider caches and operation locks are excluded from backups. Nuke
no longer deletes shared state paths or kills unrelated OpenTofu processes.
It retains identity and state for reviewed recovery rather than silently
forgetting resource ownership.
## Guard coverage
Jeannie prepends repo-owned `tofu`, `kubectl`, and `helm` launchers to its child
process PATH. Kubernetes OpenTofu applies, destroys, imports, and state mutations
verify the binding before the underlying command runs. The effective
`var.kubeconfig_path` is evaluated using the same variable arguments, rather
than trusting a shell default. Cluster provisioner name, host, and advertise
address must match the selected inventory control plane.
Saved plans get a sidecar receipt bound to their content hash, profile,
identity, provider kubeconfig, and backend path. Applying an unrecognized or
modified saved plan is refused. The provider kubeconfig is verified again at
apply time. The scheduling preflight's temporary plans receive the same checks.
Mutating kubectl/Helm commands verify their actual kubeconfig/context, including
commands launched by Jeannie child scripts. Host start/stop, worker actions,
local destructive cleanup, pod cleanup, Grafana recovery, and self-heal webhook
apply paths also have explicit entry guards. Local control-plane deletion
additionally checks the host's address and CA. Debian retirement requires
verified identities for both the active Pimox cluster and the retired Debian
cluster; former force/skip-check environment flags do not bypass this guard.
Read-only kubectl diagnostics and `cluster-context status` remain available
without enrollment. OpenTofu plans also verify identity because they can
evaluate declarative imports. No check result is
cached across mutations. Failed API reads or unavailable authentication are
failures, not evidence of a missing cluster. An unreachable API therefore blocks
mutations, including automatic API-start healing: the guard cannot prove the
current namespace UID offline. First cluster bootstrap, cluster recreation,
CA rotation, and offline recovery need a separately reviewed trust transition;
existing lifecycle commands cannot silently reenroll a replacement cluster.
`rebuild-cluster` therefore refuses before destroying anything; this change
does not provide an automatic new-cluster trust transition.
This is an accident-prevention boundary for Jeannie-managed operations, not a
sandbox. Direct absolute binary invocations, tools outside Jeannie, privileged
manual file edits, and arbitrary third-party plugins can bypass it. A provider
operation or remote provisioner is verified before it starts; the guard cannot
make a long-running operation atomic with a cluster identity change. Use
Jeannie entry points rather than raw OpenTofu commands for these stacks.
## Validation on Debian
No local application or unit tests are run on the development laptop. The
regression suite mocks Kubernetes/Tofu interactions and uses temporary files:
```bash
python3 -m unittest discover -s tests/jeannie -p 'test_cluster_context.py'
python3 -m unittest discover -s tests/jeannie -p 'test_self_heal_context.py'
python3 -m unittest discover -s tests/jeannie -p 'test_monitoring_readiness.py'
```
Live `cluster-context check` verifies the enrolled binding. Test a mismatched
context using that read-only check; do not issue a real apply to demonstrate a
refusal. Check platform/cluster/apps plans only after their state migration is
reviewed. Existing scoped state is never replaced to make a plan pass.
Backend design references: [OpenTofu local backend](https://opentofu.org/docs/language/settings/backends/local/)
and [OpenTofu CLI environment variables](https://opentofu.org/docs/cli/config/environment-variables/).