use anyhow::{Context, Result, anyhow};
use k8s_openapi::api::core::v1::Pod;
use kube::Client;
use kube::api::{Api, DeleteParams, ListParams};
use crate::scenario::Stack;
pub async fn try_client() -> Result<Client> {
Client::try_default()
.await
.context("build in-cluster kube client")
}
pub async fn kill_first_broker(client: &Client, stack: Stack, namespace: &str) -> Result<String> {
let prefix = stack.broker_pod_regex().trim_start_matches('^');
let pods: Api<Pod> = Api::namespaced(client.clone(), namespace);
let list = pods
.list(&ListParams::default())
.await
.context("list pods in namespace")?;
let mut names: Vec<String> = list
.items
.iter()
.filter_map(|p| p.metadata.name.clone())
.filter(|n| n.starts_with(prefix))
.collect();
names.sort();
let Some(target) = names.first() else {
return Err(anyhow!("no broker pod matched prefix {prefix}"));
};
let dp = DeleteParams::default().grace_period(0);
pods.delete(target, &dp)
.await
.with_context(|| format!("delete pod {target}"))?;
Ok(target.clone())
}