-
Notifications
You must be signed in to change notification settings - Fork 30
feat(recipe-v2): Add recipe upgrade command #803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gmpinder
wants to merge
1
commit into
main
Choose a base branch
from
recipe-upgrade
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,50 @@ | ||
| use std::{borrow::Cow, collections::HashMap}; | ||
| use std::{borrow::Cow, collections::HashMap, ops::Deref}; | ||
|
|
||
| use blue_build_utils::{ | ||
| constants::BLUE_BUILD_DEFAULT_IMAGE, container::Tag, env_str::EnvString, platform::Platform, | ||
| }; | ||
| use bon::Builder; | ||
| use miette::{Context, IntoDiagnostic}; | ||
| use oci_client::Reference; | ||
| use serde::{Deserialize, Serialize}; | ||
| use structstruck::strike; | ||
|
|
||
| use crate::{Module, RecipeGetters, RecipeSetters, Stage}; | ||
| use crate::{Module, RecipeGetters, RecipeSetters, RecipeV1, Stage}; | ||
|
|
||
| use super::{MaybeVersion, ModuleExt, StagesExt}; | ||
|
|
||
| #[derive(Debug, Clone, Serialize)] | ||
| pub struct RecipeV2BaseImageStr(EnvString); | ||
|
|
||
| impl From<Reference> for RecipeV2BaseImageStr { | ||
| fn from(value: Reference) -> Self { | ||
| Self(EnvString::from(value.to_string())) | ||
| } | ||
| } | ||
|
|
||
| impl<'de> Deserialize<'de> for RecipeV2BaseImageStr { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: serde::Deserializer<'de>, | ||
| { | ||
| let image = EnvString::deserialize(deserializer)?; | ||
|
|
||
| if let Err(e) = image.parse::<Reference>() { | ||
| return Err(serde::de::Error::custom(e)); | ||
| } | ||
|
|
||
| Ok(Self(image)) | ||
| } | ||
| } | ||
|
|
||
| impl Deref for RecipeV2BaseImageStr { | ||
| type Target = str; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| strike! { | ||
| /// The build recipe. | ||
| /// | ||
|
|
@@ -35,7 +68,7 @@ strike! { | |
| #![serde(untagged)] | ||
|
|
||
| /// String representation of an image ref. | ||
| Str(Reference), | ||
| Str(RecipeV2BaseImageStr), | ||
|
|
||
| /// Object representation of an image ref. | ||
| Obj { | ||
|
|
@@ -69,6 +102,7 @@ strike! { | |
| /// This will validate the image before building with it. | ||
| /// | ||
| /// URLs are supported. Paths are relative to the root of the project. | ||
| #[serde(default, skip_serializing_if = "Option::is_none")] | ||
| pub public_key: Option<EnvString>, | ||
| }, | ||
|
|
||
|
|
@@ -78,7 +112,13 @@ strike! { | |
| pub name: EnvString, | ||
|
|
||
| /// The image description. Published to GHCR in the image metadata. | ||
| pub description: EnvString, | ||
| #[serde(default, skip_serializing_if = "Option::is_none")] | ||
| pub description: Option<EnvString>, | ||
|
|
||
| /// Allows setting custom tags on the recipe’s final image. | ||
| /// Adding tags to this property will override the `latest` and timestamp tags. | ||
| #[serde(default, skip_serializing_if = "Vec::is_empty")] | ||
| pub tags: Vec<Tag>, | ||
|
|
||
| /// A collection of custom labels that will be applied to the image. | ||
| /// | ||
|
|
@@ -90,11 +130,6 @@ strike! { | |
| /// Specifications for the image that modifies how it is built and published. | ||
| #[serde(default, skip_serializing_if = "Option::is_none")] | ||
| pub spec: Option<pub struct { | ||
| /// Allows setting custom tags on the recipe’s final image. | ||
| /// Adding tags to this property will override the `latest` and timestamp tags. | ||
| #[serde(default, skip_serializing_if = "Vec::is_empty")] | ||
| pub tags: Vec<Tag>, | ||
|
|
||
| /// Specify a list of the platforms to build for your image. | ||
| /// The resulting images will be added to a manifest list that | ||
| /// allows your host’s container runtime to pull the correct | ||
|
|
@@ -159,13 +194,63 @@ strike! { | |
| } | ||
| } | ||
|
|
||
| impl From<RecipeV1> for RecipeV2 { | ||
| fn from(value: RecipeV1) -> Self { | ||
| Self { | ||
| base: RecipeV2Base { | ||
| image: RecipeV2BaseImage::Str(RecipeV2BaseImageStr(EnvString::from(format!( | ||
| "{}:{}", | ||
| value.base_image.unexpanded(), | ||
| value.image_version.unexpanded(), | ||
| )))), | ||
| public_key: None, | ||
| }, | ||
| metadata: RecipeV2Metadata { | ||
| name: value.name, | ||
| description: Some(value.description), | ||
| tags: value.alt_tags.unwrap_or_default(), | ||
| labels: value.labels, | ||
| }, | ||
| spec: { | ||
| let has_versions = value.blue_build_tag.is_some() | ||
| || value.cosign_version.is_some() | ||
| || value.nushell_version.is_some(); | ||
| let tool_versions = has_versions.then_some(RecipeV2SpecToolVersions { | ||
| bluebuild: value.blue_build_tag, | ||
| nushell: match value.nushell_version { | ||
| None | Some(MaybeVersion::None) => None, | ||
| Some(MaybeVersion::VersionOrBranch(tag)) => Some(tag), | ||
| }, | ||
| cosign: match value.cosign_version { | ||
| None | Some(MaybeVersion::None) => None, | ||
| Some(MaybeVersion::VersionOrBranch(tag)) => Some(tag), | ||
| }, | ||
| }); | ||
| match (value.platforms, has_versions) { | ||
| (None, false) => None, | ||
| (Some(platforms), false) => Some(RecipeV2Spec { | ||
| platforms, | ||
| tool_versions: None, | ||
| }), | ||
| (Some(platforms), true) => Some(RecipeV2Spec { | ||
| platforms, | ||
| tool_versions, | ||
| }), | ||
| (None, true) => Some(RecipeV2Spec { | ||
| platforms: Vec::default(), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| tool_versions, | ||
| }), | ||
| } | ||
| }, | ||
| stages_ext: value.stages_ext, | ||
| modules_ext: value.modules_ext, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Default for RecipeV2BaseImage { | ||
| fn default() -> Self { | ||
| Self::Str( | ||
| BLUE_BUILD_DEFAULT_IMAGE | ||
| .try_into() | ||
| .expect("Should be a valid image ref"), | ||
| ) | ||
| Self::Str(RecipeV2BaseImageStr(BLUE_BUILD_DEFAULT_IMAGE.into())) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -182,22 +267,21 @@ impl RecipeGetters for RecipeV2 { | |
| &self.metadata.name | ||
| } | ||
|
|
||
| fn get_description(&self) -> &str { | ||
| &self.metadata.description | ||
| fn get_description(&self) -> Option<&str> { | ||
| self.metadata.description.as_deref() | ||
| } | ||
|
|
||
| fn get_labels(&self) -> HashMap<&String, &String> { | ||
| fn get_labels(&self) -> HashMap<&str, &str> { | ||
| self.metadata | ||
| .labels | ||
| .iter() | ||
| .flatten() | ||
| .map(|(key, value)| (key, &**value)) | ||
| .map(|(key, value)| (&**key, &**value)) | ||
| .collect() | ||
| } | ||
|
|
||
| fn get_alt_tags(&self) -> Option<&[Tag]> { | ||
| let spec = self.spec.as_ref()?; | ||
| match &spec.tags[..] { | ||
| match &self.metadata.tags[..] { | ||
| [] => None, | ||
| tags => Some(tags), | ||
| } | ||
|
|
@@ -209,11 +293,13 @@ impl RecipeGetters for RecipeV2 { | |
|
|
||
| fn get_base_image(&self) -> Cow<'_, str> { | ||
| match &self.base.image { | ||
| RecipeV2BaseImage::Str(image) => Cow::Owned(format!( | ||
| "{}/{}", | ||
| image.resolve_registry(), | ||
| image.repository() | ||
| )), | ||
| RecipeV2BaseImage::Str(image) => Cow::Borrowed( | ||
| image | ||
| .split_once(':') // Split at tag start | ||
| .or_else(|| image.split_once('@')) // or digest start | ||
| .unwrap_or((image, "")) // or the image without a tag | ||
| .0, | ||
| ), | ||
| RecipeV2BaseImage::Obj { | ||
| registry, | ||
| repository, | ||
|
|
@@ -256,7 +342,10 @@ impl RecipeGetters for RecipeV2 { | |
|
|
||
| fn base_image_ref(&self) -> miette::Result<Reference> { | ||
| Ok(match &self.base.image { | ||
| RecipeV2BaseImage::Str(image) => image.clone(), | ||
| RecipeV2BaseImage::Str(RecipeV2BaseImageStr(image)) => image | ||
| .parse() | ||
| .into_diagnostic() | ||
| .wrap_err_with(|| format!("Failed to parse base image ref {image}"))?, | ||
| RecipeV2BaseImage::Obj { | ||
| registry, | ||
| repository, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to use
thenand a closure here, sincethen_someunconditionally evaluates the argument.