diff --git a/controllers/test_common.go b/controllers/test_common.go index 6fb9d6b34..5c04667cd 100644 --- a/controllers/test_common.go +++ b/controllers/test_common.go @@ -104,6 +104,7 @@ func SetupKuadrantOperatorForTest(s *runtime.Scheme, cfg *rest.Config) { err = (&TLSPolicyReconciler{ BaseReconciler: tlsPolicyBaseReconciler, TargetRefReconciler: reconcilers.TargetRefReconciler{Client: mgr.GetClient()}, + RestMapper: mgr.GetRESTMapper(), }).SetupWithManager(mgr) Expect(err).NotTo(HaveOccurred()) diff --git a/controllers/tlspolicy_controller.go b/controllers/tlspolicy_controller.go index b0c9080b1..54839ccbd 100644 --- a/controllers/tlspolicy_controller.go +++ b/controllers/tlspolicy_controller.go @@ -23,6 +23,7 @@ import ( certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -47,6 +48,7 @@ const TLSPolicyFinalizer = "kuadrant.io/tls-policy" type TLSPolicyReconciler struct { *reconcilers.BaseReconciler TargetRefReconciler reconcilers.TargetRefReconciler + RestMapper meta.RESTMapper } //+kubebuilder:rbac:groups=kuadrant.io,resources=tlspolicies,verbs=get;list;watch;update;patch;delete @@ -200,6 +202,15 @@ func (r *TLSPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error { return nil } + ok, err = kuadrantgatewayapi.IsCertManagerInstalled(mgr.GetRESTMapper()) + if err != nil { + return err + } + if !ok { + r.Logger().Info("TLSPolicy controller disabled. CertManager was not found") + return nil + } + gatewayEventMapper := mappers.NewGatewayEventMapper(mappers.WithLogger(r.Logger().WithName("gatewayEventMapper")), mappers.WithClient(mgr.GetClient())) issuerStatusChangedPredicate := predicate.Funcs{ diff --git a/main.go b/main.go index 6b04ec7fa..3853017d0 100644 --- a/main.go +++ b/main.go @@ -208,6 +208,7 @@ func main() { if err = (&controllers.TLSPolicyReconciler{ BaseReconciler: tlsPolicyBaseReconciler, TargetRefReconciler: reconcilers.TargetRefReconciler{Client: mgr.GetClient()}, + RestMapper: mgr.GetRESTMapper(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "TLSPolicy") os.Exit(1) diff --git a/pkg/library/gatewayapi/utils.go b/pkg/library/gatewayapi/utils.go index 09f9ba5df..88e940cca 100644 --- a/pkg/library/gatewayapi/utils.go +++ b/pkg/library/gatewayapi/utils.go @@ -6,6 +6,8 @@ import ( "reflect" "strings" + "github.com/cert-manager/cert-manager/pkg/apis/certmanager" + certmanv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" @@ -165,3 +167,19 @@ func IsGatewayAPIInstalled(restMapper meta.RESTMapper) (bool, error) { return false, err } + +func IsCertManagerInstalled(restMapper meta.RESTMapper) (bool, error) { + _, err := restMapper.RESTMapping( + schema.GroupKind{Group: certmanager.GroupName, Kind: certmanv1.CertificateKind}, + certmanv1.SchemeGroupVersion.Version, + ) + if err == nil { + return true, nil + } + + if meta.IsNoMatchError(err) { + return false, nil + } + + return false, err +}