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

Using utilruntime.Must() for adding schemes #1462

Merged

Conversation

christopherhein
Copy link
Member

Add the use of utilruntime.Must() from apimachinery to catch errors with scheme configurations.

Signed-off-by: Chris Hein [email protected]

@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Apr 7, 2020
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Apr 7, 2020
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 7, 2020
Signed-off-by: Chris Hein <[email protected]>
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Apr 7, 2020
@@ -113,7 +113,7 @@ const (
// change, and thus should be done for next project version.
multiGroupControllerImportCodeFragment = `%scontroller "%s/controllers/%s"
`
addschemeCodeFragment = `_ = %s.AddToScheme(scheme)
addschemeCodeFragment = `utilruntime.Must(%s.AddToScheme(scheme))
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, but could you add in the first comment one example of scenario that is solved/attended with?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not directly experiencing issues from this but from a best practices side in most other implementations of AddingToScheme() they implement utilruntime.Must() to ensure panics causing the controller to crash. https://github.com/search?q=org%3Akubernetes+utilruntime.Must&type=Code and https://github.com/search?q=org%3Akubernetes-sigs+utilruntime.Must&type=Code

Potentially it doesn't matter?

Copy link
Member

@camilamacedo86 camilamacedo86 Apr 9, 2020

Choose a reason for hiding this comment

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

HI @christopherhein,

I checked the Must impl here, and it just causes a panic error.

Regards that would be a best practices

See here that it is not used in the biggest part of the places where the addSchema is used in the example provided. Also, if it is a good practice then:

  • Should we not add this info in the AddToScheme doc?
  • Why is the controller-runtime not using it all examples/scenarios?

Regards Kubebuilder project scenarios

Note that the errors returned has been explicitly ignored in the Kubebuilder projects, and then I was curious if you face any issue that justifies it.

Also, start to cause an panic error in all these scenarios may not be the expected behaviour. For example, will the panic occurs if the CRD required not be found in the cluster? What are the possible errors/scenarios where an error can be returned from it? Would be required to kill the project in all these circumstances?

Did you checked that already?

Copy link
Member

Choose a reason for hiding this comment

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

IMHO it's reasonable to just let the controller fail if it can't even add scheme.
@droot @DirectXMan12 Thoughts?

will the panic occurs if the CRD required not be found in the cluster?

From my understanding no.

Copy link
Member Author

@christopherhein christopherhein Apr 10, 2020

Choose a reason for hiding this comment

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

Hey @camilamacedo86 sorry for the delayed response here. For now I've put in a PR to also add documentation to the scheme - kubernetes-sigs/controller-runtime#894

I had looked into a handful of the other implementation and most will cause Panics already for example if you register all the schemes from `client-go this https://github.com/kubernetes/client-go/blob/master/kubernetes/scheme/register.go#L133 will panic if there are issues.

I discussed this yesterday with @DirectXMan12 and they'd mentioned that this "either a) it was an oversight, or b) that pattern turned up after we had started writing our examples" and "we really should be checking the return value"

Copy link
Member Author

@christopherhein christopherhein Apr 10, 2020

Choose a reason for hiding this comment

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

It seems like the errors usually come from errors in the conversion or defaulting registration since I've been able to repro panics by doing things like registering schemes of CRDs with the same GVK as standard k8s objects.

@mengqiy
Copy link
Member

mengqiy commented Apr 10, 2020

Per #1462 (comment), I guess we are on the same page. We can get this in.
/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 Apr 10, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: christopherhein, mengqiy

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 Apr 10, 2020
@k8s-ci-robot k8s-ci-robot merged commit b2ce2e0 into kubernetes-sigs:master Apr 10, 2020
@christopherhein christopherhein deleted the chore/utilruntime-schemes branch April 11, 2020 00:30
@christopherhein
Copy link
Member Author

thanks @mengqiy!

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.

4 participants