From bebc7fa92e349e112bd1b2eca64ee1c4a15f0f88 Mon Sep 17 00:00:00 2001 From: Donal Byrne Date: Sat, 10 Aug 2024 22:07:25 +0200 Subject: [PATCH] Cleanup executor cmds --- src/delete.rs | 4 +++ src/executor.rs | 73 +++++++++++++++++------------------------- src/skatelet/delete.rs | 40 ++++++++++++++++++++++- 3 files changed, 72 insertions(+), 45 deletions(-) diff --git a/src/delete.rs b/src/delete.rs index bd48d29..ca88ca8 100644 --- a/src/delete.rs +++ b/src/delete.rs @@ -22,6 +22,8 @@ pub enum DeleteCommands { Ingress(DeleteResourceArgs), Cronjob(DeleteResourceArgs), Secret(DeleteResourceArgs), + Deployment(DeleteResourceArgs), + Daemonset(DeleteResourceArgs), } #[derive(Debug, Args)] @@ -39,6 +41,8 @@ pub struct DeleteResourceArgs { pub async fn delete(args: DeleteArgs) -> Result<(), Box> { match args.command { DeleteCommands::Node(args) => delete_node(args).await?, + DeleteCommands::Daemonset(args) => delete_resource(ResourceType::DaemonSet, args).await?, + DeleteCommands::Deployment(args) => delete_resource(ResourceType::Deployment, args).await?, DeleteCommands::Ingress(args) => delete_resource(ResourceType::Ingress, args).await?, DeleteCommands::Cronjob(args) => delete_resource(ResourceType::CronJob, args).await?, DeleteCommands::Secret(args) => delete_resource(ResourceType::Secret, args).await?, diff --git a/src/executor.rs b/src/executor.rs index 5a432d7..b70374c 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -7,6 +7,7 @@ use std::process::Stdio; use anyhow::anyhow; use handlebars::Handlebars; use itertools::Itertools; +use k8s_openapi::api::apps::v1::{DaemonSet, Deployment}; use k8s_openapi::api::batch::v1::CronJob; use k8s_openapi::api::core::v1::{Pod, Secret}; use k8s_openapi::api::networking::v1::Ingress; @@ -267,42 +268,33 @@ impl DefaultExecutor { args.push("--network=podman") } - let output = process::Command::new("podman") - .args(&args) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .output() - .expect(&format!("failed to apply resource via `podman {}`", &args.join(" "))); + let result = exec_cmd("podman", &args)?; - if !output.status.success() { - return Err(anyhow!("`podman {}` exited with code {}, stderr: {}", args.join(" "), output.status.code().unwrap(), String::from_utf8_lossy(&output.stderr).to_string()).into()); + if !result.is_empty() { + println!("{}", result); } - - Ok(()) } fn remove_secret(&self, secret: Secret) -> Result<(), Box> { let fqn = format!("{}.{}", secret.metadata.name.unwrap(), secret.metadata.namespace.unwrap()); + let output = exec_cmd("podman", &["secret", "rm", &fqn])?; - let output = process::Command::new("podman") - .args(["secret", "rm", &fqn]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .output() - .expect("failed to remove secret"); - - if !output.status.success() { - return Err(anyhow!("`podman secret rm {}` exited with code {}, stderr: {}", fqn, output.status.code().unwrap(), String::from_utf8_lossy(&output.stderr).trim().to_string()).into()); - } - - if !output.stdout.is_empty() { - println!("{}", String::from_utf8_lossy(&output.stdout).trim()); + if !output.is_empty() { + println!("{}", output); } Ok(()) } + fn remove_deployment(&self, deployment: Deployment) -> Result<(), Box> { + todo!("not implemented") + } + + fn remove_daemonset(&self, daemonset: DaemonSet) -> Result<(), Box> { + todo!("not implemented") + } + fn remove_pod(&self, id: &str, grace_period: Option) -> Result<(), Box> { if id.is_empty() { return Err(anyhow!("no metadata.name found").into()); @@ -315,31 +307,24 @@ impl DefaultExecutor { vec!("pod", "stop", "-t", &grace_str), vec!(&id), ].concat(); - let _output = process::Command::new("podman") - .args(stop_cmd.clone()) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .output() - .expect("failed to stop pod"); - // if !output.status.success() { - // return Err(anyhow!("{:?} - exit code {}, stderr: {}", stop_cmd, output.status, String::from_utf8_lossy(&output.stderr).to_string()).into()); - // } + let result = exec_cmd("podman", &stop_cmd); + + if result.is_err() { + eprintln!("{}", result.unwrap_err()); + } let rm_cmd = [ vec!("pod", "rm", "--force"), vec!(&id), ].concat(); - let output = process::Command::new("podman") - .args(rm_cmd.clone()) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .output() - .expect("failed to remove pod"); - if !output.status.success() { - return Err(anyhow!("{:?} - exit code {}, stderr: {}", rm_cmd, output.status, String::from_utf8_lossy(&output.stderr).to_string()).into()); + let output = exec_cmd("podman", &rm_cmd)?; + + if !output.is_empty() { + println!("{}", output); } + Ok(()) } } @@ -368,11 +353,11 @@ impl Executor for DefaultExecutor { let name = p.metadata.name.unwrap(); self.remove_pod(&name, grace_period) } - SupportedResources::Deployment(_d) => { - Err(anyhow!("removing a deployment is not supported, instead supply it's individual pods").into()) + SupportedResources::Deployment(d) => { + self.remove_deployment(d) } - SupportedResources::DaemonSet(_) => { - Err(anyhow!("removing a daemonset is not supported, instead supply it's individual pods").into()) + SupportedResources::DaemonSet(d) => { + self.remove_daemonset(d) } SupportedResources::Ingress(ingress) => { self.remove_ingress(ingress) diff --git a/src/skatelet/delete.rs b/src/skatelet/delete.rs index 68808e6..4d509a9 100644 --- a/src/skatelet/delete.rs +++ b/src/skatelet/delete.rs @@ -29,6 +29,8 @@ pub enum DeleteResourceCommands { Ingress(DeleteResourceArgs), Cronjob(DeleteResourceArgs), Secret(DeleteResourceArgs), + Deployment(DeleteResourceArgs), + Daemonset(DeleteResourceArgs), } #[derive(Debug, Args, Clone)] @@ -45,6 +47,8 @@ pub fn delete(args: DeleteArgs) -> Result<(), Box> { DeleteResourceCommands::StdinCommand(_) => delete_stdin(args), DeleteResourceCommands::Cronjob(resource_args) => delete_cronjob(args.clone(), resource_args.clone()), DeleteResourceCommands::Secret(resource_args) => delete_secret(args.clone(), resource_args.clone()), + DeleteResourceCommands::Daemonset(resource_args) => delete_daemonset(args.clone(), resource_args.clone()), + DeleteResourceCommands::Deployment(resource_args) => delete_deployment(args.clone(), resource_args.clone()), } } @@ -93,7 +97,7 @@ pub fn delete_secret(delete_args: DeleteArgs, resource_args: DeleteResourceArgs) ("skate.io/namespace".to_string(), resource_args.namespace), ])); - executor.manifest_delete(SupportedResources::Secret(Secret{ + executor.manifest_delete(SupportedResources::Secret(Secret { data: None, immutable: None, metadata: meta, @@ -114,3 +118,37 @@ pub fn delete_stdin(args: DeleteArgs) -> Result<(), Box> { let object: SupportedResources = serde_yaml::from_str(&manifest).expect("failed to deserialize manifest"); executor.manifest_delete(object, args.termination_grace_period) } + +pub fn delete_deployment(delete_args: DeleteArgs, resource_args: DeleteResourceArgs) -> Result<(), Box> { + let executor = DefaultExecutor::new(); + let mut meta = ObjectMeta::default(); + meta.name = Some(resource_args.name.clone()); + meta.namespace = Some(resource_args.namespace.clone()); + meta.labels = Some(BTreeMap::from([ + ("skate.io/name".to_string(), resource_args.name), + ("skate.io/namespace".to_string(), resource_args.namespace), + ])); + + executor.manifest_delete(SupportedResources::Deployment(k8s_openapi::api::apps::v1::Deployment { + metadata: meta, + spec: None, + status: None, + }), delete_args.termination_grace_period) +} + +pub fn delete_daemonset(delete_args: DeleteArgs, resource_args: DeleteResourceArgs) -> Result<(), Box> { + let executor = DefaultExecutor::new(); + let mut meta = ObjectMeta::default(); + meta.name = Some(resource_args.name.clone()); + meta.namespace = Some(resource_args.namespace.clone()); + meta.labels = Some(BTreeMap::from([ + ("skate.io/name".to_string(), resource_args.name), + ("skate.io/namespace".to_string(), resource_args.namespace), + ])); + + executor.manifest_delete(SupportedResources::DaemonSet(k8s_openapi::api::apps::v1::DaemonSet { + metadata: meta, + spec: None, + status: None, + }), delete_args.termination_grace_period) +}