Skip to content

Prevent creating a controller if a CRD does not exist. #840

@sophieliu15

Description

@sophieliu15

I hope to create a controller for each GVK that I added dynamically. Previously I noticed that if I started the manager and then installed a new CRD on the fly, I would get an error saying the CRD did not exist when I called NewControllerManagedBy(mgr)...Complete(r). Specifically, this line would return an error. The error message was the following, where CronTab was a new CRD that I installed after starting the controller manager:

Error while trying to create ObjectReconciler","gvk":"stable.example.com/v1, Kind=CronTab","error":"no matches for kind \"CronTab\" in version \"stable.example.com/v1\"

Then I found #554 and updated controller-runtime version to the latest one (v0.5.0) to use DynamicRESTMapper. After updated, I started manager without installing CRD for CronTab. I found that I could create controller for CronTab successfully (i.e., this line does not return any error). However, when the Reconcile method for the controller of CronTab was called, I got the error from pkg/source/source.go complaining the CRD was not found. Following was the error:

{"level":"error","ts":1583448155.5018284,"logger":"controller-runtime.source","msg":"if kind is a CRD, it should be installed before calling Start","kind":"CronTab.stable.example.com","error":"no matches for kind \"CronTab\" in version \"stable.example.com/v1\"","stacktrace":"github.com/go-logr/zapr.(*zapLogger).Error\n\t/go/pkg/mod/github.com/go-logr/[email protected]/zapr.go:128\nsigs.k8s.io/controller-runtime/pkg/source.(*Kind).Start\n\t/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/source/source.go:88\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func1\n\t/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:165\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start\n\t/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:198\nsigs.k8s.io/controller-runtime/pkg/manager.(*controllerManager).Add.func1\n\t/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/manager/internal.go:226"}

My questions are:

  • Is there any way to prevent creating controller if a specific CRD does not exist in the apiserver? I am currently using:
_, err := r.Manager.GetRESTMapper().RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
  // Do not create controller
}

Not sure if there is a better way (or common practice) to do that?

  • After updated to the latest controller-runtime, I would expect the controller creation to fail if I did not install the CRD and succeed after I created the CRD on the fly. I am curious why I was able to create the controller without installing CRD after the update? Is this change expected?

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.kind/supportCategorizes issue or PR as a support question.lifecycle/rottenDenotes an issue or PR that has aged beyond stale and will be auto-closed.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions