-
-
Notifications
You must be signed in to change notification settings - Fork 12
refactor: Extract apply and update status steps #974
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
+1,194
−577
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3046476
refactor: Add Prepared type-state marker to KubernetesResources
maltesander a5ace31
refactor: Parse the deployed product version in the validate step
maltesander 0285d43
refactor: Extract the apply step into an Applier
maltesander 2791cd8
refactor: Extract the update_status step
maltesander f5c1e91
refactor: Pass recommended labels into object_meta
maltesander 8972dc0
docs: Correct the default sensitive properties algorithm
maltesander 2b1f587
chore: adapt changelog
maltesander 0667677
Merge remote-tracking branch 'origin/main' into refactor/extract-appl…
maltesander 8547ce5
refactor: fold the operator-generated Secrets into the reconciliation…
maltesander b07a11b
refactor: drop the Default derive from ExistingSecrets
maltesander 1d9a827
refactor: move the Secret preconditions into the validate step
maltesander 9f53a8d
refactor: stop re-emitting existing Secrets
maltesander 0b741bb
test: make the Secret build tests state their own preconditions
maltesander c23ee68
Merge remote-tracking branch 'origin/main' into refactor/extract-appl…
maltesander 0e3fa61
docs: remove commet why Default derive is left out
maltesander 4f0e665
test: simplify the Secret build test assertions
maltesander dcd4cca
test: key the built Secrets by name in the tests
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
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,139 @@ | ||
| //! The apply step in the NifiCluster controller. | ||
|
|
||
| use std::marker::PhantomData; | ||
|
|
||
| use snafu::{ResultExt, Snafu}; | ||
| use stackable_operator::{ | ||
| client::Client, | ||
| cluster_resources::{ClusterResource, ClusterResourceApplyStrategy, ClusterResources}, | ||
| 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, | ||
| }; | ||
|
|
||
| #[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, | ||
| }, | ||
| } | ||
|
|
||
| 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. | ||
| pub async fn apply( | ||
| mut self, | ||
| resources: KubernetesResources<Prepared>, | ||
| ) -> Result<KubernetesResources<Applied>> { | ||
| // Destructured without `..`, so adding a field to [`KubernetesResources`] fails to | ||
| // compile here instead of silently never being applied. | ||
| let KubernetesResources { | ||
| stateful_sets, | ||
| services, | ||
| listeners, | ||
| config_maps, | ||
| secrets, | ||
| pod_disruption_budgets, | ||
| service_accounts, | ||
| role_bindings, | ||
| status: _, | ||
| } = resources; | ||
|
|
||
| // Apply order is: StatefulSets last (a changed mounted ConfigMap or Secret must exist | ||
| // first, else the Pods restart unnecessarily, see commons-operator#111). 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 secrets = self.add_resources(secrets).await?; | ||
| let pod_disruption_budgets = self.add_resources(pod_disruption_budgets).await?; | ||
| let stateful_sets = self.add_resources(stateful_sets).await?; | ||
|
|
||
| // Remove any orphaned resources that still exist in Kubernetes, but have not been added to | ||
| // the cluster resources during this reconciliation. | ||
| // TODO: this doesn't cater for a graceful cluster shrink, for that we'd need to predict | ||
| // the resources that will be removed and run a disconnect/offload job for those | ||
| // see https://github.com/stackabletech/nifi-operator/issues/314 | ||
| self.cluster_resources | ||
| .delete_orphaned_resources(self.client) | ||
| .await | ||
| .context(DeleteOrphanedResourcesSnafu)?; | ||
|
|
||
| Ok(KubernetesResources { | ||
| stateful_sets, | ||
| services, | ||
| listeners, | ||
| config_maps, | ||
| secrets, | ||
| pod_disruption_budgets, | ||
| service_accounts, | ||
| role_bindings, | ||
| status: PhantomData, | ||
| }) | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.