Skip to content

Commit 78067b1

Browse files
committed
refactor(bootstrap): deduplicate runtime resolution for gateway connections
Add connect_for_gateway(name) helper that resolves the container runtime from stored gateway metadata first, falling back to detect_runtime() with full error propagation instead of silently defaulting to Docker. Replace the duplicated inline metadata-detect-fallback blocks in extract_and_store_pki and gateway_container_logs with the new helper.
1 parent 06bac36 commit 78067b1

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

crates/openshell-bootstrap/src/docker.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,23 @@ pub(crate) fn connect_local_auto() -> Result<Docker> {
273273
connect_local(runtime)
274274
}
275275

276+
/// Connect to the local container runtime for an existing gateway.
277+
///
278+
/// Resolution order:
279+
/// 1. Stored runtime from gateway metadata (if metadata exists)
280+
/// 2. Auto-detect runtime via `detect_runtime` (propagates error on failure)
281+
///
282+
/// This is used by code paths that have a gateway `name` but no `runtime`
283+
/// in scope. Unlike `connect_local_auto()`, this checks metadata first so
284+
/// that gateways deployed with a specific runtime reconnect to the same one.
285+
pub(crate) fn connect_for_gateway(name: &str) -> Result<Docker> {
286+
let runtime = match crate::metadata::get_gateway_metadata(name) {
287+
Some(m) => m.container_runtime,
288+
None => crate::container_runtime::detect_runtime(None)?,
289+
};
290+
connect_local(runtime)
291+
}
292+
276293
/// Build a rich, user-friendly error when a container runtime is not reachable.
277294
fn runtime_not_reachable_error(
278295
runtime: ContainerRuntime,

crates/openshell-bootstrap/src/lib.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -669,12 +669,7 @@ pub async fn extract_and_store_pki(
669669
) -> Result<()> {
670670
let docker = match remote {
671671
Some(r) => create_ssh_docker_client(r).await?,
672-
None => {
673-
let runtime = get_gateway_metadata(name)
674-
.map(|m| m.container_runtime)
675-
.unwrap_or_else(|| detect_runtime(None).unwrap_or(ContainerRuntime::Docker));
676-
docker::connect_local(runtime)?
677-
}
672+
None => docker::connect_for_gateway(name)?,
678673
};
679674
let cname = docker::find_gateway_container(&docker, port).await?;
680675
let bundle = load_existing_pki_bundle(&docker, &cname, constants::KUBECONFIG_PATH)
@@ -717,12 +712,7 @@ pub async fn gateway_container_logs<W: std::io::Write>(
717712

718713
let docker = match remote {
719714
Some(remote_opts) => create_ssh_docker_client(remote_opts).await?,
720-
None => {
721-
let runtime = get_gateway_metadata(name)
722-
.map(|m| m.container_runtime)
723-
.unwrap_or_else(|| detect_runtime(None).unwrap_or(ContainerRuntime::Docker));
724-
docker::connect_local(runtime)?
725-
}
715+
None => docker::connect_for_gateway(name)?,
726716
};
727717

728718
let container = container_name(name);

0 commit comments

Comments
 (0)