Rust client for Kubernetes API forking ynqa/kubernetes-rust.
This version has more error handling and a Reflector
for easy caching of CRD state. It aims to cater to the more common controller case, but allows you sticking in dependencies like k8s-openapi for accurate struct representations.
See the examples directory for how to watch over resources in a simplistic way.
See controller-rs for a full example with actix.
The main abstraction exposed in this client is Reflector<T, U>
. This is a struct with the internal behaviour for watching kube resources, and updating internal state.
Ideally, you just feed in T
as a Spec
struct and U
as a Status
struct, which can be as complete or incomplete as you like. Here, using the complete structs via k8s-openapi:
rust
use k8s_openapi::api::core::v1::{PodSpec, PodStatus};
let resource = ResourceType::Pods(Some("kube-system".into()));
let rf : Reflector<PodSpec, PodStatus> = Reflector::new(client.clone(), resource.into())?;
then you can poll()
the reflector, and read()
to get the current cached state:
```rust rf.poll()?; // blocks and updates state
// read state and use it:
rf.read()?.intoiter().foreach(|(name, p)| {
println!("Found pod {} ({}) with {:?}",
name,
p.status.phase.unwrap(),
p.spec.containers.into_iter().map(|c| c.name).collect::
The reflector itself is responsible for acquiring the write lock and update the state as long as you call poll()
periodically.
Event handling is also exposed via the reflector at the moment:
rust
let events = rf.events()?;
reconcile(&client, events)?; // pass them on somewhere
you can use the exposed events however you wish:
rust
fn reconcile(c: &APIClient, evs: WatchEvents<PodSpec, PodStatus>) -> Result<(), failure::Error> {
for ev in &evs {
// Use the kube api client here..
match ev {
WatchEvent::Added(o) => {
println!("Handling Added in {}", o.metadata.name);
},
WatchEvent::Modified(o) => {
println!("Handling Modified Pod in {}", o.metadata.name);
},
WatchEvent::Deleted(o) => {
println!("Handling Deleted Pod in {}", o.metadata.name);
},
WatchEvent::Error(e) => {
println!("Error event: {:?}", e); // ought to refresh here
}
}
}
Ok(())
}
Note that once you have called .events()
the events are considered handled, and are removed from the internal state.
Apache 2.0 licensed. See LICENSE for details.