operator-sdk: Test fails with no matches for kind \"Route\" although routev1 is added to schema
Bug Report
What did you do?
I am following the code skeleton for writing test for controllers
Since my controller also uses openshift routes I added the route to the schema
//controllers/suite_test.go
import (
...
routev1 "github.com/openshift/api/route/v1"
"k8s.io/client-go/kubernetes/scheme"
...
)
func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecsWithDefaultAndCustomReporters(t,
"Controller Suite",
[]Reporter{printer.NewlineReporter{}})
}
var _ = BeforeSuite(func(done Done) {
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
}
var err error
cfg, err = testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expect(cfg).ToNot(BeNil())
err = elmv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
//ADD ROUTE-V1
err = routev1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).ToNot(HaveOccurred())
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
})
Expect(err).ToNot(HaveOccurred())
//try to add route v1 again - does not help
err = routev1.AddToScheme(k8sManager.GetScheme())
Expect(err).NotTo(HaveOccurred())
err = (&ELMReconciler{
Client: k8sManager.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("ELM"),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())
go func() {
//this call will fail with no matches for kind Route
err = k8sManager.Start(ctrl.SetupSignalHandler())
Expect(err).ToNot(HaveOccurred())
}()
k8sClient = k8sManager.GetClient()
Expect(k8sClient).ToNot(BeNil())
close(done)
}, 60)
What did you expect to see?
I expected the manager to start in the test suite without errors as it does in runtime
What did you see instead? Under which circumstances?
After manager.start is called the test fails with no matches for kind "Route" error
2021-01-26T13:55:05.853+0200 ERROR controller-runtime.source if kind is a CRD, it should be installed before calling Start {"kind": "Route.route.openshift.io", "error": "no matches for kind \"Route\" in version \"route.openshift.io/v1\""}
github.com/go-logr/zapr.(*zapLogger).Error
/home/avrahams/go/pkg/mod/github.com/go-logr/zapr@v0.1.0/zapr.go:128
sigs.k8s.io/controller-runtime/pkg/source.(*Kind).Start
/home/avrahams/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.6.3/pkg/source/source.go:117
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func1
/home/avrahams/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.6.3/pkg/internal/controller/controller.go:143
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start
/home/avrahams/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.6.3/pkg/internal/controller/controller.go:184
sigs.k8s.io/controller-runtime/pkg/manager.(*controllerManager).startRunnable.func1
/home/avrahams/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.6.3/pkg/manager/internal.go:661
panic:
Your test failed.
Ginkgo panics to prevent subsequent assertions from running.
Normally Ginkgo rescues this panic so you shouldn't see it.
Environment
Operator type:
/language go
Kubernetes cluster type: OpenShift
$ operator-sdk version
operator-sdk version: “v1.2.0”, commit: “215fc50b2d4acc7d92b36828f42d7d1ae212015c”, kubernetes version: “v1.18.8”, go version: “go1.15.3”, GOOS: “linux”, GOARCH: “amd64”
$ go version (if language is Go)
go version go1.15.6 linux/amd64
$ kubectl version
Possible Solution
Additional context
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 17 (10 by maintainers)
Hi All, As I mentioned. my problem was slightly different then the OP’s one(no external CRD) but with same error. What I was doing differently is that I added multi-group API in my project. The kubebuilder documentation doesn’t mention anywhere that
suite_test.goshould be updated accordingly as adding multi-group api adds additional directory to the apis/ and controller/ tree.So basically with generated suite_test.go we get:
Since adding multi-group api puts everything one directory down we needed to add additional “…” to the filepath.
So, adding that solved my particular problem.
Took to me a good while to figure this simple error. I think this should added in somewhere like here?
I hope someone facing similar issue find this helpful.
While adding the CRDs seems to work, in case of OpenShift API resources it is a bit cumbersome. Since the api is not published via tags I have to use pseudo-versions. Keeping the CRD paths in this format in sync with the
go.modis doable, but maybe streamlining that experience is possible? Especially sinceoperator-sdktargets OCP, I kinda expect to not have to do these steps manually.Storing the CRDs as fixtures in the project repository would also be possible, but that still would require to manually put them there each time I update the
go.modfor the OCP api.Another question: While a lot of CRDs for OCP APIs can be found in the corresponding api repository, I am not able to find CRDs for stuff like
Routes,DeploymentConfigs, Projects (project.openshift.io/v1), ...and it seems they cannot be generated from the api repository either. Am I missing something here? Right now I do not find a way to add these resources to envtest.