mirror of
https://github.com/kata-containers/kata-containers.git
synced 2026-07-07 10:45:21 +00:00
runtime-rs: add fallback for image reference for single container
This commit adds a fallback mechanism in `get_image_reference()` to handle SingleContainer scenarios where no container type annotation is present. The issue occurs when standalone container runtimes (like nerdctl) create containers without the Kubernetes container type annotation. In such cases, `container_type_with_id()` returns `SingleContainer` as the default, but `get_image_reference()` previously only checked for image names when a container type annotation was explicitly set to either PodSandbox or PodContainer. This caused image reference lookups to fail for standalone containers, even though the image name annotations (io.kubernetes.cri.image-name or io.kubernetes.cri-o.ImageName) were present in the spec. The fallback logic now checks for image name annotations directly when no container type annotation is found, supporting both: - io.kubernetes.cri.image-name (standard CRI) - io.kubernetes.cri-o.ImageName (CRI-O specific) This maintains backward compatibility with Kubernetes pod scenarios while enabling support for standalone container runtimes that don't provide container type annotations. Test cases have been added to verify the fallback behavior for both CRI and CRI-O image name annotations in SingleContainer scenarios. Signed-off-by: Hyounggyu Choi <Hyounggyu.Choi@ibm.com>
This commit is contained in:
@@ -47,6 +47,10 @@ fn kata_guest_root_shared_fs() -> String {
|
||||
/// If the container is a PodSandbox, it returns "pause".
|
||||
/// Otherwise, it attempts to find the image name using the appropriate Kubernetes
|
||||
/// annotation key.
|
||||
///
|
||||
/// If no container type annotation is found (SingleContainer case), it falls back to
|
||||
/// checking for image name annotations directly. This supports standalone container
|
||||
/// runtimes like nerdctl that may only provide the image name annotation.
|
||||
pub fn get_image_reference(spec_annotations: &HashMap<String, String>) -> Result<&str> {
|
||||
info!(
|
||||
sl!(),
|
||||
@@ -74,6 +78,26 @@ pub fn get_image_reference(spec_annotations: &HashMap<String, String>) -> Result
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for SingleContainer case: if no container type annotation is found,
|
||||
// try to get image name directly. This supports standalone container runtimes
|
||||
// (e.g., nerdctl) that may only provide the image name annotation without the
|
||||
// container type annotation.
|
||||
if let Some(image_name) = spec_annotations.get(KUBERNETES_CRI_IMAGE_NAME) {
|
||||
info!(
|
||||
sl!(),
|
||||
"Found image name without container type annotation (SingleContainer): {}", image_name
|
||||
);
|
||||
return Ok(image_name.as_str());
|
||||
}
|
||||
|
||||
if let Some(image_name) = spec_annotations.get(KUBERNETES_CRIO_IMAGE_NAME) {
|
||||
info!(
|
||||
sl!(),
|
||||
"Found CRI-O image name without container type annotation (SingleContainer): {}", image_name
|
||||
);
|
||||
return Ok(image_name.as_str());
|
||||
}
|
||||
|
||||
Err(anyhow!("no target image reference found"))
|
||||
}
|
||||
|
||||
@@ -256,6 +280,25 @@ mod tests {
|
||||
let image_ref_result_pod_sandbox = get_image_reference(&annotations_pod_sandbox);
|
||||
assert!(image_ref_result_pod_sandbox.is_ok());
|
||||
assert_eq!(image_ref_result_pod_sandbox.unwrap(), "pause");
|
||||
// Test SingleContainer fallback (no container type annotation)
|
||||
let mut annotations_single = HashMap::new();
|
||||
annotations_single.insert(
|
||||
"io.kubernetes.cri.image-name".to_string(),
|
||||
"example-image-single".to_string(),
|
||||
);
|
||||
let image_ref_result_single = get_image_reference(&annotations_single);
|
||||
assert!(image_ref_result_single.is_ok());
|
||||
assert_eq!(image_ref_result_single.unwrap(), "example-image-single");
|
||||
|
||||
// Test SingleContainer fallback with CRI-O annotation
|
||||
let mut annotations_single_crio = HashMap::new();
|
||||
annotations_single_crio.insert(
|
||||
"io.kubernetes.cri-o.ImageName".to_string(),
|
||||
"example-image-single-crio".to_string(),
|
||||
);
|
||||
let image_ref_result_single_crio = get_image_reference(&annotations_single_crio);
|
||||
assert!(image_ref_result_single_crio.is_ok());
|
||||
assert_eq!(image_ref_result_single_crio.unwrap(), "example-image-single-crio");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user