kube_client::discovery

Struct ApiGroup

Source
pub struct ApiGroup { /* private fields */ }
Expand description

Describes one API groups collected resources and capabilities.

Each ApiGroup contains all data pinned to a each version. In particular, one data set within the ApiGroup for "apiregistration.k8s.io" is the subset pinned to "v1"; commonly referred to as "apiregistration.k8s.io/v1".

If you know the version of the discovered group, you can fetch it directly:

use kube::{Client, api::{Api, DynamicObject}, discovery, ResourceExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::try_default().await?;
    let apigroup = discovery::group(&client, "apiregistration.k8s.io").await?;
     for (apiresource, caps) in apigroup.versioned_resources("v1") {
         println!("Found ApiResource {}", apiresource.kind);
     }
    Ok(())
}

But if you do not know this information, you can use ApiGroup::preferred_version_or_latest.

Whichever way you choose the end result is something describing a resource and its abilities:

  • Vec<(ApiResource, ApiCapabilities)>` :: for all resources in a versioned ApiGroup
  • (ApiResource, ApiCapabilities) :: for a single kind under a versioned ApiGroud

These two types: ApiResource, and ApiCapabilities should contain the information needed to construct an Api and start querying the kubernetes API. You will likely need to use DynamicObject as the generic type for Api to do this, as well as the ApiResource for the DynamicType for the Resource trait.

use kube::{Client, api::{Api, DynamicObject}, discovery, ResourceExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::try_default().await?;
    let apigroup = discovery::group(&client, "apiregistration.k8s.io").await?;
    let (ar, caps) = apigroup.recommended_kind("APIService").unwrap();
    let api: Api<DynamicObject> = Api::all_with(client.clone(), &ar);
    for service in api.list(&Default::default()).await? {
        println!("Found APIService: {}", service.name_any());
    }
    Ok(())
}

This type represents an abstraction over the native APIGroup to provide easier access to underlying group resources.

§Common Pitfall

Version preference and recommendations shown herein is a group concept, not a resource-wide concept. A common mistake is have different stored versions for resources within a group, and then receive confusing results from this module. Resources in a shared group should share versions - and transition together - to minimize confusion. See https://kubernetes.io/docs/concepts/overview/kubernetes-api/#api-groups-and-versioning for more info.

Implementations§

Source§

impl ApiGroup

Public ApiGroup interface

Source

pub const CORE_GROUP: &'static str = ""

Core group name

Source

pub fn name(&self) -> &str

Returns the name of this group.

Source

pub fn versions(&self) -> impl Iterator<Item = &str>

Returns served versions (e.g. ["v1", "v2beta1"]) of this group.

This Iterator is never empty, and returns elements in descending order of Version:

  • Stable versions (with the last being the first)
  • Beta versions (with the last being the first)
  • Alpha versions (with the last being the first)
  • Other versions, alphabetically
Source

pub fn preferred_version(&self) -> Option<&str>

Returns preferred version for working with given group.

Please note the ApiGroup Common Pitfall.

Source

pub fn preferred_version_or_latest(&self) -> &str

Returns the preferred version or latest version for working with given group.

If the server does not recommend a version, we pick the “most stable and most recent” version in accordance with kubernetes version priority via the descending sort order from Version.

Please note the ApiGroup Common Pitfall.

Source

pub fn versioned_resources( &self, ver: &str, ) -> Vec<(ApiResource, ApiCapabilities)>

Returns the resources in the group at an arbitrary version string.

If the group does not support this version, the returned vector is empty.

If you are looking for the api recommended list of resources, or just on particular kind consider ApiGroup::recommended_resources or ApiGroup::recommended_kind instead.

Source

pub fn recommended_resources(&self) -> Vec<(ApiResource, ApiCapabilities)>

Returns the recommended (preferred or latest) versioned resources in the group

use kube::{Client, api::{Api, DynamicObject}, discovery::{self, verbs}, ResourceExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::try_default().await?;
    let apigroup = discovery::group(&client, "apiregistration.k8s.io").await?;
    for (ar, caps) in apigroup.recommended_resources() {
        if !caps.supports_operation(verbs::LIST) {
            continue;
        }
        let api: Api<DynamicObject> = Api::all_with(client.clone(), &ar);
        for inst in api.list(&Default::default()).await? {
            println!("Found {}: {}", ar.kind, inst.name_any());
        }
    }
    Ok(())
}

This is equivalent to taking the ApiGroup::versioned_resources at the ApiGroup::preferred_version_or_latest.

Please note the ApiGroup Common Pitfall.

Source

pub fn resources_by_stability(&self) -> Vec<(ApiResource, ApiCapabilities)>

Returns all resources in the group at their the most stable respective version

use kube::{Client, api::{Api, DynamicObject}, discovery::{self, verbs}, ResourceExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::try_default().await?;
    let apigroup = discovery::group(&client, "apiregistration.k8s.io").await?;
    for (ar, caps) in apigroup.resources_by_stability() {
        if !caps.supports_operation(verbs::LIST) {
            continue;
        }
        let api: Api<DynamicObject> = Api::all_with(client.clone(), &ar);
        for inst in api.list(&Default::default()).await? {
            println!("Found {}: {}", ar.kind, inst.name_any());
        }
    }
    Ok(())
}

See an example in examples/kubectl.rs

Source

pub fn recommended_kind( &self, kind: &str, ) -> Option<(ApiResource, ApiCapabilities)>

Returns the recommended version of the kind in the recommended resources (if found)

use kube::{Client, api::{Api, DynamicObject}, discovery, ResourceExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::try_default().await?;
    let apigroup = discovery::group(&client, "apiregistration.k8s.io").await?;
    let (ar, caps) = apigroup.recommended_kind("APIService").unwrap();
    let api: Api<DynamicObject> = Api::all_with(client.clone(), &ar);
    for service in api.list(&Default::default()).await? {
        println!("Found APIService: {}", service.name_any());
    }
    Ok(())
}

This is equivalent to filtering the ApiGroup::versioned_resources at ApiGroup::preferred_version_or_latest against a chosen 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

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