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

feat: Add typed scale argument to derive macro #1656

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

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 {}

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 <[email protected]>
Copy link

codecov bot commented Dec 10, 2024

Codecov Report

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

Project coverage is 76.0%. Comparing base (6a980c6) to head (696d389).

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.9%   76.0%   +0.1%     
=======================================
  Files         84      84             
  Lines       7611    7664     +53     
=======================================
+ Hits        5771    5817     +46     
- Misses      1840    1847      +7     
Files with missing lines Coverage Δ
kube-derive/src/lib.rs 0.0% <ø> (ø)
kube-derive/tests/crd_schema_test.rs 96.9% <ø> (ø)
kube-runtime/src/events.rs 98.1% <100.0%> (ø)
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.

kube-derive/Cargo.toml Outdated Show resolved Hide resolved
kube-derive/src/custom_resource.rs Show resolved Hide resolved
Comment on lines +201 to +202
// - To enable backwards-compatibility. Up to version 0.97.0 it was only possible to set scale
// subresource values as a JSON string.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe there's a way to add a deprecated message to the old way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I know, there is no way to emit warning from within derive macros, only hard-errors, aka compile_error!().

Copy link
Contributor Author

@Techassi Techassi Jan 25, 2025

Choose a reason for hiding this comment

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

I instead opted to add a note to the relevant setion in the doc comment for the derive macro. See a56835e.

@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.

@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.

@Techassi Techassi requested a review from clux January 23, 2025 15:46
@Techassi Techassi force-pushed the feat/derive-typed-scale-arg branch from 0a98def to 7a86117 Compare January 23, 2025 16:14
/// This is implemented for backwards-compatibility. It allows that the scale subresource can
/// be deserialized from a JSON string.
fn from_string(value: &str) -> darling::Result<Self> {
serde_json::from_str(value).map_err(darling::Error::custom)
Copy link
Member

@clux clux Jan 24, 2025

Choose a reason for hiding this comment

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

I see most of the lines for this impl is showing up as untested. I think we can get easy coverage by adding the invocation to kube-derive/tests/crd_schema_test.rs and updating the schema changes in that file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing, I can add some tests to test both the old and the new way.

Copy link
Contributor Author

@Techassi Techassi Jan 25, 2025

Choose a reason for hiding this comment

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

Adding a test proved very valuable! Take a look at 696d389.

The addition also helps with coverage and greatly reduces the amount of missing lines. I have a few ideas how this can be improved even further in the future. I will create a PR for it soon.

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 <[email protected]>
@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
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.

2 participants