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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/scaffold/internal/templates/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

`
reconcilerSetupCodeFragment = `if err = (&controllers.%sReconciler{
Client: mgr.GetClient(),
Expand Down Expand Up @@ -206,6 +206,7 @@ import (
"flag"
"os"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -219,7 +220,7 @@ var (
)

func init() {
_ = clientgoscheme.AddToScheme(scheme)
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

%s
}
Expand Down
5 changes: 3 additions & 2 deletions testdata/project-v2-addon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -37,9 +38,9 @@ var (
)

func init() {
_ = clientgoscheme.AddToScheme(scheme)
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

_ = crewv1.AddToScheme(scheme)
utilruntime.Must(crewv1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}

Expand Down
17 changes: 9 additions & 8 deletions testdata/project-v2-multigroup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -46,15 +47,15 @@ var (
)

func init() {
_ = clientgoscheme.AddToScheme(scheme)
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

_ = crewv1.AddToScheme(scheme)
_ = shipv1beta1.AddToScheme(scheme)
_ = shipv1.AddToScheme(scheme)
_ = shipv2alpha1.AddToScheme(scheme)
_ = seacreaturesv1beta1.AddToScheme(scheme)
_ = seacreaturesv1beta2.AddToScheme(scheme)
_ = foopolicyv1.AddToScheme(scheme)
utilruntime.Must(crewv1.AddToScheme(scheme))
utilruntime.Must(shipv1beta1.AddToScheme(scheme))
utilruntime.Must(shipv1.AddToScheme(scheme))
utilruntime.Must(shipv2alpha1.AddToScheme(scheme))
utilruntime.Must(seacreaturesv1beta1.AddToScheme(scheme))
utilruntime.Must(seacreaturesv1beta2.AddToScheme(scheme))
utilruntime.Must(foopolicyv1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}

Expand Down
5 changes: 3 additions & 2 deletions testdata/project-v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -37,9 +38,9 @@ var (
)

func init() {
_ = clientgoscheme.AddToScheme(scheme)
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

_ = crewv1.AddToScheme(scheme)
utilruntime.Must(crewv1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}

Expand Down