Builder: restart an idle running builder after a registry settings change (#329)

This commit is contained in:
Precious Okwu authored and Precious Okwu committed 2026-11-23 12:15:58 +01:00
1 parent 7898c46c72
commit 06fc8cb699
4 files changed
+54 -13

No files matched your search

+33 -12
View File
@@ -533,9 +533,18 @@ pub fn create_for_buildx(engine: &Engine, name: Option<&str>) -> Option<Result<V
Some(create(engine, &mut |_| {}).map(|id| json!({"Id": id, "Warnings": []})))
}
/// Claims the builder for a build or prune after checking whether it must
/// be restarted first: this build is counted in [`BUILDS`] only after the
/// busy check, so an idle stale builder is restarted while one another
/// build is using is not.
fn claim_for_build(up: bool, execs: usize, stale: bool) -> (bool, Busy) {
let restart = restart_now(up, BUILDS.load(Ordering::Relaxed) > 0 || execs > 0, stale);
(restart, Busy::new())
}
/// The builder, made if need be, started and answering. `say` hears what
/// takes a while (a pull, a start).
fn ensure_running(engine: &Engine, say: &mut dyn FnMut(&str)) -> Result<String, Error> {
fn ensure_running(engine: &Engine, say: &mut dyn FnMut(&str)) -> Result<(String, Busy), Error> {
if find(engine).is_none() {
// Before the lock: a pull must not hold the builder up for others.
ensure_image(engine, say)?;
@@ -545,12 +554,10 @@ fn ensure_running(engine: &Engine, say: &mut dyn FnMut(&str)) -> Result<String,
Some(found) => found,
None => (create(engine, say)?, Status::Created),
};
// daemon.json edited by hand since the builder started (Settings and
// `kiri registry` refresh it at once): restarted with the new file
// while no build uses it, as buildkitd reads it only when it starts.
let stale = status == Status::Running
&& !builder_busy(engine, &id)
&& is_stale(WRITTEN.lock().unwrap_or_else(|e| e.into_inner()).as_deref(), &Registries::now(&super::mirror::registry_names(engine)));
// daemon.json edited by hand, or a setting saved while the builder ran:
// restarted with the new file while no other build uses it, as buildkitd
// reads it only when it starts.
let (stale, busy) = claim_for_build(status == Status::Running, engine.running_execs(&id), stale_now(engine));
if stale {
if engine.is_paused(&id) {
engine.pause(&id, false)?;
@@ -566,7 +573,7 @@ fn ensure_running(engine: &Engine, say: &mut dyn FnMut(&str)) -> Result<String,
}
LAST_USE.store(now_secs(), Ordering::Relaxed);
wait_ready(engine, &id)?;
Ok(id)
Ok((id, busy))
}
/// Waits for a started builder's buildkitd to answer.
@@ -1012,7 +1019,6 @@ pub fn build(req: &Request, engine: &Arc<Engine>, w: &mut UnixStream, r: &mut Bu
*h = format!("{}={host}", &h[..h.len() - mac.len()]);
}
}
let _busy = Busy::new();
// The context may be left partly unread: the connection closes after.
http::begin_stream_with(w, 200, "application/json", http::CLOSE)?;
let probe = w.try_clone()?;
@@ -1051,7 +1057,7 @@ pub fn build(req: &Request, engine: &Arc<Engine>, w: &mut UnixStream, r: &mut Bu
fn run(req: &Request, engine: &Engine, o: &mut Options, r: &mut BufReader<UnixStream>, out: &mut Out<'_>, probe: &UnixStream) -> Result<Vec<kiri_image::Image>, String> {
let quiet = o.quiet;
let id = ensure_running(engine, &mut |m| {
let (id, _busy) = ensure_running(engine, &mut |m| {
if !quiet {
out.stream(&format!("{m}\n"));
}
@@ -1220,8 +1226,7 @@ pub fn prune(req: &Request, engine: &Engine) -> Result<Value, Error> {
if find(engine).is_none() {
return Ok(json!({"CachesDeleted": [], "SpaceReclaimed": 0}));
}
let _busy = Busy::new();
let id = ensure_running(engine, &mut |_| {})?;
let (id, _busy) = ensure_running(engine, &mut |_| {})?;
let argv: Vec<&str> = argv.iter().map(String::as_str).collect();
let (code, text) = engine.exec_capture(&id, &argv)?;
if code != 0 {
@@ -1376,6 +1381,22 @@ mod tests {
assert!(!restart_now(true, false, false));
let at = |s: Step| STOP_THEN_WRITE.iter().position(|x| *x == s).unwrap();
assert!(at(Step::Resume) < at(Step::Stop) && at(Step::Stop) < at(Step::Write), "{STOP_THEN_WRITE:?}");
let _g = ENSURE.lock().unwrap_or_else(|e| e.into_inner());
assert_eq!(BUILDS.load(Ordering::Relaxed), 0);
let (restart_first, b1) = claim_for_build(true, 0, true);
assert!(restart_first, "an idle stale builder is restarted before this build marks it busy");
assert_eq!(BUILDS.load(Ordering::Relaxed), 1);
let (restart_while_busy, b2) = claim_for_build(true, 0, true);
assert!(!restart_while_busy, "a builder another build is using must not be restarted");
drop((b1, b2));
assert_eq!(BUILDS.load(Ordering::Relaxed), 0);
let (restart_fresh, b3) = claim_for_build(true, 0, false);
assert!(!restart_fresh, "a fresh builder is not restarted");
drop(b3);
assert_eq!(BUILDS.load(Ordering::Relaxed), 0);
let (restart_exec_busy, _b4) = claim_for_build(true, 1, true);
assert!(!restart_exec_busy, "a builder with an active buildx exec is not restarted");
}
/// `before_exec` re-checks the builder's state under `ENSURE` (so a waiter
+7 -1
View File
@@ -13,6 +13,12 @@ A log of every merged bug fix in Kiri, newest first, together with any later bug
## Fixes
### Builder: restart an idle running builder after a registry settings change (unreleased)
- Fixes: #329 Support insecure-registries (plain-HTTP and self-signed private registries), and read Docker's daemon.json; #118 Registry mirrors for Docker Hub (Settings + CLI)
- Caused: none known
- Caused by: #119
- Check: `insecure-selfsigned-build`, `insecure-selfsigned-unlisted-build-refused`, `mirror-build`
### #608 Mac: the memory balloon leaves the guest its headroom, so containers don't stall every few seconds (unreleased)
- Fixes: #509 Containers on a user network get TCP resets under sustained load (Supabase local stack: Kong → PostgREST); #646 kiri-balloon oscillates violently on idle VM: 100% CPU, 18+ TB page-in thrashing, bloated host RAM; #210 macOS: containers report hundreds of GB of block reads while the guest has no page cache
- Caused: none known
@@ -33,7 +39,7 @@ A log of every merged bug fix in Kiri, newest first, together with any later bug
### #119 Images: registry mirrors for Docker Hub, in Settings and the CLI (unreleased)
- Fixes: #118 Registry mirrors for Docker Hub (Settings + CLI); #329 Support insecure-registries (plain-HTTP and self-signed private registries), and read Docker's daemon.json; #611 Could support for pulling images from private repositories be added as soon as possible?
- Caused: none known
- Caused: `build()` and `prune()` in `daemon/builder.rs` took `Busy::new()` before `ensure_running()` checked `!builder_busy()`, so a running builder was never restarted after a mirror or insecure-registry change and `mirror-build` and `insecure-selfsigned-build` failed (found running `tests/linux-host.sh` on Linux 6.8)
- Caused by: -
- Check: `insecure-http-unlisted-refused`, `insecure-tls-unlisted-refused`, `insecure-login-unlisted-refused`, `insecure-cli-add`, `insecure-cli-refuses-everything`, `insecure-http-pull`, `insecure-http-push`, `insecure-selfsigned-pull`, `insecure-http-login`, `insecure-http-auth-pull`, `insecure-other-port-still-refused`, `insecure-docker-info`, `insecure-build`, `insecure-selfsigned-build`, `insecure-removed-refused`, `insecure-hand-edit`, `insecure-hand-edit-keeps-keys`, `mirror-http-pull`, `mirror-docker-info`, `mirror-build`, `insecure-cli-remove`, `mirror-cli-add`, `hub-downgrade-refused`, `socket-mount-settings-refused`, `mirror-cli-plain-needs-listing`, `insecure-daemon-json`
+13
View File
@@ -1514,6 +1514,19 @@ if [ -n "$HOSTIP" ]; then
t mirror-cli-add "http://$HOSTIP:5056" "$KIRI" registry mirror add "http://$HOSTIP:5056"
t mirror-http-pull "3." bash -c "$D pull -q kiri-mirror-only:1 >/dev/null && $D run --rm kiri-mirror-only:1 cat /etc/alpine-release"
t mirror-docker-info "http://$HOSTIP:5056/" bash -c "$D info 2>/dev/null | sed -n '/Registry Mirrors/,/^ [A-Z]/p'"
if command -v openssl >/dev/null 2>&1 && openssl req -x509 -newkey rsa:2048 -nodes -days 3 -subj /CN=kiri-other.test -addext subjectAltName=DNS:kiri-other.test -keyout "$REG/other.key" -out "$REG/other.pem" 2>/dev/null; then
chmod 644 "$REG/other.key"
$D run -d --name reg3 -p 5444:5000 -v "$REG:/tls:ro" -e REGISTRY_HTTP_TLS_CERTIFICATE=/tls/other.pem -e REGISTRY_HTTP_TLS_KEY=/tls/other.key registry:2 >/dev/null
for _ in $(seq 50); do curl -sk -m 1 -o /dev/null https://localhost:5444/v2/ && break; sleep 0.2; done
$D tag alpine localhost:5444/team/alp:1 && $D push -q localhost:5444/team/alp:1 >/dev/null && $D rmi localhost:5444/team/alp:1 >/dev/null
t insecure-selfsigned-unlisted-build-refused "refused" bash -c "gw=\$($D inspect reg3 --format '{{.NetworkSettings.IPAddress}}'):5000; mkdir -p \"$REG/bins\" && printf 'FROM %s/team/alp:1\nRUN true\n' \"\$gw\" > \"$REG/bins/Dockerfile\" && (DOCKER_BUILDKIT=0 $D build --no-cache \"$REG/bins\" >/dev/null 2>&1 && echo built || echo refused)"
t insecure-selfsigned-build "dengis-fles" bash -c "gw=\$($D inspect reg3 --format '{{.NetworkSettings.IPAddress}}'):5000; \"$KIRI\" registry insecure add \$gw >/dev/null 2>&1; mkdir -p \"$REG/bins\" && printf 'FROM %s/team/alp:1\nRUN echo self-signed | rev\n' \"\$gw\" > \"$REG/bins/Dockerfile\" && DOCKER_BUILDKIT=0 $D build --no-cache \"$REG/bins\" >\"$REG/b.log\" 2>&1; grep -m1 -o dengis-fles \"$REG/b.log\" || grep -Ei -m3 'error|fail' \"$REG/b.log\"; \"$KIRI\" registry insecure remove \$gw >/dev/null 2>&1"
$D rm -f reg3 >/dev/null
else
skip_setup insecure-selfsigned-unlisted-build-refused "openssl is not installed"
skip_setup insecure-selfsigned-build "openssl is not installed"
fi
t mirror-build "rorrim-tliub" bash -c "gw=\$($D inspect reg2 --format '{{.NetworkSettings.IPAddress}}'):5000; \"$KIRI\" registry mirror remove http://$HOSTIP:5056 >/dev/null 2>&1; \"$KIRI\" registry insecure add \$gw >/dev/null 2>&1; \"$KIRI\" registry mirror add http://\$gw >/dev/null 2>&1; $D rmi kiri-mirror-only:1 >/dev/null 2>&1; mkdir -p \"$REG/bins\" && printf 'FROM kiri-mirror-only:1\nRUN echo built-mirror | rev\n' > \"$REG/bins/Dockerfile\" && DOCKER_BUILDKIT=0 $D build --no-cache \"$REG/bins\" >\"$REG/b.log\" 2>&1; grep -m1 -o rorrim-tliub \"$REG/b.log\" || grep -Ei -m3 'error|fail' \"$REG/b.log\"; \"$KIRI\" registry mirror remove http://\$gw >/dev/null 2>&1; \"$KIRI\" registry insecure remove \$gw >/dev/null 2>&1"
"$KIRI" registry mirror remove "http://$HOSTIP:5056" >/dev/null 2>&1; "$KIRI" registry insecure remove "$HOSTIP:5056" >/dev/null 2>&1; $R logout "$HOSTIP:5055" >/dev/null 2>&1
$D rmi kiri-mirror-only:1 >/dev/null 2>&1; $D rm -f reg2 >/dev/null
else
+1
View File
@@ -351,6 +351,7 @@ t insecure-docker-info "kiri-trusted.test:5055" "docker info 2>/dev/null | sed -
t insecure-build "erucesni-tliub" "gw=\$(docker inspect kiri-reg-http --format '{{.NetworkSettings.IPAddress}}'):5000; /opt/kiri/kiri registry insecure add \$gw >/dev/null 2>&1; mkdir -p /bins && printf \"FROM \$gw/team/alp:1\\nRUN echo built-insecure | rev\\n\" > /bins/Dockerfile && DOCKER_BUILDKIT=0 docker build --no-cache /bins >/tmp/b.log 2>&1; grep -m1 -o erucesni-tliub /tmp/b.log || grep -i -m3 'error\|fail' /tmp/b.log"
# The same from a registry on self-signed HTTPS, cold: unchecked HTTPS,
# not plain HTTP to its TLS port.
t insecure-selfsigned-unlisted-build-refused "refused" "gw=\$(docker inspect kiri-reg-other --format '{{.NetworkSettings.IPAddress}}'):5000; mkdir -p /bins && printf \"FROM \$gw/team/alp:1\\nRUN true\\n\" > /bins/Dockerfile && (DOCKER_BUILDKIT=0 docker build --no-cache /bins >/dev/null 2>&1 && echo built || echo refused)"
t insecure-selfsigned-build "dengis-fles" "gw=\$(docker inspect kiri-reg-other --format '{{.NetworkSettings.IPAddress}}'):5000; /opt/kiri/kiri registry insecure add \$gw >/dev/null 2>&1; printf \"FROM \$gw/team/alp:1\\nRUN echo self-signed | rev\\n\" > /bins/Dockerfile && DOCKER_BUILDKIT=0 docker build --no-cache /bins >/tmp/b.log 2>&1; grep -m1 -o dengis-fles /tmp/b.log || grep -i -m3 'error\|fail' /tmp/b.log"
t insecure-cli-remove "kiri-trusted.test:5056" "/opt/kiri/kiri registry insecure remove kiri-trusted.test:5055 kiri-other.test:5444 2>&1"
t insecure-removed-refused "refused" "docker rmi kiri-other.test:5444/team/alp:1 >/dev/null 2>&1; docker pull kiri-other.test:5444/team/alp:1 >/dev/null 2>&1 $PULLED"