Engine picker: switch between Docker contexts #16
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Krane uses
DOCKER_HOSTor/var/run/docker.sockonly.What
~/.docker/contexts, current context from~/.docker/config.json) and offer a picker in the sidebar footer and the tray.DOCKER_HOSTstill wins when set.Approach for #16, researched and tested by Nye (Krane team) on a real box. Probe code went to the dev directly.
Krane #16 engine picker: how to build it (Nye, tested Nov 30)
Tested on a real box with bollard 0.20 (features
ssh,ssl), rust 1.100. Five engines, eachconnected,
version(), 5xping(), and received a liveevents()message from a containerstarted through it:
docker system dial-stdio, events OKRootless Docker ($XDG_RUNTIME_DIR/docker.sock) isn't installed here, but it's the same unix path
as Podman's, so the same code. Probe: issue16-probe.rs (+ Cargo.toml) alongside this file.
1. Reading contexts
$DOCKER_CONFIGif set, else~/.docker.contexts/meta/<sha256(name) hex>/meta.json:{"Name":"podman","Metadata":{"Description":"..."},"Endpoints":{"docker":{"Host":"unix:///...","SkipTLSVerify":false}}}Use
Endpoints.docker.Host; skip entries without a docker endpoint; ignore bad JSON (log it).Description is optional (Metadata may be
{}).contexts/tls/<sha256(name)>/docker/{ca.pem,cert.pem,key.pem}(exists only when thecontext was created with ca=/cert=/key=).
defaultis never on disk. Synthesize it: hostunix:///var/run/docker.sock(label "Docker (local)").$DOCKER_CONTEXT, elsecurrentContextinconfig.json, elsedefault(
docker context use defaultremoves the key, so missing = default).sha2(or hash with what's already in the tree) +serdederive; or parse with serde_json::Value.2. Connecting each kind (bollard 0.20)
Enable features:
bollard = { version = "0.20.0", features = ["ssh", "ssl"] }.sslpulls rustls+ring (+native certs);sshpullsopenssh, which shells out to the systemsshclient (needs openssh-client installed at runtime; uses ~/.ssh/config, agent, known_hosts).$DOCKER_CERT_PATH(default ~/.docker).Docker::connect_with_defaults()already does exactly theenv case (unix/tcp/https/ssh) once the features are on; use it for the DOCKER_HOST path.
SkipTLSVerify: truecontexts: bollard has no skip-verify switch. List them, mark "not supported".connect_with_local_defaults()(unix only), so DOCKER_HOST=tcp/ssh doesn'twork at all now; this fixes that too.
Must-do: one shared client, not connect-per-call
Krane calls
Docker::connect_with_local_defaults()in ~14 places (docker.rs, window/mod.rs,window/logs.rs). For unix that's harmless; for ssh every new client launches a new ssh master
process (bollard's SshConnector does
launch_masterper connection), and each connect wouldre-auth (slow, agent prompts). Make one
engine::current() -> Docker(Docker is Clone and sharesits pool) behind an
RwLock/arc-swap plus a generation counter, and replace every call site.Must-do: timeouts
A dead tcp host made the docker CLI hang 1m38s before "i/o timeout". Wrap the first
version()/ping()on connect (and the 3 s poll) intokio::time::timeout(5s)so the UI shows"engine unreachable" quickly instead of hanging the fetch loop.
Volume sizes
volume_sizes()opens the socket fromDOCKER_HOSTdirectly. Point it at the selected engine'sunix path (and skip for ssh/tcp, as it already does for non-unix). Podman's df JSON may differ;
the parser already tolerates missing keys (returns empty), so sizes just disappear there.
3. Precedence and persistence (matches the docker CLI; verified)
DOCKER_HOSTset and non-empty -> it wins. Picker shows it as a fixed "DOCKER_HOST (tcp://...)"entry, others disabled with a tooltip. (CLI check: DOCKER_HOST + DOCKER_CONTEXT=podman -> Docker.)
$DOCKER_CONTEXT, else config.jsoncurrentContext, elsedefault.Store the choice by context name in
$XDG_CONFIG_HOME/krane/settings.json(default~/.config/krane/),
{"engine":"podman"}; write via temp file + rename. Don't calldocker context use(Krane mustn't change the user's shell default). If #18/#19 add a settings filefirst, share it.
4. Switching engine: resetting app state
On select: build the new client,
version()with 5 s timeout; on success:ENGINE_GENERATIONand swap the shared client;the UI shows "connecting" not stale data from the old engine;
watch_resource_events) and any stats/logs streams must drop: easiest is eachloop captures the generation at start and breaks when it changes (or
select!on awatch::Receiver<u64>); then it reconnects withengine::current(). Same for open log windows(close them or show "engine changed") and the #11 stats stream;
RESOURCE_KICK+ container refresh immediately (don't wait the 3 s tick);version.platform.name/version("Podman 5.4.2" vs"Docker 26.1.5"); tray menu rebuilt.
On failure: keep the old engine, show the error, don't persist.
5. #14 debug shell (and #20 open-in-terminal) must target the same engine
The terminal runs the docker CLI, which by itself follows DOCKER_HOST / currentContext, not Krane's
choice. Pass the engine explicitly:
default):docker --context <name> start -ai <helper>.Use
--context, not-H <host>:-H tcp://...loses the context's TLS certs.docker ...(env is inherited). Never add --context in that case:tested,
--context podmanbeats DOCKER_HOST, so it would hit a different engine than Krane.dockertyped by the userhits the same engine.
Checked the #14 helper on Podman 5.4.2 via the CLI:
--pid container:X --network container:X --cap-add SYS_PTRACEworks (ps shows target PID 1, /proc/1/root readable).For ssh engines the helper runs remotely;
docker --context <ssh ctx> start -aiworks (dial-stdio).6. Test plan for review
name, currentContext missing/
default, precedence table (DOCKER_HOST > saved > DOCKER_CONTEXT >config > default), saved context deleted -> fallback.
Calls for Jeremy
None needed. Defaults chosen: Krane remembers its own choice (doesn't touch
docker context use);DOCKER_HOST locks the picker; SkipTLSVerify contexts shown as unsupported.