mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-02 09:24:35 +00:00
agent:image: Support different pause image in the guest for guest pull
Support different pause images in the guest for guest-pull, such as k8s pause image (registry.k8s.io/pause) and openshift pause image (quay.io/bpradipt/okd-pause). Fixes: #9225 -- part III Signed-off-by: ChengyuZhu6 <chengyu.zhu@intel.com>
This commit is contained in:
@@ -41,6 +41,15 @@ fn sl() -> slog::Logger {
|
|||||||
slog_scope::logger().new(o!("subsystem" => "image"))
|
slog_scope::logger().new(o!("subsystem" => "image"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to copy a file if it does not exist at the destination
|
||||||
|
fn copy_if_not_exists(src: &Path, dst: &Path) -> Result<()> {
|
||||||
|
if let Some(dst_dir) = dst.parent() {
|
||||||
|
fs::create_dir_all(dst_dir)?;
|
||||||
|
}
|
||||||
|
fs::copy(src, dst)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ImageService {
|
pub struct ImageService {
|
||||||
image_client: ImageClient,
|
image_client: ImageClient,
|
||||||
images: HashMap<String, String>,
|
images: HashMap<String, String>,
|
||||||
@@ -66,28 +75,47 @@ impl ImageService {
|
|||||||
if !guest_pause_bundle.exists() {
|
if !guest_pause_bundle.exists() {
|
||||||
bail!("Pause image not present in rootfs");
|
bail!("Pause image not present in rootfs");
|
||||||
}
|
}
|
||||||
|
let guest_pause_config = scoped_join(guest_pause_bundle, CONFIG_JSON)?;
|
||||||
info!(sl(), "use guest pause image cid {:?}", cid);
|
info!(sl(), "use guest pause image cid {:?}", cid);
|
||||||
let pause_bundle = Path::new(CONTAINER_BASE).join(cid).join(target_subpath);
|
|
||||||
let pause_rootfs = pause_bundle.join("rootfs");
|
let image_oci = oci::Spec::load(guest_pause_config.to_str().ok_or_else(|| {
|
||||||
|
anyhow!(
|
||||||
|
"Failed to load the guest pause image config from {:?}",
|
||||||
|
guest_pause_config
|
||||||
|
)
|
||||||
|
})?)
|
||||||
|
.context("load image config file")?;
|
||||||
|
|
||||||
|
let image_oci_process = image_oci.process.ok_or_else(|| {
|
||||||
|
anyhow!("The guest pause image config does not contain a process specification. Please check the pause image.")
|
||||||
|
})?;
|
||||||
|
info!(
|
||||||
|
sl(),
|
||||||
|
"pause image oci process {:?}",
|
||||||
|
image_oci_process.clone()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Ensure that the args vector is not empty before accessing its elements.
|
||||||
|
let args = image_oci_process.args;
|
||||||
|
// Check the number of arguments.
|
||||||
|
if args.is_empty() {
|
||||||
|
bail!("The number of args should be greater than or equal to one! Please check the pause image.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let container_bundle = scoped_join(CONTAINER_BASE, cid)?;
|
||||||
|
fs::create_dir_all(&container_bundle)?;
|
||||||
|
let pause_bundle = scoped_join(&container_bundle, target_subpath)?;
|
||||||
|
fs::create_dir_all(&pause_bundle)?;
|
||||||
|
let pause_rootfs = scoped_join(&pause_bundle, "rootfs")?;
|
||||||
fs::create_dir_all(&pause_rootfs)?;
|
fs::create_dir_all(&pause_rootfs)?;
|
||||||
|
info!(sl(), "pause_rootfs {:?}", pause_rootfs);
|
||||||
|
|
||||||
let copy_if_not_exists = |src: &Path, dst: &Path| -> Result<()> {
|
copy_if_not_exists(&guest_pause_config, &pause_bundle.join(CONFIG_JSON))?;
|
||||||
if !dst.exists() {
|
let arg_path = Path::new(&args[0]).strip_prefix("/")?;
|
||||||
info!(sl(), "copying file {src:?} to {dst:?}");
|
|
||||||
fs::copy(src, dst)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
};
|
|
||||||
copy_if_not_exists(
|
copy_if_not_exists(
|
||||||
&guest_pause_bundle.join(CONFIG_JSON),
|
&guest_pause_bundle.join("rootfs").join(arg_path),
|
||||||
&pause_bundle.join(CONFIG_JSON),
|
&pause_rootfs.join(arg_path),
|
||||||
)?;
|
)?;
|
||||||
copy_if_not_exists(
|
|
||||||
&guest_pause_bundle.join("rootfs/pause"),
|
|
||||||
&pause_rootfs.join("pause"),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(pause_rootfs.display().to_string())
|
Ok(pause_rootfs.display().to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user