Skip to content

Commit

Permalink
tls policy: check cert-manager API is available before starting contr…
Browse files Browse the repository at this point in the history
…oller
  • Loading branch information
KevFan committed Jul 8, 2024
1 parent d831395 commit 6444c64
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions controllers/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
11 changes: 11 additions & 0 deletions controllers/tlspolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions pkg/library/gatewayapi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 6444c64

Please sign in to comment.