Crate kube_client

Source
Expand description

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_client::api::{Api, ResourceExt, ListParams, PatchParams, Patch};
use kube_client::Client;
use k8s_openapi::api::core::v1::Pod;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Read the environment to find config for kube client.
    // Note that this tries an in-cluster configuration first,
    // then falls back on a kubeconfig file.
    let client = Client::try_default().await?;

    // Interact with pods in the configured namespace with the typed interface from k8s-openapi
    let pods: Api<Pod> = Api::default_namespaced(client);

    // Create a Pod (cheating here with json, but it has to validate against the type):
    let patch: 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",
                },
            ],
        }
    }))?;

    // Apply the Pod via server-side apply
    let params = PatchParams::apply("myapp");
    let result = pods.patch("my-pod", &params, &Patch::Apply(&patch)).await?;

    // List pods in the configured namespace
    for p in pods.list(&ListParams::default()).await? {
        println!("found pod {}", p.name_any());
    }

    Ok(())
}

For more details, see:

  • Client for the extensible Kubernetes client
  • Config for the Kubernetes config abstraction
  • Api for the generic api methods available on Kubernetes resources
  • k8s-openapi for how to create typed kubernetes objects directly

Re-exports§

pub use kube_core as core;

Modules§

apiclient
API helpers for structured interaction with the Kubernetes API
clientclient
API client for interacting with the Kubernetes API
configconfig
Kubernetes configuration objects.
discoveryclient
High-level utilities for runtime API discovery.
errorconfig or client
Error handling and error types

Structs§

Apiclient
The generic Api abstraction
Clientclient
Client for connecting with a Kubernetes cluster.
Configconfig
Configuration object for accessing a Kuernetes cluster
Discoveryclient
A caching client for running API discovery against the Kubernetes API.

Enums§

Errorconfig or client
Possible errors from the Client

Traits§

CustomResourceExt
Extension trait that is implemented by kube-derive
Resource
An accessor trait for a kubernetes Resource.
ResourceExt
Helper methods for resources.

Type Aliases§

Resultconfig or client
Convient alias for Result<T, Error>