-
-
Notifications
You must be signed in to change notification settings - Fork 15
feat: Add stackable-versioned
and k8s-version
crates for CRD versioning
#764
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
Merged
Merged
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
31df5b1
chore: Add skeleton code
Techassi c93cdee
feat: Add basic container and attribute validation
Techassi 109e38f
Start field attribute validation
Techassi 40df3af
Move code generation into structs
Techassi cff3fe9
Adjust field actions which require generation in multiple versions
Techassi aaaedaa
Add basic support for added and always present fields
Techassi be20ad8
feat(k8s-version): Add Kubernetes version crate
Techassi f46d370
feat(k8s-version): Add support for FromMeta
Techassi 50f8076
chore(k8s-version): Add changelog
Techassi b09ad5f
test(k8s-version): Add more unit tests
Techassi 015ce8d
docs(k8s-version): Add README
Techassi 9ba774f
chore: Switch work machine
Techassi 03c32e7
Add basic support for renamed fields
Techassi 5448375
Enfore version sorting, add option to opt out
Techassi cc2190a
Add basic support for deprecated fields
Techassi 2eb23d7
Remove unused dependency
Techassi 2586e77
Fix k8s-version unit tests
Techassi 9f6c682
chore: Merge branch 'main' into feat/crd-versioning
Techassi 2506fc4
Add basic support for multiple field actions on one field
Techassi 957dd91
Restructure field validation code
Techassi cba9153
Generate chain of statuses
Techassi 2269675
Merge branch 'main' into feat/crd-versioning
Techassi f3515e2
Add Ord impl for Level and Version
Techassi e324d30
Add Part(Ord) unit tests for Level and Version
Techassi 3fa20f4
Add FromMeta unit test for Level
Techassi bd935d6
Generate code for multiple field actions
Techassi a9eeafd
Improve field attribute validation
Techassi 0916a93
Improve error handling, add doc comments
Techassi 727fbdf
k8s-version: Add validated Group
Techassi 92fafcf
k8s-version: Add library doc comments
Techassi 1952db8
k8s-version: Add doc comments for error enums
Techassi 6148a09
Add more (doc) comments
Techassi 3252c55
Add changelog for stackable-versioned
Techassi 2393ae0
Apply suggestions
Techassi 9e6fdef
Clean-up suggestions
Techassi 8a38f47
Rename API_VERSION_REGEX to API_GROUP_REGEX
Techassi 1aa7fcf
Use expect instead of unwrap for regular expressions
Techassi 36279ff
Include duplicate version name in error message
Techassi cbf7c8c
Bump json-patch to 1.4.0 because 1.3.0 was yanked
Techassi 0fdd492
Adjust level format
Techassi 005c203
Improve derive macro test
Techassi 4944a7f
Add how to use the ApiVersion::new() function
Techassi 3a2e052
Add doc comment for FieldAttributes::validate_versions
Techassi 232f883
Fix doc comment
Techassi ba3fc19
Fix doc tests
Techassi 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
## [Unreleased] | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "k8s-version" | ||
version = "0.1.0" | ||
authors.workspace = true | ||
license.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
|
||
[features] | ||
darling = ["dep:darling"] | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[dependencies] | ||
darling = { workspace = true, optional = true } | ||
lazy_static.workspace = true | ||
regex.workspace = true | ||
snafu.workspace = true | ||
|
||
[dev-dependencies] | ||
rstest.workspace = true | ||
rstest_reuse.workspace = true | ||
quote.workspace = true | ||
proc-macro2.workspace = true | ||
syn.workspace = true |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# k8s-version | ||
|
||
A small helper crate to parse and validate Kubernetes resource API versions. | ||
|
||
```rust | ||
use k8s_version::ApiVersion; | ||
|
||
let api_version = ApiVersion::from_str("extensions/v1beta1")?; | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,158 @@ | ||
use std::{cmp::Ordering, fmt::Display, str::FromStr}; | ||
|
||
use snafu::{ResultExt, Snafu}; | ||
|
||
#[cfg(feature = "darling")] | ||
use darling::FromMeta; | ||
|
||
use crate::{Group, ParseGroupError, ParseVersionError, Version}; | ||
|
||
/// Error variants which can be encountered when creating a new [`ApiVersion`] | ||
/// from unparsed input. | ||
#[derive(Debug, PartialEq, Snafu)] | ||
pub enum ParseApiVersionError { | ||
#[snafu(display("failed to parse version"))] | ||
ParseVersion { source: ParseVersionError }, | ||
|
||
#[snafu(display("failed to parse group"))] | ||
ParseGroup { source: ParseGroupError }, | ||
} | ||
|
||
/// A Kubernetes API version, following the `(<GROUP>/)<VERSION>` format. | ||
/// | ||
/// The `<VERSION>` string must follow the DNS label format defined in the | ||
/// [Kubernetes design proposals archive][1]. The `<GROUP>` string must be lower | ||
/// case and must be a valid DNS subdomain. | ||
/// | ||
/// ### See | ||
/// | ||
/// - <https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#api-conventions> | ||
/// - <https://kubernetes.io/docs/reference/using-api/#api-versioning> | ||
/// - <https://kubernetes.io/docs/reference/using-api/#api-groups> | ||
/// | ||
/// [1]: https://github.com/kubernetes/design-proposals-archive/blob/main/architecture/identifiers.md#definitions | ||
#[derive(Clone, Debug, Hash, PartialEq, Eq)] | ||
pub struct ApiVersion { | ||
pub group: Option<Group>, | ||
pub version: Version, | ||
} | ||
|
||
impl FromStr for ApiVersion { | ||
type Err = ParseApiVersionError; | ||
|
||
fn from_str(input: &str) -> Result<Self, Self::Err> { | ||
let (group, version) = if let Some((group, version)) = input.split_once('/') { | ||
let group = Group::from_str(group).context(ParseGroupSnafu)?; | ||
|
||
( | ||
Some(group), | ||
Version::from_str(version).context(ParseVersionSnafu)?, | ||
) | ||
} else { | ||
(None, Version::from_str(input).context(ParseVersionSnafu)?) | ||
}; | ||
|
||
Ok(Self { group, version }) | ||
} | ||
} | ||
|
||
impl PartialOrd for ApiVersion { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
match self.group.partial_cmp(&other.group) { | ||
Some(Ordering::Equal) => {} | ||
_ => return None, | ||
} | ||
self.version.partial_cmp(&other.version) | ||
} | ||
} | ||
|
||
impl Display for ApiVersion { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match &self.group { | ||
Some(group) => write!(f, "{group}/{version}", version = self.version), | ||
None => write!(f, "{version}", version = self.version), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "darling")] | ||
impl FromMeta for ApiVersion { | ||
fn from_string(value: &str) -> darling::Result<Self> { | ||
Self::from_str(value).map_err(darling::Error::custom) | ||
} | ||
} | ||
|
||
impl ApiVersion { | ||
/// Create a new Kubernetes API version. | ||
pub fn new(group: Option<Group>, version: Version) -> Self { | ||
Self { group, version } | ||
} | ||
|
||
/// Try to create a new Kubernetes API version based on the unvalidated | ||
/// `group` string. | ||
pub fn try_new(group: Option<&str>, version: Version) -> Result<Self, ParseApiVersionError> { | ||
let group = group | ||
.map(|g| g.parse()) | ||
.transpose() | ||
.context(ParseGroupSnafu)?; | ||
|
||
Ok(Self { group, version }) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::Level; | ||
|
||
use rstest::rstest; | ||
|
||
#[cfg(feature = "darling")] | ||
use quote::quote; | ||
|
||
#[cfg(feature = "darling")] | ||
fn parse_meta(tokens: proc_macro2::TokenStream) -> ::std::result::Result<syn::Meta, String> { | ||
let attribute: syn::Attribute = syn::parse_quote!(#[#tokens]); | ||
Ok(attribute.meta) | ||
} | ||
|
||
#[rstest] | ||
#[case("extensions/v1beta1", ApiVersion { group: Some("extensions".parse().unwrap()), version: Version { major: 1, level: Some(Level::Beta(1)) } })] | ||
#[case("v1beta1", ApiVersion { group: None, version: Version { major: 1, level: Some(Level::Beta(1)) } })] | ||
#[case("v1", ApiVersion { group: None, version: Version { major: 1, level: None } })] | ||
fn valid_api_version(#[case] input: &str, #[case] expected: ApiVersion) { | ||
let api_version = ApiVersion::from_str(input).expect("valid Kubernetes api version"); | ||
assert_eq!(api_version, expected); | ||
} | ||
|
||
#[rstest] | ||
#[case("extensions/beta1", ParseApiVersionError::ParseVersion { source: ParseVersionError::InvalidFormat })] | ||
#[case("/v1beta1", ParseApiVersionError::ParseGroup { source: ParseGroupError::Empty })] | ||
fn invalid_api_version(#[case] input: &str, #[case] error: ParseApiVersionError) { | ||
let err = ApiVersion::from_str(input).expect_err("invalid Kubernetes api versions"); | ||
assert_eq!(err, error); | ||
} | ||
|
||
#[rstest] | ||
#[case(Version {major: 1, level: Some(Level::Alpha(2))}, Version {major: 1, level: Some(Level::Alpha(1))}, Ordering::Greater)] | ||
#[case(Version {major: 1, level: Some(Level::Alpha(1))}, Version {major: 1, level: Some(Level::Alpha(1))}, Ordering::Equal)] | ||
#[case(Version {major: 1, level: Some(Level::Alpha(1))}, Version {major: 1, level: Some(Level::Alpha(2))}, Ordering::Less)] | ||
#[case(Version {major: 1, level: None}, Version {major: 1, level: Some(Level::Alpha(2))}, Ordering::Greater)] | ||
#[case(Version {major: 1, level: None}, Version {major: 1, level: Some(Level::Beta(2))}, Ordering::Greater)] | ||
#[case(Version {major: 1, level: None}, Version {major: 1, level: None}, Ordering::Equal)] | ||
#[case(Version {major: 1, level: None}, Version {major: 2, level: None}, Ordering::Less)] | ||
fn partial_ord(#[case] input: Version, #[case] other: Version, #[case] expected: Ordering) { | ||
assert_eq!(input.partial_cmp(&other), Some(expected)); | ||
} | ||
|
||
#[cfg(feature = "darling")] | ||
#[rstest] | ||
#[case(quote!(ignore = "extensions/v1beta1"), ApiVersion { group: Some("extensions".parse().unwrap()), version: Version { major: 1, level: Some(Level::Beta(1)) } })] | ||
#[case(quote!(ignore = "v1beta1"), ApiVersion { group: None, version: Version { major: 1, level: Some(Level::Beta(1)) } })] | ||
#[case(quote!(ignore = "v1"), ApiVersion { group: None, version: Version { major: 1, level: None } })] | ||
fn from_meta(#[case] input: proc_macro2::TokenStream, #[case] expected: ApiVersion) { | ||
let meta = parse_meta(input).expect("valid attribute tokens"); | ||
let api_version = ApiVersion::from_meta(&meta).expect("version must parse from attribute"); | ||
assert_eq!(api_version, expected); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::{fmt, ops::Deref, str::FromStr}; | ||
|
||
use lazy_static::lazy_static; | ||
use regex::Regex; | ||
use snafu::{ensure, Snafu}; | ||
|
||
const MAX_GROUP_LENGTH: usize = 253; | ||
|
||
lazy_static! { | ||
static ref API_GROUP_REGEX: Regex = | ||
Regex::new(r"^(?:(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9])\.?)+$") | ||
.expect("failed to compile API group regex"); | ||
} | ||
|
||
/// Error variants which can be encountered when creating a new [`Group`] from | ||
/// unparsed input. | ||
#[derive(Debug, PartialEq, Snafu)] | ||
pub enum ParseGroupError { | ||
#[snafu(display("group must not be empty"))] | ||
Empty, | ||
|
||
#[snafu(display("group must not be longer than 253 characters"))] | ||
TooLong, | ||
|
||
#[snafu(display("group must be a valid DNS subdomain"))] | ||
InvalidFormat, | ||
} | ||
|
||
/// A validated Kubernetes group. | ||
/// | ||
/// The group string must follow these rules: | ||
/// | ||
/// - must be non-empty | ||
/// - must only contain lower case characters | ||
/// - and must be a valid DNS subdomain | ||
/// | ||
/// ### See | ||
/// | ||
/// - <https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#api-conventions> | ||
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd)] | ||
pub struct Group(String); | ||
|
||
impl FromStr for Group { | ||
type Err = ParseGroupError; | ||
|
||
fn from_str(group: &str) -> Result<Self, Self::Err> { | ||
ensure!(!group.is_empty(), EmptySnafu); | ||
ensure!(group.len() <= MAX_GROUP_LENGTH, TooLongSnafu); | ||
ensure!(API_GROUP_REGEX.is_match(group), InvalidFormatSnafu); | ||
|
||
Ok(Self(group.to_string())) | ||
} | ||
} | ||
|
||
impl fmt::Display for Group { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
impl Deref for Group { | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type Target = str; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.