-
-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add rolling upgrade support for nifi 2.x #771
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
19 commits
Select commit
Hold shift + click to select a range
4ab4b52
feat: add rolling upgrade support for nifi 2.x
xeniape 3279fae
Merge branch 'main' into feat/rolling-upgrade-support
xeniape 752fc44
cargo fmt
xeniape 50a27af
cargo fmt
xeniape 74cdf40
cargo update and make regenerate-nix
xeniape 06e02de
add changelog entry
xeniape 919cb0e
add documentation
xeniape 448d586
Merge branch 'main' into feat/rolling-upgrade-support
xeniape 2be1298
cargo update and make regenerate-nix
xeniape cea1267
fix upgrade intergration tests with custom images
xeniape d72e8c5
Merge branch 'main' into feat/rolling-upgrade-support
xeniape f2c8ecf
cargo update and make regenerate-nix
xeniape 8b20f2a
fix integration tests when using custom images
xeniape 827ac39
rename VersionChangeState enum and some variants
xeniape 450367c
rename enum variant
xeniape 1d15a83
move upgrade code in separate module
xeniape 8f9a3c8
Merge branch 'main' into feat/rolling-upgrade-support
xeniape fc38d3a
cargo fmt
xeniape e191d84
adapt variable names
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| pub mod graceful_shutdown; | ||
| pub mod pdb; | ||
| pub mod upgrade; |
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,124 @@ | ||
| // TODO: This module can be removed once we don't support NiFi 1.x versions anymore | ||
| // It manages the version upgrade procedure for NiFi versions prior to NiFi 2, since rolling upgrade is not supported there yet | ||
|
|
||
| use snafu::{OptionExt, ResultExt, Snafu}; | ||
| use stackable_operator::{ | ||
| client::Client, | ||
| k8s_openapi::{api::apps::v1::StatefulSet, apimachinery::pkg::apis::meta::v1::LabelSelector}, | ||
| kvp::Labels, | ||
| }; | ||
|
|
||
| use crate::crd::{APP_NAME, NifiRole, v1alpha1}; | ||
|
|
||
| #[derive(Snafu, Debug)] | ||
| pub enum Error { | ||
| #[snafu(display("object defines no namespace"))] | ||
| ObjectHasNoNamespace, | ||
|
|
||
| #[snafu(display("failed to fetch deployed StatefulSets"))] | ||
| FetchStatefulsets { | ||
| source: stackable_operator::client::Error, | ||
| }, | ||
|
|
||
| #[snafu(display("failed to build labels"))] | ||
| LabelBuild { | ||
| source: stackable_operator::kvp::LabelError, | ||
| }, | ||
| } | ||
|
|
||
| type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
|
||
| // This struct is used for NiFi versions not supporting rolling upgrades since in that case | ||
| // we have to manage the restart process ourselves and need to track the state of it | ||
| #[derive(Debug, PartialEq, Eq)] | ||
| pub enum ClusterVersionUpdateState { | ||
| UpdateRequested, | ||
| UpdateInProgress, | ||
| ClusterStopped, | ||
| NoVersionChange, | ||
| } | ||
|
|
||
| pub async fn cluster_version_update_state( | ||
| nifi: &v1alpha1::NifiCluster, | ||
| client: &Client, | ||
| resolved_version: &String, | ||
| deployed_version: Option<&String>, | ||
| ) -> Result<ClusterVersionUpdateState> { | ||
| let namespace = &nifi | ||
| .metadata | ||
| .namespace | ||
| .clone() | ||
| .with_context(|| ObjectHasNoNamespaceSnafu {})?; | ||
|
|
||
| // Handle full restarts for a version change | ||
| match deployed_version { | ||
| Some(deployed_version) => { | ||
| if deployed_version != resolved_version { | ||
| // Check if statefulsets are already scaled to zero, if not - requeue | ||
| let selector = LabelSelector { | ||
| match_expressions: None, | ||
| match_labels: Some( | ||
| Labels::role_selector(nifi, APP_NAME, &NifiRole::Node.to_string()) | ||
| .context(LabelBuildSnafu)? | ||
| .into(), | ||
| ), | ||
| }; | ||
|
|
||
| // Retrieve the deployed statefulsets to check on the current status of the restart | ||
| let deployed_statefulsets = client | ||
| .list_with_label_selector::<StatefulSet>(namespace, &selector) | ||
| .await | ||
| .context(FetchStatefulsetsSnafu)?; | ||
|
|
||
| // Sum target replicas for all statefulsets | ||
| let target_replicas = deployed_statefulsets | ||
| .iter() | ||
| .filter_map(|statefulset| statefulset.spec.as_ref()) | ||
| .filter_map(|spec| spec.replicas) | ||
| .sum::<i32>(); | ||
|
|
||
| // Sum current ready replicas for all statefulsets | ||
| let current_replicas = deployed_statefulsets | ||
| .iter() | ||
| .filter_map(|statefulset| statefulset.status.as_ref()) | ||
| .map(|status| status.replicas) | ||
| .sum::<i32>(); | ||
|
|
||
| // If statefulsets have already been scaled to zero, but have remaining replicas | ||
| // we requeue to wait until a full stop has been performed. | ||
| if target_replicas == 0 && current_replicas > 0 { | ||
| tracing::info!( | ||
| "Cluster is performing a full restart at the moment and still shutting down, remaining replicas: [{}] - requeueing to wait for shutdown to finish", | ||
| current_replicas | ||
| ); | ||
| return Ok(ClusterVersionUpdateState::UpdateInProgress); | ||
| } | ||
|
|
||
| // Otherwise we either still need to scale the statefulsets to 0 or all replicas have | ||
| // been stopped and we can restart the cluster. | ||
| // Both actions will be taken in the regular reconciliation, so we can simply continue | ||
| // here | ||
| if target_replicas > 0 { | ||
| tracing::info!( | ||
| "Version change detected, we'll need to scale down the cluster for a full restart." | ||
| ); | ||
| Ok(ClusterVersionUpdateState::UpdateRequested) | ||
| } else { | ||
| tracing::info!("Cluster has been stopped for a restart, will scale back up."); | ||
| Ok(ClusterVersionUpdateState::ClusterStopped) | ||
| } | ||
| } else { | ||
| // No version change detected, propagate this to the reconciliation | ||
| Ok(ClusterVersionUpdateState::NoVersionChange) | ||
| } | ||
| } | ||
| None => { | ||
| // No deployed version set in status, this is probably the first reconciliation ever | ||
| // for this cluster, so just let it progress normally | ||
| tracing::debug!( | ||
| "No deployed version found for this cluster, this is probably the first start, continue reconciliation" | ||
| ); | ||
| Ok(ClusterVersionUpdateState::NoVersionChange) | ||
| } | ||
| } | ||
| } |
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.
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.