-
-
Notifications
You must be signed in to change notification settings - Fork 12
refactor: Extract apply and update_status steps #923
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
6 commits
Select commit
Hold shift + click to select a range
3af09ac
refactor: Add pipeline stage marker to KubernetesResources
maltesander 879a40c
refactor: Extract the apply step into an Applier
maltesander 0b95b10
refactor: Extract the update_status step
maltesander 8d69bf2
chore: adapt changelog
maltesander a16831f
Merge remote-tracking branch 'origin/main' into refactor/extract-appl…
maltesander 8caaa2c
refactor: Address review nits on apply and update_status
maltesander 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| //! The apply step in the TrinoCluster controller. | ||
|
|
||
| use std::marker::PhantomData; | ||
|
|
||
| use snafu::{ResultExt, Snafu}; | ||
| use stackable_operator::{ | ||
| client::Client, | ||
| cluster_resources::{ClusterResource, ClusterResourceApplyStrategy, ClusterResources}, | ||
| commons::random_secret_creation, | ||
| deep_merger::ObjectOverrides, | ||
| v2::cluster_resources::cluster_resources_new, | ||
| }; | ||
| use strum::{EnumDiscriminants, IntoStaticStr}; | ||
|
|
||
| use crate::{ | ||
| controller::{ | ||
| Applied, KubernetesResources, Prepared, ValidatedCluster, controller_name, operator_name, | ||
| product_name, shared_internal_secret_name, shared_spooling_secret_name, | ||
| }, | ||
| crd::{ENV_INTERNAL_SECRET, ENV_SPOOLING_SECRET}, | ||
| }; | ||
|
|
||
| #[derive(Snafu, Debug, EnumDiscriminants)] | ||
| #[strum_discriminants(derive(IntoStaticStr))] | ||
| pub enum Error { | ||
| #[snafu(display("failed to apply Kubernetes resource"))] | ||
| ApplyResource { | ||
| source: stackable_operator::cluster_resources::Error, | ||
| }, | ||
|
|
||
| #[snafu(display("failed to delete orphaned resources"))] | ||
| DeleteOrphanedResources { | ||
| source: stackable_operator::cluster_resources::Error, | ||
| }, | ||
|
|
||
| #[snafu(display("failed to create internal secret"))] | ||
| CreateInternalSecret { | ||
| source: random_secret_creation::Error, | ||
| }, | ||
|
|
||
| #[snafu(display("failed to create spooling secret"))] | ||
| CreateSpoolingSecret { | ||
| source: random_secret_creation::Error, | ||
| }, | ||
| } | ||
|
|
||
| type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
|
||
| /// Applier for the Kubernetes resource specifications produced by this controller. | ||
| /// | ||
| /// The implementation is not tied to this controller and could theoretically be moved to | ||
| /// stackable_operator if [`KubernetesResources`] would contain all possible resource types. | ||
| pub struct Applier<'a> { | ||
| client: &'a Client, | ||
| cluster_resources: ClusterResources<'a>, | ||
| } | ||
|
|
||
| impl<'a> Applier<'a> { | ||
| pub fn new( | ||
| client: &'a Client, | ||
| cluster: &ValidatedCluster, | ||
| apply_strategy: ClusterResourceApplyStrategy, | ||
| object_overrides: &'a ObjectOverrides, | ||
| ) -> Applier<'a> { | ||
| let cluster_resources = cluster_resources_new( | ||
| &product_name(), | ||
| &operator_name(), | ||
| &controller_name(), | ||
| &cluster.name, | ||
| &cluster.namespace, | ||
| &cluster.uid, | ||
| apply_strategy, | ||
| object_overrides, | ||
| ); | ||
|
|
||
| Applier { | ||
| client, | ||
| cluster_resources, | ||
| } | ||
| } | ||
|
|
||
| /// Applies the given Kubernetes resources and marks them as applied. | ||
| /// | ||
| /// Resources that are owned by this cluster but no longer part of `resources` are deleted | ||
| /// afterwards. | ||
| pub async fn apply( | ||
| mut self, | ||
| resources: KubernetesResources<Prepared>, | ||
| ) -> Result<KubernetesResources<Applied>> { | ||
| // Destructured without `..`, so that adding a field to [`KubernetesResources`] fails to | ||
| // compile here instead of the new resource silently never being applied. | ||
| let KubernetesResources { | ||
| stateful_sets, | ||
| services, | ||
| listeners, | ||
| config_maps, | ||
| pod_disruption_budgets, | ||
| service_accounts, | ||
| role_bindings, | ||
| status: _, | ||
| } = resources; | ||
|
|
||
| // The ServiceAccount comes first, because the Pods reference it at creation time. | ||
| let service_accounts = self.add_resources(service_accounts).await?; | ||
| let role_bindings = self.add_resources(role_bindings).await?; | ||
| let services = self.add_resources(services).await?; | ||
| let listeners = self.add_resources(listeners).await?; | ||
| let config_maps = self.add_resources(config_maps).await?; | ||
| let pod_disruption_budgets = self.add_resources(pod_disruption_budgets).await?; | ||
|
|
||
| // Note: The StatefulSet needs to be applied after all ConfigMaps and Secrets it mounts | ||
| // to prevent unnecessary Pod restarts. | ||
| // See https://github.com/stackabletech/commons-operator/issues/111 for details. | ||
| let stateful_sets = self.add_resources(stateful_sets).await?; | ||
|
|
||
| self.cluster_resources | ||
| .delete_orphaned_resources(self.client) | ||
| .await | ||
| .context(DeleteOrphanedResourcesSnafu)?; | ||
|
|
||
| Ok(KubernetesResources { | ||
| stateful_sets, | ||
| services, | ||
| listeners, | ||
| config_maps, | ||
| pod_disruption_budgets, | ||
| service_accounts, | ||
| role_bindings, | ||
| status: PhantomData, | ||
| }) | ||
| } | ||
|
|
||
| /// Applies the given resources and returns them as the API server echoed them back. | ||
| async fn add_resources<T: ClusterResource + Sync>( | ||
| &mut self, | ||
| resources: Vec<T>, | ||
| ) -> Result<Vec<T>> { | ||
| let mut applied_resources = vec![]; | ||
|
|
||
| for resource in resources { | ||
| let applied_resource = self | ||
| .cluster_resources | ||
| .add(self.client, resource) | ||
| .await | ||
| .context(ApplyResourceSnafu)?; | ||
| applied_resources.push(applied_resource); | ||
| } | ||
|
|
||
| Ok(applied_resources) | ||
| } | ||
| } | ||
|
|
||
| /// Ensures the two shared random Secrets (internal communication and spooling) exist, creating | ||
| /// any that are missing. | ||
| /// | ||
| /// These are read-or-create client operations, so they cannot be part of the client-free | ||
| /// `build()` step. They are also deliberately not tracked in [`ClusterResources`], so that they | ||
| /// survive orphan deletion and an existing Secret is never overwritten (rotating them would | ||
| /// invalidate all running queries). | ||
| pub async fn ensure_random_secrets(client: &Client, cluster: &ValidatedCluster) -> Result<()> { | ||
| random_secret_creation::create_random_secret_if_not_exists( | ||
| &shared_internal_secret_name(&cluster.name), | ||
| ENV_INTERNAL_SECRET, | ||
| 512, | ||
| cluster, | ||
| client, | ||
| ) | ||
| .await | ||
| .context(CreateInternalSecretSnafu)?; | ||
|
|
||
| // This secret is created even if spooling is not configured. | ||
| // Trino currently requires the secret to be exactly 256 bits long. | ||
| random_secret_creation::create_random_secret_if_not_exists( | ||
| &shared_spooling_secret_name(&cluster.name), | ||
| ENV_SPOOLING_SECRET, | ||
| 32, | ||
| cluster, | ||
| client, | ||
| ) | ||
| .await | ||
| .context(CreateSpoolingSecretSnafu)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
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,62 @@ | ||
| //! The update_status step in the TrinoCluster controller. | ||
|
|
||
| use snafu::{ResultExt, Snafu}; | ||
| use stackable_operator::{ | ||
| client::Client, | ||
| status::condition::{ | ||
| compute_conditions, operations::ClusterOperationsConditionBuilder, | ||
| statefulset::StatefulSetConditionBuilder, | ||
| }, | ||
| }; | ||
| use strum::{EnumDiscriminants, IntoStaticStr}; | ||
|
|
||
| use crate::{ | ||
| controller::{Applied, KubernetesResources}, | ||
| crd::v1alpha1, | ||
| trino_controller::OPERATOR_NAME, | ||
| }; | ||
|
|
||
| #[derive(Snafu, Debug, EnumDiscriminants)] | ||
| #[strum_discriminants(derive(IntoStaticStr))] | ||
| pub enum Error { | ||
| #[snafu(display("failed to update status"))] | ||
| ApplyStatus { | ||
| source: stackable_operator::client::Error, | ||
| }, | ||
| } | ||
|
|
||
| type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
|
||
| /// Computes the cluster status from the applied resources and patches it onto the | ||
| /// [`v1alpha1::TrinoCluster`]. | ||
| /// | ||
| /// Takes [`KubernetesResources<Applied>`], so the type system proves that the status is derived | ||
| /// from the resources the API server acknowledged and not from the ones we merely built. They are | ||
| /// consumed, because this is the last step of the reconciliation pipeline. | ||
| pub async fn update_status( | ||
| client: &Client, | ||
| trino: &v1alpha1::TrinoCluster, | ||
| applied: KubernetesResources<Applied>, | ||
| ) -> Result<()> { | ||
| let mut sts_cond_builder = StatefulSetConditionBuilder::default(); | ||
| for stateful_set in applied.stateful_sets { | ||
| sts_cond_builder.add(stateful_set); | ||
| } | ||
|
|
||
| let cluster_operation_cond_builder = | ||
| ClusterOperationsConditionBuilder::new(&trino.spec.cluster_operation); | ||
|
|
||
| let status = v1alpha1::TrinoClusterStatus { | ||
| conditions: compute_conditions( | ||
| trino, | ||
| &[&sts_cond_builder, &cluster_operation_cond_builder], | ||
| ), | ||
| }; | ||
|
|
||
| client | ||
| .apply_patch_status(OPERATOR_NAME, trino, &status) | ||
| .await | ||
| .context(ApplyStatusSnafu)?; | ||
|
|
||
| Ok(()) | ||
| } |
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.