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

✨ Add crd:validation:Schemaless marker #528

Merged
merged 2 commits into from
Jan 29, 2021

Conversation

maxsmythe
Copy link
Contributor

@maxsmythe maxsmythe commented Nov 26, 2020

This marker will avoid trying to do any type detection on any struct field on which it is set. This gives users a safety valve when they hit an edge case where type inference does the wrong thing for them.

This fixes #291, which was recently re-broken by fixing #502

Signed-off-by: Max Smythe smythe@google.com

@k8s-ci-robot k8s-ci-robot added do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Nov 26, 2020
@k8s-ci-robot
Copy link
Contributor

Welcome @maxsmythe!

It looks like this is your first PR to kubernetes-sigs/controller-tools 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-tools has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 26, 2020
This marker will avoid trying to do any type detection
on any struct field on which it is set. This gives
users a safety valve when they hit an edge case where
type inference does the wrong thing for them.

Signed-off-by: Max Smythe <smythe@google.com>
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Nov 26, 2020
@lkysow
Copy link

lkysow commented Nov 27, 2020

We have a situation where we want to model map[string]interface{}, e.g.

spec:
  config: # we want this to be map[string]interface
    foo: '{some-fun-json}'
    members: 3

Currently we're using:

	Config json.RawMessage `json:"config,omitempty"`

And then converting to map[string]interface{}.

func (in *ProxyDefaults) convertConfig() map[string]interface{} {
	if in.Spec.Config == nil {
		return nil
	}
	var outConfig map[string]interface{}
	json.Unmarshal(in.Spec.Config, &outConfig)
	return outConfig
}

The only CRD type we've found that works is object:

          properties:
            config:
              description: yada
              type: object

However the CRD generation gives us:

            config:
              description: yada
              format: byte
              type: string

If we use the annotation

	// +kubebuilder:validation:Type=object
	Config json.RawMessage `json:"config,omitempty"`

We get an error:

github.com/hashicorp/consul-k8s/api/v1alpha1:-: conflicting types in allOf branches in schema: string vs object

Would this change allow us to do:

	// +kubebuilder:validation:Type=object
        // +kubebuilder:validation:Schemaless
	Config json.RawMessage `json:"config,omitempty"`

?

@maxsmythe
Copy link
Contributor Author

maxsmythe commented Nov 30, 2020

It should be able to.

Some alternatives for you:

  • Use runtime.RawExtension to wrap an object one level higher, and user XPreserveUnknownFields to allow any field to be set on that object. You will need to do the serialization/deserialization yourself if you do this.
  • Consider a different API design, as maps of objects accepting arbitrary fields is against the K8s API Conventions, though this is certainly not universally applied.

If neither of these alternatives work for you, this PR would be helpful.

@DirectXMan12 DirectXMan12 changed the title 🐛 (:bug:, patch) Add crd:validation:Schemaless marker 🐛 Add crd:validation:Schemaless marker Jan 27, 2021
Copy link
Contributor

@DirectXMan12 DirectXMan12 left a comment

Choose a reason for hiding this comment

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

minor comment on a go modules file change, otherwise lgtm

@@ -1,6 +1,4 @@
run:
modules-download-mode: readonly
Copy link
Contributor

Choose a reason for hiding this comment

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

??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That had fixed an error I was having running the linter. It's been a while, so I can't remember what the error was.

Testing again with this line re-added doesn't cause a problem anymore. I wonder if it only breaks if your go modules aren't already up-to-date?

In any case, not really in scope for this PR, so reverting.

Signed-off-by: Max Smythe <smythe@google.com>
@DirectXMan12
Copy link
Contributor

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 29, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: DirectXMan12, maxsmythe

The full list of commands accepted by this bot can be found here.

The pull request process is described here

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 Jan 29, 2021
@DirectXMan12 DirectXMan12 changed the title 🐛 Add crd:validation:Schemaless marker ✨ Add crd:validation:Schemaless marker Jan 29, 2021
@k8s-ci-robot k8s-ci-robot merged commit 557da25 into kubernetes-sigs:master Jan 29, 2021
@maxsmythe
Copy link
Contributor Author

Thanks!

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/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

controller-gen doesn't work with k8s.io/apiextensions-apiserver
4 participants