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

Add typed scale argument to derive macro #1656

Merged
merged 12 commits into from
Feb 19, 2025

Conversation

Techassi
Copy link
Contributor

@Techassi Techassi commented Dec 2, 2024

Motivation

Customizing the scale subresource via #[kube(scale = r#"{}"#)] can feel very out-of-place and requires writing a raw JSON string. In addition, this can lead to a very long line, eg:

#[kube(
    version = "v1alpha1",
    kind = "MyScaler",
    namespaced,
    scale = r#"{"specReplicasPath": ".spec.replicas", "statusReplicaPath": ".status.replicas", "labelSelectorPath": ".spec.labelSelector"}"#
)]
struct ScalerSpec {}

Solution

This allows customizing the scale subresource by providing key-value items instead of a raw JSON string. For backwards-compatibility, it is still supported to provide a JSON string. However, all examples and tests were converted to the new format.

#[kube(
    version = "v1alpha1",
    kind = "MyScaler",
    namespaced,
    scale(
        specReplicasPath = ".spec.replicas",
        statusReplicaPath = ".status.replicas",
        labelSelectorPath = ".spec.labelSelector"
   )
)]
struct ScalerSpec {}

Verified

This commit was signed with the committer’s verified signature.
Techassi Techassi
This allows cutomizing the scale subresource by providing key-value
items instead of a raw JSON string. For backwards-compatibility, it
is still supported to provide a JSON string. However, all examples
and tests were converted to the new format.

Signed-off-by: Techassi <git@techassi.dev>

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Copy link

codecov bot commented Dec 10, 2024

Codecov Report

Attention: Patch coverage is 75.92593% with 13 lines in your changes missing coverage. Please review.

Project coverage is 75.9%. Comparing base (07b7891) to head (57e9c4e).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
kube-derive/src/custom_resource.rs 76.0% 13 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1656     +/-   ##
=======================================
+ Coverage   75.8%   75.9%   +0.1%     
=======================================
  Files         84      84             
  Lines       7626    7679     +53     
=======================================
+ Hits        5776    5822     +46     
- Misses      1850    1857      +7     
Files with missing lines Coverage Δ
kube-derive/src/lib.rs 0.0% <ø> (ø)
kube-derive/tests/crd_schema_test.rs 96.9% <ø> (ø)
kube/src/lib.rs 88.5% <ø> (ø)
kube-derive/src/custom_resource.rs 84.1% <76.0%> (+0.7%) ⬆️

Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

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

Hey there. Thanks for this! Apologise for the delay.

I think this makes sense, given you've made it non-breaking, but ideally the dependency on the openapi crate should be severed if it's just for the interface. Have added a few suggestive comments.

@clux clux added the changelog-add changelog added category for prs label Dec 12, 2024
@Techassi
Copy link
Contributor Author

Apologise for the delay.

No worries. I will shortly take a look at your comments. I will also see if we can get rid of the k8s_openapi dependency.

Verified

This commit was signed with the committer’s verified signature.
Techassi Techassi
Signed-off-by: Techassi <git@techassi.dev>

Verified

This commit was signed with the committer’s verified signature.
Techassi Techassi
@Techassi
Copy link
Contributor Author

Sorry for the delayed work on this, but I think I have addressed both of your biggest concerns in d93d708.

Verified

This commit was signed with the committer’s verified signature.
Techassi Techassi
Signed-off-by: Techassi <git@techassi.dev>

Verified

This commit was signed with the committer’s verified signature.
Techassi Techassi
Signed-off-by: Techassi <git@techassi.dev>
@Techassi Techassi requested a review from clux January 23, 2025 15:46

Verified

This commit was signed with the committer’s verified signature.
Techassi Techassi
Signed-off-by: Techassi <git@techassi.dev>

Verified

This commit was signed with the committer’s verified signature.
Techassi Techassi
Signed-off-by: Techassi <git@techassi.dev>

Verified

This commit was signed with the committer’s verified signature.
Techassi Techassi
Signed-off-by: Techassi <git@techassi.dev>
@Techassi Techassi force-pushed the feat/derive-typed-scale-arg branch from 0a98def to 7a86117 Compare January 23, 2025 16:14

Verified

This commit was signed with the committer’s verified signature.
Techassi Techassi
Adding this test proved to be very valuable because the FromMeta
implemenetation had a few errors and resulted in different panic
messages coming from the derive macro.

I also added a small note to the #[kube(scale(...))] section stating
that the scale subresource can only be used when the status subresource
is used as well. I plan to further improve the validation in a future
pull request.

Signed-off-by: Techassi <git@techassi.dev>
@Techassi Techassi force-pushed the feat/derive-typed-scale-arg branch from cccc052 to 696d389 Compare January 25, 2025 12:53
@Techassi Techassi requested a review from clux January 25, 2025 12:58

Verified

This commit was signed with the committer’s verified signature.
Techassi Techassi
@Techassi
Copy link
Contributor Author

I think this PR is ready. Any objections merging this @clux?

Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

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

Any objections merging this

Ahh, sorry, I forgot about this. Had a busy month. Yes, this looks sensible. I had a quick re-look over, and one nit, maybe. See if you agree. Otherwise it's all good. Happy for you to iteate more if you have other ideas.

I think the thing that made this go to the back of my mind for a while was I wasn't sure about the complexity tradeoff here about supporting two modes if we had no good way to deprecate the old one. But I suspect we can gradually phase out the old raw string stuff and simplify this down the line anyway.

@clux clux added this to the 0.99.0 milestone Feb 19, 2025
@Techassi
Copy link
Contributor Author

Techassi commented Feb 19, 2025

I wasn't sure about the complexity tradeoff here about supporting two modes if we had no good way to deprecate the old one. But I suspect we can gradually phase out the old raw string stuff and simplify this down the line anyway.

Yes, that is indeed a tradeoff. Luckily, we can get rid of most of the added code once we deprecate the old way:

  • We won't need the from_string implementation, because we don't need to parse the JSON string anymore.
  • We can drop the custom from_list implementation and just use #[derive(FromMeta)] on Scale.

@Techassi
Copy link
Contributor Author

Let me know if this answer is satisfactory: #1656 (comment). If so, we can move forward and merge.

@clux
Copy link
Member

clux commented Feb 19, 2025

All good. Ill merge. Thank you very much!

@clux clux merged commit c191439 into kube-rs:main Feb 19, 2025
17 checks passed
@Techassi
Copy link
Contributor Author

Alright, cool. Thank you as well!

@Techassi Techassi deleted the feat/derive-typed-scale-arg branch February 19, 2025 15:03
@clux clux changed the title feat: Add typed scale argument to derive macro Add typed scale argument to derive macro Mar 12, 2025
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.

None yet

2 participants