Skip to content

Commit

Permalink
container: Add support for copying optionally-present keys
Browse files Browse the repository at this point in the history
This is to aid coreos/coreos-assembler#3214
which is trying to inject the metadata key `fedora-coreos.stream`
into the container image.  However, this value will only be
present in Fedora derivatives, and not RHEL/CentOS.

Add support for copying a key only if present, instead of
erroring if it's missing.
  • Loading branch information
cgwalters committed Nov 21, 2022
1 parent 2414d4b commit ee73b19
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ pub(crate) enum ContainerOpts {
#[clap(name = "copymeta", long)]
copy_meta_keys: Vec<String>,

/// Propagate an optionally-present OSTree commit metadata key to container label
#[clap(name = "copymeta-opt", long)]
copy_meta_opt_keys: Vec<String>,

/// Corresponds to the Dockerfile `CMD` instruction.
#[clap(long)]
cmd: Option<Vec<String>>,
Expand Down Expand Up @@ -537,6 +541,7 @@ async fn container_export(
imgref: &ImageReference,
labels: BTreeMap<String, String>,
copy_meta_keys: Vec<String>,
copy_meta_opt_keys: Vec<String>,
cmd: Option<Vec<String>>,
compression_fast: bool,
) -> Result<()> {
Expand All @@ -546,6 +551,7 @@ async fn container_export(
};
let opts = crate::container::ExportOpts {
copy_meta_keys,
copy_meta_opt_keys,
skip_compression: compression_fast, // TODO rename this in the struct at the next semver break
..Default::default()
};
Expand Down Expand Up @@ -723,6 +729,7 @@ where
imgref,
labels,
copy_meta_keys,
copy_meta_opt_keys,
cmd,
compression_fast,
} => {
Expand All @@ -742,6 +749,7 @@ where
&imgref,
labels?,
copy_meta_keys,
copy_meta_opt_keys,
cmd,
compression_fast,
)
Expand Down
12 changes: 12 additions & 0 deletions lib/src/container/encapsulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub struct Config {
fn commit_meta_to_labels<'a>(
meta: &glib::VariantDict,
keys: impl IntoIterator<Item = &'a str>,
opt_keys: impl IntoIterator<Item = &'a str>,
labels: &mut HashMap<String, String>,
) -> Result<()> {
for k in keys {
Expand All @@ -74,6 +75,14 @@ fn commit_meta_to_labels<'a>(
.ok_or_else(|| anyhow!("Could not find commit metadata key: {}", k))?;
labels.insert(k.to_string(), v);
}
for k in opt_keys {
let v = meta
.lookup::<String>(k)
.context("Expected string for commit metadata value")?;
if let Some(v) = v {
labels.insert(k.to_string(), v);
}
}
// Copy standard metadata keys `ostree.bootable` and `ostree.linux`.
// Bootable is an odd one out in being a boolean.
if let Some(v) = meta.lookup::<bool>(*ostree::METADATA_KEY_BOOTABLE)? {
Expand Down Expand Up @@ -217,6 +226,7 @@ fn build_oci(
commit_meta_to_labels(
&commit_meta,
opts.copy_meta_keys.iter().map(|k| k.as_str()),
opts.copy_meta_opt_keys.iter().map(|k| k.as_str()),
labels,
)?;

Expand Down Expand Up @@ -361,6 +371,8 @@ pub struct ExportOpts {
pub skip_compression: bool,
/// A set of commit metadata keys to copy as image labels.
pub copy_meta_keys: Vec<String>,
/// A set of optionally-present commit metadata keys to copy as image labels.
pub copy_meta_opt_keys: Vec<String>,
/// Maximum number of layers to use
pub max_layers: Option<NonZeroU32>,
/// The container image layout
Expand Down
1 change: 1 addition & 0 deletions lib/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ async fn impl_test_container_import_export(
.transpose()?;
let opts = ExportOpts {
copy_meta_keys: vec!["buildsys.checksum".to_string()],
copy_meta_opt_keys: vec!["nosuchvalue".to_string()],
format: export_format,
..Default::default()
};
Expand Down

0 comments on commit ee73b19

Please sign in to comment.