Skip to content

Commit

Permalink
config: Add a version API
Browse files Browse the repository at this point in the history
First, there are still important images that use a plain `version`
key and not the OCI one.  Let's reflect that reality here.

Add a `version` API that knows how to look up either one.

Signed-off-by: Colin Walters <walters@verbum.org>
  • Loading branch information
cgwalters committed Jan 13, 2023
1 parent 530fca1 commit 61b323b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/image/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ use std::{
path::Path,
};

/// In theory, this key is not standard. In practice, it's used by at least the
/// RHEL UBI images for a long time.
pub const LABEL_VERSION: &str = "version";

#[derive(
Builder,
Clone,
Expand Down Expand Up @@ -219,6 +223,20 @@ impl ImageConfiguration {
pub fn to_string_pretty(&self) -> Result<String> {
to_string(&self, true)
}

/// Retrieve the version number associated with this configuration. This will try
/// to use several well-known label keys.
pub fn version(&self) -> Option<&str> {
let labels = self.config().as_ref().and_then(|c| c.labels().as_ref());
if let Some(labels) = labels {
for k in [super::ANNOTATION_VERSION, LABEL_VERSION] {
if let Some(v) = labels.get(k) {
return Some(v.as_str());
}
}
}
None
}
}

/// Implement `ToString` directly since we cannot avoid twice memory allocation
Expand Down

0 comments on commit 61b323b

Please sign in to comment.