Skip to content

✨ Add a marker to support field defaulting in crd schema - #323

Merged
k8s-ci-robot merged 3 commits into
kubernetes-sigs:masterfrom
marun:crd-schema-defaulting
Sep 30, 2019
Merged

k8s-ci-robot merged 3 commits into
kubernetes-sigs:masterfrom
marun:crd-schema-defaulting

Conversation

@marun

@marun marun commented Sep 20, 2019 •

Copy link
Copy Markdown
Contributor

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

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Sep 20, 2019
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Sep 20, 2019
@sttts

sttts commented Sep 20, 2019

Copy link
Copy Markdown
Contributor

Also for arbitrary x-kubernetes-preserve-unknown-fields fields.

@DirectXMan12

Copy link
Copy Markdown
Contributor

we'll need to extend the marker syntax a bit for arbitrary objects, but it should be doable

@DirectXMan12

DirectXMan12 commented Sep 20, 2019 •

Copy link
Copy Markdown
Contributor

arrays of primitives or other arrays should be doable today

@DirectXMan12

Copy link
Copy Markdown
Contributor

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.

@marun

marun commented Sep 21, 2019 •

Copy link
Copy Markdown
Contributor Author

@DirectXMan12

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?

we'll need to extend the marker syntax a bit for arbitrary objects, but it should be doable

Assuming continuing to use strings is acceptable, what are your thoughts on allowing json-formatted slices ([1,2]) and objects ({"foo": 1, "bar": 2}) to be specified? This would require allowing commas to appear in strings when enclosed in square or curly braces instead of having a comma appearing outside a quoted string be treated as an argument separator.

Edit: I've updated the string parsing to return the full text as a single argument if either [ or { are the first characters. Thoughts?

@marun

marun commented Sep 23, 2019 •

Copy link
Copy Markdown
Contributor Author

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.

@marun marun changed the title WIP ✨ Add a marker to support field defaulting in crd schema ✨ Add a marker to support field defaulting in crd schema Sep 23, 2019
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 23, 2019
@marun

marun commented Sep 23, 2019

Copy link
Copy Markdown
Contributor Author

Ready for review

@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 24, 2019
Comment thread pkg/crd/markers/validation.go Outdated
return nil
}

// TODO(marun) Defaults should only be generated for v1 crds

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

and rejected or ignored for v1beta1? Tending to the former.

Comment thread pkg/crd/markers/validation.go Outdated
// 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as we discussed, would type Default struct { interface{} } work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Comment thread pkg/crd/markers/validation.go Outdated
if err != nil {
return err
}
unversionedSchema := &apiext.JSONSchemaProps{}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No good reason, just conforming to what was required by the methods I was trying to call.

Comment thread pkg/crd/markers/validation.go Outdated
validator, _, err := validation.NewSchemaValidator(crValidation)
if err != nil {
return err
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sigh I guess the use of unversioned API types would be my first clue.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

They might go away any time. If we finally move internal packages to internal, there won't be a replacement.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am fine with this now.

Comment thread pkg/crd/markers/validation.go Outdated
result := validator.Validate(obj)
if len(result.Errors) > 0 {
return fmt.Errorf("default fails validation: %v", result.Errors)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don't want to and cannot do this validation here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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?

@sttts sttts Sep 26, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Sep 27, 2019
@marun marun changed the title ✨ Add a marker to support field defaulting in crd schema WIP ✨ Add a marker to support field defaulting in crd schema Sep 27, 2019
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 27, 2019
@marun

marun commented Sep 27, 2019

Copy link
Copy Markdown
Contributor Author

@sttts Rebased on top of #329, which required a change in format for how defaults are specified in the marker (no longer strict json). I'm working on validating defaults with the schema post-flattening rather than at time of application.

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.",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

leading space?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe document that this must be pruned and validate against the schema.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The leading space seems to be common to all generated help. @DirectXMan12 is this a bug?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe document that this must be pruned and validate against the schema.

Done

@marun marun changed the title WIP ✨ Add a marker to support field defaulting in crd schema ✨ Add a marker to support field defaulting in crd schema Sep 30, 2019
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 30, 2019
@marun

marun commented Sep 30, 2019

Copy link
Copy Markdown
Contributor Author

Ready for review

@DirectXMan12 DirectXMan12 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

/lgtm
/approve

Can you update the marker syntax in the kubebulider book's reference section to mention the map syntax?

Comment thread pkg/crd/markers/validation.go Outdated
//
// A default value will be accepted as a string and validated as the type
// indicated for the field.
type Default string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

might want to make this any so we get parsing

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm guessing this is an old comment?

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 30, 2019
@k8s-ci-robot

Copy link
Copy Markdown
Contributor

[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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 30, 2019
@k8s-ci-robot
k8s-ci-robot merged commit 4752ed2 into kubernetes-sigs:master Sep 30, 2019
@marun

marun commented Sep 30, 2019

Copy link
Copy Markdown
Contributor Author

Can you update the marker syntax in the kubebulider book's reference section to mention the map syntax?

Will do!

@marun
marun deleted the crd-schema-defaulting branch September 30, 2019 22:26
must(markers.MakeDefinition("nullable", markers.DescribesField, Nullable{})).
WithHelp(Nullable{}.Help()),

must(markers.MakeAnyTypeDefinition("kubebuilder:validation:Default", markers.DescribesField, Default{})).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll remove.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

PR: #337

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants