Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
extern crate serde_derive;
use kube::{
api::{Informer, Object, RawApi, Void, WatchEvent},
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Book {
pub title: String,
pub authors: Option<Vec<String>>,
// This is a convenience alias that describes the object we get from Kubernetes
type KubeBook = Object<Book, Void>;
fn main() {
// Load the kubeconfig file.
let kubeconfig = config::load_kube_config().expect("kubeconfig failed to load");
// Create a new client
let client = APIClient::new(kubeconfig);
// Set a namespace. We're just hard-coding for now.
let namespace = "default";
// Describe the CRD we're working with.
// This is basically the fields from our CRD definition.
let resource = RawApi::customResource("books")
// Create our informer and start listening.
let informer = Informer::raw(client, resource)
.expect("informer init failed");
loop {
informer.poll().expect("informer poll failed");
// Now we just do something each time a new book event is triggered.
while let Some(event) = informer.pop() {
fn handle(event: WatchEvent<KubeBook>) {
// This will receive events each time something
match event {
WatchEvent::Added(book) => println!(
"Added a book {} with title '{}'",, book.spec.title
WatchEvent::Deleted(book) => println!("Deleted a book {}",,
_ => println!("another event"),