Skip to content

Commit

Permalink
Allow creating oci-archive and oci-dir without image name (#132)
Browse files Browse the repository at this point in the history
In some cases, one want to create an oci-archive or oci-dir which never
pushed to registry.
  • Loading branch information
termoshtt committed May 10, 2024
1 parent 7806cf3 commit 3df48ac
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
5 changes: 2 additions & 3 deletions ocipkg/src/image/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ use anyhow::{bail, Context, Result};
use oci_spec::image::{Descriptor, DescriptorBuilder, ImageIndex, ImageManifest, MediaType};
use std::path::Path;

/// Handler of [OCI Image Layout] with containing single manifest and its name.
/// Handler of [OCI Image Layout] with containing single manifest
///
/// - [OCI Image Layout] allows empty image name, i.e. no `org.opencontainers.image.ref.name` annotation, but this trait does not allow it.
/// - [OCI Image Layout] allows containing multiple manifests in a single layout,
/// this trait assumes a single manifest in a single layout.
///
/// [OCI Image Layout]: https://github.com/opencontainers/image-spec/blob/v1.1.0/image-layout.md
///
pub trait Image {
/// The name of this image.
/// The name of this image. This fails if the image does not have name.
fn get_name(&mut self) -> Result<ImageName>;

/// Get blob content.
Expand Down
25 changes: 21 additions & 4 deletions ocipkg/src/image/oci_archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@ use std::{

/// Build an [OciArchive]
pub struct OciArchiveBuilder {
image_name: ImageName,
image_name: Option<ImageName>,
path: PathBuf,
ar: tar::Builder<fs::File>,
}

impl OciArchiveBuilder {
pub fn new_unnamed(path: PathBuf) -> Result<Self> {
if path.exists() {
bail!("File already exists: {}", path.display());
}
let f = fs::File::create(&path)?;
let ar = tar::Builder::new(f);
Ok(Self {
ar,
path,
image_name: None,
})
}

pub fn new(path: PathBuf, image_name: ImageName) -> Result<Self> {
if path.exists() {
bail!("File already exists: {}", path.display());
Expand All @@ -29,7 +42,7 @@ impl OciArchiveBuilder {
Ok(Self {
ar,
path,
image_name,
image_name: Some(image_name),
})
}
}
Expand All @@ -51,8 +64,12 @@ impl ImageBuilder for OciArchiveBuilder {
.media_type(MediaType::ImageManifest)
.size(size)
.digest(digest.to_string())
.annotations(hashmap! {
"org.opencontainers.image.ref.name".to_string() => self.image_name.to_string()
.annotations(if let Some(name) = &self.image_name {
hashmap! {
"org.opencontainers.image.ref.name".to_string() => name.to_string()
}
} else {
hashmap! {}
})
.build()?;
let index = ImageIndexBuilder::default()
Expand Down
24 changes: 20 additions & 4 deletions ocipkg/src/image/oci_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::get_name_from_index;

/// Build an [OciDir]
pub struct OciDirBuilder {
image_name: ImageName,
image_name: Option<ImageName>,
oci_dir_root: PathBuf,
is_finished: bool,
}
Expand All @@ -37,13 +37,25 @@ impl Drop for OciDirBuilder {
}

impl OciDirBuilder {
pub fn new_unnamed(oci_dir_root: PathBuf) -> Result<Self> {
if oci_dir_root.exists() {
bail!("oci-dir {} already exists", oci_dir_root.display());
}
fs::create_dir_all(&oci_dir_root)?;
Ok(Self {
image_name: None,
oci_dir_root,
is_finished: false,
})
}

pub fn new(oci_dir_root: PathBuf, image_name: ImageName) -> Result<Self> {
if oci_dir_root.exists() {
bail!("oci-dir {} already exists", oci_dir_root.display());
}
fs::create_dir_all(&oci_dir_root)?;
Ok(Self {
image_name,
image_name: Some(image_name),
oci_dir_root,
is_finished: false,
})
Expand All @@ -68,8 +80,12 @@ impl ImageBuilder for OciDirBuilder {
.media_type(MediaType::ImageManifest)
.size(size)
.digest(digest.to_string())
.annotations(hashmap! {
"org.opencontainers.image.ref.name".to_string() => self.image_name.to_string(),
.annotations(if let Some(name) = &self.image_name {
hashmap! {
"org.opencontainers.image.ref.name".to_string() => name.to_string()
}
} else {
hashmap! {}
})
.build()?;
let index = ImageIndexBuilder::default()
Expand Down

0 comments on commit 3df48ac

Please sign in to comment.