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 3 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 4.08163% with 47 lines in your changes missing coverage. Please review.

Project coverage is 75.1%. Comparing base (60d3ebc) to head (a36d0bd).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
kube-derive/src/custom_resource.rs 4.1% 47 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1656     +/-   ##
=======================================
- Coverage   75.6%   75.1%   -0.4%     
=======================================
  Files         82      82             
  Lines       7405    7453     +48     
=======================================
+ Hits        5591    5592      +1     
- Misses      1814    1861     +47     
Files with missing lines Coverage Δ
kube/src/lib.rs 88.5% <ø> (ø)
kube-derive/src/custom_resource.rs 67.2% <4.1%> (-15.2%) ⬇️

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.

@@ -19,6 +19,7 @@ proc-macro2.workspace = true
quote.workspace = true
syn = { workspace = true, features = ["extra-traits"] }
serde_json.workspace = true
k8s-openapi = { workspace = true, features = ["latest"] }
Copy link
Member

Choose a reason for hiding this comment

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

this feels unfortunate. don't really want a derive crate (which is hard to optimise compile wise) to depend on such a big crate.

/// Customize the scale subresource, see [Kubernetes docs][1].
///
/// [1]: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#scale-subresource
scale: Option<Scale>,
Copy link
Member

Choose a reason for hiding this comment

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

if the dependency is just for this struct, then maybe we should inline that for the interface instead. It would also avoid having to do the newtyping you do below.

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?

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

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