#![cfg_attr(not(doctest), doc = include_str!("../README.md"))]
#![cfg_attr(docsrs, feature(doc_cfg))]
pub use k8s_openapi;
pub use kube;
pub mod discover;
pub mod dynamic;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::APIResource;
use kube::config::Kubeconfig;
pub fn determine_context(context: &Option<String>) -> anyhow::Result<String> {
match context {
Some(context) => Ok(context.to_string()),
_ => {
let kubeconfig = Kubeconfig::read()?;
Ok(kubeconfig
.current_context
.ok_or_else(|| anyhow::anyhow!("current_context is not set"))?)
}
}
}
pub fn determine_namespace(namespace: Option<String>, context: &str) -> String {
if let Some(ns) = namespace {
return ns;
}
match Kubeconfig::read() {
Ok(kubeconfig) => kubeconfig
.contexts
.iter()
.find(|c| Some(c.name.as_str()) == Some(context))
.and_then(|context| {
context
.context
.as_ref()
.and_then(|ctx| ctx.namespace.clone())
})
.unwrap_or_else(|| String::from("default")),
Err(_) => String::from("default"),
}
}
pub fn find_resource(target: &str, api_resources: &[APIResource]) -> Option<APIResource> {
api_resources
.iter()
.find(|api_resource| match_resource(target, api_resource))
.cloned()
}
pub fn match_resource(target: &str, api_resource: &APIResource) -> bool {
api_resource.name == target
|| api_resource.singular_name == target
|| api_resource
.short_names
.as_ref()
.is_some_and(|short_names| short_names.contains(&target.to_string()))
|| api_resource
.group
.as_ref()
.is_some_and(|group| format!("{}.{}", api_resource.name, group) == target)
}