Struct kube_client::Discovery

source ·
pub struct Discovery { /* private fields */ }
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> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.
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