Repository navigation
✨ Add a marker to support field defaulting in crd schema - #323
Conversation
|
Also for arbitrary |
|
we'll need to extend the marker syntax a bit for arbitrary objects, but it should be doable |
|
arrays of primitives or other arrays should be doable today |
|
Ah, nvm. I see, you're just using strings. I think we might want to use structured parsing (you can use the "any" type here) so you can get parsing errors when generating config. |
I've updated the code to validate the default according to the provided schema rather than manually validating based on the field type. This supports all field types and errors will result if the supplied value doesn't match the schema. Is this approach acceptable?
Assuming continuing to use strings is acceptable, what are your thoughts on allowing json-formatted slices ( Edit: I've updated the string parsing to return the full text as a single argument if either |
|
Latest iteration includes preliminary support for json-formatted strings. Working on the integration failure, which is due to a difference in the ordering of a default object's properties between serialized and generated forms. |
|
Ready for review |
| return nil | ||
| } | ||
|
|
||
| // TODO(marun) Defaults should only be generated for v1 crds |
There was a problem hiding this comment.
and rejected or ignored for v1beta1? Tending to the former.
| // A default value will be accepted as a json-formatted string (e.g. `true`, | ||
| // `"Cluster"`, `1.24`, `[1,2]`, `{"policy": "delete"}`) and validated as the | ||
| // type indicated for the field. | ||
| type Default string |
There was a problem hiding this comment.
as we discussed, would type Default struct { interface{} } work?
There was a problem hiding this comment.
That is not a valid golang type definition. It would be necessary to define the empty interface as a named field rather than embedding it, requiring either that the field name be provided (e.g. Default=Value=<value>) or that the parser be special-cased to not require the field name. AnyType parsing currently lacks support for object parsing (e.g. {"foo": "bar"}), and I'm not sure it's worth implementing for defaulting since the raw argument can be trivially json unmarshalled.
Instead, I defined Default as markers.RawArguments ([]byte) and revised marker construction to enable the use of RawType parsing. The previous strategy to detect when a field should be parsed as RawType relied upon comparing the field type to RawArguments. This works for a nested type (e.g. slice element or struct field) since the type can be identical, but a user-defined type fails that check (e.g. reflect.TypeOf(Default) != reflect.TypeOf(RawArguments)).
| if err != nil { | ||
| return err | ||
| } | ||
| unversionedSchema := &apiext.JSONSchemaProps{} |
There was a problem hiding this comment.
the internal types are not meant for public consumption and might become inaccessible to this code base any time in the future. Why do we need that?
There was a problem hiding this comment.
No good reason, just conforming to what was required by the methods I was trying to call.
| validator, _, err := validation.NewSchemaValidator(crValidation) | ||
| if err != nil { | ||
| return err | ||
| } |
There was a problem hiding this comment.
if you really want to validate the value here, you can just unmarshal the schema bytes into a go-openapi spec and call go-openapi's validation directly, without accessing apiextensions-apiserver internals.
There was a problem hiding this comment.
I didn't realize these exported methods were intended to be internal. How do I determine whether any given functionality provided by a dependency is fair game for use?
There was a problem hiding this comment.
sigh I guess the use of unversioned API types would be my first clue.
There was a problem hiding this comment.
They might go away any time. If we finally move internal packages to internal, there won't be a replacement.
| result := validator.Validate(obj) | ||
| if len(result.Errors) > 0 { | ||
| return fmt.Errorf("default fails validation: %v", result.Errors) | ||
| } |
There was a problem hiding this comment.
note that this is by far not complete. This lacks
- the apiextensions/v1 validation
- a check for structurality of the provided schema
- a check that we actually have a v1 CRD
- validation that the value is pruned
- logic to cope with
x-kubernetes-preserve-unknown-fields - logic to handle defaults under ObjectMeta correctly.
There was a problem hiding this comment.
We don't want to and cannot do this validation here.
There was a problem hiding this comment.
I'm going to treat this as an educational opportunity. :)
note that this is by far not complete. This lacks
- the apiextensions/v1 validation
What is this and how is it relevant to default validation?
- a check for structurality of the provided schema
This method is validating a default value, not a schema. Am I missing something?
- a check that we actually have a v1 CRD
As previously discussed, the current architecture of schema generation will likely require removal of generated defaults via post-processing.
- validation that the value is pruned
Is this something I should just ignore then?
- logic to cope with
x-kubernetes-preserve-unknown-fields
Hmm, I was optimistically hoping that the validation method I was calling would be considering this extension. So the reason my test is passing is the lack of a field type and the absence of pruning?
- logic to handle defaults under ObjectMeta correctly.
Special-casing presumably - not all fields can be defaulted?
There was a problem hiding this comment.
I'm going to treat this as an educational opportunity. :)
note that this is by far not complete. This lacks
- the apiextensions/v1 validation
What is this and how is it relevant to default validation?
pkg/apiextensions/validation
It determines whether a default is allowed at all. It forbid certain schema fields although they show up in the schema types. For example we forbid defaults for fields in metadata.
- a check for structurality of the provided schema
This method is validating a default value, not a schema. Am I missing something?
Only structural schemas allow defaults. But I agree: if you do the structural check elsewhere, we can probbaly assume it here.
- a check that we actually have a v1 CRD
As previously discussed, the current architecture of schema generation will likely require removal of generated defaults via post-processing.
- validation that the value is pruned
Is this something I should just ignore then?
I think you can only ignore this requirement with a reasonable amount of code. It is tricky in combination with metadata and x-kubernetes-embedded-object. In other words, we have to be aware that the validation here is incomplete. This can be fine.
- logic to cope with
x-kubernetes-preserve-unknown-fields
Hmm, I was optimistically hoping that the validation method I was calling would be considering this extension. So the reason my test is passing is the lack of a field type and the absence of pruning?
I meant the pruning check here. The validation you call does not check pruning.
- logic to handle defaults under ObjectMeta correctly.
Special-casing presumably - not all fields can be defaulted?
None can in the top-level ObjectMeta.
<-- snip -->
tl/dr: we can call validation via go-openapi here. That's fine and might catch errors from users. But it will be incomplete. I.e. you will be able to define APIs with markers which will result in CRD manifests which are rejected. Be clear about that in a Go comment at the top that this is best-effort.
After accepting that it is incomplete, we have to make sure the validation is correct, i.e. that you don't forbid defaults which would be valid in a CRD. I believe this is the case as we don't drop anything from the CRD schema, but only add more restriction through the x-kubernetes-* fields.
| Category: "CRD validation", | ||
| DetailedHelp: markers.DetailedHelp{ | ||
| Summary: "sets the default value for this field. ", | ||
| Details: " A default value will be accepted as any valid default value (e.g. boolean: `true`, string: `Cluster`, numerical: `1.24`, array: `{1,2}`, object: `{policy: \"delete\"}`) and validated as the type indicated for the field.", |
There was a problem hiding this comment.
Maybe document that this must be pruned and validate against the schema.
There was a problem hiding this comment.
The leading space seems to be common to all generated help. @DirectXMan12 is this a bug?
There was a problem hiding this comment.
Maybe document that this must be pruned and validate against the schema.
Done
|
Ready for review |
DirectXMan12
left a comment
There was a problem hiding this comment.
/lgtm
/approve
Can you update the marker syntax in the kubebulider book's reference section to mention the map syntax?
| // | ||
| // A default value will be accepted as a string and validated as the type | ||
| // indicated for the field. | ||
| type Default string |
There was a problem hiding this comment.
might want to make this any so we get parsing
There was a problem hiding this comment.
I'm guessing this is an old comment?
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: DirectXMan12, marun The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Will do! |
| must(markers.MakeDefinition("nullable", markers.DescribesField, Nullable{})). | ||
| WithHelp(Nullable{}.Help()), | ||
|
|
||
| must(markers.MakeAnyTypeDefinition("kubebuilder:validation:Default", markers.DescribesField, Default{})). |
There was a problem hiding this comment.
@marun before we release a release with this, do we want :validation in the middle, since this isn't actually validation (even though it's in the same place as validation)? Not sure if that's confusing
NOTE: This PR is not ready for review until #329 merges.
Kube 1.16 adds beta support for providing default values for fields in a CRD schema. This PR adds a marker that supports specifying defaults for all supported types.
cc: @sttts