From 3ca0878855f0c60f2fa092f5a605f853cb499936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= Date: Thu, 7 Sep 2023 17:43:55 +0100 Subject: [PATCH] Do not register VM controller if KubeVirt CRDs are not installed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Vilaça --- controllers/setup.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/controllers/setup.go b/controllers/setup.go index a5cd9b353..9fbbef429 100644 --- a/controllers/setup.go +++ b/controllers/setup.go @@ -4,9 +4,11 @@ import ( "context" "fmt" "path/filepath" + "strings" "github.com/go-logr/logr" v1 "github.com/openshift/api/config/v1" + kubevirtv1 "kubevirt.io/api/core/v1" "kubevirt.io/ssp-operator/internal/common" crd_watch "kubevirt.io/ssp-operator/internal/crd-watch" "kubevirt.io/ssp-operator/internal/operands" @@ -103,6 +105,11 @@ func setupManager(ctx context.Context, cancel context.CancelFunc, mgr controller requiredCrds = append(requiredCrds, getRequiredCrds(sspOperands[i])...) } + // Add VMController necessary VirtualMachine CRD + vmKind := strings.ToLower(kubevirtv1.VirtualMachineGroupVersionKind.Kind) + "s" + vmCRD := vmKind + "." + kubevirtv1.VirtualMachineGroupVersionKind.Group + requiredCrds = append(requiredCrds, vmCRD) + crdWatch := crd_watch.New(requiredCrds...) // Cleanly stops the manager and exit. The pod will be restarted. crdWatch.AllCrdsAddedHandler = cancel @@ -139,13 +146,17 @@ func setupManager(ctx context.Context, cancel context.CancelFunc, mgr controller return fmt.Errorf("error adding service controller: %w", err) } - vmController, err := CreateVmController(mgr) - if err != nil { - return fmt.Errorf("failed to create vm controller: %w", err) - } + if crdWatch.CrdExists(vmCRD) { + vmController, cErr := CreateVmController(mgr) + if cErr != nil { + return fmt.Errorf("[vm controller] failed to create vm controller: %w", cErr) + } + + if cErr = mgr.Add(getRunnable(mgr, vmController)); cErr != nil { + return fmt.Errorf("[vm controller] error adding: %w", cErr) + } - if err = mgr.Add(getRunnable(mgr, vmController)); err != nil { - return fmt.Errorf("error adding vm-controller: %w", err) + mgr.GetLogger().Info("[vm controller] added") } reconciler := NewSspReconciler(mgr.GetClient(), mgr.GetAPIReader(), infrastructureTopology, sspOperands, crdWatch)