Skip to content

Commit 5832a22

Browse files
committed
refactor(vm): model local image sources
Signed-off-by: Evan Lezar <[email protected]>
1 parent 903ef71 commit 5832a22

2 files changed

Lines changed: 128 additions & 36 deletions

File tree

‎architecture/compute-runtimes.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ existing VM image acquisition -> UnpackedImage -> VmRootfsImage
3838
construction functions. Both cached-image paths use it before handing the
3939
resulting ext4 image to the existing VM launch path.
4040

41+
Local image acquisition identifies Docker and Podman as distinct
42+
Docker-compatible image sources, while preserving the existing Docker-first,
43+
Podman-fallback selection policy. Registry acquisition remains a separate path.
44+
4145
The capability RPC reports driver identity, version, and the default sandbox
4246
image used by the gateway. GPU availability stays driver-local and is validated
4347
when a sandbox create request asks for GPU resources.

‎crates/openshell-driver-vm/src/driver.rs‎

Lines changed: 124 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,39 @@ struct GuestImagePayload {
203203

204204
#[derive(Debug, Clone)]
205205
enum GuestImagePayloadSource {
206-
RegistryOciLayout { layout_dir: PathBuf },
207-
LocalDocker { rootfs_archive: PathBuf },
206+
RegistryOciLayout {
207+
layout_dir: PathBuf,
208+
},
209+
LocalContainer {
210+
engine: LocalContainerEngine,
211+
rootfs_archive: PathBuf,
212+
},
213+
}
214+
215+
/// The local container engine that supplied an image to the VM driver.
216+
///
217+
/// Both engines implement the Docker-compatible API used for inspect and
218+
/// export, but retaining their identity keeps source selection and progress
219+
/// reporting accurate.
220+
#[derive(Debug, Clone, Copy)]
221+
enum LocalContainerEngine {
222+
Docker,
223+
Podman,
224+
}
225+
226+
impl LocalContainerEngine {
227+
const fn image_source(self) -> &'static str {
228+
match self {
229+
Self::Docker => "local_docker",
230+
Self::Podman => "local_podman",
231+
}
232+
}
233+
}
234+
235+
/// A local image source backed by a Docker-compatible container engine.
236+
struct LocalContainerImageSource {
237+
client: Docker,
238+
engine: LocalContainerEngine,
208239
}
209240

210241
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
@@ -1953,7 +1984,7 @@ impl VmDriver {
19531984
async fn resolve_local_container_image(
19541985
&self,
19551986
image_ref: &str,
1956-
) -> Result<Option<(Docker, String)>, Status> {
1987+
) -> Result<Option<(LocalContainerImageSource, String)>, Status> {
19571988
let required_local_image = is_openshell_local_build_image_ref(image_ref);
19581989
let engine = match connect_local_container_engine().await {
19591990
Some(engine) => engine,
@@ -1971,7 +2002,7 @@ impl VmDriver {
19712002
}
19722003
};
19732004

1974-
match engine.inspect_image(image_ref).await {
2005+
match engine.client.inspect_image(image_ref).await {
19752006
Ok(inspect) => {
19762007
if let Some(message) = local_image_platform_mismatch(
19772008
image_ref,
@@ -1999,6 +2030,7 @@ impl VmDriver {
19992030
info!(
20002031
image_ref = %image_ref,
20012032
image_identity = %image_identity,
2033+
image_source = engine.engine.image_source(),
20022034
"vm driver: resolved image from local container engine"
20032035
);
20042036
Ok(Some((engine, image_identity)))
@@ -2027,7 +2059,7 @@ impl VmDriver {
20272059
&self,
20282060
sandbox_id: &str,
20292061
image_ref: &str,
2030-
docker: &Docker,
2062+
source: &LocalContainerImageSource,
20312063
image_identity: &str,
20322064
) -> Result<String, Status> {
20332065
let cache_identity = bootstrap_image_cache_identity(image_identity);
@@ -2050,7 +2082,10 @@ impl VmDriver {
20502082
format!("Using cached VM root disk for local image \"{image_ref}\""),
20512083
HashMap::from([
20522084
("image_ref".to_string(), image_ref.to_string()),
2053-
("image_source".to_string(), "local_docker".to_string()),
2085+
(
2086+
"image_source".to_string(),
2087+
source.engine.image_source().to_string(),
2088+
),
20542089
("cache_hit".to_string(), "true".to_string()),
20552090
("image_identity".to_string(), cache_identity.clone()),
20562091
]),
@@ -2066,7 +2101,10 @@ impl VmDriver {
20662101
format!("Preparing VM root disk cache for local image \"{image_ref}\""),
20672102
HashMap::from([
20682103
("image_ref".to_string(), image_ref.to_string()),
2069-
("image_source".to_string(), "local_docker".to_string()),
2104+
(
2105+
"image_source".to_string(),
2106+
source.engine.image_source().to_string(),
2107+
),
20702108
("cache_hit".to_string(), "false".to_string()),
20712109
("image_identity".to_string(), cache_identity.clone()),
20722110
]),
@@ -2088,7 +2126,10 @@ impl VmDriver {
20882126
format!("Using cached VM root disk for local image \"{image_ref}\""),
20892127
HashMap::from([
20902128
("image_ref".to_string(), image_ref.to_string()),
2091-
("image_source".to_string(), "local_docker".to_string()),
2129+
(
2130+
"image_source".to_string(),
2131+
source.engine.image_source().to_string(),
2132+
),
20922133
("cache_hit".to_string(), "true".to_string()),
20932134
("image_identity".to_string(), cache_identity.clone()),
20942135
]),
@@ -2098,7 +2139,7 @@ impl VmDriver {
20982139
return Ok(cache_identity);
20992140
}
21002141

2101-
self.build_cached_local_image_rootfs_image(sandbox_id, docker, image_ref, &cache_identity)
2142+
self.build_cached_local_image_rootfs_image(sandbox_id, source, image_ref, &cache_identity)
21022143
.await?;
21032144
self.publish_pulled_event(sandbox_id, image_ref, &image_path)
21042145
.await;
@@ -2111,14 +2152,14 @@ impl VmDriver {
21112152
image_ref: &str,
21122153
bootstrap_root_disk: &Path,
21132154
) -> Result<PreparedImageDisk, Status> {
2114-
if let Some((docker, image_identity)) =
2155+
if let Some((source, image_identity)) =
21152156
self.resolve_local_container_image(image_ref).await?
21162157
{
21172158
return self
21182159
.ensure_prepared_local_image_disk(
21192160
sandbox_id,
21202161
image_ref,
2121-
&docker,
2162+
&source,
21222163
&image_identity,
21232164
bootstrap_root_disk,
21242165
)
@@ -2133,25 +2174,40 @@ impl VmDriver {
21332174
&self,
21342175
sandbox_id: &str,
21352176
image_ref: &str,
2136-
docker: &Docker,
2177+
source: &LocalContainerImageSource,
21372178
image_identity: &str,
21382179
bootstrap_root_disk: &Path,
21392180
) -> Result<PreparedImageDisk, Status> {
21402181
let cache_identity = prepared_image_cache_identity(image_identity);
21412182
let image_path = image_cache_rootfs_image(&self.config.state_dir, &cache_identity);
21422183

21432184
if tokio::fs::metadata(&image_path).await.is_ok() {
2144-
self.publish_prepared_cache_hit(sandbox_id, image_ref, "local_docker", &cache_identity);
2185+
self.publish_prepared_cache_hit(
2186+
sandbox_id,
2187+
image_ref,
2188+
source.engine.image_source(),
2189+
&cache_identity,
2190+
);
21452191
return Ok(PreparedImageDisk {
21462192
image_identity: cache_identity,
21472193
disk_path: image_path,
21482194
});
21492195
}
21502196

2151-
self.publish_prepared_cache_miss(sandbox_id, image_ref, "local_docker", &cache_identity);
2197+
self.publish_prepared_cache_miss(
2198+
sandbox_id,
2199+
image_ref,
2200+
source.engine.image_source(),
2201+
&cache_identity,
2202+
);
21522203
let _cache_guard = self.image_cache_lock.lock().await;
21532204
if tokio::fs::metadata(&image_path).await.is_ok() {
2154-
self.publish_prepared_cache_hit(sandbox_id, image_ref, "local_docker", &cache_identity);
2205+
self.publish_prepared_cache_hit(
2206+
sandbox_id,
2207+
image_ref,
2208+
source.engine.image_source(),
2209+
&cache_identity,
2210+
);
21552211
return Ok(PreparedImageDisk {
21562212
image_identity: cache_identity,
21572213
disk_path: image_path,
@@ -2168,12 +2224,15 @@ impl VmDriver {
21682224
format!("Exporting rootfs from local image \"{image_ref}\""),
21692225
HashMap::from([
21702226
("image_ref".to_string(), image_ref.to_string()),
2171-
("image_source".to_string(), "local_docker".to_string()),
2227+
(
2228+
"image_source".to_string(),
2229+
source.engine.image_source().to_string(),
2230+
),
21722231
("image_identity".to_string(), cache_identity.clone()),
21732232
]),
21742233
);
21752234
if let Err(err) =
2176-
export_local_image_rootfs_to_path(docker, image_ref, &rootfs_archive).await
2235+
export_local_image_rootfs_to_path(&source.client, image_ref, &rootfs_archive).await
21772236
{
21782237
let _ = tokio::fs::remove_dir_all(&staging_dir).await;
21792238
return Err(err);
@@ -2182,12 +2241,15 @@ impl VmDriver {
21822241
let payload = GuestImagePayload {
21832242
image_ref: image_ref.to_string(),
21842243
image_identity: cache_identity.clone(),
2185-
source: GuestImagePayloadSource::LocalDocker { rootfs_archive },
2244+
source: GuestImagePayloadSource::LocalContainer {
2245+
engine: source.engine,
2246+
rootfs_archive,
2247+
},
21862248
};
21872249
self.build_prepared_image_disk(
21882250
sandbox_id,
21892251
image_ref,
2190-
"local_docker",
2252+
source.engine.image_source(),
21912253
&cache_identity,
21922254
bootstrap_root_disk,
21932255
&staging_dir,
@@ -2539,7 +2601,7 @@ impl VmDriver {
25392601
async fn build_cached_local_image_rootfs_image(
25402602
&self,
25412603
sandbox_id: &str,
2542-
docker: &Docker,
2604+
source: &LocalContainerImageSource,
25432605
image_ref: &str,
25442606
image_identity: &str,
25452607
) -> Result<(), Status> {
@@ -2577,12 +2639,15 @@ impl VmDriver {
25772639
format!("Exporting rootfs from local image \"{image_ref}\""),
25782640
HashMap::from([
25792641
("image_ref".to_string(), image_ref.to_string()),
2580-
("image_source".to_string(), "local_docker".to_string()),
2642+
(
2643+
"image_source".to_string(),
2644+
source.engine.image_source().to_string(),
2645+
),
25812646
("image_identity".to_string(), image_identity.to_string()),
25822647
]),
25832648
);
25842649
if let Err(err) =
2585-
export_local_image_rootfs_to_path(docker, image_ref, &exported_rootfs).await
2650+
export_local_image_rootfs_to_path(&source.client, image_ref, &exported_rootfs).await
25862651
{
25872652
let _ = tokio::fs::remove_dir_all(&staging_dir).await;
25882653
return Err(err);
@@ -2600,7 +2665,10 @@ impl VmDriver {
26002665
),
26012666
HashMap::from([
26022667
("image_ref".to_string(), image_ref.to_string()),
2603-
("image_source".to_string(), "local_docker".to_string()),
2668+
(
2669+
"image_source".to_string(),
2670+
source.engine.image_source().to_string(),
2671+
),
26042672
("image_identity".to_string(), image_identity.to_string()),
26052673
("sandbox_uid".to_string(), sandbox_uid.to_string()),
26062674
]),
@@ -2622,7 +2690,10 @@ impl VmDriver {
26222690
"Formatting VM root disk image".to_string(),
26232691
HashMap::from([
26242692
("image_ref".to_string(), image_ref.to_string()),
2625-
("image_source".to_string(), "local_docker".to_string()),
2693+
(
2694+
"image_source".to_string(),
2695+
source.engine.image_source().to_string(),
2696+
),
26262697
("image_identity".to_string(), image_identity.to_string()),
26272698
]),
26282699
);
@@ -3239,11 +3310,14 @@ fn parse_registry_reference(image_ref: &str) -> Result<Reference, Status> {
32393310
/// Tries Docker first (`connect_with_local_defaults`, which respects
32403311
/// `DOCKER_HOST`). If Docker is unavailable, falls back to the Podman
32413312
/// socket, which exposes a Docker-compatible API.
3242-
async fn connect_local_container_engine() -> Option<Docker> {
3313+
async fn connect_local_container_engine() -> Option<LocalContainerImageSource> {
32433314
if let Ok(docker) = Docker::connect_with_local_defaults()
32443315
&& docker.ping().await.is_ok()
32453316
{
3246-
return Some(docker);
3317+
return Some(LocalContainerImageSource {
3318+
client: docker,
3319+
engine: LocalContainerEngine::Docker,
3320+
});
32473321
}
32483322

32493323
let podman_socket = podman_socket_path();
@@ -3256,7 +3330,10 @@ async fn connect_local_container_engine() -> Option<Docker> {
32563330
socket = %podman_socket.display(),
32573331
"vm driver: connected to Podman (Docker-compatible API)"
32583332
);
3259-
return Some(docker);
3333+
return Some(LocalContainerImageSource {
3334+
client: docker,
3335+
engine: LocalContainerEngine::Podman,
3336+
});
32603337
}
32613338

32623339
None
@@ -3297,7 +3374,7 @@ fn local_image_platform_mismatch(
32973374

32983375
(actual_os != expected_os || actual_arch != expected_arch).then(|| {
32993376
format!(
3300-
"local Docker image '{image_ref}' is {actual_os}/{actual_arch}, but VM sandboxes require {expected_os}/{expected_arch}"
3377+
"local container image '{image_ref}' is {actual_os}/{actual_arch}, but VM sandboxes require {expected_os}/{expected_arch}"
33013378
)
33023379
})
33033380
}
@@ -3338,7 +3415,7 @@ async fn export_local_image_rootfs_to_path(
33383415
.await
33393416
.map_err(|err| {
33403417
Status::failed_precondition(format!(
3341-
"failed to create temporary export container for local Docker image '{image_ref}': {err}"
3418+
"failed to create temporary export container for local container image '{image_ref}': {err}"
33423419
))
33433420
})?;
33443421
let container_id = container.id;
@@ -3359,7 +3436,7 @@ async fn export_local_image_rootfs_to_path(
33593436
while let Some(chunk) = stream.next().await {
33603437
let chunk = chunk.map_err(|err| {
33613438
Status::failed_precondition(format!(
3362-
"failed to export local Docker image '{image_ref}': {err}"
3439+
"failed to export local container image '{image_ref}': {err}"
33633440
))
33643441
})?;
33653442
file.write_all(&chunk).await.map_err(|err| {
@@ -3383,7 +3460,7 @@ async fn export_local_image_rootfs_to_path(
33833460
(Ok(()), Ok(())) => Ok(()),
33843461
(Err(err), _) => Err(err),
33853462
(Ok(()), Err(err)) => Err(Status::internal(format!(
3386-
"failed to remove temporary export container for local Docker image '{image_ref}': {err}"
3463+
"failed to remove temporary export container for local container image '{image_ref}': {err}"
33873464
))),
33883465
}
33893466
}
@@ -4801,8 +4878,11 @@ fn stage_guest_image_payload(
48014878
.map_err(|err| format!("write guest image source: {err}"))?;
48024879
copy_dir_recursive(layout_dir, &image_dir.join(GUEST_IMAGE_OCI_LAYOUT_DIR))?;
48034880
}
4804-
GuestImagePayloadSource::LocalDocker { rootfs_archive } => {
4805-
fs::write(image_dir.join("source"), b"local-docker")
4881+
GuestImagePayloadSource::LocalContainer {
4882+
engine,
4883+
rootfs_archive,
4884+
} => {
4885+
fs::write(image_dir.join("source"), engine.image_source())
48064886
.map_err(|err| format!("write guest image source: {err}"))?;
48074887
let dest = image_dir.join(IMAGE_EXPORT_ROOTFS_ARCHIVE);
48084888
fs::copy(rootfs_archive, &dest).map_err(|err| {
@@ -4856,9 +4936,11 @@ fn prepared_image_disk_size_bytes(
48564936
) -> Result<u64, String> {
48574937
let payload_size = match &payload.source {
48584938
GuestImagePayloadSource::RegistryOciLayout { layout_dir } => dir_size_bytes(layout_dir)?,
4859-
GuestImagePayloadSource::LocalDocker { rootfs_archive } => fs::metadata(rootfs_archive)
4860-
.map_err(|err| format!("stat {}: {err}", rootfs_archive.display()))?
4861-
.len(),
4939+
GuestImagePayloadSource::LocalContainer { rootfs_archive, .. } => {
4940+
fs::metadata(rootfs_archive)
4941+
.map_err(|err| format!("stat {}: {err}", rootfs_archive.display()))?
4942+
.len()
4943+
}
48624944
};
48634945
let requested = payload_size
48644946
.saturating_mul(3)
@@ -6139,6 +6221,12 @@ mod tests {
61396221
));
61406222
}
61416223

6224+
#[test]
6225+
fn local_container_engine_preserves_image_source_identity() {
6226+
assert_eq!(LocalContainerEngine::Docker.image_source(), "local_docker");
6227+
assert_eq!(LocalContainerEngine::Podman.image_source(), "local_podman");
6228+
}
6229+
61426230
#[test]
61436231
fn local_image_platform_mismatch_checks_guest_platform() {
61446232
assert!(

0 commit comments

Comments
 (0)