chore: Use Semver to grab OS version from image

This commit is contained in:
Gerald Pinder 2024-04-24 22:42:44 -04:00
parent 1d05290266
commit 2bf7c99aaf
7 changed files with 14 additions and 15 deletions

View file

@ -89,7 +89,7 @@ impl<'a> Module<'a> {
}
#[must_use]
pub fn generate_akmods_info(&'a self, os_version: &str) -> AkmodsInfo {
pub fn generate_akmods_info(&'a self, os_version: &u64) -> AkmodsInfo {
#[derive(Debug, Copy, Clone)]
enum NvidiaAkmods {
Nvidia(bool),

View file

@ -45,7 +45,7 @@ impl ModuleExt<'_> {
}
#[must_use]
pub fn get_akmods_info_list(&self, os_version: &str) -> Vec<AkmodsInfo> {
pub fn get_akmods_info_list(&self, os_version: &u64) -> Vec<AkmodsInfo> {
trace!("get_akmods_image_list({self:#?}, {os_version})");
let mut seen = HashSet::new();

View file

@ -44,7 +44,7 @@ pub struct Recipe<'a> {
impl<'a> Recipe<'a> {
#[must_use]
pub fn generate_tags(&self, os_version: &str) -> Vec<String> {
pub fn generate_tags(&self, os_version: u64) -> Vec<String> {
trace!("Recipe::generate_tags()");
trace!("Generating image tags for {}", &self.name);

View file

@ -211,7 +211,7 @@ impl BuildCommand {
let recipe = Recipe::parse(&recipe_path)?;
let os_version = Driver::get_os_version(&recipe)?;
let tags = recipe.generate_tags(&os_version);
let tags = recipe.generate_tags(os_version);
let image_name = self.generate_full_image_name(&recipe)?;
if self.push {

View file

@ -95,7 +95,7 @@ static INSPECT_DRIVER: Lazy<Arc<dyn InspectDriver>> = Lazy::new(|| {
static BUILD_ID: Lazy<Uuid> = Lazy::new(Uuid::new_v4);
/// The cached os versions
static OS_VERSION: Lazy<Mutex<HashMap<String, String>>> = Lazy::new(|| Mutex::new(HashMap::new()));
static OS_VERSION: Lazy<Mutex<HashMap<String, u64>>> = Lazy::new(|| Mutex::new(HashMap::new()));
/// Trait for retrieving version of a driver.
pub trait DriverVersion {
@ -309,7 +309,7 @@ impl Driver<'_> {
/// # Errors
/// Will error if the image doesn't have OS version info
/// or we are unable to lock a mutex.
pub fn get_os_version(recipe: &Recipe) -> Result<String> {
pub fn get_os_version(recipe: &Recipe) -> Result<u64> {
trace!("Driver::get_os_version({recipe:#?})");
let image = format!("{}:{}", &recipe.base_image, &recipe.image_version);
@ -339,13 +339,13 @@ impl Driver<'_> {
}
Some(os_version) => {
debug!("Found cached {os_version} for {image}");
os_version.clone()
*os_version
}
};
if let Entry::Vacant(entry) = os_version_lock.entry(image.clone()) {
trace!("Caching version {os_version} for {image}");
entry.insert(os_version.clone());
entry.insert(os_version);
}
drop(os_version_lock);
Ok(os_version)

View file

@ -1,4 +1,5 @@
use blue_build_utils::constants::IMAGE_VERSION_LABEL;
use semver::Version;
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
@ -13,15 +14,14 @@ pub struct ImageMetadata {
}
impl ImageMetadata {
pub fn get_version(&self) -> Option<String> {
#[must_use]
pub fn get_version(&self) -> Option<u64> {
Some(
self.labels
.get(IMAGE_VERSION_LABEL)?
.as_str()
.map(std::string::ToString::to_string)?
.split('.')
.take(1)
.collect(),
.and_then(|v| Version::parse(v).ok())?
.major,
)
}
}

View file

@ -23,8 +23,7 @@ pub struct ContainerFileTemplate<'a> {
#[builder(setter(into))]
build_id: Uuid,
#[builder(setter(into))]
os_version: Cow<'a, str>,
os_version: u64,
#[builder(setter(into))]
registry: Cow<'a, str>,