Crate for interacting with the Kubernetes API
This crate includes the tools for manipulating Kubernetes resources as
well as keeping track of those resources as they change over time
Example
The following example will crate a [Pod
][k8s_openapi::api::core::v1::Pod]
and then watch for it to become available
use futures::{StreamExt, TryStreamExt};
use kube::api::{Api, ListParams, PostParams, WatchEvent};
use kube::Client;
use kube::runtime::Informer;
use k8s_openapi::api::core::v1::Pod;
#[tokio::main]
async fn main() -> Result<(), kube::Error> {
let kube_client = Client::try_default().await?;
let pods: Api<Pod> = Api::namespaced(kube_client, "default");
let pod = serde_json::from_value(serde_json::json!({
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "my-pod"
},
"spec": {
"containers": [
{
"name": "my-container",
"image": "myregistry.azurecr.io/hello-world:v1",
},
],
}
}))?;
let pod = pods.create(&PostParams::default(), &pod).await?;
let informer = Informer::new(pods).params(
ListParams::default()
.fields("metadata.name=my-container")
.timeout(10),
);
let mut events_stream = informer.poll().await?.boxed();
while let Some(event) = events_stream.try_next().await? {
match event {
WatchEvent::Modified(e) if e.status.as_ref().unwrap().phase.as_ref().unwrap() == "Running" => {
println!("It's running!");
}
WatchEvent::Error(e) => {
panic!("WatchEvent error: {:?}", e);
}
_ => {}
}
}
Ok(())
}