Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export Version priority parser with Ord impls in kube_core #764

Merged
merged 13 commits into from
Dec 27, 2021
Merged
22 changes: 11 additions & 11 deletions kube-client/src/discovery/apigroup.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::{
parse::{self, GroupVersionData},
version::Version,
};
use super::parse::{self, GroupVersionData};
use crate::{error::DiscoveryError, Client, Error, Result};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{APIGroup, APIVersions};
pub use kube_core::discovery::{verbs, ApiCapabilities, ApiResource, Scope};
use kube_core::gvk::{GroupVersion, GroupVersionKind, ParseGroupVersionError};
use kube_core::{
gvk::{GroupVersion, GroupVersionKind, ParseGroupVersionError},
Version,
};
use std::cmp::Reverse;


/// Describes one API groups collected resources and capabilities.
Expand Down Expand Up @@ -117,7 +118,7 @@ impl ApiGroup {

fn sort_versions(&mut self) {
self.data
.sort_by_cached_key(|gvd| Version::parse(gvd.version.as_str()))
.sort_by_cached_key(|gvd| Reverse(Version::parse(gvd.version.as_str()).priority()))
}

// shortcut method to give cheapest return for a single GVK
Expand Down Expand Up @@ -176,13 +177,11 @@ impl ApiGroup {

/// Returns served versions (e.g. `["v1", "v2beta1"]`) of this group.
///
/// This list is always non-empty, and sorted in the following order:
/// This iterator is never empty, and returns elements in descending order of [`Version`](kube_core::Version):
clux marked this conversation as resolved.
Show resolved Hide resolved
/// - 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
///
/// in accordance with [kubernetes version priority](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#version-priority).
pub fn versions(&self) -> impl Iterator<Item = &str> {
self.data.as_slice().iter().map(|gvd| gvd.version.as_str())
}
Expand All @@ -194,8 +193,9 @@ impl ApiGroup {

/// Returns the preferred version or latest version for working with given group.
///
/// If server does not recommend one, we pick the "most stable and most recent" version
/// in accordance with [kubernetes version priority](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#version-priority).
/// If server does not recommend a version, we pick the "most stable and most recent" version
clux marked this conversation as resolved.
Show resolved Hide resolved
/// in accordance with [kubernetes version priority](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#version-priority)
/// via the descending sort order from [`Version`](kube_core::Version).
pub fn preferred_version_or_latest(&self) -> &str {
// NB: self.versions is non-empty by construction in ApiGroup
self.preferred
Expand Down
2 changes: 0 additions & 2 deletions kube-client/src/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ mod apigroup;
pub mod oneshot;
pub use apigroup::ApiGroup;
mod parse;
// an implementation of mentioned kubernetes version priority
mod version;

// re-export one-shots
pub use oneshot::{group, pinned_group, pinned_kind};
Expand Down
150 changes: 0 additions & 150 deletions kube-client/src/discovery/version.rs

This file was deleted.

3 changes: 3 additions & 0 deletions kube-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ pub use watch::WatchEvent;

mod error;
pub use error::ErrorResponse;

mod version;
pub use version::Version;
Loading