Skip to content

Commit 441afd9

Browse files
committed
Add hash of layers to make Unique ImageID for existing tooling
Signed-off-by: James Sturtevant <jstur@microsoft.com>
1 parent 458a37b commit 441afd9

File tree

1 file changed

+27
-0
lines changed
  • crates/oci-tar-builder/src

1 file changed

+27
-0
lines changed

crates/oci-tar-builder/src/bin.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::fs::File;
23
use std::path::PathBuf;
34
use std::{env, fs};
@@ -6,6 +7,7 @@ use anyhow::Context;
67
use clap::Parser;
78
use oci_spec::image::{self as spec, Arch};
89
use oci_tar_builder::{Builder, WASM_LAYER_MEDIA_TYPE};
10+
use sha256::{digest, try_digest};
911

1012
pub fn main() {
1113
let args = Args::parse();
@@ -21,9 +23,15 @@ pub fn main() {
2123
let entry_point = args.name.clone() + ".wasm";
2224

2325
let mut builder = Builder::default();
26+
let mut layer_digests = Vec::new();
2427
for module_path in args.module.iter() {
2528
let module_path = PathBuf::from(module_path);
2629
builder.add_layer_with_media_type(&module_path, WASM_LAYER_MEDIA_TYPE.to_string());
30+
layer_digests.push(
31+
try_digest(&module_path)
32+
.context("failed to calculate digest for module")
33+
.unwrap(),
34+
);
2735
}
2836

2937
for layer_config in args.layer.iter() {
@@ -33,6 +41,11 @@ pub fn main() {
3341
let layer_type = layer_options.first().unwrap();
3442
let layer_path = PathBuf::from(layer_options.last().unwrap());
3543
builder.add_layer_with_media_type(&layer_path, layer_type.to_string());
44+
layer_digests.push(
45+
try_digest(&layer_path)
46+
.context("failed to calculate digest for module")
47+
.unwrap(),
48+
);
3649
}
3750

3851
if let Some(components_path) = args.components.as_deref() {
@@ -44,6 +57,11 @@ pub fn main() {
4457
match ext {
4558
"wasm" => {
4659
builder.add_layer_with_media_type(&path, WASM_LAYER_MEDIA_TYPE.to_string());
60+
layer_digests.push(
61+
try_digest(&path)
62+
.context("failed to calculate digest for module")
63+
.unwrap(),
64+
);
4765
}
4866
_ => println!(
4967
"Skipping Unknown file type: {:?} with extension {:?}",
@@ -54,8 +72,17 @@ pub fn main() {
5472
}
5573
}
5674

75+
// Need each config to be unique since we don't have layers to make them unique in the rootfs
76+
// https://github.com/opencontainers/image-spec/pull/1173
77+
let unique_id = digest(layer_digests.join(""));
78+
let mut labels: HashMap<String, String> = HashMap::new();
79+
labels.insert(
80+
"containerd.runwasi.layers".to_string(),
81+
unique_id,
82+
);
5783
let config = spec::ConfigBuilder::default()
5884
.entrypoint(vec![entry_point])
85+
.labels(labels)
5986
.build()
6087
.unwrap();
6188

0 commit comments

Comments
 (0)