Skip to content

Commit

Permalink
Merge pull request #284 from microsoft/mitchzhu/solar
Browse files Browse the repository at this point in the history
Extend layer decompression based on mediatype label from containerd
  • Loading branch information
miz060 authored Jan 7, 2025
2 parents 701efad + fe5e1ce commit f310713
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/tardev-snapshotter/src/snapshotter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,40 +856,53 @@ impl TarDevSnapshotter {
"missing target layer digest label",
));
};

// Determine image layer media type
let Some(media_type) = labels.get(TARGET_LAYER_MEDIA_TYPE_LABEL) else {
error!("missing target layer media type label");
return Err(Status::invalid_argument(
"missing target layer media type label",
));
};

trace!("layer digest {} media_type: {:?}", digest_str, media_type);

// TODO use the media_type:
// gzipped: "application/vnd.docker.image.rootfs.diff.tar.gzip", "application/vnd.oci.image.layer.v1.tar+gzip";
// uncompressed: "application/vnd.oci.image.layer.v1.tar", "application/vnd.docker.image.rootfs.diff.tar"
let layer_type = match media_type.as_str() {
"application/vnd.docker.image.rootfs.diff.tar.gzip"
| "application/vnd.oci.image.layer.v1.tar+gzip" => "tar.gz",
"application/vnd.oci.image.layer.v1.tar"
| "application/vnd.docker.image.rootfs.diff.tar" => "tar",
_ => {
error!("unsupported media type: {}", media_type);
return Err(Status::invalid_argument(format!(
"unsupported media type: {}",
media_type
)));
}
};

// Fetch the layer image
let name = dir.path().join(name_to_hash(&key));
let mut gzname = name.clone();
gzname.set_extension("gz");
trace!("Fetching layer image to {:?}", &gzname);
self.get_layer_image(&gzname, digest_str).await?;

// TODO: Decompress in stream instead of reopening.
// Decompress data.
trace!("Decompressing {:?} to {:?}", &gzname, &name);
trace!("Fetching {} layer image to {:?}", layer_type, name);
self.get_layer_image(&name, digest_str).await?;
let mut target_name = name.clone();
target_name.set_extension(layer_type);

// Decompress and process the layer
trace!("Decompressing {:?} to {:?}", &target_name, &name);
let generated_root_hash = tokio::task::spawn_blocking(move || -> Result<_> {
let compressed = fs::File::open(&gzname)?;
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(&name)?;
let mut gz_decoder = flate2::read::GzDecoder::new(compressed);
std::io::copy(&mut gz_decoder, &mut file)
if layer_type == "tar.gz" {
let compressed = fs::File::open(&target_name)?;
let mut gz_decoder = flate2::read::GzDecoder::new(compressed);
std::io::copy(&mut gz_decoder, &mut file)
.context("failed to copy payload from gz decoder")?;

}

trace!("Appending index to {:?}", &name);
file.rewind().context("failed to rewind the file handle")?;
tarindex::append_index(&mut file).context("failed to append tar index")?;
Expand Down

0 comments on commit f310713

Please sign in to comment.