From 9b5d2ed657d2d99dc88e1d07674e4f0b6add77dc Mon Sep 17 00:00:00 2001 From: yukkop Date: Mon, 2 Nov 2026 20:06:04 +0000 Subject: [PATCH] fix: gitea controller --- infra/gitea-runners/runbook.md | 9 +- package/gitea-runner-controller/controller.sh | 24 ++- package/gitea-runner-controller/gitea.sh | 142 +++++++++---- package/gitea-runner-controller/hcloud.sh | 53 ++++- package/gitea-runner-controller/state.sh | 1 + .../test/idle-reuse.sh | 32 ++- .../test/lifecycle-race.sh | 187 ++++++++++++++++++ 7 files changed, 388 insertions(+), 60 deletions(-) create mode 100644 test/package/gitea-runner-controller/test/lifecycle-race.sh diff --git a/infra/gitea-runners/runbook.md b/infra/gitea-runners/runbook.md index b0553237..16069a80 100644 --- a/infra/gitea-runners/runbook.md +++ b/infra/gitea-runners/runbook.md @@ -581,9 +581,12 @@ immediate teardown; failed jobs retain pre-destroy diagnostics. A healthy, bootstrapped terminal VM may stay running until the next hourly boundary measured from original VM creation, capped by its original hard TTL. -Retention first enters a durable retirement state. The controller disables the -exact repository runner registration, deletes it, and confirms its absence -before clearing repository, label, and runner ownership into generic idle. A +Each controller-managed registration is ephemeral: Gitea permits it to accept +exactly one task and revokes reuse credentials after assignment. Retention first +enters a durable retirement state. The controller disables the exact repository +runner registration, waits if a pre-upgrade non-ephemeral registration is still +assigned to an in-progress job, then deletes it and confirms its absence before +clearing repository, label, and runner ownership into generic idle. A queued job from any allowed repository can atomically claim generic idle only when persisted server type, location, architecture, and image match one of the job label's candidates. The VM then remains unschedulable while Hetzner rebuilds diff --git a/package/gitea-runner-controller/controller.sh b/package/gitea-runner-controller/controller.sh index fccc0af7..ca04e9ed 100644 --- a/package/gitea-runner-controller/controller.sh +++ b/package/gitea-runner-controller/controller.sh @@ -522,7 +522,7 @@ gcr_sweep_stale_runners() { runners="$(gcr_gitea_list_runners "$repo")" || continue # Here-doc instead of pipe: dash runs pipe tails in a subshell, which # would strand gcr_event/audit writes from the caller's perspective. - while read -r rid rname; do + while read -r rid _rbusy _rdisabled rname; do [ -n "${rid:-}" ] || continue case "$rname" in gcr-*) ;; @@ -534,11 +534,23 @@ gcr_sweep_stale_runners() { jid="${rest%-*}" if ! gcr_record_exists_for_runner "$repo" "$rname"; then gcr_log warn --ns=sweep "stale repo=$repo registration id=$rid name=$rname -> retire" - if gcr_gitea_deregister_runner_exact "$repo" "$rname"; then - gcr_event "stale-runner-deleted" "${jid:-unknown}" "{\"repo\":\"$repo\",\"runner_id\":$rid,\"name\":\"$rname\"}" - else - gcr_log error --ns=sweep "failed deleting repo=$repo runner id=$rid" - fi + stale_status=0 + gcr_gitea_deregister_runner_exact "$repo" "$rname" \ + || stale_status="$?" + case "$stale_status" in + 0) + gcr_event "stale-runner-deleted" "${jid:-unknown}" \ + "{\"repo\":\"$repo\",\"runner_id\":$rid,\"name\":\"$rname\"}" + ;; + 2|3) + gcr_log info --ns=sweep \ + "stale runner retirement pending repo=$repo runner id=$rid" + ;; + *) + gcr_log error --ns=sweep \ + "failed deleting repo=$repo runner id=$rid" + ;; + esac fi done </dev/null || return 1 @@ -50,10 +50,14 @@ gcr_gitea_list_runners() { || return 1 printf '%s' "$runner_page" | jq -e \ '.runners | select(type == "array") - | all(.[]; (.id | type) == "number" and (.name | type) == "string")' \ + | all(.[]; (.id | type) == "number" + and (.busy | type) == "boolean" + and (.disabled | type) == "boolean" + and (.name | type) == "string")' \ >/dev/null || return 1 printf '%s' "$runner_page" \ - | jq -r '.runners[] | "\(.id) \(.name)"' || return 1 + | jq -r '.runners[] | "\(.id) \(.busy) \(.disabled) \(.name)"' \ + || return 1 runner_count="$(printf '%s' "$runner_page" | jq -er '.runners | length')" \ || return 1 [ "$runner_count" -lt 50 ] && return 0 @@ -61,6 +65,37 @@ gcr_gitea_list_runners() { done } +# Find one exact runner after a fresh list. Globals avoid command-substitution +# subshell state loss. Exit 0 = found, 1 = absent, 2 = ambiguous/list failure. +gcr_gitea_find_runner_exact() { + find_runner_repo="$1"; find_runner_name="$2" + GCR_RUNNER_ID="" + GCR_RUNNER_DISABLED="" + find_runner_list="$(gcr_gitea_list_runners "$find_runner_repo")" || return 2 + find_runner_count=0 + while read -r find_runner_id find_runner_busy find_runner_disabled \ + find_runner_candidate_name; do + [ "${find_runner_candidate_name:-}" = "$find_runner_name" ] || continue + case "$find_runner_id" in ''|*[!0-9]*) return 2 ;; esac + case "$find_runner_busy" in true|false) ;; *) return 2 ;; esac + case "$find_runner_disabled" in true|false) ;; *) return 2 ;; esac + find_runner_count=$((find_runner_count + 1)) + GCR_RUNNER_ID="$find_runner_id" + GCR_RUNNER_DISABLED="$find_runner_disabled" + done </dev/null || return 2 + if printf '%s' "$active_jobs" | jq -e --arg runner "$active_runner_id" \ + '.jobs | any(.[]; + .status == "in_progress" and (.runner_id | tostring) == $runner)' \ + >/dev/null; then + return 0 + fi + active_count="$(printf '%s' "$active_jobs" | jq -er '.jobs | length')" \ + || return 2 + [ "$active_count" -lt 50 ] && return 1 + active_page=$((active_page + 1)) + done +} + gcr_gitea_delete_runner() { repo="$1"; id="$2" gcr_gitea_admin_token >/dev/null || return 1 @@ -129,46 +196,51 @@ gcr_gitea_delete_runner() { # name disappeared. Duplicate exact names fail closed for durable retry. gcr_gitea_deregister_runner_exact() { dereg_repo="$1"; dereg_name="$2" + # Stale registrations have no allocation record for a durable barrier. + # Their sweep already retries disable on a later pass; VM retirement + # explicitly supplies an empty third argument to request a new barrier. + dereg_delete_not_before="${3-0}" [ -n "$dereg_repo" ] && [ -n "$dereg_name" ] || return 1 + case "$dereg_delete_not_before" in *[!0-9]*) return 1 ;; esac - dereg_runners="$(gcr_gitea_list_runners "$dereg_repo")" || return 1 - dereg_count=0 - dereg_id="" - while read -r dereg_candidate_id dereg_candidate_name; do - [ "${dereg_candidate_name:-}" = "$dereg_name" ] || continue - case "$dereg_candidate_id" in ''|*[!0-9]*) return 1 ;; esac - dereg_count=$((dereg_count + 1)) - dereg_id="$dereg_candidate_id" - done < "\$token_file" trap 'rm -f "\$token_file"' EXIT HUP INT TERM if [ ! -s /var/lib/gitea-runner/.runner ]; then - /usr/local/bin/gitea-runner register --no-interactive \ + /usr/local/bin/gitea-runner register --no-interactive --ephemeral \ --instance '$GCR_GITEA_URL' --token "\$(cat "\$token_file")" \ --name '$runner_name' --labels '$label:host' \ --config /etc/gitea-runner/config.yaml diff --git a/package/gitea-runner-controller/state.sh b/package/gitea-runner-controller/state.sh index b0b64008..ad6ad10c 100644 --- a/package/gitea-runner-controller/state.sh +++ b/package/gitea-runner-controller/state.sh @@ -167,6 +167,7 @@ gcr_record_retiring_json() { | .bootstrapped = false | .retirement_repo = $repo | .retirement_runner_name = $runner + | del(.retirement_delete_not_before) | .idle_since = ($now | tonumber) | .idle_expires_at = ($expires | tonumber)' } diff --git a/test/package/gitea-runner-controller/test/idle-reuse.sh b/test/package/gitea-runner-controller/test/idle-reuse.sh index b175b2e5..1b67f404 100644 --- a/test/package/gitea-runner-controller/test/idle-reuse.sh +++ b/test/package/gitea-runner-controller/test/idle-reuse.sh @@ -34,12 +34,14 @@ curl() { hostile-token) printf '%s\n' '{"token":"odd $ token"}' ;; paginated) case "$url" in - *page=1*) jq -cn '{runners:[range(1;51) | {id:.,name:("runner-" + tostring)}]}' ;; - *page=2*) printf '{"runners":[{"id":51,"name":"runner-51"}]}\n' ;; + *page=1*) jq -cn '{runners:[range(1;51) | {id:.,busy:false,disabled:false,name:("runner-" + tostring)}]}' ;; + *page=2*) printf '{"runners":[{"id":51,"busy":false,"disabled":false,"name":"runner-51"}]}\n' ;; *) return 1 ;; esac ;; - malformed-page) printf '{"runners":[{"id":"bad","name":"runner"}]}\n' ;; + malformed-page) printf '{"runners":[{"id":"bad","busy":false,"disabled":false,"name":"runner"}]}\n' ;; + active-job) printf '{"jobs":[{"id":465,"status":"in_progress","runner_id":17}]}\n' ;; + inactive-job) printf '{"jobs":[]}\n' ;; *) return 1 ;; esac } @@ -53,8 +55,8 @@ rm -f "$hardening_calls" curl_mode=paginated runners="$(gcr_gitea_list_runners hinterland/hearth)" test "$(printf '%s\n' "$runners" | wc -l | tr -d ' ')" = 51 -printf '%s\n' "$runners" | grep -q '^1 runner-1$' -printf '%s\n' "$runners" | grep -q '^51 runner-51$' +printf '%s\n' "$runners" | grep -q '^1 false false runner-1$' +printf '%s\n' "$runners" | grep -q '^51 false false runner-51$' grep -q 'page=1&limit=50$' "$hardening_calls" grep -q 'page=2&limit=50$' "$hardening_calls" curl_mode=malformed-page @@ -62,9 +64,16 @@ if gcr_gitea_list_runners hinterland/hearth >/dev/null 2>&1; then printf 'malformed runner list must fail closed\n' >&2 exit 1 fi +curl_mode=active-job +gcr_gitea_runner_has_active_job hinterland/hearth 17 +if gcr_gitea_runner_has_active_job hinterland/hearth 18; then + printf 'active job must match exact runner id\n' >&2 + exit 1 +fi +curl_mode=inactive-job retire_calls="$GCR_STATE_DIR/retire-calls" -gcr_gitea_list_runners() { printf '17 gcr-77-1\n'; } +gcr_gitea_list_runners() { printf '17 false false gcr-77-1\n'; } gcr_gitea_set_runner_disabled() { printf 'disable %s %s %s\n' "$1" "$2" "$3" >> "$retire_calls" return 1 @@ -116,9 +125,11 @@ gcr_gitea_runner_disabled() { # Terminal registration is retired exactly before VM becomes generic idle. runner_present=1 +runner_disabled=false gcr_gitea_list_runners() { printf 'list-runners repo=%s\n' "$1" >> "$calls" - [ "$runner_present" -eq 0 ] || printf '17 gcr-301-1\n' + [ "$runner_present" -eq 0 ] \ + || printf '17 false %s gcr-301-1\n' "$runner_disabled" } gcr_gitea_delete_runner() { printf 'delete-runner repo=%s id=%s\n' "$1" "$2" >> "$calls" @@ -127,17 +138,22 @@ gcr_gitea_delete_runner() { gcr_gitea_set_runner_disabled() { printf 'disable-runner repo=%s id=%s value=%s\n' \ "$1" "$2" "$3" >> "$calls" + runner_disabled="$3" } original='{"job_id":"301","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":1000,"ttl_min":480,"vm_id":71,"vm_name":"gcr-301-1","runner_name":"gcr-301-1","server_type":"cx53","budget_rate":"0.032","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}' gcr_record_put 301 1 "$original" gcr_deallocate 301 1 completed:success +test "$(gcr_record_field "$(gcr_record_get 301 1)" status)" = retiring_vm +NOW=2860 +gcr_sweep_retiring_vms +NOW=2800 idle="$(gcr_record_get 301 1)" jq -e 'select(.status == "idle_vm" and .idle_expires_at == 4600 and .repo == "" and .label == "" and .runner_name == "" and .bootstrapped == false and .created_at == 1000 and .ttl_min == 480 and .server_type == "cx53" and .location == "nbg1" and .arch == "amd64" and .image_id == "424242")' "$(gcr_record_path 301 1)" >/dev/null -test "$(grep -c '^list-runners repo=hinterland/hearth$' "$calls")" = 2 +test "$(grep -c '^list-runners repo=hinterland/hearth$' "$calls")" = 3 grep -q '^disable-runner repo=hinterland/hearth id=17 value=true$' "$calls" grep -q '^delete-runner repo=hinterland/hearth id=17$' "$calls" test "$(grep -E '^(disable-runner|delete-runner)' "$calls")" = 'disable-runner repo=hinterland/hearth id=17 value=true diff --git a/test/package/gitea-runner-controller/test/lifecycle-race.sh b/test/package/gitea-runner-controller/test/lifecycle-race.sh new file mode 100644 index 00000000..be163e2d --- /dev/null +++ b/test/package/gitea-runner-controller/test/lifecycle-race.sh @@ -0,0 +1,187 @@ +#!/bin/dash +set -eu + +. "$LOG_SH" +. "$STATE_SH" +. "$DECIDE_SH" +. "$HCLOUD_SH" +. "$GITEA_SH" +. "$CONTROLLER_SH" +. "$WEBHOOK_SH" + +gcr_state_init +export GCR_ALLOWED_REPOS='hectic-lab/util.nix' +export GCR_CONCURRENCY_CAP=1 +export GCR_PER_REPO_CAP=1 +export GCR_NIX_IMAGE_ID=424242 +export GCR_NIX_VERSION=2.35.0 +export GCR_NIX_TARBALL_SHA256=sha256 +export GCR_ARM_NIX_TARBALL_SHA256=sha256 +export GCR_ACT_RUNNER_VERSION=1.0.6 +export GCR_ACT_RUNNER_SHA256=sha256 +export GCR_GITEA_URL=https://gitea.example.test + +NOW=2800 +gcr_now_epoch() { printf '%s' "$NOW"; } +calls="$GCR_STATE_DIR/calls" +runner_present=true +runner_busy=true +runner_disabled=false +disable_delay=120 + +gcr_gitea_list_runners() { + [ "$runner_present" = false ] \ + || printf '127 %s %s gcr-464-1\n' "$runner_busy" "$runner_disabled" +} +gcr_gitea_set_runner_disabled() { + printf 'disable repo=%s id=%s value=%s\n' "$1" "$2" "$3" >> "$calls" + NOW=$((NOW + disable_delay)) + runner_disabled="$3" +} +gcr_gitea_delete_runner() { + printf 'delete repo=%s id=%s\n' "$1" "$2" >> "$calls" + runner_present=false +} +gcr_gitea_job_state() { + case "$2" in + 465) printf '%s' "${JOB_465_STATE:-in_progress:}" ;; + *) return 1 ;; + esac +} +gcr_gitea_runner_has_active_job() { + [ "${JOB_465_STATE:-in_progress:}" = completed:success ] && return 1 + return 0 +} +gcr_vm_create() { + printf 'unexpected-create\n' >> "$calls" + return 1 +} +gcr_vm_destroy() { + printf 'unexpected-destroy vm=%s\n' "$1" >> "$calls" +} + +# Pre-upgrade reusable runner has already accepted job 465 when job 464's +# terminal event starts retirement. Busy recheck must preserve runner and VM. +job_464='{"job_id":"464","run_attempt":"1","repo":"hectic-lab/util.nix","label":"nix","created_at":1000,"ttl_min":480,"vm_id":174028836,"vm_name":"gcr-464-1","runner_name":"gcr-464-1","server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}' +job_465='{"job_id":"465","run_attempt":"1","repo":"hectic-lab/util.nix","label":"nix","created_at":2000,"ttl_min":null,"vm_id":"","vm_name":"","status":"deferred"}' +gcr_record_put 464 1 "$job_464" +gcr_record_put 465 1 "$job_465" + +gcr_deallocate 464 1 completed:success +test "$(gcr_record_field "$(gcr_record_get 464 1)" status)" = retiring_vm +test "$(gcr_record_field "$(gcr_record_get 464 1)" retirement_delete_not_before)" = 2980 +test "$(gcr_record_field "$(gcr_record_get 465 1)" status)" = deferred +test "$(grep -c '^disable ' "$calls")" = 1 +test "$(grep -Ec '^(delete|unexpected-destroy|unexpected-create)' "$calls" || true)" = 0 + +gcr_retry_deferred +gcr_sweep_retiring_vms +test "$(gcr_record_field "$(gcr_record_get 464 1)" status)" = retiring_vm +test "$(gcr_record_field "$(gcr_record_get 465 1)" status)" = deferred +test "$(grep -Ec '^(delete|unexpected-destroy|unexpected-create)' "$calls" || true)" = 0 + +# Once second task is no longer active, retirement can complete. Polling then +# observes job 465 terminal and drops its deferred allocation without rebuild. +runner_busy=false +gcr_sweep_retiring_vms +test "$(gcr_record_field "$(gcr_record_get 464 1)" status)" = retiring_vm +if grep -q '^delete ' "$calls"; then + printf 'retirement must wait one reconcile interval after disable\n' >&2 + exit 1 +fi +NOW=2980 +gcr_sweep_retiring_vms +test "$(gcr_record_field "$(gcr_record_get 464 1)" status)" = retiring_vm +if grep -q '^delete ' "$calls"; then + printf 'quiet in-progress job must block retirement when busy=false\n' >&2 + exit 1 +fi +JOB_465_STATE=completed:success +export JOB_465_STATE +gcr_sweep_retiring_vms +test "$(gcr_record_field "$(gcr_record_get 464 1)" status)" = idle_vm +grep -q '^delete repo=hectic-lab/util.nix id=127$' "$calls" +gcr_retry_deferred +test ! -e "$(gcr_record_path 465 1)" +test "$(grep -Ec '^(unexpected-destroy|unexpected-create)' "$calls" || true)" = 0 + +# Re-disabling a runner refreshes even an expired persisted barrier. Model a +# PATCH taking longer than one reconciliation interval and require the fresh +# barrier to start after PATCH completion. +runner_present=true +runner_disabled=false +NOW=4000 +stale_barrier='{"job_id":"467","run_attempt":"1","repo":"hectic-lab/util.nix","label":"nix","created_at":1000,"ttl_min":480,"vm_id":174028838,"vm_name":"gcr-464-1","runner_name":"gcr-464-1","server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":false,"status":"retiring_vm","retirement_repo":"hectic-lab/util.nix","retirement_runner_name":"gcr-464-1","retirement_delete_not_before":3000,"idle_since":2800,"idle_expires_at":4600}' +gcr_record_put 467 1 "$stale_barrier" +retire_status=0 +gcr_vm_retire_pending 467 1 "$stale_barrier" || retire_status="$?" +test "$retire_status" = 2 +test "$(gcr_record_field "$(gcr_record_get 467 1)" retirement_delete_not_before)" = 4180 +test "$(grep -c '^delete ' "$calls")" = 1 +gcr_record_del 467 1 + +# Busy migration safety never weakens profile hard TTL. Expired retirement is +# destroyed without waiting for runner status or attempting deregistration. +gcr_gitea_deregister_runner_exact() { + printf 'unexpected-deregister\n' >> "$calls" + return 2 +} +gcr_vm_destroy() { + printf 'hard-ttl-destroy vm=%s\n' "$1" >> "$calls" +} +hard_ttl='{"job_id":"466","run_attempt":"1","repo":"hectic-lab/util.nix","label":"nix","created_at":1000,"ttl_min":30,"vm_id":174028837,"vm_name":"gcr-466-1","runner_name":"gcr-466-1","server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":false,"status":"retiring_vm","retirement_repo":"hectic-lab/util.nix","retirement_runner_name":"gcr-466-1","idle_since":2000,"idle_expires_at":2500}' +gcr_record_put 466 1 "$hard_ttl" +gcr_vm_retire_pending 466 1 "$hard_ttl" +test ! -e "$(gcr_record_path 466 1)" +grep -q '^hard-ttl-destroy vm=174028837$' "$calls" +if grep -q '^unexpected-deregister$' "$calls"; then + printf 'hard TTL must not wait on busy runner deregistration\n' >&2 + exit 1 +fi + +# Registration-time ephemeral state is mandatory; daemon --once is not enough +# because it does not provide Gitea's server-side one-task enforcement. +bootstrap="$(gcr_bootstrap_script nix test-token 480 gcr-465-1)" +printf '%s\n' "$bootstrap" \ + | grep -Fq 'gitea-runner register --no-interactive --ephemeral' +if printf '%s\n' "$bootstrap" | grep -Fq 'daemon --once'; then + printf 'daemon --once must not replace ephemeral registration\n' >&2 + exit 1 +fi + +# Stale registrations have no allocation record to persist a barrier. Disable +# on the first sweep, retain active jobs, then delete after they drain. +( + . "$GITEA_SH" + runner_present=true + runner_disabled=false + stale_active=true + stale_calls="$GCR_STATE_DIR/stale-calls" + gcr_gitea_list_runners() { + [ "$runner_present" = false ] \ + || printf '128 false %s gcr-999-1\n' "$runner_disabled" + } + gcr_record_exists_for_runner() { return 1; } + gcr_gitea_set_runner_disabled() { + printf 'disable\n' >> "$stale_calls" + runner_disabled=true + } + gcr_gitea_runner_has_active_job() { + [ "$stale_active" = true ] && return 0 + return 1 + } + gcr_gitea_delete_runner() { + printf 'delete\n' >> "$stale_calls" + runner_present=false + } + gcr_sweep_stale_runners + test "$runner_disabled" = true + test "$(grep -c '^disable$' "$stale_calls")" = 1 + test "$(grep -c '^delete$' "$stale_calls" || true)" = 0 + gcr_sweep_stale_runners + test "$runner_present" = true + stale_active=false + gcr_sweep_stale_runners + test "$runner_present" = false + test "$(grep -c '^delete$' "$stale_calls")" = 1 +)