Crates.io | k8s-controller |
lib.rs | k8s-controller |
version | |
source | src |
created_at | 2023-07-10 16:33:12.80596+00 |
updated_at | 2025-03-04 15:37:35.147244+00 |
description | lightweight framework for writing kubernetes controllers |
homepage | |
repository | https://github.com/MaterializeInc/k8s-controller |
max_upload_size | |
id | 913131 |
Cargo.toml error: | TOML parse error at line 24, column 1 | 24 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This crate implements a lightweight framework around
kube_runtime::Controller
which provides a simpler interface for common controller patterns. To use it,
you define the data that your controller is going to operate over, and
implement the Context
trait on that struct:
#[derive(Default, Clone)]
struct PodCounter {
pods: Arc<Mutex<BTreeSet<String>>>,
}
impl PodCounter {
fn pod_count(&self) -> usize {
let mut pods = self.pods.lock().unwrap();
pods.len()
}
}
#[async_trait::async_trait]
impl k8s_controller::Context for PodCounter {
type Resource = Pod;
type Error = kube::Error;
const FINALIZER_NAME: &'static str = "example.com/pod-counter";
async fn apply(
&self,
client: Client,
pod: &Self::Resource,
) -> Result<Option<Action>, Self::Error> {
let mut pods = self.pods.lock().unwrap();
pods.insert(pod.meta().uid.as_ref().unwrap().clone());
Ok(None)
}
async fn cleanup(
&self,
client: Client,
pod: &Self::Resource,
) -> Result<Option<Action>, Self::Error> {
let mut pods = self.pods.lock().unwrap();
pods.remove(pod.meta().uid.as_ref().unwrap());
Ok(None)
}
}
Then you can run it against your Kubernetes cluster by creating a
Controller
:
let kube_config = Config::infer().await.unwrap();
let kube_client = Client::try_from(kube_config).unwrap();
let context = PodCounter::default();
let controller = k8s_controller::Controller::namespaced_all(
kube_client,
context.clone(),
ListParams::default(),
);
task::spawn(controller.run());
loop {
println!("{} pods running", context.pod_count());
sleep(Duration::from_secs(1));
}