Skip to content

Commit

Permalink
delete existing pods to let the patched stateful set restart another …
Browse files Browse the repository at this point in the history
…one automatically
  • Loading branch information
imor committed Aug 5, 2024
1 parent 5edada9 commit ba6c2f2
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions api/src/k8s_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use k8s_openapi::api::{
apps::v1::StatefulSet,
core::v1::{ConfigMap, Secret},
core::v1::{ConfigMap, Pod, Secret},
};
use serde_json::json;
use thiserror::Error;
Expand All @@ -24,6 +24,7 @@ pub struct K8sClient {
secrets_api: Api<Secret>,
config_maps_api: Api<ConfigMap>,
stateful_sets_api: Api<StatefulSet>,
pods_api: Api<Pod>,
}

const SECRET_NAME_SUFFIX: &str = "bq-service-account-key";
Expand All @@ -37,12 +38,14 @@ impl K8sClient {

let secrets_api: Api<Secret> = Api::default_namespaced(client.clone());
let config_maps_api: Api<ConfigMap> = Api::default_namespaced(client.clone());
let stateful_sets_api: Api<StatefulSet> = Api::default_namespaced(client);
let stateful_sets_api: Api<StatefulSet> = Api::default_namespaced(client.clone());
let pods_api: Api<Pod> = Api::default_namespaced(client);

Ok(K8sClient {
secrets_api,
config_maps_api,
stateful_sets_api,
pods_api,
})
}

Expand Down Expand Up @@ -217,6 +220,8 @@ impl K8sClient {
.patch(&stateful_set_name, &pp, &Patch::Apply(stateful_set))
.await?;

self.delete_pod(prefix).await?;

info!("patched stateful set");

Ok(())
Expand All @@ -240,4 +245,23 @@ impl K8sClient {
info!("deleted stateful set");
Ok(())
}

pub async fn delete_pod(&self, prefix: &str) -> Result<(), K8sError> {
info!("deleting pod");
let pod_name = format!("{prefix}-{STATEFUL_SET_NAME_SUFFIX}-0");
let dp = DeleteParams::default();
match self.pods_api.delete(&pod_name, &dp).await {
Ok(_) => {}
Err(e) => match e {
kube::Error::Api(ref er) => {
if er.code != 404 {
return Err(e.into());
}
}
e => return Err(e.into()),
},
}
info!("deleted pod");
Ok(())
}
}

0 comments on commit ba6c2f2

Please sign in to comment.