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 create a Pod
and then watch for it to become available using a manual [Api::watch
] call.
use futures::{StreamExt, TryStreamExt};
use kube::api::{Api, ResourceExt, ListParams, PostParams, WatchEvent};
use kube::Client;
use k8s_openapi::api::core::v1::Pod;
#[tokio::main]
async fn main() -> Result<(), kube::Error> {
let client = Client::try_default().await?;
let pods: Api<Pod> = Api::namespaced(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 lp = ListParams::default()
.fields(&format!("metadata.name={}", "my-pod"))
.timeout(10);
let mut stream = pods.watch(&lp, "0").await?.boxed();
while let Some(status) = stream.try_next().await? {
match status {
WatchEvent::Added(o) => println!("Added {}", o.name()),
WatchEvent::Modified(o) => {
let s = o.status.as_ref().expect("status exists on pod");
let phase = s.phase.clone().unwrap_or_default();
println!("Modified: {} with phase: {}", o.name(), phase);
}
WatchEvent::Deleted(o) => println!("Deleted {}", o.name()),
WatchEvent::Error(e) => println!("Error {}", e),
_ => {}
}
}
Ok(())
}