Skip to content

Commit

Permalink
improve recommended_resources
Browse files Browse the repository at this point in the history
Signed-off-by: chengqinglin <chengqinglin@icloud.com>
  • Loading branch information
imuxin committed Sep 29, 2022
1 parent 58e5cdf commit 03e0dbe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions kube-client/Cargo.toml
Expand Up @@ -71,6 +71,7 @@ rand = { version = "0.8.3", optional = true }
secrecy = { version = "0.8.0", features = ["alloc", "serde"] }
tracing = { version = "0.1.29", features = ["log"], optional = true }
hyper-openssl = { version = "0.9.2", optional = true }
itertools = "0.10.5"

[dependencies.k8s-openapi]
version = "0.16.0"
Expand Down
16 changes: 13 additions & 3 deletions kube-client/src/discovery/apigroup.rs
@@ -1,5 +1,6 @@
use super::parse::{self, GroupVersionData};
use crate::{error::DiscoveryError, Client, Error, Result};
use itertools::Itertools;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{APIGroup, APIVersions};
pub use kube_core::discovery::{verbs, ApiCapabilities, ApiResource, Scope};
use kube_core::{
Expand All @@ -8,7 +9,6 @@ use kube_core::{
};
use std::cmp::Reverse;


/// Describes one API groups collected resources and capabilities.
///
/// Each `ApiGroup` contains all data pinned to a each version.
Expand Down Expand Up @@ -240,8 +240,18 @@ impl ApiGroup {
///
/// This is equivalent to taking the [`ApiGroup::versioned_resources`] at the [`ApiGroup::preferred_version_or_latest`].
pub fn recommended_resources(&self) -> Vec<(ApiResource, ApiCapabilities)> {
let ver = self.preferred_version_or_latest();
self.versioned_resources(ver)
self.data
.iter()
.map(|gvd| gvd.resources.clone())
.concat()
.iter()
.into_group_map_by(|(ar, _)| ar.kind.clone())
.into_iter()
.map(|(_, mut v)| {
v.sort_by_cached_key(|(ar, _)| Reverse(Version::parse(ar.version.as_str()).priority()));
v[0].to_owned()
})
.collect()
}

/// Returns the recommended version of the `kind` in the recommended resources (if found)
Expand Down

0 comments on commit 03e0dbe

Please sign in to comment.