kube_client

Struct Discovery

Source
pub struct Discovery { /* private fields */ }
Available on crate feature client only.
Expand description

A caching client for running API discovery against the Kubernetes API.

This simplifies the required querying and type matching, and stores the responses for each discovered api group and exposes helpers to access them.

The discovery process varies in complexity depending on:

  • how much you know about the kind(s) and group(s) you are interested in
  • how many groups you are interested in

Discovery can be performed on:

  • all api groups (default)
  • a subset of api groups (by setting Discovery::filter)

To make use of discovered apis, extract one or more ApiGroups from it, or resolve a precise one using Discovery::resolve_gvk.

If caching of results is not required, then a simpler oneshot discovery system can be used.

Implementations§

Source§

impl Discovery

Caching discovery interface

Builds an internal map of its cache

Source

pub fn new(client: Client) -> Self

Construct a caching api discovery client

Source

pub fn filter(self, allow: &[&str]) -> Self

Configure the discovery client to only look for the listed apigroups

Source

pub fn exclude(self, deny: &[&str]) -> Self

Configure the discovery client to look for all apigroups except the listed ones

Source

pub async fn run(self) -> Result<Self>

Runs or re-runs the configured discovery algorithm and updates/populates the cache

The cache is empty cleared when this is started. By default, every api group found is checked, causing N+2 queries to the api server (where N is number of api groups).

use kube::{Client, api::{Api, DynamicObject}, discovery::{Discovery, verbs, Scope}, ResourceExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::try_default().await?;
    let discovery = Discovery::new(client.clone()).run().await?;
    for group in discovery.groups() {
        for (ar, caps) in group.recommended_resources() {
            if !caps.supports_operation(verbs::LIST) {
                continue;
            }
            let api: Api<DynamicObject> = Api::all_with(client.clone(), &ar);
            // can now api.list() to emulate kubectl get all --all
            for obj in api.list(&Default::default()).await? {
                println!("{} {}: {}", ar.api_version, ar.kind, obj.name_any());
            }
        }
    }
    Ok(())
}

See a bigger example in examples/dynamic.api

Source§

impl Discovery

Interface to the Discovery cache

Source

pub fn groups(&self) -> impl Iterator<Item = &ApiGroup>

Returns iterator over all served groups

Source

pub fn groups_alphabetical(&self) -> Vec<&ApiGroup>

Returns a sorted vector of all served groups

This vector is in kubectl’s normal alphabetical group order

Source

pub fn get(&self, group: &str) -> Option<&ApiGroup>

Returns the ApiGroup for a given group if served

Source

pub fn has_group(&self, group: &str) -> bool

Check if a group is served by the apiserver

Source

pub fn resolve_gvk( &self, gvk: &GroupVersionKind, ) -> Option<(ApiResource, ApiCapabilities)>

Finds an ApiResource and its ApiCapabilities after discovery by matching a GVK

This is for quick extraction after having done a complete discovery. If you are only interested in a single kind, consider oneshot::pinned_kind.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
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 T
where 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

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

Source§

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
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T