Skip to content

Commit

Permalink
Export Version priority parser with Ord impls in kube_core (#764)
Browse files Browse the repository at this point in the history
* Export Version parser with Ord impl in kube_core

doing the cleanup steps described to make this worthy of exporting.
needed for kube-rs/kopium#23

Signed-off-by: clux <[email protected]>

* remove leftover TODO + improve doc links between discovery and core

Signed-off-by: clux <[email protected]>

* import more tests, fix one, better docs

Signed-off-by: clux <[email protected]>

* a bit less Reverse and less double negatives

Signed-off-by: clux <[email protected]>

* Version export - explicitly request ordering (#767)

* Explicitly request which ordering to use

Signed-off-by: Teo Klestrup Röijezon <[email protected]>

* Add `Version::latest` that includes considers v2 > v2beta1 > v1

Signed-off-by: Teo Klestrup Röijezon <[email protected]>

* Rename Version::latest_stable to priority

Signed-off-by: Teo Klestrup Röijezon <[email protected]>

* Also rename LatestStable accordingly

Signed-off-by: Teo Klestrup Röijezon <[email protected]>

* rename latest to semantic for language consistency

we had two orders ::latest and ::priority, but v.latest() was a
ambiguous. v.latest could mean:

- we are sorting by age (but that's not true)
- we are sorting by latest version in semver semantics (true

so think v.semantic() is slightly more clear. v2.semantic() >
v1.semantic() feels a little easier to comprehend than v2.latest() >
v1.latest()

Signed-off-by: clux <[email protected]>

* fix self-references and rename semantic to distance

Signed-off-by: clux <[email protected]>

* rename to generation

Signed-off-by: clux <[email protected]>

* We need to split on byte lengths, not code point counts (#772)

See #764 (comment)

Signed-off-by: Teo Klestrup Röijezon <[email protected]>

* Apply suggestions from code review

Co-authored-by: Teo Klestrup Röijezon <[email protected]>

* Update kube-core/src/version.rs

Co-authored-by: kazk <[email protected]>

Co-authored-by: Teo Klestrup Röijezon <[email protected]>
Co-authored-by: kazk <[email protected]>
  • Loading branch information
3 people authored Dec 27, 2021
1 parent 6dfe170 commit 4017a3f
Show file tree
Hide file tree
Showing 5 changed files with 408 additions and 163 deletions.
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):
/// - 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 the server does not recommend a version, 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)
/// 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

0 comments on commit 4017a3f

Please sign in to comment.