From b2c18fd3d9741c8147cd0ff731aa8363817d289f Mon Sep 17 00:00:00 2001 From: Ce Gao Date: Mon, 17 Jun 2019 15:56:51 +0800 Subject: [PATCH] fix: Do not use webhook in UT Signed-off-by: Ce Gao --- .../experiment/experiment_controller.go | 23 +++++++++++++------ .../experiment_controller_suite_test.go | 19 +++++++++++++++ .../experiment/experiment_controller_test.go | 4 ++-- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/pkg/controller/v1alpha2/experiment/experiment_controller.go b/pkg/controller/v1alpha2/experiment/experiment_controller.go index 338cdd0e948..d4dcfeedaf7 100644 --- a/pkg/controller/v1alpha2/experiment/experiment_controller.go +++ b/pkg/controller/v1alpha2/experiment/experiment_controller.go @@ -104,8 +104,23 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error { return err } + if err = addWatch(mgr, c); err != nil { + log.Error(err, "Trial watch failed") + return err + } + if err = addWebhook(mgr); err != nil { + log.Error(err, "Failed to create webhook") + return err + } + + log.Info("Experiment controller created") + return nil +} + +// addWatch adds a new Controller to mgr with r as the reconcile.Reconciler +func addWatch(mgr manager.Manager, c controller.Controller) error { // Watch for changes to Experiment - err = c.Watch(&source.Kind{Type: &experimentsv1alpha2.Experiment{}}, &handler.EnqueueRequestForObject{}) + err := c.Watch(&source.Kind{Type: &experimentsv1alpha2.Experiment{}}, &handler.EnqueueRequestForObject{}) if err != nil { log.Error(err, "Experiment watch failed") return err @@ -123,12 +138,6 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error { log.Error(err, "Trial watch failed") return err } - if err = addWebhook(mgr); err != nil { - log.Error(err, "Failed to create webhook") - return err - } - - log.Info("Experiment controller created") return nil } diff --git a/pkg/controller/v1alpha2/experiment/experiment_controller_suite_test.go b/pkg/controller/v1alpha2/experiment/experiment_controller_suite_test.go index 6b86d699c58..8dfaa4493c1 100644 --- a/pkg/controller/v1alpha2/experiment/experiment_controller_suite_test.go +++ b/pkg/controller/v1alpha2/experiment/experiment_controller_suite_test.go @@ -27,6 +27,7 @@ import ( "github.com/onsi/gomega" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" + "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/envtest" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -81,3 +82,21 @@ func StartTestManager(mgr manager.Manager, g *gomega.GomegaWithT) (chan struct{} }() return stop, wg } + +// addForTestPurpose adds a new Controller to mgr with r as the reconcile.Reconciler. +func addForTestPurpose(mgr manager.Manager, r reconcile.Reconciler) error { + // Create a new controller + c, err := controller.New("test-experiment-controller", mgr, controller.Options{Reconciler: r}) + if err != nil { + log.Error(err, "Failed to create experiment controller for test purpose.") + return err + } + + if err = addWatch(mgr, c); err != nil { + log.Error(err, "Trial watch failed") + return err + } + + log.Info("Experiment controller created") + return nil +} diff --git a/pkg/controller/v1alpha2/experiment/experiment_controller_test.go b/pkg/controller/v1alpha2/experiment/experiment_controller_test.go index 8b80df041d1..14479ffb7e0 100644 --- a/pkg/controller/v1alpha2/experiment/experiment_controller_test.go +++ b/pkg/controller/v1alpha2/experiment/experiment_controller_test.go @@ -79,7 +79,7 @@ func TestCreateExperiment(t *testing.T) { return nil }, }) - g.Expect(add(mgr, recFn)).NotTo(gomega.HaveOccurred()) + g.Expect(addForTestPurpose(mgr, recFn)).NotTo(gomega.HaveOccurred()) stopMgr, mgrStopped := StartTestManager(mgr, g) @@ -211,7 +211,7 @@ spec: } recFn := SetupTestReconcile(r) - g.Expect(add(mgr, recFn)).NotTo(gomega.HaveOccurred()) + g.Expect(addForTestPurpose(mgr, recFn)).NotTo(gomega.HaveOccurred()) stopMgr, mgrStopped := StartTestManager(mgr, g)