Struct kube::discovery::ApiGroup

source ·
pub struct ApiGroup { /* private fields */ }
Available on crate feature client only.
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

This impl block contains no items.

Internal queriers to convert from an APIGroup (or APIVersions for core) to our ApiGroup

These queriers ignore groups with empty versions. This ensures that ApiGroup::preferred_version_or_latest always have an answer. On construction, they also sort the internal vec of GroupVersionData according to Version.

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

§

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