-
-
Notifications
You must be signed in to change notification settings - Fork 292
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
Support complex enums in CRDs #779
Conversation
See GREsau/schemars#84 Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
Codecov Report
@@ Coverage Diff @@
## master #779 +/- ##
==========================================
+ Coverage 71.89% 72.00% +0.10%
==========================================
Files 54 55 +1
Lines 3700 3725 +25
==========================================
+ Hits 2660 2682 +22
- Misses 1040 1043 +3
Continue to review full report at Codecov.
|
This comment has been minimized.
This comment has been minimized.
cc @cassandracomar @LukeMathWalker @clux @webern who have expressed interest in GREsau/schemars#84 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! And the api-server accepts this? Beautiful.
Left some quick comments/questions.
Yes :) |
Co-authored-by: kazk <kazk.dev@gmail.com> Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
609fde7
to
c46c596
Compare
I just ran a few simple tests as well and it worked like a charm! |
Co-authored-by: Eirik A <sszynrae@gmail.com> Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
aef0969
to
08974cc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't checked the code (yet), but I thought of a couple of edge cases. You may have already tested these.
Does an enum like this work? The Empty
variant would be some kind of empty object with no properties?
enum DoesThisWork {
Empty,
NotEmpty(MyStruct),
}
If we do the same thing with some nesting?
enum InnerEnum {
Nope,
Yep(SomeStruct)
}
enum Outer {
Inner(Inner),
Foo(AnotherStruct),
Nothing,
}
I normally avoid having a variant and struct by the same name (even though that's actually what I want, but my IDE gets confused). But in the above example I thought having the same name for variant and struct makes for a good edge case.
Not directly, no. That would serialize as: # assuming this is wrapped in a field does_this_work
doesThisWork: empty
# or...
doesThisWork:
notEmpty:
# MyStruct contents This violates the rule that all variants must have the same type. Changing enum DoesThisWork {
Empty {},
NotEmpty(MyStruct),
} That causes the serialization to look like: doesThisWork:
empty: {}
# or...
notEmpty:
# MyStruct contents
Same thing here, it should work as long as you change the
Type names are ignored here anyway, only variant names matter. |
I see, thank you for explaining this. So all of the following would work? enum {
A(String),
B(String),
} enum {
A(u8),
B(u8),
} enum {
A(SomeStruct{}),
B(DifferentStruct{}),
C{},
} And this would not work? enum {
A(u8),
B(String),
} |
Yes.
After fixing
Yes it would, this would serialize as: foo:
a: 5
# or...
b: foo Which is valid. I think I explained this poorly, the enum itself must always serialize to the same JSON "type" (bool, number, string, array, object). For externally tagged enums (the default), the result of this is that either all or none of the variants must be unit variants ( Each non-unit variant gets serialized as an object with a unique key, so there is no limit on what type those variants' values can have (apart from the usual constraint that they must also have a |
Motivation
See GREsau/schemars#84
Currently
#[derive(CustomResource)]
generates invalid CRD objects for enum variants with fields, because the generated schema is rejected by K8s as not being structural.Solution
This PR adds a
Visitor
that transparently rewrites enum schemas from:to: