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

Conversation

clux
Copy link
Member

@clux clux commented Dec 21, 2021

needed for kube-rs/kopium#23

This does the self-comment cleanup steps described to make this worthy of exporting:

  • reverse Ord impl so we have semantic coherence (stable 2 > stable 1)
  • reverse sort usage internally to compensate (ascending version order default, matches rust conventions)

Also adds a small doctest for the thing, and an infallible FromStr impl (which may end up being useful to someone - maybe).

@clux clux added the changelog-add changelog added category for prs label Dec 21, 2021
@@ -137,7 +207,7 @@ mod tests {
Version::Stable(2),
Version::Beta(2, Some(3)),
];
vers.sort();
vers.sort_by_cached_key(|x| Reverse(x.clone()));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only semi-annoying consequence of the reversal here.
A clone is forced on us from rust-lang/rust#34162 unless we use the manual cmp calll reversed ourselves.

I think this is ok, the usage in kube_client does not need to clone because the Version parsing IS the temporary step, and the test here is probably less realistic of a use-case)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ord has a blanket impl that &T: Ord where T: Ord, so I think this should line up correctly. Or do the lifetimes not match properly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

End up getting this :/

error: lifetime may not live long enough
   --> kube-core/src/version.rs:208:37
    |
208 |         vers.sort_by_cached_key(|x| Reverse(x));
    |                                  -- ^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
    |                                  ||
    |                                  |return type of closure is Reverse<&'2 version::Version>
    |                                  has type `&'1 version::Version`

@codecov-commenter

This comment has been minimized.

Alpha(u32, Option<u32>),
// CRDs and APIServices can use arbitrary strings as versions.
/// An non-conformant api string (sorted lexicographically)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really new, but this might be worth reconsidering before stabilizing. Keeping this "everything else" case means that anyone relying on it will silently be broken if we add new primary cases.

Instead, we might want to turn Nonconformant into the error case on <Version as FromStr>?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Version Priority any non-conformant value is legal. I may want to set my CRD version to testing and expect it to work with k8s. So failing the parser on that will make it unusable from kube

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear: I am not saying that a non-conformant version is illegal, or that we should reject working with it. Rather, that we should treat it as an opaque String in general, parse it into a Version when needed (such as for comparisons), and accept that that parsing may fail (with reasonable fallback behaviour, as described in the PR body).

kube-core/src/version.rs Outdated Show resolved Hide resolved
kube-core/src/version.rs Outdated Show resolved Hide resolved
@@ -137,7 +207,7 @@ mod tests {
Version::Stable(2),
Version::Beta(2, Some(3)),
];
vers.sort();
vers.sort_by_cached_key(|x| Reverse(x.clone()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ord has a blanket impl that &T: Ord where T: Ord, so I think this should line up correctly. Or do the lifetimes not match properly?

kube-core/src/version.rs Outdated Show resolved Hide resolved
kube-core/src/version.rs Outdated Show resolved Hide resolved
doing the cleanup steps described to make this worthy of exporting.
needed for kube-rs/kopium#23

Signed-off-by: clux <sszynrae@gmail.com>
Signed-off-by: clux <sszynrae@gmail.com>
Signed-off-by: clux <sszynrae@gmail.com>
nightkr and others added 3 commits December 24, 2021 09:04
* Explicitly request which ordering to use

Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>

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

Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>

* Rename Version::latest_stable to priority

Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>

* Also rename LatestStable accordingly

Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
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 <sszynrae@gmail.com>
kube-core/src/version.rs Outdated Show resolved Hide resolved
kube-core/src/version.rs Outdated Show resolved Hide resolved
kube-core/src/version.rs Outdated Show resolved Hide resolved
Signed-off-by: clux <sszynrae@gmail.com>
Signed-off-by: clux <sszynrae@gmail.com>
@clux clux changed the title Export Version priority parser with Ord impl in kube_core Export Version priority parser with Ord impls in kube_core Dec 26, 2021
Copy link
Member

@nightkr nightkr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a few minor nits remaining

kube-client/src/discovery/apigroup.rs Outdated Show resolved Hide resolved
kube-client/src/discovery/apigroup.rs Outdated Show resolved Hide resolved
kube-core/src/version.rs Outdated Show resolved Hide resolved
nightkr added a commit to nightkr/kube-rs that referenced this pull request Dec 26, 2021
nightkr added a commit to nightkr/kube-rs that referenced this pull request Dec 26, 2021
See kube-rs#764 (comment)

Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
nightkr and others added 2 commits December 26, 2021 17:09
See #764 (comment)

Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
Co-authored-by: Teo Klestrup Röijezon <teo.roijezon@stackable.de>
@clux
Copy link
Member Author

clux commented Dec 26, 2021

Thanks for all the diligent review here 👍

Happy to merge at this point.

As a funny side-note about this naming: Nonconformant("Z").generation() is now a thing we can ask for.

kube-core/src/version.rs Outdated Show resolved Hide resolved
Co-authored-by: kazk <kazk.dev@gmail.com>
@clux
Copy link
Member Author

clux commented Dec 27, 2021

CI instability from github's side this morning. Either it's the cargo deny locking up in a docker run, or k3d failing to start the cluster, so guessing there are some temporary docker issues on some actions runners. In either case everything here has passed checks. Merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-add changelog added category for prs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants