https://github.com/materializeinc/k8s-controller
https://github.com/materializeinc/k8s-controller
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/materializeinc/k8s-controller
- Owner: MaterializeInc
- License: apache-2.0
- Created: 2023-07-07T23:12:54.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-09-17T17:06:56.000Z (9 months ago)
- Last Synced: 2025-09-17T19:41:36.649Z (9 months ago)
- Language: Rust
- Size: 43 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# k8s-controller
This crate implements a lightweight framework around
[`kube_runtime::Controller`](https://docs.rs/kube-runtime/latest/kube_runtime/controller/struct.Controller.html)
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:
```rust
#[derive(Default, Clone)]
struct PodCounter {
pods: Arc>>,
}
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, 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, 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`:
```rust
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));
}
```