Repository navigation
feat: Platform Access utilities #1290
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
Draft
labrenbe
wants to merge
9
commits into
main
Choose a base branch
from
feat/platform-access
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.
+549
−2
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e8a291d
add agent label utility
labrenbe 6ca45a0
add TlsClientCredential enum as platform access utility
labrenbe 7cb677e
add changelog entry
labrenbe 7a0d956
fix cargo deny by upgrading yoke-derive
labrenbe f3d2da7
use hard-coded volume and mount names
labrenbe 3b91d73
add support for anonymous platform access
labrenbe e364acd
make crd fragments versioned
labrenbe 7687938
add ManagementPolicy CRD fragment
labrenbe 754e444
add Ready & Synced conditions for resources reconciled by agents
labrenbe 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
144 changes: 144 additions & 0 deletions
144
crates/stackable-operator/src/commons/platform_access/condition.rs
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,144 @@ | ||
| use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, Time}; | ||
| use strum::Display; | ||
|
|
||
| /// Condition types of resources an agent manages inside a product. | ||
| #[derive(Clone, Copy, Debug, Display, Eq, PartialEq)] | ||
| pub enum ManagedConditionType { | ||
| /// The resource exists in the product and is usable. | ||
| Ready, | ||
|
|
||
| /// The last reconciliation applied the spec to the resource in the product. | ||
| Synced, | ||
| } | ||
|
|
||
| /// The desired state of one condition, without the bookkeeping fields. | ||
| #[derive(Clone, Debug, Eq, PartialEq)] | ||
| pub struct ManagedCondition { | ||
| pub type_: ManagedConditionType, | ||
|
|
||
| pub status: bool, | ||
|
|
||
| /// A CamelCase reason like `Created` or `UnsupportedChange`. | ||
| pub reason: String, | ||
|
|
||
| pub message: String, | ||
| } | ||
|
|
||
| /// Merges the given conditions into the current ones. | ||
| /// | ||
| /// `lastTransitionTime` only changes if the status of a condition changes. Conditions of other | ||
| /// types are kept. | ||
| pub fn update_conditions( | ||
| current: &[Condition], | ||
| updates: Vec<ManagedCondition>, | ||
| now: &Time, | ||
| ) -> Vec<Condition> { | ||
| let mut conditions = current.to_vec(); | ||
|
|
||
| for update in updates { | ||
| let type_ = update.type_.to_string(); | ||
| let status = if update.status { "True" } else { "False" }.to_owned(); | ||
| let existing = conditions | ||
| .iter() | ||
| .position(|condition| condition.type_ == type_); | ||
|
|
||
| let last_transition_time = match existing { | ||
| Some(index) if conditions[index].status == status => { | ||
| conditions[index].last_transition_time.clone() | ||
| } | ||
| _ => now.clone(), | ||
| }; | ||
|
|
||
| let condition = Condition { | ||
| last_transition_time, | ||
| message: update.message, | ||
| observed_generation: None, | ||
| reason: update.reason, | ||
| status, | ||
| type_, | ||
| }; | ||
|
|
||
| match existing { | ||
| Some(index) => conditions[index] = condition, | ||
| None => conditions.push(condition), | ||
| } | ||
| } | ||
|
|
||
| conditions | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use k8s_openapi::jiff::Timestamp; | ||
|
|
||
| use super::*; | ||
|
|
||
| fn time(seconds: i64) -> Time { | ||
| Time(Timestamp::from_second(seconds).expect("the timestamp should be valid")) | ||
| } | ||
|
|
||
| fn ready(status: bool, reason: &str) -> ManagedCondition { | ||
| ManagedCondition { | ||
| type_: ManagedConditionType::Ready, | ||
| status, | ||
| reason: reason.to_owned(), | ||
| message: String::new(), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn add_missing_condition() { | ||
| let conditions = update_conditions(&[], vec![ready(true, "Created")], &time(10)); | ||
|
|
||
| assert_eq!(1, conditions.len()); | ||
| assert_eq!("Ready", conditions[0].type_); | ||
| assert_eq!("True", conditions[0].status); | ||
| assert_eq!("Created", conditions[0].reason); | ||
| assert_eq!(time(10), conditions[0].last_transition_time); | ||
| } | ||
|
|
||
| #[test] | ||
| fn keep_transition_time_if_status_is_unchanged() { | ||
| let current = update_conditions(&[], vec![ready(true, "Created")], &time(10)); | ||
|
|
||
| let conditions = update_conditions(¤t, vec![ready(true, "Updated")], &time(20)); | ||
|
|
||
| assert_eq!("Updated", conditions[0].reason); | ||
| assert_eq!(time(10), conditions[0].last_transition_time); | ||
| } | ||
|
|
||
| #[test] | ||
| fn update_transition_time_if_status_changes() { | ||
| let current = update_conditions(&[], vec![ready(true, "Created")], &time(10)); | ||
|
|
||
| let conditions = | ||
| update_conditions(¤t, vec![ready(false, "KafkaRejected")], &time(20)); | ||
|
|
||
| assert_eq!("False", conditions[0].status); | ||
| assert_eq!(time(20), conditions[0].last_transition_time); | ||
| } | ||
|
|
||
| #[test] | ||
| fn keep_conditions_of_other_types() { | ||
| let other = Condition { | ||
| last_transition_time: time(5), | ||
| message: String::new(), | ||
| observed_generation: None, | ||
| reason: "Other".to_owned(), | ||
| status: "True".to_owned(), | ||
| type_: "Other".to_owned(), | ||
| }; | ||
|
|
||
| let conditions = update_conditions( | ||
| std::slice::from_ref(&other), | ||
| vec![ready(true, "Created")], | ||
| &time(10), | ||
| ); | ||
|
|
||
| let types: Vec<_> = conditions | ||
| .iter() | ||
| .map(|condition| condition.type_.as_str()) | ||
| .collect(); | ||
| assert_eq!(vec!["Other", "Ready"], types); | ||
| } | ||
| } | ||
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 @@ | ||
| //! Helpers for agents managing resources inside a product. The CRD fragments live in | ||
| //! [`crate::crd::platform_access`]. | ||
|
|
||
| pub mod condition; | ||
| pub mod ownership; |
37 changes: 37 additions & 0 deletions
37
crates/stackable-operator/src/commons/platform_access/ownership.rs
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,37 @@ | ||
| use kube::api::ObjectMeta; | ||
|
|
||
| /// Annotation an agent sets on an object once it created or adopted the resource in the product. | ||
| /// | ||
| /// The value is the RFC 3339 timestamp of that moment. Agents only delete resources they own. | ||
| pub const OWNED_SINCE_ANNOTATION: &str = "stackable.tech/owned-since"; | ||
|
|
||
| /// Whether the agent owns the resource in the product that the object with this metadata manages. | ||
| pub fn is_owned(metadata: &ObjectMeta) -> bool { | ||
| metadata | ||
| .annotations | ||
| .as_ref() | ||
| .is_some_and(|annotations| annotations.contains_key(OWNED_SINCE_ANNOTATION)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::collections::BTreeMap; | ||
|
|
||
| use rstest::rstest; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[rstest] | ||
| #[case::no_annotations(None, false)] | ||
| #[case::other_annotation(Some(("stackable.tech/other", "x")), false)] | ||
| #[case::owned(Some((OWNED_SINCE_ANNOTATION, "2026-10-06T09:12:44Z")), true)] | ||
| fn ownership(#[case] annotation: Option<(&str, &str)>, #[case] expected: bool) { | ||
| let metadata = ObjectMeta { | ||
| annotations: annotation | ||
| .map(|(key, value)| BTreeMap::from([(key.to_owned(), value.to_owned())])), | ||
| ..ObjectMeta::default() | ||
| }; | ||
|
|
||
| assert_eq!(expected, is_owned(&metadata)); | ||
| } | ||
| } |
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
32 changes: 32 additions & 0 deletions
32
crates/stackable-operator/src/crd/platform_access/anonymous/mod.rs
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,32 @@ | ||
| use schemars::JsonSchema; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| use crate::versioned::versioned; | ||
|
|
||
| #[versioned(version(name = "v1alpha1"))] | ||
| pub mod versioned { | ||
| /// No credential: the product does not authenticate the agent. | ||
| #[derive(Clone, Debug, Default, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] | ||
| pub struct Anonymous {} | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::v1alpha1::Anonymous; | ||
|
|
||
| #[test] | ||
| fn deserialize_empty_object() { | ||
| let anonymous: Anonymous = | ||
| serde_json::from_str("{}").expect("an empty object should be valid"); | ||
|
|
||
| assert_eq!(Anonymous {}, anonymous); | ||
| } | ||
|
|
||
| #[test] | ||
| fn schema_is_an_object() { | ||
| let schema = serde_json::to_value(schemars::schema_for!(Anonymous)) | ||
| .expect("the schema should be serializable"); | ||
|
|
||
| assert_eq!("object", schema["type"]); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
crates/stackable-operator/src/crd/platform_access/management/mod.rs
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,44 @@ | ||
| use schemars::JsonSchema; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| use crate::versioned::versioned; | ||
|
|
||
| mod v1alpha1_impl; | ||
|
|
||
| #[versioned(version(name = "v1alpha1"))] | ||
| pub mod versioned { | ||
| /// What an agent may do with a resource inside the product beyond applying its spec. | ||
| #[derive(Clone, Debug, Default, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] | ||
| #[serde(default, rename_all = "camelCase")] | ||
| pub struct ManagementPolicy { | ||
| /// Whether a resource that already exists in the product, but was not created by the | ||
| /// agent, is taken over. `Refuse` by default. | ||
| pub adoption: AdoptionPolicy, | ||
|
|
||
| /// What happens to the resource in the product when this object is deleted. `Retain` by | ||
| /// default. | ||
| pub deletion: DeletionPolicy, | ||
| } | ||
|
|
||
| /// Whether an agent takes over a resource that already exists in the product. | ||
| #[derive(Clone, Copy, Debug, Default, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] | ||
| pub enum AdoptionPolicy { | ||
| /// Report the existing resource as a conflict and leave it untouched. | ||
| #[default] | ||
| Refuse, | ||
|
|
||
| /// Take over the existing resource and apply the spec to it. | ||
| Adopt, | ||
| } | ||
|
|
||
| /// What happens to a resource in the product when the object managing it is deleted. | ||
| #[derive(Clone, Copy, Debug, Default, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] | ||
| pub enum DeletionPolicy { | ||
| /// Keep the resource in the product. | ||
| #[default] | ||
| Retain, | ||
|
|
||
| /// Delete the resource in the product, if the agent created or adopted it. | ||
| Delete, | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
crates/stackable-operator/src/crd/platform_access/management/v1alpha1_impl.rs
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,44 @@ | ||
| use crate::crd::platform_access::management::v1alpha1::{ | ||
| AdoptionPolicy, DeletionPolicy, ManagementPolicy, | ||
| }; | ||
|
|
||
| impl ManagementPolicy { | ||
| /// Whether the agent takes over a resource that already exists in the product. | ||
| pub fn adopts_existing(&self) -> bool { | ||
| self.adoption == AdoptionPolicy::Adopt | ||
| } | ||
|
|
||
| /// Whether the agent deletes the resource in the product when this object is deleted. | ||
| pub fn deletes_on_cleanup(&self) -> bool { | ||
| self.deletion == DeletionPolicy::Delete | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use rstest::rstest; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[rstest] | ||
| #[case::defaults("{}", false, false)] | ||
| #[case::adopt(r#"{"adoption": "Adopt"}"#, true, false)] | ||
| #[case::delete(r#"{"deletion": "Delete"}"#, false, true)] | ||
| #[case::explicit_defaults(r#"{"adoption": "Refuse", "deletion": "Retain"}"#, false, false)] | ||
| fn deserialize_policy( | ||
| #[case] input: &str, | ||
| #[case] adopts_existing: bool, | ||
| #[case] deletes_on_cleanup: bool, | ||
| ) { | ||
| let policy: ManagementPolicy = | ||
| serde_json::from_str(input).expect("the policy should be valid"); | ||
|
|
||
| assert_eq!(adopts_existing, policy.adopts_existing()); | ||
| assert_eq!(deletes_on_cleanup, policy.deletes_on_cleanup()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn reject_unknown_policy() { | ||
| assert!(serde_json::from_str::<ManagementPolicy>(r#"{"deletion": "Orphan"}"#).is_err()); | ||
| } | ||
| } |
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 @@ | ||
| //! CRD fragments for resources managed by product agents, and for the credentials the agents use. | ||
|
|
||
| pub mod anonymous; | ||
| pub mod management; | ||
| pub mod tls; | ||
|
|
||
| pub mod v1alpha1 { | ||
| pub use super::{anonymous::v1alpha1::*, management::v1alpha1::*, tls::v1alpha1::*}; | ||
| } |
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.
We have proper ClusterConditions https://github.com/stackabletech/operator-rs/blob/main/crates/stackable-operator/src/status/condition/mod.rs#L90 that might be reused or made generic to add custom condition type / etc.?