mirror of
https://github.com/block/buzz.git
synced 2026-11-24 18:21:59 +00:00
# Kubernetes backend plugin (crates/buzz-backend-kubernetes) + desktop deploy path Implements docs/remote-agents.md (merged @28ae6cd21) as ONE PR: the provider binary, the desktop changes that make it work, the harness inactivity reaper, the Sprig image, and the conformance/live-test suites. Channel: buzz-remote-agents (29414326-dba7-402d-b384-b1b34d63a2e6), thread c42b70ef. ## What's here (by lane) - **crates/buzz-backend-kubernetes** (Dawn): stdin/stdout JSON provider, info + deploy; pure classify.rs (one match arm per spec state-machine row); reconcile/GC with ownership-marker gate + same-clock orphan check; per-attempt immutable Secrets; three-tier env with clear-then-write authoritative tier. - **Desktop** (Mari): KD3 launch block from resolved descriptor, KD5 pre-secret negotiation gate (resolve-once → stage-and-digest → info → protocol gate → deploy), KD1 Windows extension strip, bundling (externalBin + Justfile + release/canary workflows + stub loops), tauri.windows.conf.json platform override (Decision B: no Windows artifact). - **buzz-acp** (Max): KD4 BUZZ_ACP_EXIT_AFTER_INACTIVITY reaper (pool-independent; reset only at accepted dispatch; in-flight turn/heartbeat defers, never resets); BUZZ_ACP_EXIT_AFTER_INACTIVITY + BUZZ_ACP_NO_PRESENCE reserved. KD8 fix. - **Image + tests** (Perci): Dockerfile.sprig (digest-pinned bases, exec buzz-acp PID 1, relay-scoped credential config), image contract script, provider conformance suites (golden wire fixtures shared with desktop tests), live-local runbook (namespace-scoped, shared-cluster safe). - **Docs** (Sami, first commit): citation re-pin c1bca1b56 →28ae6cd21(44/49 were already byte-exact; 3 offsets fixed) + I3 presence-bound correction (below). ## Named spec deviations (deliberate, each with rationale) 1. **No baked default image yet.** ghcr.io/block/buzz-sprig is unpublished (verified: anonymous pull 403 vs control 200). Omitted `image` returns an in-band field-required error instead of a default. 2. **Image override STRICTER than spec §Image:** digest-only (`name@sha256:<64hex>`); ALL tags rejected; `name:tag@digest` normalized. With no baked default the override is the only path, so tag-acceptance would make mutability the v1 norm. Strictness is reversible; a moved tag under an nsec is not. Baked digest default + tag re-acceptance = follow-up with image publish. 3. **imagePullSecrets not in schema (v1).** Explicit user images may rely on namespace-preprovisioned pull credentials — the substrate boundary. Field added only if the publish decision proves it necessary. 9-field budget intact. 4. **Decision A closed: writable empty workspace.** Nest projection = named follow-up; no image-side scaffolding. 5. **Decision D overridden by Tyler (event b55398d8):** provider ships bundled with the desktop like buzz-acp/buzz-agent; spec §Distribution's separate release workflow deleted for v1. 6. **I3/vision presence bound corrected 90s → 180s.** PRESENCE_TTL_SECS moved in #3783 during this spec's base→merge window; the number was inherited, not chosen. Spec :206/:216/:928 + inline quote + VISION_REMOTE_AGENTS.md:59 corrected. ← Tyler: the vision is your document; this edit is flagged for your explicit eyes. 7. **Spec citations are pinned to 28ae6cd21** (main at spec merge) and resolve there, not at this PR's head — this PR's own lanes move crates/buzz-acp/src/lib.rs by ~100 lines (19 citations across KD4/KD6/KD7/ §Stop/§Launch data). Known Defects rows fixed BY this PR retire on merge; the section documents main as of the pin. 8. **KD7 grace tension declared:** pod terminationGracePeriodSeconds=60 vs KD7's measured ~87s shutdown tail at parallelism 10 (~197s at cap 32). KD7 is ruled out of scope, so L1-3's "enough grace for full graceful shutdown" is NOT met at default config — deliberate, resolved by the KD7 follow-up, not silently. ## Question for Tyler Will ghcr.io/block/buzz-sprig publish PUBLIC? If private-by-policy, §Image needs an imagePullSecrets story before the baked-default follow-up can land. ## Out of scope (named follow-ups) KD6 exit-code contract + KD7 shutdown budget (gate OnFailure), OnFailure restart policy, Windows provider binary, PVCs/nest projection, mesh deployability, sprig image publish workflow + baked multi-arch digest default. ## Reproduce locally (four traps that cost us real time) **1. Git hooks inherit the invoking shell's PATH — pin the shell, not just your verification commands.** `rust-toolchain.toml` pins `1.95.0`, but the rustup shim that honors that pin lives in `~/.cargo/bin`. If Homebrew's cargo is earlier on PATH, `cargo` in this repo is 1.89.0, which cannot build the workspace at all: ``` $ /opt/homebrew/bin/cargo check -p buzz-db error: rustc 1.89.0 is not supported by the following packages: sqlx@0.9.0 requires rustc 1.94.0 ... # exit 101 ``` Verifying with `PATH="$HOME/.cargo/bin:$PATH" cargo test` does *not* protect the push: lefthook's `pre-push` → `just test-unit` re-resolves `cargo` from the shell's own PATH, so a green local run is followed by a hook failure on a crate you never touched. Export the PATH for the whole shell, not per-command. This bit twice. **2. Line-scope your mutations, or the mutation edits its own detector.** When mutation-testing the respond-to guard, a whole-file `sed` on the mode literal touches 5 sites — the guard *and* the fixtures/assertions that test it. The mutation and its detector move together and the suite stays green, which reads as "this code is dead" when it actually means "you deleted the experiment": ``` # WRONG — 5 sites, guard and tests mutate together $ sed -i '' 's/"allowlist"/"allowlist-DISABLED"/g' src/env.rs test result: ok. 145 passed; 0 failed # false survivor # RIGHT — 1 site, anchored to the guard's own definition line $ sed -i '' '/^const RESPOND_TO_ALLOWLIST/s/"allowlist"/"allowlist-DISABLED"/' src/env.rs failures: env::tests::allowlist_mode_with_an_empty_list_is_refused env::tests::an_allowlist_entry_that_is_not_64_hex_is_refused test result: FAILED. 143 passed; 2 failed # real kill ``` Restore by copying a pristine file back and confirming `git diff --stat` is empty, not by re-running an inverse `sed`. **3. A completeness guard is not a correctness guard.** The shared wire fixture `tests/fixtures/provider-wire/deploy-full-launch.request.json` passed every test we had while containing four classes of invented data (wrong `respond_to` encoding, an env key no emitter writes, allowlist entries that fail the harness's own 64-hex rule, a `launch.env` key from no descriptor layer). The provider's tests could not have caught this: its types are deliberately indifferent to these values (`Option<String>`, `Vec<String>`, arbitrary map), so "the provider parses it" was never evidence that the desktop emits it. The fix was not a stronger provider assertion but a rule about provenance — "recorded" means executed-and-transcribed, and the desktop's whole-object equality test is the only enforcement that can exist. See the fixture README. **4. Every drift this arc was a value that agreed with itself.** Five invented values were found, and not one was caught by an assertion failing — each was caught by someone asking where a value came from. A named constant referenced symbolically on both the fixture and assertion side. A `sed` that mutated its own detector. Six probe rows that all died at the same unrelated error. A descriptor struct literal compared against a fixture built from that literal (`launch.args: ["run","--session"]`, which the resolver actually returns as `["acp"]`). The general defense is not more assertions but provenance: a stub is a control that varies nothing, and the more faithful it looks the better it hides. Ask what executed, not what passed. *Fixture-test determinism caveat (post-verification, Quinn + Dawn).* The desktop's whole-object fixture test calls the real resolver, which consults a process-global harness registry whose own docs require `registry_test_lock` for any test touching it. The fixture test holds no lock and is nonetheless deterministic — but by containment, not by ordering. Measured, not derived: planting a definition with `id: "goose"` directly into the registry (bypassing the loader) changes the resolved descriptor from `args: ["acp"]` to `args: ["--poisoned"]`, so `resolve_effective_harness_descriptor` **does** reach the registry for this id — it does not short-circuit on the builtin table first. Two controls discriminate: an empty registry and a registry poisoned under a *different* id both return `["acp"]`. What actually protects the test is that the registry has exactly one writer (`update_loaded_harness_registry`, reached only via `warm_harness_registry_from_dir`) — but that writer concatenates **two** sources of unequal strength (`custom_harnesses.rs:319-326`). Custom files pass through `load_custom_harnesses`, whose `check_id_collision` rejects the reserved builtin id `goose` case-insensitively at the loader — and that leg is tested (`load_applies_id_collision_check` writes a real `goose.json` and asserts the loader drops it). Preset definitions (`preset_harness_definitions`, `presets.rs:177-193`) are a bare `.map` over `PRESET_HARNESSES` with **no collision check** — exhaustive call-site enumeration at `60007fda4` finds four production `check_id_collision` sites, none on the preset path. That leg holds only because `goose` is not in the preset table today (intersection of TIER1 and preset ids is empty) — executed, not just read: adding a preset with `id: "goose"`, `args: ["--poisoned"]` and warming via the normal preset-only path (`warm_harness_registry_from_dir(None)`, no custom dir, no direct writer) flips the fixture's emitted `launch.args` from `["acp"]` to `["--poisoned"]` at `60007fda4`, command/env/policy_env unchanged. So: no test in the suite can put a `goose` entry in the registry via the custom path, and no preset currently carries one, so no interleaving can perturb this fixture — containment with one checked leg and one coincidental one. A future fixture built on a **non-builtin** runtime id has no containment at all — it would be order-dependent against whatever registry-writing test ran last and must take the lock. *Late instance, found while reviewing the mode guard.* The guard exact-matches `respond_to` untrimmed and case-sensitively, which is only correct if clap's `ValueEnum` derive is case-sensitive. `config.rs` gives two answers: the derive at `:448-453` carries no `ignore_case`, while the crate's own tests call `RespondTo::from_str(s, true)` — `ignore_case = true`. Reading the source supports either. Measured on the built binary instead: `owner-only` starts, `OWNER-ONLY` / `Owner-Only` / `ALLOWLIST` / `NOBODY` all exit rc=2 `invalid value`. Case-sensitive at the CLI, so the guard is right — and right for a reason the source does not state. The `from_str(_, true)` tests exercise a different surface and are not evidence about the CLI. *Corollary, and the sharper half.* When a test helper **reimplements** production instead of calling it, the helper is a fork — and a fork can be right while production is wrong, or wrong in the same way, and the suite reports green either way. Both `BUZZ_ACP_ALLOWED_*` gates are forked like this: production compares **strings** while the helpers compare **post-parse enums** (`config.rs:2623`) or re-derive the split (`buzz-cli/.../channels.rs:1296`). Production and the helper each carry their *own* copy of the empty-entry filter (`:1025` and `:1300`), so fixing one says nothing about the other. Measured on `buzz-cli`, restoring byte-exact between runs: | tree | result | |---|---| | baseline | 274 passed | | drop the empty-filter in **production** only (the real fix) | **274 passed** — no signal | | drop it in the **test helper** only | **273 passed, 1 failed** (`channels.rs:1338`) | Two independent defects, stacked, and worse together than either alone: production can be fixed with no test ever noticing, *and* the helper cannot be corrected without a false alarm demanding the bug back. The root cause is one bit of type information — `check_allowed_channel_add_policy(allowed_raw: &str, ..)` cannot represent "unset", while production reads `env::var(..) -> Result`, where unset and `""` are different states. A helper whose parameter type can't represent all of production's input states isn't testing production's states — it's testing a subset it silently chose. Same family as the struct-literal descriptor and the fixture drift: the test and the thing it tests agreeing with each other, rather than the test measuring the thing. Neither defect is in this PR's diff (`git diff --name-only28ae6cd21<head> -- crates/buzz-cli` is empty); both are now filed as NIP-34 issues on this repo: the fail-open + fork-helper defect at issue event `0524a4113f2d97fd…` and the respond-to self-lock at `e32837498969b5e7…` (filed 2026-08-02 after Quinn measured that no prior filing existed — zero hits on GitHub `block/buzz` open *or* closed and zero on the relay's kind:1621 issues, against working positive controls). The prescription was itself mutation-tested before being written down: repairing the fork's signature (`Option<&str>` + assertion → `None`) still let the reintroduced production bug ship 274-green — an expressive fork is still a fork; it never executes production. So the `buzz-cli` fix has **three parts and one explicit keep**: drop the production filter; **delete** the helper and point its tests at the real `cmd_set_add_policy` (which self-discriminates by error variant — `Usage` = refused, `Network(BadScheme)` = passed the gate — no relay needed); serialize the env-var tests behind one **`tokio::sync::Mutex::const_new`** lock taken with `.lock().await`, including the pre-existing `:1362` integration test (the fork was silently buying test isolation — without the lock, parallel runs flake nondeterministically; a `std::sync::Mutex` held across `.await` trips `clippy::await_holding_lock` under `-D warnings`); and **keep** the then-dead `!allowed.is_empty()` clause with a comment saying why. It is unreachable-false (`split(',')` never yields an empty vec), but it is the only thing that keeps the reintroduced production bug detectable — mutation-tested: on a tree that deletes the clause, reintroducing the empty-filter bug survives 275/0, because `""`/`","`/`" "` refuse either way and the filter goes semantically inert. Dead code can be load-bearing for tests: "provably unreachable" is an argument about behavior, never about coverage. When a helper forks production, the fix has to delete the fork: any change that leaves two implementations standing can only ever be verified against the one the tests call. *Final shape:* the keep and the broad lock are both artifacts of the fork surviving in some form. The extraction variant (Dawn, mutation-tested at `60007fda4`) removes the tension: extract one `check_channel_add_policy_allowed(Option<&str>, &str)` that **production calls**, with the `Option` placed at the env boundary where the `Result<String, VarError>` bit actually lives. 5/6 mutants killed; the empty-filter survivor is proven **equivalent** (exhaustive 6174-pair check, 0 divergences, with a diverging negative control; independently re-derived by a second generator — different tokens and shape — 0 divergences on admitted policies, 500 on a non-admitted control), not a coverage hole — on a one-implementation tree there is no fork left to witness, so no dead clause needs keeping. One scope line on that equivalence: it is **caller-conditional**, a property of the only current caller, not of the gate function — `cmd_set_add_policy`'s own match at `:1027-1034` admits only three policies before the gate runs; a second caller reaching the gate with arbitrary strings resurrects m1 as a real hole. The lock does not disappear, it narrows (Dawn's own correction, caught by Mari): lock exactly the tests that mutate the process env — three-plus-one on a fork tree, two on the extraction tree — behind one `tokio::sync::Mutex`, and the lock is part of the assertion, not hygiene: with it deleted, the gate test fails 8/8 runs deterministically by receiving `Network(BadScheme)` where it expects `Usage` — the unset test's `remove_var` clobbers the other's `set_var`, and **the gate test passes straight through the gate**, a false negative on the exact authz assertion the test exists to make. State it as an outcome: these two tests must not observe each other's env writes. 276/0 stable across 5 parallel runs, clippy `-D warnings` clean; independently verified (patch applied to a second worktree: result blob `d67e584be` matches the patch index, full mutant matrix reproduces row for row). One new row no earlier prescription covered: collapsing unset into `Some("")` fails **closed** — an unconfigured deployment refuses every policy — killed by the unset test. Patch: `OUTBOX/BUZZ_CLI_ADD_POLICY_GATE_EXTRACT_FIX.patch`. The filed issue (`0524a411…`) carries the fork-shape prescription; whoever picks it up should prefer the extraction shape, drop the dead-clause keep with it, and keep part 3 outcome-shaped: serialize whichever tests mutate the env. ## Verification (final HEAD `60007fda4`) - Full touched-package suites at each integration merge (log in plan file). At candidate parent `00e5b5fe9`: buzz-backend-kubernetes 154, buzz-acp 673, desktop tauri 2100+3, pnpm 3908, workspace clippy/fmt/tsc all clean. The only delta to `60007fda4` is one character in `scripts/test-k8s-sprig-image-live.sh` (heredoc escape so the readlink probe evaluates pod-side, not host-side at render); `crates/` tree hash is byte-identical at both SHAs, so the Rust receipts attach by tree identity. buzz-backend-kubernetes suite re-run in-shell at `HEAD == 60007fda4`: 154 passed. - Adversarial one-HEAD gate (Sami): guard matrix 12/12, predicate mutants 7/7, doomed-invocation finding closed end-to-end; tree-hash carry to `60007fda4` confirmed (crates/buzz-backend-kubernetes blob unchanged). - Live-local pass per TESTING.md + skill-buzz-testing (Perci, at `60007fda4`): explicit `docker-desktop` context, digest-qualified image imported into node containerd `k8s.io` namespace, pull policy `Never`; pod printed `DIGEST_ABI_OK`, `resolved_spec` and `image_id` both the exact requested digest, script exit 0. Dedicated per-run namespace, ownership labels on every object, scoped cleanup verified empty after. - Implementation review (Wren) at `60007fda4`: 9.6 minimalness / 9.4 elegance / 9.3 correctness, no blocker. - `origin/eva/k8s-backend` == `60007fda4` (ls-remote verified; SHA identity is byte identity). --------- Signed-off-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@buzz.block.builderlab.xyz> Signed-off-by: Tyler <109685178+tlongwell-block@users.noreply.github.com> Signed-off-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@buzz.block.builderlab.xyz> Signed-off-by: tlongwell-block <109685178+tlongwell-block@users.noreply.github.com> Signed-off-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@buzz.block.builderlab.xyz> Signed-off-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@buzz.block.builderlab.xyz> Co-authored-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@buzz.block.builderlab.xyz> Co-authored-by: npub17jjz49l9jjmhhk7cac63j8yt9z555n9cw8vk7v5jz4vzw4ppld5qgj57cc <f4a42a97e594b77bdbd8ee35191c8b28a94a4cb871d96f32921558275421fb68@buzz.block.builderlab.xyz> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@buzz.block.builderlab.xyz> Co-authored-by: Dawn (sprout agent) <c6237ef84fa537c78dcee78efd2d4e59f728859c7f194da42ac51ededfa0be05@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@buzz.block.builderlab.xyz> Co-authored-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@buzz.block.builderlab.xyz>
1586 lines
64 KiB
Rust
1586 lines
64 KiB
Rust
//! The deploy loop: executes [`crate::classify`]'s actions against a substrate
|
|
//! and re-enters (spec §Deploy State Machine).
|
|
//!
|
|
//! The substrate is a trait so the conformance tests drive this exact
|
|
//! reconciler — the shipped code path, not a test-only reimplementation — with
|
|
//! a fake cluster and a fake clock.
|
|
//!
|
|
//! Two shapes are worth naming up front, because they are what keep the loop
|
|
//! terminating:
|
|
//!
|
|
//! * **Success means the harness container started** (`:696-699`). There is no
|
|
//! "deployed but not confirmed" success: `deploy` returns an `agent_id` or an
|
|
//! in-band error carrying the latest condition. The wire has no third form.
|
|
//! * **A create-conflict loser never repairs.** It verifies the winner, drops
|
|
//! its own Secret, and *observes* until the winner starts. Applying the
|
|
//! divergence row to the pod that just beat it is exactly the ping-pong the
|
|
//! spec forbids (`:845-850`), and the escape it names is the *next* deploy,
|
|
//! not this one.
|
|
|
|
use crate::classify::{self, Action, Fence, Startup, VerifiedPod};
|
|
use crate::config::ProviderConfig;
|
|
use crate::gc;
|
|
use crate::naming::AgentIdentity;
|
|
use crate::observe::{self, StartupObservation};
|
|
use chrono::{DateTime, Utc};
|
|
use k8s_openapi::api::core::v1::{Pod, Secret};
|
|
use std::collections::BTreeMap;
|
|
use std::time::Duration;
|
|
|
|
/// Outcome of a create against the deterministic pod name.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum CreateOutcome {
|
|
Created,
|
|
/// 409 with `Status.reason: AlreadyExists` — a concurrent attempt won.
|
|
/// Discriminated on the typed reason, never on the HTTP code alone: 409 is
|
|
/// also `Conflict`, which means a failed precondition (`:780-794`).
|
|
AlreadyExists,
|
|
}
|
|
|
|
/// Outcome of a fenced delete.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum DeleteOutcome {
|
|
Accepted,
|
|
/// Already gone. Delete-not-found is success (`:842`).
|
|
NotFound,
|
|
/// 409 with `Status.reason: Conflict` — the object changed since the
|
|
/// observation that authorized this delete. Neither an error nor
|
|
/// permission to retry: re-enter and classify what exists now
|
|
/// (`:775-777`).
|
|
PreconditionFailed,
|
|
}
|
|
|
|
/// The cluster operations the reconciler needs. Everything here is I/O;
|
|
/// everything that decides is pure and lives in `classify`/`gc`.
|
|
#[allow(async_fn_in_trait)]
|
|
pub trait Substrate {
|
|
/// Create the namespace if absent. On RBAC denial the error MUST name the
|
|
/// literal `kubectl create namespace <name>` command and MUST NOT fall
|
|
/// back to `default` (`:1002-1005`).
|
|
async fn ensure_namespace(&self, namespace: &str) -> Result<(), String>;
|
|
|
|
/// Most-recent read of the pods matching this identity's selector, plus
|
|
/// the apiserver's clock from the same call's HTTP `Date` header. `None`
|
|
/// clock means the header was absent or unparseable, which makes the
|
|
/// orphan-Secret sweep skip (`:1321-1335`).
|
|
async fn list_pods(&self, selector: &str) -> Result<(Vec<Pod>, Option<DateTime<Utc>>), String>;
|
|
|
|
async fn list_secrets(&self, selector: &str) -> Result<Vec<Secret>, String>;
|
|
|
|
/// Most-recent existence check (`resourceVersion` explicitly unset, not
|
|
/// `"0"`): the classifier treats a confirmed absence as proof, so a
|
|
/// possibly-stale cache read would be proof of nothing (`:761-769`).
|
|
async fn secret_exists(&self, name: &str) -> Result<bool, String>;
|
|
|
|
async fn create_secret(&self, secret: &Secret) -> Result<(), String>;
|
|
|
|
async fn create_pod(&self, pod: &Pod) -> Result<CreateOutcome, String>;
|
|
|
|
/// Compare-and-delete against the fence from the authorizing observation.
|
|
/// Uses the object's own grace period — never `grace_period_seconds: 0`,
|
|
/// which is a force-kill that discards the declared 60s shutdown budget
|
|
/// (`:1185-1189`).
|
|
async fn delete_pod(&self, name: &str, fence: &Fence) -> Result<DeleteOutcome, String>;
|
|
|
|
/// Best-effort: the Secret may already be gone, which is success.
|
|
async fn delete_secret(&self, name: &str) -> Result<(), String>;
|
|
|
|
/// Read one pod by name, most-recent. `None` is a confirmed absence.
|
|
async fn get_pod(&self, name: &str) -> Result<Option<Pod>, String>;
|
|
|
|
async fn sleep(&self, duration: Duration);
|
|
|
|
/// Monotonic elapsed time since the operation began. Fake-clock driven in
|
|
/// tests; the deadline must not depend on wall-clock adjustments.
|
|
fn elapsed(&self) -> Duration;
|
|
}
|
|
|
|
/// Interval between reconciler polls. Short enough that a fast start is
|
|
/// reported promptly, long enough not to hammer the apiserver for 600s.
|
|
const POLL_INTERVAL: Duration = Duration::from_secs(2);
|
|
|
|
/// The deploy operation deadline (spec §Deploy: `timeout: 600s`).
|
|
const DEADLINE: Duration = Duration::from_secs(gc::OPERATION_DEADLINE_SECS as u64);
|
|
|
|
/// Settle a pod's startup state, performing the one most-recent Secret read
|
|
/// that `CreateContainerConfigError` requires.
|
|
async fn settle(substrate: &impl Substrate, pod: &Pod) -> Result<Startup, String> {
|
|
Ok(match observe::decode_startup(pod) {
|
|
StartupObservation::Resolved(startup) => startup,
|
|
StartupObservation::ConfigErrorPendingSecretCheck { secret_name } => {
|
|
// A confirmed absence is proof; anything else stays recoverable,
|
|
// because the kubelet's reason string alone is not evidence.
|
|
if substrate.secret_exists(&secret_name).await? {
|
|
Startup::NeverStartedRecoverable
|
|
} else {
|
|
Startup::NeverStartedProvablyBroken
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
/// Observe the single pod owned by this identity, verified.
|
|
///
|
|
/// `Ok(None)` conflates "absent" with "present but not ours" on purpose *here*
|
|
/// — both mean the classifier has nothing it may act on. The create path
|
|
/// separates them, because only there is the difference actionable.
|
|
async fn observe_pod(
|
|
substrate: &impl Substrate,
|
|
identity: &AgentIdentity,
|
|
) -> Result<Option<VerifiedPod>, String> {
|
|
let Some(pod) = substrate.get_pod(&identity.pod_name()).await? else {
|
|
return Ok(None);
|
|
};
|
|
let startup = settle(substrate, &pod).await?;
|
|
Ok(observe::verify(&pod, identity, startup))
|
|
}
|
|
|
|
/// The latest condition to report, read fresh at the moment of reporting.
|
|
///
|
|
/// Reading it here rather than threading it through every loop iteration is
|
|
/// what "the *latest* redacted condition" (`:689`) asks for, and it costs a
|
|
/// read only on the paths that are already failing.
|
|
async fn latest_condition(substrate: &impl Substrate, identity: &AgentIdentity) -> String {
|
|
match substrate.get_pod(&identity.pod_name()).await {
|
|
Ok(Some(pod)) => observe::condition(&pod)
|
|
.unwrap_or_else(|| "no condition reported by the cluster".to_string()),
|
|
Ok(None) => "the pod no longer exists".to_string(),
|
|
Err(e) => format!("the pod's condition could not be read: {e}"),
|
|
}
|
|
}
|
|
|
|
/// Preflight GC (§K8s GC). Failures are logged and swallowed: GC is hygiene,
|
|
/// and a deploy must not fail because a stale object could not be listed or
|
|
/// removed. A denial that actually blocks this deploy resurfaces at create,
|
|
/// where the message names the operation the user was denied.
|
|
async fn preflight_gc(substrate: &impl Substrate, identity: &AgentIdentity) {
|
|
if let Err(e) = try_preflight_gc(substrate, identity).await {
|
|
eprintln!("gc: preflight pass skipped: {e}");
|
|
}
|
|
}
|
|
|
|
async fn try_preflight_gc(
|
|
substrate: &impl Substrate,
|
|
identity: &AgentIdentity,
|
|
) -> Result<(), String> {
|
|
let selector = identity.selector();
|
|
let (pods, server_now) = substrate.list_pods(&selector).await?;
|
|
let secrets = substrate.list_secrets(&selector).await?;
|
|
|
|
let mut terminated: Vec<String> = Vec::new();
|
|
for pod in &pods {
|
|
if matches!(settle(substrate, pod).await?, Startup::Terminated) {
|
|
if let Some(name) = pod.metadata.name.clone() {
|
|
terminated.push(name);
|
|
}
|
|
}
|
|
}
|
|
|
|
let plan = gc::plan(
|
|
identity,
|
|
&pods,
|
|
&secrets,
|
|
|pod| {
|
|
pod.metadata
|
|
.name
|
|
.as_deref()
|
|
.map(|n| terminated.iter().any(|t| t == n))
|
|
.unwrap_or(false)
|
|
},
|
|
server_now,
|
|
);
|
|
|
|
for name in &plan.pods {
|
|
// Re-read to fence the delete against the object we just observed; a
|
|
// pod that changed since the list is simply skipped this pass.
|
|
let Some(pod) = substrate.get_pod(name).await? else {
|
|
continue;
|
|
};
|
|
let (Some(uid), Some(rv)) = (
|
|
pod.metadata.uid.clone(),
|
|
pod.metadata.resource_version.clone(),
|
|
) else {
|
|
continue;
|
|
};
|
|
if !matches!(settle(substrate, &pod).await?, Startup::Terminated) {
|
|
continue;
|
|
}
|
|
let fence = Fence {
|
|
uid,
|
|
resource_version: rv,
|
|
};
|
|
if let Err(e) = substrate.delete_pod(name, &fence).await {
|
|
eprintln!("gc: could not delete terminated pod {name}: {e}");
|
|
}
|
|
}
|
|
for name in &plan.secrets {
|
|
if let Err(e) = substrate.delete_secret(name).await {
|
|
eprintln!("gc: could not delete secret {name}: {e}");
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Wait for a pod to actually disappear.
|
|
///
|
|
/// Mandatory before recreating: `DELETE` returns success while the object
|
|
/// still exists, and the deterministic name stays taken for the whole grace
|
|
/// period (`:1177-1195`).
|
|
async fn await_disappearance(substrate: &impl Substrate, name: &str) -> Result<(), String> {
|
|
while substrate.elapsed() < DEADLINE {
|
|
if substrate.get_pod(name).await?.is_none() {
|
|
return Ok(());
|
|
}
|
|
substrate.sleep(POLL_INTERVAL).await;
|
|
}
|
|
Err(format!(
|
|
"timed out after {}s waiting for {name} to finish terminating",
|
|
DEADLINE.as_secs()
|
|
))
|
|
}
|
|
|
|
/// Is `secret` referenced by any pod that currently exists under this
|
|
/// identity's selector?
|
|
///
|
|
/// Protection deliberately spans *all* our pods, not just the winner: an
|
|
/// `envFrom` reference from a pod still pulling its image is exactly as
|
|
/// load-bearing as one from a running pod (`:1261-1264`).
|
|
async fn secret_is_referenced(
|
|
substrate: &impl Substrate,
|
|
identity: &AgentIdentity,
|
|
secret: &str,
|
|
) -> Result<bool, String> {
|
|
let (pods, _) = substrate.list_pods(&identity.selector()).await?;
|
|
Ok(pods
|
|
.iter()
|
|
.filter_map(observe::referenced_secret)
|
|
.any(|name| name == secret))
|
|
}
|
|
|
|
/// Drop this attempt's own Secret once nothing references it.
|
|
///
|
|
/// Only ever called with a name this process generated, and gated on the
|
|
/// reference check: "never the winner's, never any Secret referenced by an
|
|
/// existing pod" (`:1259-1264`). Failure is logged, not fatal — a leaked
|
|
/// Secret is collected by the age-gated sweep.
|
|
async fn drop_own_secret(substrate: &impl Substrate, identity: &AgentIdentity, secret: &str) {
|
|
match secret_is_referenced(substrate, identity, secret).await {
|
|
Ok(false) => {
|
|
if let Err(e) = substrate.delete_secret(secret).await {
|
|
eprintln!("could not clean up own unreferenced secret {secret}: {e}");
|
|
}
|
|
}
|
|
Ok(true) => {}
|
|
Err(e) => eprintln!("could not check whether {secret} is still referenced: {e}"),
|
|
}
|
|
}
|
|
|
|
/// Lost the create race: adopt the elected winner.
|
|
///
|
|
/// Observe-only by construction — this function has no delete edge for the
|
|
/// pod. A winner that is terminated or provably broken is reported, not
|
|
/// repaired; the spec's escape is "a *subsequent* deploy that walks in and
|
|
/// observes that never-started divergent winner replaces it normally"
|
|
/// (`:849-850`).
|
|
async fn adopt_winner(
|
|
substrate: &impl Substrate,
|
|
identity: &AgentIdentity,
|
|
own_secret: &str,
|
|
) -> Result<String, String> {
|
|
let name = identity.pod_name();
|
|
|
|
// Verify before adopting. A pod under our deterministic name that fails
|
|
// the marker/annotation check is not ours to adopt, wait for, or touch —
|
|
// and it will never become ours, so this is terminal rather than a retry.
|
|
match substrate.get_pod(&name).await? {
|
|
None => {}
|
|
Some(pod) => {
|
|
let startup = settle(substrate, &pod).await?;
|
|
if observe::verify(&pod, identity, startup).is_none() {
|
|
drop_own_secret(substrate, identity, own_secret).await;
|
|
return Err(format!(
|
|
"a pod named {name} already exists in this namespace but is not \
|
|
managed by this provider for this agent (it lacks the management \
|
|
marker or carries a different agent identity). Remove it, or \
|
|
deploy this agent to a different namespace."
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
drop_own_secret(substrate, identity, own_secret).await;
|
|
|
|
// Then wait for the winner exactly as we would wait for our own pod:
|
|
// success still means the harness container started.
|
|
loop {
|
|
if substrate.elapsed() >= DEADLINE {
|
|
return Err(format!(
|
|
"startup not confirmed within {}s for {name} (another deploy of this \
|
|
agent created it): {}",
|
|
DEADLINE.as_secs(),
|
|
latest_condition(substrate, identity).await
|
|
));
|
|
}
|
|
match observe_pod(substrate, identity).await? {
|
|
Some(pod) if matches!(pod.startup, Startup::Started) => return Ok(pod.name),
|
|
Some(pod) if pod.deletion_marked => {
|
|
return Err(format!(
|
|
"{name} was created by another deploy of this agent and is already \
|
|
being deleted; try again"
|
|
))
|
|
}
|
|
Some(pod)
|
|
if matches!(
|
|
pod.startup,
|
|
Startup::Terminated | Startup::NeverStartedProvablyBroken
|
|
) =>
|
|
{
|
|
return Err(format!(
|
|
"{name} was created by another deploy of this agent and did not \
|
|
start: {}",
|
|
latest_condition(substrate, identity).await
|
|
))
|
|
}
|
|
// Gone again, or still coming up: keep observing under this
|
|
// operation's deadline.
|
|
_ => substrate.sleep(POLL_INTERVAL).await,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Run the deploy state machine to a terminal outcome: the started pod's name,
|
|
/// or an in-band error carrying the latest condition.
|
|
pub async fn deploy(
|
|
substrate: &impl Substrate,
|
|
identity: &AgentIdentity,
|
|
cfg: &ProviderConfig,
|
|
env: BTreeMap<String, String>,
|
|
) -> Result<String, String> {
|
|
substrate.ensure_namespace(&cfg.namespace).await?;
|
|
preflight_gc(substrate, identity).await;
|
|
|
|
let desired = crate::pod::intent_template(cfg, env.keys().cloned()).fingerprint();
|
|
|
|
// Has THIS call created a pod? Set once its create lands. The replacement
|
|
// rows below are for residue from a previous life; once this call has made
|
|
// its own attempt, a replace-classification means that attempt failed —
|
|
// and startup verification is part of create, so the failure is reported
|
|
// in-band rather than retried. Without this bound a deterministic startup
|
|
// failure (the harness starts, rejects its configuration, exits) is
|
|
// delete-recreated every poll for the whole deadline, minting an immutable
|
|
// Secret per cycle — measured live at 107 Secrets in one 600s call, every
|
|
// one younger than the orphan sweep's age gate.
|
|
let mut created_this_call = false;
|
|
|
|
loop {
|
|
if substrate.elapsed() >= DEADLINE {
|
|
return Err(format!(
|
|
"startup not confirmed within {}s for {}: {}",
|
|
DEADLINE.as_secs(),
|
|
identity.pod_name(),
|
|
latest_condition(substrate, identity).await
|
|
));
|
|
}
|
|
|
|
let observed = observe_pod(substrate, identity).await?;
|
|
match classify::classify(observed.as_ref(), &desired) {
|
|
// The only success edge: the harness container is running.
|
|
Action::NoOp { agent_id } => return Ok(agent_id),
|
|
|
|
// Self-healing states. Never delete, on this call or any later one
|
|
// — what replaces a never-started pod is a config change, never a
|
|
// deadline (`:717-729`).
|
|
Action::Observe { .. } => substrate.sleep(POLL_INTERVAL).await,
|
|
|
|
// A pull that will not self-heal: report now rather than spend the
|
|
// remaining deadline on it. Still no delete authority.
|
|
Action::Report { name, failure } => {
|
|
return Err(format!(
|
|
"{name} did not start: {}",
|
|
observe::pull_failure_message(failure, cfg.image.as_str())
|
|
))
|
|
}
|
|
|
|
Action::AwaitDisappearance { name } => await_disappearance(substrate, &name).await?,
|
|
|
|
Action::Delete { name, fence } => {
|
|
// This call already made its own attempt, and that attempt is
|
|
// what the classification wants replaced: it terminated (the
|
|
// deterministic startup failure — the harness starts, rejects
|
|
// its configuration, exits) or was proven broken. Replacing it
|
|
// here retries the identical configuration against the same
|
|
// cluster: a hot delete/mint/create cycle every poll for the
|
|
// whole deadline, an immutable Secret per cycle — measured
|
|
// live at 107 Secrets in one 600s call, all younger than the
|
|
// orphan sweep's age gate. Report in-band instead. The residue
|
|
// is deliberate: the next Start's preflight GC collects the
|
|
// terminated pod and its referenced Secret together, so retry
|
|
// is gated on fresh owner intent and litter stays bounded at
|
|
// one pod + one Secret per press.
|
|
if created_this_call {
|
|
return Err(format!(
|
|
"{name} was created by this deploy and did not stay \
|
|
running: {}. Not retrying in this call — an immediate \
|
|
exit recurs until its cause is fixed. Check the \
|
|
agent's configuration and press Start to try again.",
|
|
latest_condition(substrate, identity).await
|
|
));
|
|
}
|
|
match substrate.delete_pod(&name, &fence).await? {
|
|
// Accepted or already gone: both need the disappearance
|
|
// poll before the name is free again.
|
|
DeleteOutcome::Accepted | DeleteOutcome::NotFound => {
|
|
await_disappearance(substrate, &name).await?
|
|
}
|
|
// The object changed since the observation that authorized
|
|
// this delete. Discard the action and re-classify — never
|
|
// retry with a fresher fence, which would delete something
|
|
// we never examined. Sleep before re-entering: the losing
|
|
// race is against another writer, and re-reading at full
|
|
// speed is a busy-retry with no better odds than a paced
|
|
// one.
|
|
DeleteOutcome::PreconditionFailed => substrate.sleep(POLL_INTERVAL).await,
|
|
}
|
|
}
|
|
|
|
Action::Create => {
|
|
let generation = crate::naming::new_generation();
|
|
let secret_name = identity.secret_name(&generation);
|
|
|
|
// The generation is minted *per attempt*, and it is two things
|
|
// at once: the Secret's name suffix and the lifecycle
|
|
// correlator the harness reports. `build_env` stamped the
|
|
// caller's generation, so on any attempt after the first the
|
|
// two would name different generations — pod logs correlating
|
|
// to a Secret that is not the one mounted. Restamp so there is
|
|
// exactly one generation per attempt (§K8s Secrets).
|
|
let mut env = env.clone();
|
|
env.insert(crate::env::START_NONCE_KEY.to_string(), generation.clone());
|
|
|
|
// Secret first: the pod's spec references this exact name, so
|
|
// payload and Secret are atomic at the pod-spec boundary.
|
|
let secret = crate::pod::build_secret(identity, &cfg.namespace, &generation, env);
|
|
substrate.create_secret(&secret).await?;
|
|
|
|
let pod = crate::pod::build_pod(identity, cfg, &generation, &desired);
|
|
match substrate.create_pod(&pod).await? {
|
|
// Re-enter rather than wait inline: the next iteration
|
|
// observes what we just created and runs the same rows
|
|
// every other state runs through. One loop, one table.
|
|
//
|
|
// Sleep first. A just-created pod cannot already be
|
|
// started, so the immediate observation has no outcome but
|
|
// "still coming up" — and if it ever came back
|
|
// unverifiable, re-entering without advancing the clock
|
|
// would hot-spin creates against the apiserver for the
|
|
// whole deadline.
|
|
CreateOutcome::Created => {
|
|
created_this_call = true;
|
|
substrate.sleep(POLL_INTERVAL).await
|
|
}
|
|
CreateOutcome::AlreadyExists => {
|
|
return adopt_winner(substrate, identity, &secret_name).await
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::config::Resources;
|
|
use crate::naming::{ANNOTATION_CREATE_INTENT, ANNOTATION_PUBKEY_FULL, LABEL_MANAGED_BY};
|
|
use k8s_openapi::api::core::v1::{
|
|
ContainerState, ContainerStateRunning, ContainerStateTerminated, ContainerStateWaiting,
|
|
ContainerStatus, PodStatus,
|
|
};
|
|
use k8s_openapi::apimachinery::pkg::apis::meta::v1::Time;
|
|
use std::cell::RefCell;
|
|
use std::future::Future;
|
|
use std::pin::pin;
|
|
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
|
|
|
|
/// Mutates the pod map after a poll, so a test can script a pod that
|
|
/// starts (or vanishes) partway through an observation loop.
|
|
type PollHook = Box<dyn Fn(&mut BTreeMap<String, Pod>)>;
|
|
|
|
/// A scripted cluster. Single-threaded on purpose: the reconciler is one
|
|
/// process per operation, and `RefCell` keeps the assertions readable.
|
|
///
|
|
/// This drives the *shipped* `deploy` — the point of the `Substrate` seam.
|
|
/// Every fake here answers with real `k8s_openapi` objects, so the decode
|
|
/// and verification layers under test are the ones that run in a cluster.
|
|
#[derive(Default)]
|
|
struct Fake {
|
|
pods: RefCell<BTreeMap<String, Pod>>,
|
|
secrets: RefCell<Vec<Secret>>,
|
|
/// Server clock for the GC age gate; `None` models a missing `Date`.
|
|
server_now: Option<DateTime<Utc>>,
|
|
/// Elapsed time, advanced only by `sleep` — a fake clock, so a 600s
|
|
/// deadline test runs instantly and cannot flake on a slow machine.
|
|
elapsed: RefCell<Duration>,
|
|
/// Queued create outcomes; the default is `Created`.
|
|
create_outcomes: RefCell<Vec<CreateOutcome>>,
|
|
/// Installed when a create loses the race. A winner must be *absent*
|
|
/// at the observation that decides to create and *present* by the time
|
|
/// the create lands — pre-seeding it instead makes the loop no-op
|
|
/// before it ever reaches the create edge.
|
|
winner: RefCell<Option<Pod>>,
|
|
/// Every Secret ever created, retained across deletion.
|
|
created_secrets: RefCell<Vec<Secret>>,
|
|
/// Queued delete outcomes; the default is `Accepted`.
|
|
delete_outcomes: RefCell<Vec<DeleteOutcome>>,
|
|
/// Every mutating call, in order — the anti-mutation assertions read
|
|
/// this rather than guessing from final state.
|
|
calls: RefCell<Vec<String>>,
|
|
/// Applied to the pod map after each poll, so a test can script a pod
|
|
/// that starts (or vanishes) partway through an observation loop.
|
|
on_poll: RefCell<Vec<PollHook>>,
|
|
/// `ensure_namespace` fails with this, if set.
|
|
namespace_error: Option<String>,
|
|
}
|
|
|
|
impl Fake {
|
|
fn with_pod(self, pod: Pod) -> Self {
|
|
self.pods
|
|
.borrow_mut()
|
|
.insert(pod.metadata.name.clone().unwrap(), pod);
|
|
self
|
|
}
|
|
fn log(&self, entry: impl Into<String>) {
|
|
self.calls.borrow_mut().push(entry.into());
|
|
}
|
|
fn mutations(&self) -> Vec<String> {
|
|
self.calls.borrow().clone()
|
|
}
|
|
}
|
|
|
|
impl Substrate for Fake {
|
|
async fn ensure_namespace(&self, namespace: &str) -> Result<(), String> {
|
|
match &self.namespace_error {
|
|
Some(e) => Err(e.clone()),
|
|
None => {
|
|
self.log(format!("ensure_namespace {namespace}"));
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn list_pods(
|
|
&self,
|
|
_selector: &str,
|
|
) -> Result<(Vec<Pod>, Option<DateTime<Utc>>), String> {
|
|
Ok((
|
|
self.pods.borrow().values().cloned().collect(),
|
|
self.server_now,
|
|
))
|
|
}
|
|
|
|
async fn list_secrets(&self, _selector: &str) -> Result<Vec<Secret>, String> {
|
|
Ok(self.secrets.borrow().clone())
|
|
}
|
|
|
|
async fn secret_exists(&self, name: &str) -> Result<bool, String> {
|
|
Ok(self
|
|
.secrets
|
|
.borrow()
|
|
.iter()
|
|
.any(|s| s.metadata.name.as_deref() == Some(name)))
|
|
}
|
|
|
|
async fn create_secret(&self, secret: &Secret) -> Result<(), String> {
|
|
let name = secret.metadata.name.clone().unwrap();
|
|
self.log(format!("create_secret {name}"));
|
|
self.secrets.borrow_mut().push(secret.clone());
|
|
// Kept even after the Secret is deleted: assertions about what an
|
|
// attempt *wrote* must not be silently vacuous once cleanup runs.
|
|
self.created_secrets.borrow_mut().push(secret.clone());
|
|
Ok(())
|
|
}
|
|
|
|
async fn create_pod(&self, pod: &Pod) -> Result<CreateOutcome, String> {
|
|
let name = pod.metadata.name.clone().unwrap();
|
|
self.log(format!("create_pod {name}"));
|
|
let outcome = if self.create_outcomes.borrow().is_empty() {
|
|
CreateOutcome::Created
|
|
} else {
|
|
self.create_outcomes.borrow_mut().remove(0)
|
|
};
|
|
if outcome == CreateOutcome::Created {
|
|
// The apiserver stamps these on admission; a builder never
|
|
// carries them. Without them the pod fails `verify`'s fence
|
|
// extraction and the loop can never see what it just created.
|
|
let mut pod = pod.clone();
|
|
pod.metadata.uid = Some(format!("uid-created-{}", self.calls.borrow().len()));
|
|
pod.metadata.resource_version = Some("1".into());
|
|
self.pods.borrow_mut().insert(name, pod);
|
|
} else if let Some(winner) = self.winner.borrow_mut().take() {
|
|
// The concurrent attempt's pod becomes visible exactly when our
|
|
// create is rejected — the ordering a real race produces.
|
|
self.pods.borrow_mut().insert(name, winner);
|
|
}
|
|
Ok(outcome)
|
|
}
|
|
|
|
async fn delete_pod(&self, name: &str, fence: &Fence) -> Result<DeleteOutcome, String> {
|
|
self.log(format!(
|
|
"delete_pod {name} uid={} rv={}",
|
|
fence.uid, fence.resource_version
|
|
));
|
|
let outcome = if self.delete_outcomes.borrow().is_empty() {
|
|
DeleteOutcome::Accepted
|
|
} else {
|
|
self.delete_outcomes.borrow_mut().remove(0)
|
|
};
|
|
if outcome == DeleteOutcome::Accepted {
|
|
self.pods.borrow_mut().remove(name);
|
|
}
|
|
Ok(outcome)
|
|
}
|
|
|
|
async fn delete_secret(&self, name: &str) -> Result<(), String> {
|
|
self.log(format!("delete_secret {name}"));
|
|
self.secrets
|
|
.borrow_mut()
|
|
.retain(|s| s.metadata.name.as_deref() != Some(name));
|
|
Ok(())
|
|
}
|
|
|
|
async fn get_pod(&self, name: &str) -> Result<Option<Pod>, String> {
|
|
Ok(self.pods.borrow().get(name).cloned())
|
|
}
|
|
|
|
async fn sleep(&self, duration: Duration) {
|
|
*self.elapsed.borrow_mut() += duration;
|
|
let hooks = self.on_poll.borrow();
|
|
let mut pods = self.pods.borrow_mut();
|
|
for hook in hooks.iter() {
|
|
hook(&mut pods);
|
|
}
|
|
}
|
|
|
|
fn elapsed(&self) -> Duration {
|
|
*self.elapsed.borrow()
|
|
}
|
|
}
|
|
|
|
fn identity() -> AgentIdentity {
|
|
use nostr::nips::nip19::ToBech32;
|
|
let keys = nostr::Keys::generate();
|
|
AgentIdentity::from_nsec(&keys.secret_key().to_bech32().unwrap()).unwrap()
|
|
}
|
|
|
|
fn config() -> ProviderConfig {
|
|
ProviderConfig {
|
|
context: None,
|
|
namespace: "buzz-agents-test".into(),
|
|
image: crate::image::parse(&format!(
|
|
"ghcr.io/block/buzz-sprig@sha256:{}",
|
|
"a".repeat(64)
|
|
))
|
|
.unwrap(),
|
|
resources: Resources::default(),
|
|
inactivity_seconds: Some(7200),
|
|
service_account: None,
|
|
}
|
|
}
|
|
|
|
fn env() -> BTreeMap<String, String> {
|
|
[("BUZZ_RELAY_URL".to_string(), "wss://r".to_string())]
|
|
.into_iter()
|
|
.collect()
|
|
}
|
|
|
|
/// A pod exactly as this provider would have created it — same builder the
|
|
/// reconciler uses, so verification is exercised rather than bypassed.
|
|
fn our_pod(id: &AgentIdentity, cfg: &ProviderConfig, state: Option<ContainerState>) -> Pod {
|
|
let fp = crate::pod::intent_template(cfg, env().keys().cloned()).fingerprint();
|
|
let mut pod = crate::pod::build_pod(id, cfg, "gen-existing", &fp);
|
|
pod.metadata.uid = Some("uid-1".into());
|
|
pod.metadata.resource_version = Some("100".into());
|
|
pod.status = Some(PodStatus {
|
|
phase: Some("Running".into()),
|
|
container_statuses: state.map(|s| {
|
|
vec![ContainerStatus {
|
|
name: crate::observe::CONTAINER_NAME.into(),
|
|
state: Some(s),
|
|
..Default::default()
|
|
}]
|
|
}),
|
|
..Default::default()
|
|
});
|
|
pod
|
|
}
|
|
|
|
fn running() -> ContainerState {
|
|
ContainerState {
|
|
running: Some(ContainerStateRunning::default()),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
fn terminated() -> ContainerState {
|
|
ContainerState {
|
|
terminated: Some(ContainerStateTerminated::default()),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
fn waiting(reason: &str) -> ContainerState {
|
|
ContainerState {
|
|
waiting: Some(ContainerStateWaiting {
|
|
reason: Some(reason.into()),
|
|
message: None,
|
|
}),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
fn run(fake: &Fake, id: &AgentIdentity, cfg: &ProviderConfig) -> Result<String, String> {
|
|
block_on(deploy(fake, id, cfg, env()))
|
|
}
|
|
|
|
/// Minimal executor: the fake never yields to a reactor (no timers, no
|
|
/// I/O — `sleep` just advances a counter), so polling to completion is
|
|
/// sufficient and avoids pulling a runtime into the unit job.
|
|
fn block_on<T>(fut: impl Future<Output = T>) -> T {
|
|
fn noop_waker() -> Waker {
|
|
fn nop(_: *const ()) {}
|
|
fn clone(_: *const ()) -> RawWaker {
|
|
RawWaker::new(std::ptr::null(), &VTABLE)
|
|
}
|
|
static VTABLE: RawWakerVTable = RawWakerVTable::new(clone, nop, nop, nop);
|
|
unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &VTABLE)) }
|
|
}
|
|
|
|
let waker = noop_waker();
|
|
let mut cx = Context::from_waker(&waker);
|
|
let mut fut = pin!(fut);
|
|
match fut.as_mut().poll(&mut cx) {
|
|
Poll::Ready(v) => v,
|
|
Poll::Pending => panic!("fake substrate future parked — it has no reactor"),
|
|
}
|
|
}
|
|
|
|
// ---- the state machine's rows, end to end -------------------------------
|
|
|
|
/// First deploy: Secret before pod, and the returned id is the pod name.
|
|
#[test]
|
|
fn creates_secret_then_pod_and_returns_the_pod_name() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default();
|
|
fake.on_poll.borrow_mut().push({
|
|
let name = id.pod_name();
|
|
Box::new(move |pods: &mut BTreeMap<String, Pod>| {
|
|
if let Some(pod) = pods.get_mut(&name) {
|
|
pod.status = Some(PodStatus {
|
|
phase: Some("Running".into()),
|
|
container_statuses: Some(vec![ContainerStatus {
|
|
name: crate::observe::CONTAINER_NAME.into(),
|
|
state: Some(running()),
|
|
..Default::default()
|
|
}]),
|
|
..Default::default()
|
|
});
|
|
}
|
|
})
|
|
});
|
|
|
|
assert_eq!(run(&fake, &id, &cfg).unwrap(), id.pod_name());
|
|
|
|
let calls = fake.mutations();
|
|
let secret_at = calls
|
|
.iter()
|
|
.position(|c| c.starts_with("create_secret"))
|
|
.unwrap();
|
|
let pod_at = calls
|
|
.iter()
|
|
.position(|c| c.starts_with("create_pod"))
|
|
.unwrap();
|
|
assert!(
|
|
secret_at < pod_at,
|
|
"pod created before its Secret: {calls:?}"
|
|
);
|
|
}
|
|
|
|
/// The strict no-op row: a started pod returns its id having mutated
|
|
/// nothing at all. Asserted on the *call log*, not on final state — a
|
|
/// delete-then-recreate would leave identical final state.
|
|
#[test]
|
|
fn started_pod_is_a_zero_mutation_no_op() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default().with_pod(our_pod(&id, &cfg, Some(running())));
|
|
|
|
assert_eq!(run(&fake, &id, &cfg).unwrap(), id.pod_name());
|
|
assert_eq!(
|
|
fake.mutations(),
|
|
[format!("ensure_namespace {}", cfg.namespace)],
|
|
"the no-op row mutated something"
|
|
);
|
|
}
|
|
|
|
/// ...including when the desired intent has diverged. Edits reach a
|
|
/// started pod only via the next generation (`:861-865`).
|
|
#[test]
|
|
fn started_pod_no_ops_under_divergent_intent() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let mut pod = our_pod(&id, &cfg, Some(running()));
|
|
pod.metadata.annotations.as_mut().unwrap().insert(
|
|
ANNOTATION_CREATE_INTENT.to_string(),
|
|
"stale-fingerprint".into(),
|
|
);
|
|
let fake = Fake::default().with_pod(pod);
|
|
|
|
assert_eq!(run(&fake, &id, &cfg).unwrap(), id.pod_name());
|
|
assert!(
|
|
!fake.mutations().iter().any(|c| c.starts_with("delete_pod")),
|
|
"divergence deleted a started pod"
|
|
);
|
|
}
|
|
|
|
/// Terminated → fenced delete → disappearance → recreate. The normal
|
|
/// restart path, and the fence must carry the observed uid/rv.
|
|
#[test]
|
|
fn terminated_pod_is_replaced_with_a_fenced_delete() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default().with_pod(our_pod(&id, &cfg, Some(terminated())));
|
|
fake.on_poll.borrow_mut().push({
|
|
let name = id.pod_name();
|
|
Box::new(move |pods: &mut BTreeMap<String, Pod>| {
|
|
if let Some(pod) = pods.get_mut(&name) {
|
|
if pod
|
|
.status
|
|
.as_ref()
|
|
.and_then(|s| s.container_statuses.as_ref())
|
|
.is_none()
|
|
{
|
|
pod.status = Some(PodStatus {
|
|
phase: Some("Running".into()),
|
|
container_statuses: Some(vec![ContainerStatus {
|
|
name: crate::observe::CONTAINER_NAME.into(),
|
|
state: Some(running()),
|
|
..Default::default()
|
|
}]),
|
|
..Default::default()
|
|
});
|
|
}
|
|
}
|
|
})
|
|
});
|
|
|
|
assert_eq!(run(&fake, &id, &cfg).unwrap(), id.pod_name());
|
|
let calls = fake.mutations();
|
|
assert!(
|
|
calls
|
|
.iter()
|
|
.any(|c| c == &format!("delete_pod {} uid=uid-1 rv=100", id.pod_name())),
|
|
"delete was not fenced to the observed uid+resourceVersion: {calls:?}"
|
|
);
|
|
assert!(calls.iter().any(|c| c.starts_with("create_pod")));
|
|
}
|
|
|
|
/// A failed precondition is neither an error nor permission to retry the
|
|
/// delete: re-enter and classify what exists now (`:775-777`). Here the
|
|
/// object has become a *started* pod, so the correct outcome is the no-op
|
|
/// row — never a second delete with a fresher fence.
|
|
#[test]
|
|
fn precondition_failure_reclassifies_instead_of_retrying() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default().with_pod(our_pod(&id, &cfg, Some(terminated())));
|
|
fake.delete_outcomes
|
|
.borrow_mut()
|
|
.push(DeleteOutcome::PreconditionFailed);
|
|
// The writer we lost the race to: between our observation and our
|
|
// delete, the pod under this name became a *started* one with a new
|
|
// resourceVersion. Installed on the poll that follows the failed
|
|
// precondition, so the sequence is observe → delete → lose → re-observe.
|
|
fake.on_poll.borrow_mut().push({
|
|
let name = id.pod_name();
|
|
let started = {
|
|
let mut p = our_pod(&id, &cfg, Some(running()));
|
|
p.metadata.resource_version = Some("200".into());
|
|
p
|
|
};
|
|
Box::new(move |pods: &mut BTreeMap<String, Pod>| {
|
|
pods.insert(name.clone(), started.clone());
|
|
})
|
|
});
|
|
|
|
assert_eq!(run(&fake, &id, &cfg).unwrap(), id.pod_name());
|
|
let deletes: Vec<_> = fake
|
|
.mutations()
|
|
.into_iter()
|
|
.filter(|c| c.starts_with("delete_pod"))
|
|
.collect();
|
|
// Two call sites legitimately target a terminated pod: preflight GC's
|
|
// sweep (which is the one that loses the precondition here) and the
|
|
// state machine's terminated row. Both are fenced on their own
|
|
// observation. What must never happen is a *third* — a retry of the
|
|
// failed delete — so the invariant is the fence, not the count: every
|
|
// delete carries rv=100, the version we observed. A retry would carry
|
|
// the racing writer's rv=200.
|
|
assert_eq!(deletes.len(), 2, "unexpected delete traffic: {deletes:?}");
|
|
assert!(
|
|
deletes.iter().all(|d| d.contains("rv=100")),
|
|
"retried with a fresher fence: {deletes:?}"
|
|
);
|
|
}
|
|
|
|
/// The anti-livelock rule, at the loop level: a recoverable pod whose
|
|
/// intent matches is observed until the deadline and **never** deleted —
|
|
/// the case that would otherwise reset the pod age Cluster Autoscaler
|
|
/// keys on (`:689`).
|
|
#[test]
|
|
fn recoverable_pod_with_matching_intent_is_never_deleted() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default().with_pod(our_pod(&id, &cfg, Some(waiting("Unschedulable"))));
|
|
|
|
let err = run(&fake, &id, &cfg).unwrap_err();
|
|
assert!(err.contains("startup not confirmed"), "got: {err}");
|
|
assert!(
|
|
!fake.mutations().iter().any(|c| c.starts_with("delete_pod")),
|
|
"deleted a recoverable pod: {:?}",
|
|
fake.mutations()
|
|
);
|
|
assert!(fake.elapsed() >= DEADLINE, "gave up before the deadline");
|
|
}
|
|
|
|
/// The same pod, provisioned late: the observation loop must *succeed*
|
|
/// when the autoscaler eventually lands the node, not merely avoid
|
|
/// deleting.
|
|
#[test]
|
|
fn recoverable_pod_that_starts_late_succeeds() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default().with_pod(our_pod(&id, &cfg, Some(waiting("Unschedulable"))));
|
|
let polls = RefCell::new(0);
|
|
fake.on_poll.borrow_mut().push({
|
|
let name = id.pod_name();
|
|
Box::new(move |pods: &mut BTreeMap<String, Pod>| {
|
|
*polls.borrow_mut() += 1;
|
|
if *polls.borrow() >= 5 {
|
|
if let Some(pod) = pods.get_mut(&name) {
|
|
pod.status.as_mut().unwrap().container_statuses =
|
|
Some(vec![ContainerStatus {
|
|
name: crate::observe::CONTAINER_NAME.into(),
|
|
state: Some(running()),
|
|
..Default::default()
|
|
}]);
|
|
}
|
|
}
|
|
})
|
|
});
|
|
|
|
assert_eq!(run(&fake, &id, &cfg).unwrap(), id.pod_name());
|
|
assert!(fake.elapsed() < DEADLINE);
|
|
}
|
|
|
|
/// A permanent pull failure reports immediately rather than burning the
|
|
/// deadline — and still never deletes.
|
|
#[test]
|
|
fn permanent_pull_failure_reports_without_deleting() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let mut pod = our_pod(&id, &cfg, None);
|
|
pod.status = Some(PodStatus {
|
|
phase: Some("Pending".into()),
|
|
container_statuses: Some(vec![ContainerStatus {
|
|
name: crate::observe::CONTAINER_NAME.into(),
|
|
state: Some(ContainerState {
|
|
waiting: Some(ContainerStateWaiting {
|
|
reason: Some("ImagePullBackOff".into()),
|
|
message: Some("manifest unknown".into()),
|
|
}),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
}]),
|
|
..Default::default()
|
|
});
|
|
let fake = Fake::default().with_pod(pod);
|
|
|
|
let err = run(&fake, &id, &cfg).unwrap_err();
|
|
assert!(err.contains("no image at"), "got: {err}");
|
|
assert!(
|
|
fake.elapsed() < DEADLINE,
|
|
"burned the deadline on a permanent failure"
|
|
);
|
|
assert!(!fake.mutations().iter().any(|c| c.starts_with("delete_pod")));
|
|
}
|
|
|
|
/// The kubelet's pull *message* can echo a registry request; only the
|
|
/// classified outcome and the image reference reach the user.
|
|
#[test]
|
|
fn pull_failure_message_is_not_echoed_verbatim() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let mut pod = our_pod(&id, &cfg, None);
|
|
pod.status = Some(PodStatus {
|
|
phase: Some("Pending".into()),
|
|
container_statuses: Some(vec![ContainerStatus {
|
|
name: crate::observe::CONTAINER_NAME.into(),
|
|
state: Some(ContainerState {
|
|
waiting: Some(ContainerStateWaiting {
|
|
reason: Some("ErrImagePull".into()),
|
|
message: Some(
|
|
"unauthorized: authentication required, token=SUPERSECRET".into(),
|
|
),
|
|
}),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
}]),
|
|
..Default::default()
|
|
});
|
|
let fake = Fake::default().with_pod(pod);
|
|
|
|
let err = run(&fake, &id, &cfg).unwrap_err();
|
|
assert!(
|
|
!err.contains("SUPERSECRET"),
|
|
"kubelet message echoed: {err}"
|
|
);
|
|
}
|
|
|
|
/// A pod being gracefully deleted stays in phase `Running` for its whole
|
|
/// grace period. The reconciler must wait it out and recreate, not return
|
|
/// an id that evaporates.
|
|
#[test]
|
|
fn deletion_marked_pod_is_awaited_then_recreated() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let mut dying = our_pod(&id, &cfg, Some(running()));
|
|
dying.metadata.deletion_timestamp = Some(Time(Utc::now()));
|
|
let fake = Fake::default().with_pod(dying);
|
|
fake.on_poll.borrow_mut().push({
|
|
let name = id.pod_name();
|
|
Box::new(move |pods: &mut BTreeMap<String, Pod>| {
|
|
match pods
|
|
.get(&name)
|
|
.map(|p| p.metadata.deletion_timestamp.is_some())
|
|
{
|
|
// The grace period elapses: the object disappears.
|
|
Some(true) => {
|
|
pods.remove(&name);
|
|
}
|
|
// The replacement we create then starts.
|
|
Some(false) => {
|
|
let pod = pods.get_mut(&name).unwrap();
|
|
pod.status = Some(PodStatus {
|
|
phase: Some("Running".into()),
|
|
container_statuses: Some(vec![ContainerStatus {
|
|
name: crate::observe::CONTAINER_NAME.into(),
|
|
state: Some(running()),
|
|
..Default::default()
|
|
}]),
|
|
..Default::default()
|
|
});
|
|
}
|
|
None => {}
|
|
}
|
|
})
|
|
});
|
|
|
|
assert_eq!(run(&fake, &id, &cfg).unwrap(), id.pod_name());
|
|
assert!(
|
|
!fake.mutations().iter().any(|c| c.starts_with("delete_pod")),
|
|
"deleted a pod that was already terminating"
|
|
);
|
|
}
|
|
|
|
/// `CreateContainerConfigError` is settled by a most-recent Secret read,
|
|
/// never by the reason string: Secret present → recoverable (observe).
|
|
#[test]
|
|
fn config_error_with_a_present_secret_is_recoverable() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let pod = our_pod(&id, &cfg, Some(waiting("CreateContainerConfigError")));
|
|
let referenced = crate::observe::referenced_secret(&pod).unwrap();
|
|
let fake = Fake::default().with_pod(pod);
|
|
fake.secrets.borrow_mut().push(crate::pod::build_secret(
|
|
&id,
|
|
&cfg.namespace,
|
|
"gen-existing",
|
|
env(),
|
|
));
|
|
assert_eq!(referenced, id.secret_name("gen-existing"));
|
|
|
|
let err = run(&fake, &id, &cfg).unwrap_err();
|
|
assert!(err.contains("startup not confirmed"), "got: {err}");
|
|
assert!(
|
|
!fake.mutations().iter().any(|c| c.starts_with("delete_pod")),
|
|
"deleted a pod whose Secret exists"
|
|
);
|
|
}
|
|
|
|
/// ...and Secret confirmed absent → provably broken → fenced replace.
|
|
#[test]
|
|
fn config_error_with_a_confirmed_absent_secret_is_replaced() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default().with_pod(our_pod(
|
|
&id,
|
|
&cfg,
|
|
Some(waiting("CreateContainerConfigError")),
|
|
));
|
|
fake.on_poll.borrow_mut().push({
|
|
let name = id.pod_name();
|
|
Box::new(move |pods: &mut BTreeMap<String, Pod>| {
|
|
if let Some(pod) = pods.get_mut(&name) {
|
|
if pod
|
|
.status
|
|
.as_ref()
|
|
.and_then(|s| s.container_statuses.as_ref())
|
|
.is_none()
|
|
{
|
|
pod.status = Some(PodStatus {
|
|
phase: Some("Running".into()),
|
|
container_statuses: Some(vec![ContainerStatus {
|
|
name: crate::observe::CONTAINER_NAME.into(),
|
|
state: Some(running()),
|
|
..Default::default()
|
|
}]),
|
|
..Default::default()
|
|
});
|
|
}
|
|
}
|
|
})
|
|
});
|
|
|
|
assert_eq!(run(&fake, &id, &cfg).unwrap(), id.pod_name());
|
|
assert!(fake.mutations().iter().any(|c| c.starts_with("delete_pod")));
|
|
}
|
|
|
|
/// The generation is the Secret's name suffix *and* the lifecycle
|
|
/// correlator inside it. They must name the same generation, or pod logs
|
|
/// point at a Secret that was never mounted. The caller stamps one
|
|
/// generation into the env once; each create attempt mints its own, so
|
|
/// only a restamp keeps the pair together across a retry.
|
|
#[test]
|
|
fn each_attempt_stamps_its_own_generation_into_its_own_secret() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
// First attempt loses the create race and is cleaned up; the operation
|
|
// then adopts. Two creates, two generations.
|
|
let fake = Fake::default();
|
|
*fake.winner.borrow_mut() = Some(our_pod(&id, &cfg, Some(running())));
|
|
fake.create_outcomes
|
|
.borrow_mut()
|
|
.push(CreateOutcome::AlreadyExists);
|
|
|
|
run(&fake, &id, &cfg).unwrap();
|
|
|
|
let created = fake.created_secrets.borrow();
|
|
// Guard the guard: this assertion is over a list that cleanup empties,
|
|
// so an empty list would make every check below vacuously true.
|
|
assert_eq!(created.len(), 1, "expected one create attempt");
|
|
for secret in created.iter() {
|
|
let name = secret.metadata.name.as_deref().unwrap();
|
|
let nonce = secret
|
|
.string_data
|
|
.as_ref()
|
|
.and_then(|d| d.get(crate::env::START_NONCE_KEY))
|
|
.expect("no lifecycle correlator in the Secret");
|
|
assert!(
|
|
name.ends_with(nonce.as_str()),
|
|
"secret {name} carries a correlator for a different generation ({nonce})"
|
|
);
|
|
}
|
|
}
|
|
|
|
// ---- bounded replacement ------------------------------------------------
|
|
|
|
/// Installs a hook that makes every pod under `name` crash-exit before the
|
|
/// next poll — a deterministic startup failure (the harness starts, rejects
|
|
/// its configuration, and exits) as observed live: the container reaches
|
|
/// `state.terminated` with a nonzero exit code.
|
|
fn crash_exits_immediately(fake: &Fake, name: String) {
|
|
fake.on_poll
|
|
.borrow_mut()
|
|
.push(Box::new(move |pods: &mut BTreeMap<String, Pod>| {
|
|
if let Some(pod) = pods.get_mut(&name) {
|
|
if pod
|
|
.status
|
|
.as_ref()
|
|
.and_then(|s| s.container_statuses.as_ref())
|
|
.is_none()
|
|
{
|
|
pod.status = Some(PodStatus {
|
|
phase: Some("Failed".into()),
|
|
container_statuses: Some(vec![ContainerStatus {
|
|
name: crate::observe::CONTAINER_NAME.into(),
|
|
state: Some(ContainerState {
|
|
terminated: Some(ContainerStateTerminated {
|
|
exit_code: 1,
|
|
reason: Some("Error".into()),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
}]),
|
|
..Default::default()
|
|
});
|
|
}
|
|
}
|
|
}));
|
|
}
|
|
|
|
/// A pod that this call created and that crash-exits deterministically is
|
|
/// reported in-band after exactly ONE attempt — never hot-replaced until
|
|
/// the deadline. The live failure this pins: one delete/create cycle every
|
|
/// ~4s minted 107 immutable Secrets in a single 600s deploy call, all
|
|
/// younger than the orphan sweep's age gate.
|
|
#[test]
|
|
fn a_deterministic_crash_exit_is_reported_not_hot_replaced() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default();
|
|
crash_exits_immediately(&fake, id.pod_name());
|
|
|
|
let err = run(&fake, &id, &cfg).unwrap_err();
|
|
|
|
let creates = fake
|
|
.mutations()
|
|
.iter()
|
|
.filter(|c| c.starts_with("create_secret"))
|
|
.count();
|
|
assert_eq!(
|
|
creates, 1,
|
|
"hot replacement loop: {creates} Secrets minted in one deploy call"
|
|
);
|
|
assert!(
|
|
err.contains("exited with code 1"),
|
|
"error does not carry the exit: {err}"
|
|
);
|
|
// The failed attempt is left in place as evidence — no cleanup on the
|
|
// error path. Its Secret stays referenced by the terminated pod, so
|
|
// the next deploy's preflight GC collects both together.
|
|
assert!(
|
|
!fake
|
|
.mutations()
|
|
.iter()
|
|
.any(|c| c.starts_with("delete_pod") || c.starts_with("delete_secret")),
|
|
"the failed attempt was cleaned up on the error path: {:?}",
|
|
fake.mutations()
|
|
);
|
|
assert!(
|
|
fake.elapsed() < DEADLINE,
|
|
"burned the whole deadline on a deterministic failure"
|
|
);
|
|
}
|
|
|
|
/// The revive path is untouched: terminated residue from a *previous*
|
|
/// life is still replaced — the bound is on pods this call created, not
|
|
/// on the row.
|
|
#[test]
|
|
fn pre_existing_terminated_residue_is_still_replaced_once() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default().with_pod(our_pod(&id, &cfg, Some(terminated())));
|
|
crash_exits_immediately(&fake, id.pod_name());
|
|
|
|
let err = run(&fake, &id, &cfg).unwrap_err();
|
|
assert!(err.contains("exited with code 1"), "got: {err}");
|
|
|
|
let calls = fake.mutations();
|
|
let deletes = calls.iter().filter(|c| c.starts_with("delete_pod")).count();
|
|
let creates = calls
|
|
.iter()
|
|
.filter(|c| c.starts_with("create_secret"))
|
|
.count();
|
|
// Exactly one residue delete (the normal restart path) and one fresh
|
|
// attempt — then report, not another cycle.
|
|
assert_eq!(deletes, 1, "unexpected delete traffic: {calls:?}");
|
|
assert_eq!(creates, 1, "unexpected create traffic: {calls:?}");
|
|
}
|
|
|
|
/// Retry is gated on fresh owner intent: the *next* deploy call clears
|
|
/// the crashed attempt (preflight GC collects the terminated pod and its
|
|
/// referenced Secret together) and makes exactly one new attempt — total
|
|
/// litter stays one pod + one Secret however many times Start is pressed.
|
|
#[test]
|
|
fn the_next_deploy_collects_the_crashed_attempt_before_its_own_attempt() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default();
|
|
crash_exits_immediately(&fake, id.pod_name());
|
|
|
|
run(&fake, &id, &cfg).unwrap_err();
|
|
// Second Start: fresh call, fresh clock.
|
|
*fake.elapsed.borrow_mut() = Duration::ZERO;
|
|
run(&fake, &id, &cfg).unwrap_err();
|
|
|
|
assert_eq!(
|
|
fake.secrets.borrow().len(),
|
|
1,
|
|
"crashed attempts accumulated Secrets across calls"
|
|
);
|
|
assert_eq!(fake.pods.borrow().len(), 1, "crashed pods accumulated");
|
|
}
|
|
|
|
// ---- the auto-repair fence ----------------------------------------------
|
|
|
|
/// An object under our deterministic name that lacks the management
|
|
/// marker is not ours. The reconciler must not adopt it, delete it, or
|
|
/// return its name — it fails closed to the operator (`:1156-1162`).
|
|
#[test]
|
|
fn an_unmarked_look_alike_is_never_touched_or_adopted() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let mut look_alike = our_pod(&id, &cfg, Some(running()));
|
|
look_alike
|
|
.metadata
|
|
.labels
|
|
.as_mut()
|
|
.unwrap()
|
|
.remove(LABEL_MANAGED_BY);
|
|
let fake = Fake::default();
|
|
// Our create loses to the object already sitting on the name.
|
|
*fake.winner.borrow_mut() = Some(look_alike);
|
|
fake.create_outcomes
|
|
.borrow_mut()
|
|
.push(CreateOutcome::AlreadyExists);
|
|
|
|
let err = run(&fake, &id, &cfg).unwrap_err();
|
|
assert!(err.contains("not managed by this provider"), "got: {err}");
|
|
assert!(
|
|
!fake.mutations().iter().any(|c| c.starts_with("delete_pod")),
|
|
"deleted an object we do not own"
|
|
);
|
|
}
|
|
|
|
/// Same fence, other direction: the marker is present but the annotation
|
|
/// carries a different agent's pubkey. The 32-hex label is
|
|
/// collision-resistant, not collision-free (`:1152-1155`).
|
|
#[test]
|
|
fn a_pubkey_mismatch_is_never_adopted() {
|
|
let id = identity();
|
|
let other = identity();
|
|
let cfg = config();
|
|
let mut foreign = our_pod(&id, &cfg, Some(running()));
|
|
foreign.metadata.annotations.as_mut().unwrap().insert(
|
|
ANNOTATION_PUBKEY_FULL.to_string(),
|
|
other.pubkey_hex().to_string(),
|
|
);
|
|
let fake = Fake::default();
|
|
*fake.winner.borrow_mut() = Some(foreign);
|
|
fake.create_outcomes
|
|
.borrow_mut()
|
|
.push(CreateOutcome::AlreadyExists);
|
|
|
|
let err = run(&fake, &id, &cfg).unwrap_err();
|
|
assert!(err.contains("different agent identity"), "got: {err}");
|
|
assert!(!fake.mutations().iter().any(|c| c.starts_with("delete_pod")));
|
|
}
|
|
|
|
// ---- create-conflict convergence ---------------------------------------
|
|
|
|
/// The loser adopts the winner, returns the winner's id, and deletes
|
|
/// **only its own** Secret — never the winner's (`:1259-1264`).
|
|
#[test]
|
|
fn create_loser_adopts_the_winner_and_drops_only_its_own_secret() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let winner = our_pod(&id, &cfg, Some(running()));
|
|
let winners_secret = crate::observe::referenced_secret(&winner).unwrap();
|
|
let fake = Fake::default();
|
|
*fake.winner.borrow_mut() = Some(winner);
|
|
fake.secrets.borrow_mut().push(crate::pod::build_secret(
|
|
&id,
|
|
&cfg.namespace,
|
|
"gen-existing",
|
|
env(),
|
|
));
|
|
fake.create_outcomes
|
|
.borrow_mut()
|
|
.push(CreateOutcome::AlreadyExists);
|
|
|
|
assert_eq!(run(&fake, &id, &cfg).unwrap(), id.pod_name());
|
|
|
|
let deleted: Vec<_> = fake
|
|
.mutations()
|
|
.into_iter()
|
|
.filter(|c| c.starts_with("delete_secret"))
|
|
.collect();
|
|
assert_eq!(
|
|
deleted.len(),
|
|
1,
|
|
"expected exactly our own Secret dropped: {deleted:?}"
|
|
);
|
|
assert!(
|
|
!deleted[0].contains(&winners_secret),
|
|
"deleted the winner's Secret: {deleted:?}"
|
|
);
|
|
assert!(
|
|
fake.secrets
|
|
.borrow()
|
|
.iter()
|
|
.any(|s| s.metadata.name.as_deref() == Some(winners_secret.as_str())),
|
|
"the winner's Secret is gone"
|
|
);
|
|
}
|
|
|
|
/// The loser must not apply the divergence row to the pod that just beat
|
|
/// it — that is the ping-pong the spec forbids (`:845-850`). A divergent
|
|
/// *started* winner is adopted as-is.
|
|
#[test]
|
|
fn create_loser_does_not_replace_a_divergent_winner() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let mut winner = our_pod(&id, &cfg, Some(running()));
|
|
winner.metadata.annotations.as_mut().unwrap().insert(
|
|
ANNOTATION_CREATE_INTENT.to_string(),
|
|
"a-different-intent".into(),
|
|
);
|
|
let fake = Fake::default();
|
|
*fake.winner.borrow_mut() = Some(winner);
|
|
fake.create_outcomes
|
|
.borrow_mut()
|
|
.push(CreateOutcome::AlreadyExists);
|
|
|
|
assert_eq!(run(&fake, &id, &cfg).unwrap(), id.pod_name());
|
|
assert!(
|
|
!fake.mutations().iter().any(|c| c.starts_with("delete_pod")),
|
|
"the create loser deleted the winner"
|
|
);
|
|
}
|
|
|
|
/// The loser waits for the winner to *start* — adopting a not-yet-started
|
|
/// winner as success would report an agent that is not running.
|
|
#[test]
|
|
fn create_loser_waits_for_the_winner_to_start() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default();
|
|
*fake.winner.borrow_mut() = Some(our_pod(&id, &cfg, Some(waiting("ContainerCreating"))));
|
|
fake.create_outcomes
|
|
.borrow_mut()
|
|
.push(CreateOutcome::AlreadyExists);
|
|
let polls = RefCell::new(0);
|
|
fake.on_poll.borrow_mut().push({
|
|
let name = id.pod_name();
|
|
Box::new(move |pods: &mut BTreeMap<String, Pod>| {
|
|
*polls.borrow_mut() += 1;
|
|
if *polls.borrow() >= 3 {
|
|
if let Some(pod) = pods.get_mut(&name) {
|
|
pod.status.as_mut().unwrap().container_statuses =
|
|
Some(vec![ContainerStatus {
|
|
name: crate::observe::CONTAINER_NAME.into(),
|
|
state: Some(running()),
|
|
..Default::default()
|
|
}]);
|
|
}
|
|
}
|
|
})
|
|
});
|
|
|
|
assert_eq!(run(&fake, &id, &cfg).unwrap(), id.pod_name());
|
|
assert!(fake.elapsed() > Duration::ZERO, "did not wait at all");
|
|
}
|
|
|
|
// ---- deadline and namespace --------------------------------------------
|
|
|
|
/// Deadline expiry reports the *latest* condition, not a generic timeout
|
|
/// (`:699-701`), and triggers no cleanup (`:720-724`).
|
|
#[test]
|
|
fn deadline_expiry_reports_the_condition_and_cleans_up_nothing() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake::default().with_pod(our_pod(&id, &cfg, Some(waiting("ContainerCreating"))));
|
|
|
|
let err = run(&fake, &id, &cfg).unwrap_err();
|
|
assert!(err.contains("ContainerCreating"), "generic timeout: {err}");
|
|
let calls = fake.mutations();
|
|
assert!(
|
|
!calls.iter().any(|c| c.starts_with("delete_")),
|
|
"deadline expiry triggered cleanup: {calls:?}"
|
|
);
|
|
}
|
|
|
|
/// An RBAC denial on namespace create fails the deploy with the literal
|
|
/// command to run, before any Secret is written (`:1002-1005`).
|
|
#[test]
|
|
fn namespace_denial_fails_before_writing_any_secret() {
|
|
let id = identity();
|
|
let cfg = config();
|
|
let fake = Fake {
|
|
namespace_error: Some(format!(
|
|
"not authorized to create namespaces: run `kubectl create namespace {}`",
|
|
cfg.namespace
|
|
)),
|
|
..Default::default()
|
|
};
|
|
|
|
let err = run(&fake, &id, &cfg).unwrap_err();
|
|
assert!(err.contains("kubectl create namespace"), "got: {err}");
|
|
assert!(
|
|
fake.mutations().is_empty(),
|
|
"wrote something after a namespace denial: {:?}",
|
|
fake.mutations()
|
|
);
|
|
}
|
|
|
|
/// GC failures are hygiene, not deploy failures: a list the user cannot
|
|
/// perform must not block a deploy they can.
|
|
#[test]
|
|
fn a_gc_failure_does_not_fail_the_deploy() {
|
|
struct GcDenied(Fake);
|
|
impl Substrate for GcDenied {
|
|
async fn ensure_namespace(&self, ns: &str) -> Result<(), String> {
|
|
self.0.ensure_namespace(ns).await
|
|
}
|
|
async fn list_pods(
|
|
&self,
|
|
_s: &str,
|
|
) -> Result<(Vec<Pod>, Option<DateTime<Utc>>), String> {
|
|
Err("forbidden: cannot list pods".into())
|
|
}
|
|
async fn list_secrets(&self, _s: &str) -> Result<Vec<Secret>, String> {
|
|
Err("forbidden: cannot list secrets".into())
|
|
}
|
|
async fn secret_exists(&self, n: &str) -> Result<bool, String> {
|
|
self.0.secret_exists(n).await
|
|
}
|
|
async fn create_secret(&self, s: &Secret) -> Result<(), String> {
|
|
self.0.create_secret(s).await
|
|
}
|
|
async fn create_pod(&self, p: &Pod) -> Result<CreateOutcome, String> {
|
|
self.0.create_pod(p).await
|
|
}
|
|
async fn delete_pod(&self, n: &str, f: &Fence) -> Result<DeleteOutcome, String> {
|
|
self.0.delete_pod(n, f).await
|
|
}
|
|
async fn delete_secret(&self, n: &str) -> Result<(), String> {
|
|
self.0.delete_secret(n).await
|
|
}
|
|
async fn get_pod(&self, n: &str) -> Result<Option<Pod>, String> {
|
|
self.0.get_pod(n).await
|
|
}
|
|
async fn sleep(&self, d: Duration) {
|
|
self.0.sleep(d).await
|
|
}
|
|
fn elapsed(&self) -> Duration {
|
|
self.0.elapsed()
|
|
}
|
|
}
|
|
|
|
let id = identity();
|
|
let cfg = config();
|
|
let inner = Fake::default().with_pod(our_pod(&id, &cfg, Some(running())));
|
|
let fake = GcDenied(inner);
|
|
|
|
assert_eq!(
|
|
block_on(deploy(&fake, &id, &cfg, env())).unwrap(),
|
|
id.pod_name()
|
|
);
|
|
}
|
|
}
|