use anyhow::{Context as _, Result, anyhow};
use k8s_openapi::api::core::v1::Pod;
use kube::{Api, Client, api::ListParams};
pub const NAMESPACE: &str = "polychrome";
pub const CONTROL_PLANE_SELECTOR: &str = "app.kubernetes.io/component=control-plane";
pub const AGENT_PORT: u16 = 8080;
pub const FORENSICS_PORT: u16 = 8090;
pub async fn pick_control_plane_pod(client: &Client, namespace: &str) -> Result<String> {
let pods: Api<Pod> = Api::namespaced(client.clone(), namespace);
let list = pods
.list(&ListParams::default().labels(CONTROL_PLANE_SELECTOR))
.await
.context("list control-plane pods")?;
for p in list.items {
let phase = p
.status
.as_ref()
.and_then(|s| s.phase.as_deref())
.unwrap_or("");
if phase == "Running"
&& let Some(name) = p.metadata.name
{
return Ok(name);
}
}
Err(anyhow!(
"no Running control-plane pod in namespace {namespace:?} (selector {CONTROL_PLANE_SELECTOR}); is the cluster up and is your kube context right?"
))
}