From 27cba612d6e4acea872d143b7e9d4a28798933b1 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 1 Aug 2024 11:29:42 -0400 Subject: [PATCH] lib: Add an API to write layers with timestamps I didn't have an immediate use case, I was just reading the code for unrelated reasons and noticed this. But I'm sure we'd want this for reproducible builds. Signed-off-by: Colin Walters --- src/lib.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 06271da..d9e0b6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -275,6 +275,22 @@ impl OciDir { layer: Layer, annotations: Option>>, description: &str, + ) { + let created = chrono::offset::Utc::now(); + self.push_layer_full(manifest, config, layer, annotations, description, created) + } + + /// Add a layer to the top of the image stack with optional annotations and desired timestamp. + /// + /// This is otherwise equivalent to [`Self::push_layer_annotated`]. + pub fn push_layer_full( + &self, + manifest: &mut oci_image::ImageManifest, + config: &mut oci_image::ImageConfiguration, + layer: Layer, + annotations: Option>>, + description: &str, + created: chrono::DateTime, ) { let mut builder = layer.descriptor().media_type(MediaType::ImageLayerGzip); if let Some(annotations) = annotations { @@ -287,9 +303,8 @@ impl OciDir { .diff_ids_mut() .push(format!("{SHA256_NAME}:{}", layer.uncompressed_sha256)); config.set_rootfs(rootfs); - let now = chrono::offset::Utc::now(); let h = oci_image::HistoryBuilder::default() - .created(now.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) + .created(created.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) .created_by(description.to_string()) .build() .unwrap();