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 3 years ago
  • Reactions: 1
  • Comments: 17 (10 by maintainers)

Most upvoted comments

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.go should 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:

	testEnv = &envtest.Environment{
		CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
	}

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.

	testEnv = &envtest.Environment{
		CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
	}

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.