1pub use k8s_openapi;
19pub use kube;
20#[cfg(feature = "k8s")]
21pub use schemars;
22
23pub mod client;
24pub mod leader;
25#[cfg(feature = "k8s")]
26pub mod config;
27#[cfg(feature = "k8s")]
28pub mod controller;
29#[cfg(feature = "k8s")]
30pub mod discovery;
31#[cfg(feature = "k8s")]
32pub mod manifests;
33#[cfg(feature = "k8s")]
34pub mod testing;
35#[cfg(feature = "k8s")]
36pub mod webhook;
37
38#[derive(Debug)]
40pub enum Error {
41 Kube(kube::Error),
42 Other(String),
43}
44
45impl std::fmt::Display for Error {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 match self {
48 Error::Kube(e) => write!(f, "{e}"),
49 Error::Other(e) => f.write_str(e),
50 }
51 }
52}
53
54impl std::error::Error for Error {}
55
56impl From<kube::Error> for Error {
57 fn from(e: kube::Error) -> Self {
58 Error::Kube(e)
59 }
60}
61
62impl From<String> for Error {
63 fn from(e: String) -> Self {
64 Error::Other(e)
65 }
66}
67
68impl From<&str> for Error {
69 fn from(e: &str) -> Self {
70 Error::Other(e.to_string())
71 }
72}
73
74pub type Result<T, E = Error> = std::result::Result<T, E>;
75
76pub trait Scope {
80 fn api<K: kube::Resource<Scope = Self, DynamicType = ()>>(client: kube::Client, namespace: Option<&str>) -> kube::Api<K>;
81}
82
83impl Scope for k8s_openapi::NamespaceResourceScope {
84 fn api<K: kube::Resource<Scope = Self, DynamicType = ()>>(client: kube::Client, namespace: Option<&str>) -> kube::Api<K> {
85 match namespace {
86 Some(ns) => kube::Api::namespaced(client, ns),
87 None => kube::Api::all(client),
88 }
89 }
90}
91
92impl Scope for k8s_openapi::ClusterResourceScope {
93 fn api<K: kube::Resource<Scope = Self, DynamicType = ()>>(client: kube::Client, _namespace: Option<&str>) -> kube::Api<K> {
94 kube::Api::all(client)
95 }
96}
97
98pub fn api_for<K>(client: &kube::Client, obj: &K) -> kube::Api<K>
100where
101 K: kube::Resource<DynamicType = ()>,
102 K::Scope: Scope,
103{
104 <K::Scope as Scope>::api::<K>(client.clone(), obj.meta().namespace.as_deref())
105}