Struct kube::Client

source ·
pub struct Client { /* private fields */ }
Available on crate feature client only.
Expand description

Client for connecting with a Kubernetes cluster.

The easiest way to instantiate the client is either by inferring the configuration from the environment using Client::try_default or with an existing Config using Client::try_from.

Implementations§

source§

impl Client

source

pub fn new<S, B, T>(service: S, default_namespace: T) -> Clientwhere S: Service<Request<Body>, Response = Response<B>> + Send + 'static, <S as Service<Request<Body>>>::Future: Send + 'static, <S as Service<Request<Body>>>::Error: Into<Box<dyn Error + Send + Sync, Global>>, B: Body<Data = Bytes> + Send + 'static, <B as Body>::Error: Into<Box<dyn Error + Send + Sync, Global>>, T: Into<String>,

Create a Client using a custom Service stack.

ConfigExt provides extensions for building a custom stack.

To create with the default stack with a Config, use Client::try_from.

To create with the default stack with an inferred Config, use Client::try_default.

Example
use kube::{client::ConfigExt, Client, Config};
use tower::ServiceBuilder;

let config = Config::infer().await?;
let service = ServiceBuilder::new()
    .layer(config.base_uri_layer())
    .option_layer(config.auth_layer()?)
    .service(hyper::Client::new());
let client = Client::new(service, config.default_namespace);
source

pub async fn try_default() -> impl Future<Output = Result<Client, Error>>

Create and initialize a Client using the inferred configuration.

Will use Config::infer which attempts to load the local kubeconfig first, and then if that fails, trying the in-cluster environment variables.

Will fail if neither configuration could be loaded.

If you already have a Config then use Client::try_from instead.

source

pub fn default_namespace(&self) -> &str

Get the default namespace for the client

The namespace is either configured on context in the kubeconfig, falls back to default when running locally, or uses the service account’s namespace when deployed in-cluster.

source

pub async fn send( &self, request: Request<Body> ) -> impl Future<Output = Result<Response<Body>, Error>>

Perform a raw HTTP request against the API and return the raw response back. This method can be used to get raw access to the API which may be used to, for example, create a proxy server or application-level gateway between localhost and the API server.

source

pub async fn connect( &self, request: Request<Vec<u8, Global>> ) -> impl Future<Output = Result<WebSocketStream<Upgraded>, Error>>

Make WebSocket connection.

source

pub async fn request<T>( &self, request: Request<Vec<u8, Global>> ) -> impl Future<Output = Result<T, Error>>where T: DeserializeOwned,

Perform a raw HTTP request against the API and deserialize the response as JSON to some known type.

source

pub async fn request_text( &self, request: Request<Vec<u8, Global>> ) -> impl Future<Output = Result<String, Error>>

Perform a raw HTTP request against the API and get back the response as a string

source

pub async fn request_stream( &self, request: Request<Vec<u8, Global>> ) -> impl Future<Output = Result<impl AsyncBufRead, Error>>

Perform a raw HTTP request against the API and stream the response body.

The response can be processed using AsyncReadExt and AsyncBufReadExt.

source

pub async fn request_status<T>( &self, request: Request<Vec<u8, Global>> ) -> impl Future<Output = Result<Either<T, Status>, Error>>where T: DeserializeOwned,

Perform a raw HTTP request against the API and get back either an object deserialized as JSON or a Status Object.

source

pub async fn request_events<T>( &self, request: Request<Vec<u8, Global>> ) -> impl Future<Output = Result<impl TryStream, Error>>where T: Clone + DeserializeOwned,

Perform a raw request and get back a stream of WatchEvent objects

source§

impl Client

Low level discovery methods using k8s_openapi types.

Consider using the discovery module for easier-to-use variants of this functionality. The following methods might be deprecated to avoid confusion between similarly named types within discovery.

source

pub async fn apiserver_version( &self ) -> impl Future<Output = Result<Info, Error>>

Returns apiserver version.

source

pub async fn list_api_groups( &self ) -> impl Future<Output = Result<APIGroupList, Error>>

Lists api groups that apiserver serves.

source

pub async fn list_api_group_resources( &self, apiversion: &str ) -> impl Future<Output = Result<APIResourceList, Error>>

Lists resources served in given API group.

Example usage:
let apigroups = client.list_api_groups().await?;
for g in apigroups.groups {
    let ver = g
        .preferred_version
        .as_ref()
        .or_else(|| g.versions.first())
        .expect("preferred or versions exists");
    let apis = client.list_api_group_resources(&ver.group_version).await?;
    dbg!(apis);
}
source

pub async fn list_core_api_versions( &self ) -> impl Future<Output = Result<APIVersions, Error>>

Lists versions of core a.k.a. "" legacy API group.

source

pub async fn list_core_api_resources( &self, version: &str ) -> impl Future<Output = Result<APIResourceList, Error>>

Lists resources served in particular core group version.

Trait Implementations§

source§

impl Clone for Client

source§

fn clone(&self) -> Client

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<K> From<Api<K>> for Client

source§

fn from(api: Api<K>) -> Client

Converts to this type from the input type.
source§

impl TryFrom<Config> for Client

source§

fn try_from(config: Config) -> Result<Client, Error>

Builds a default Client from a Config, see ClientBuilder if more customization is required

§

type Error = Error

The type returned in the event of a conversion error.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for Twhere T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more