Skip to content
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

feat(upgrade): add force flag for deleting upgrade resources #245

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions k8s/upgrade/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ pub enum Error {
#[snafu(display("Upgrade Job: {} status not present.", name))]
UpgradeJobStatusNotPresent { name: String },

/// Error for when the job.status is a None.
#[snafu(display("Upgrade Job: {} not completed.", name))]
UpgradeJobNotCompleted { name: String },
/// Error for when the upgrade job is not present.
#[snafu(display("Upgrade Job: {} in namespace {} does not exist.", name, namespace))]
UpgradeJobNotPresent { name: String, namespace: String },

/// Error for when a Kubernetes API request for GET-ing a list of Pods filtered by label(s)
/// fails.
Expand Down Expand Up @@ -260,7 +260,7 @@ impl From<Error> for i32 {
Error::NodeSpecNotPresent { .. } => 423,
Error::PodNameNotPresent { .. } => 424,
Error::UpgradeJobStatusNotPresent { .. } => 425,
Error::UpgradeJobNotCompleted { .. } => 426,
Error::UpgradeJobNotPresent { .. } => 426,
Error::ListPodsWithLabel { .. } => 427,
Error::ListDeploymantsWithLabel { .. } => 428,
Error::ListEventsWithFieldSelector { .. } => 429,
Expand Down
62 changes: 30 additions & 32 deletions k8s/upgrade/src/upgrade_resources/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::{
upgrade_resources::objects,
user_prompt::{
upgrade_dry_run_summary, CONTROL_PLANE_PODS_LIST, DATA_PLANE_PODS_LIST,
DATA_PLANE_PODS_LIST_SKIP_RESTART, UPGRADE_DRY_RUN_SUMMARY, UPGRADE_JOB_STARTED,
DATA_PLANE_PODS_LIST_SKIP_RESTART, DELETE_INCOMPLETE_JOB, UPGRADE_DRY_RUN_SUMMARY,
UPGRADE_JOB_STARTED,
},
};
use k8s_openapi::api::{
Expand All @@ -38,16 +39,21 @@ pub enum DeleteResources {

/// Delete Upgrade resource.
#[derive(clap::Args, Debug)]
pub struct DeleteUpgradeArgs {}
pub struct DeleteUpgradeArgs {
/// If true, immediately remove upgrade resources bypass graceful deletion.
#[clap(global = false, long, short, default_value_t = false)]
pub force: bool,
}

impl DeleteUpgradeArgs {
/// Delete the upgrade resources
pub async fn delete(&self, ns: &str) {
// Delete upgrade resources once job completes

match is_upgrade_job_completed(ns).await {
Ok(job_completed) => {
if job_completed {
if !job_completed && !self.force {
console_logger::error("", DELETE_INCOMPLETE_JOB);
}
if job_completed || self.force {
_ = UpgradeResources::delete_upgrade_resources(ns)
.await
.map_err(|error| {
Expand All @@ -56,9 +62,8 @@ impl DeleteUpgradeArgs {
}
}
Err(error) => {
eprintln!(
"error occured while fetching the completion status of upgrade job {error}"
);
eprintln!("error : {error}");
std::process::exit(error.into());
}
}
}
Expand Down Expand Up @@ -663,33 +668,26 @@ pub async fn is_upgrade_job_completed(ns: &str) -> error::Result<bool> {
})?;
match option_job {
Some(job) => {
if matches!(
job.status
.as_ref()
.ok_or(
error::UpgradeJobStatusNotPresent {
name: job_name.clone()
}
.build()
)?
.succeeded
.as_ref()
.ok_or(
error::UpgradeJobNotCompleted {
name: job_name.clone()
}
.build()
)?,
1
) {
return Ok(true);
}
let status = job.status.as_ref().ok_or(
error::UpgradeJobStatusNotPresent {
name: job_name.clone(),
}
.build(),
)?;

let is_job_completed = match status.succeeded {
None => false,
Some(count) => count == 1,
};
Ok(is_job_completed)
}
None => {
eprintln!("Upgrade job {job_name} in namespace {ns} does not exist");

None => error::UpgradeJobNotPresent {
name: job_name,
namespace: ns,
}
.fail(),
}
Ok(false)
}

struct ImageProperties {
Expand Down
8 changes: 6 additions & 2 deletions k8s/upgrade/src/user_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub const UPGRADE_JOB_STARTED: &str =
pub const UPGRADE_PATH_NOT_VALID: &str =
"\nThe upgrade path is not valid. The source version is in the list of unsupported versions:";

/// Upgrade to unsuppoted version not valid.
/// Upgrade to unsupported version not valid.
pub const UPGRADE_TO_UNSUPPORTED_VERSION: &str =
"\nUpgrade failed as destination version is unsupported. Please try with `--skip-upgrade-path-validation-for-unsupported-version`";
"\nUpgrade failed as destination version is unsupported. Please try with `--skip-upgrade-path-validation-for-unsupported-version.`";

/// Delete an incomplete job.
pub const DELETE_INCOMPLETE_JOB: &str =
"\n Cant delete an incomplete upgrade job. Please try with `--force` flag to forcefully remove upgrade resources and bypass graceful deletion.";