diff --git a/controllers/bootstrap/init.go b/controllers/bootstrap/init.go index e4ecdbc9c..c1bf664d5 100644 --- a/controllers/bootstrap/init.go +++ b/controllers/bootstrap/init.go @@ -562,6 +562,37 @@ func (b *Bootstrap) CheckOperatorCatalog(ns string) error { return err } +// CheckCRD returns true if the given crd is existent +func (b *Bootstrap) CheckCRD(apiGroupVersion string, kind string) (bool, error) { + dc := discovery.NewDiscoveryClientForConfigOrDie(b.Config) + exist, err := b.ResourceExists(dc, apiGroupVersion, kind) + if err != nil { + return false, err + } + if !exist { + return false, nil + } + return true, nil +} + +// WaitResourceReady returns true only when the specific resource CRD is created and wait for infinite time +func (b *Bootstrap) WaitResourceReady(apiGroupVersion string, kind string) error { + dc := discovery.NewDiscoveryClientForConfigOrDie(b.Config) + if err := utilwait.PollImmediateInfinite(time.Second*10, func() (done bool, err error) { + exist, err := b.ResourceExists(dc, apiGroupVersion, kind) + if err != nil { + return exist, err + } + if !exist { + klog.V(2).Infof("waiting for resource ready with kind: %s, apiGroupVersion: %s", kind, apiGroupVersion) + } + return exist, nil + }); err != nil { + return err + } + return nil +} + func (b *Bootstrap) waitResourceReady(apiGroupVersion, kind string) error { dc := discovery.NewDiscoveryClientForConfigOrDie(b.Config) if err := utilwait.PollImmediate(time.Second*10, time.Minute*2, func() (done bool, err error) { @@ -879,24 +910,6 @@ func (b *Bootstrap) updateApprovalMode() error { return nil } -// WaitResourceReady returns true only when the specific resource CRD is created and wait for infinite time -func (b *Bootstrap) WaitResourceReady(apiGroupVersion string, kind string) error { - dc := discovery.NewDiscoveryClientForConfigOrDie(b.Config) - if err := utilwait.PollImmediateInfinite(time.Second*10, func() (done bool, err error) { - exist, err := b.ResourceExists(dc, apiGroupVersion, kind) - if err != nil { - return exist, err - } - if !exist { - klog.V(2).Infof("waiting for resource ready with kind: %s, apiGroupVersion: %s", kind, apiGroupVersion) - } - return exist, nil - }); err != nil { - return err - } - return nil -} - // deployResource deploys the given resource CR func (b *Bootstrap) DeployResource(cr, placeholder string) bool { if err := utilwait.PollImmediateInfinite(time.Second*10, func() (done bool, err error) { @@ -1011,6 +1024,20 @@ func (b *Bootstrap) IsBYOCert() (bool, error) { } func (b *Bootstrap) DeployCertManagerCR() error { + for _, kind := range constant.CertManagerKinds { + klog.Infof("Checking if resource %s CRD exsits ", kind) + // if the crd is not exist, skip it + exist, err := b.CheckCRD(constant.CertManagerAPIGroupVersionV1, kind) + if err != nil { + klog.Errorf("Failed to check resource with kind: %s, apiGroupVersion: %s", kind, constant.CertManagerAPIGroupVersionV1) + return err + } + if !exist { + klog.Infof("Skiped deploying %s, it is not exist in cluster", kind) + return nil + } + } + klog.V(2).Info("Fetch all the CommonService instances") csReq, err := labels.NewRequirement(constant.CsClonedFromLabel, selection.DoesNotExist, []string{}) if err != nil { @@ -1050,12 +1077,6 @@ func (b *Bootstrap) DeployCertManagerCR() error { } klog.Info("Deploying Cert Manager CRs") - for _, kind := range constant.CertManagerKinds { - // wait for v1 crd ready - if err := b.waitResourceReady(constant.CertManagerAPIGroupVersionV1, kind); err != nil { - klog.Errorf("Failed to wait for resource ready with kind: %s, apiGroupVersion: %s", kind, constant.CertManagerAPIGroupVersionV1) - } - } // will use v1 cert instead of v1alpha cert // delete v1alpha1 cert if it exist var resourceList = []*Resource{ diff --git a/main.go b/main.go index 9e4a8126e..27b410c3f 100644 --- a/main.go +++ b/main.go @@ -168,26 +168,38 @@ func main() { klog.Errorf("Unable to create controller CommonService: %v", err) os.Exit(1) } - if err = (&certmanagerv1controllers.CertificateRefreshReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - }).SetupWithManager(mgr); err != nil { - klog.Error(err, "unable to create controller", "controller", "CertificateRefresh") - os.Exit(1) - } - if err = (&certmanagerv1controllers.PodRefreshReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - }).SetupWithManager(mgr); err != nil { - klog.Error(err, "unable to create controller", "controller", "PodRefresh") + + // check if cert-manager CRD does not exist, then skip cert-manager related controllers initialization + exist, err := bs.CheckCRD(constant.CertManagerAPIGroupVersionV1, "certificates") + if err != nil { + klog.Errorf("Failed to check if cert-manager CRD exists: %v", err) os.Exit(1) } - if err = (&certmanagerv1controllers.V1AddLabelReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - }).SetupWithManager(mgr); err != nil { - klog.Error(err, "unable to create controller", "controller", "V1AddLabel") - os.Exit(1) + if !exist && err == nil { + klog.Infof("cert-manager CRD does not exist, skip cert-manager related controllers initialization") + } else if exist && err == nil { + + if err = (&certmanagerv1controllers.CertificateRefreshReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + klog.Error(err, "unable to create controller", "controller", "CertificateRefresh") + os.Exit(1) + } + if err = (&certmanagerv1controllers.PodRefreshReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + klog.Error(err, "unable to create controller", "controller", "PodRefresh") + os.Exit(1) + } + if err = (&certmanagerv1controllers.V1AddLabelReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + klog.Error(err, "unable to create controller", "controller", "V1AddLabel") + os.Exit(1) + } } } else { klog.Infof("Common Service Operator goes dormant in the namespace %s", operatorNs)