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
impl ApiGroup
Public ApiGroup interface
Sourcepub const CORE_GROUP: &'static str = ""
pub const CORE_GROUP: &'static str = ""
Core group name
Sourcepub fn versions(&self) -> impl Iterator<Item = &str>
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
Sourcepub fn preferred_version(&self) -> Option<&str>
pub fn preferred_version(&self) -> Option<&str>
Returns preferred version for working with given group.
Please note the ApiGroup Common Pitfall.
Sourcepub fn preferred_version_or_latest(&self) -> &str
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.
Sourcepub fn versioned_resources(
&self,
ver: &str,
) -> Vec<(ApiResource, ApiCapabilities)>
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.
Sourcepub fn recommended_resources(&self) -> Vec<(ApiResource, ApiCapabilities)>
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.
Sourcepub fn resources_by_stability(&self) -> Vec<(ApiResource, ApiCapabilities)>
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
Sourcepub fn recommended_kind(
&self,
kind: &str,
) -> Option<(ApiResource, ApiCapabilities)>
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§
impl Freeze for ApiGroup
impl RefUnwindSafe for ApiGroup
impl Send for ApiGroup
impl Sync for ApiGroup
impl Unpin for ApiGroup
impl UnwindSafe for ApiGroup
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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