From c57f5284ed871dff840c370ead2896dc2434c1cb Mon Sep 17 00:00:00 2001 From: KevFan Date: Fri, 8 Nov 2024 13:30:59 +0000 Subject: [PATCH] feat: validate dns operator crd is installed for dnspolicy status Signed-off-by: KevFan --- controllers/dns_workflow.go | 4 ++-- controllers/dnspolicies_validator.go | 14 +++++++++--- controllers/state_of_the_world.go | 34 +++++++++++++++++----------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/controllers/dns_workflow.go b/controllers/dns_workflow.go index ced545c95..d8333c366 100644 --- a/controllers/dns_workflow.go +++ b/controllers/dns_workflow.go @@ -46,9 +46,9 @@ var ( //+kubebuilder:rbac:groups=kuadrant.io,resources=dnsrecords,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=kuadrant.io,resources=dnsrecords/status,verbs=get -func NewDNSWorkflow(client *dynamic.DynamicClient, scheme *runtime.Scheme) *controller.Workflow { +func NewDNSWorkflow(client *dynamic.DynamicClient, scheme *runtime.Scheme, isDNSOperatorInstalled bool) *controller.Workflow { return &controller.Workflow{ - Precondition: NewDNSPoliciesValidator().Subscription().Reconcile, + Precondition: NewDNSPoliciesValidator(isDNSOperatorInstalled).Subscription().Reconcile, Tasks: []controller.ReconcileFunc{ NewEffectiveDNSPoliciesReconciler(client, scheme).Subscription().Reconcile, }, diff --git a/controllers/dnspolicies_validator.go b/controllers/dnspolicies_validator.go index 776d98b4d..261a2ff56 100644 --- a/controllers/dnspolicies_validator.go +++ b/controllers/dnspolicies_validator.go @@ -18,11 +18,15 @@ import ( "github.com/kuadrant/kuadrant-operator/pkg/kuadrant" ) -func NewDNSPoliciesValidator() *DNSPoliciesValidator { - return &DNSPoliciesValidator{} +func NewDNSPoliciesValidator(isDNSOperatorInstalled bool) *DNSPoliciesValidator { + return &DNSPoliciesValidator{ + isDNSOperatorInstalled: isDNSOperatorInstalled, + } } -type DNSPoliciesValidator struct{} +type DNSPoliciesValidator struct { + isDNSOperatorInstalled bool +} func (r *DNSPoliciesValidator) Subscription() controller.Subscription { return controller.Subscription{ @@ -45,6 +49,10 @@ func (r *DNSPoliciesValidator) validate(ctx context.Context, _ []controller.Reso logger.V(1).Info("validating dns policies", "policies", len(policies)) state.Store(StateDNSPolicyAcceptedKey, lo.SliceToMap(policies, func(p machinery.Policy) (string, error) { + if !r.isDNSOperatorInstalled { + return p.GetLocator(), kuadrant.NewErrDependencyNotInstalled("DNS Operator") + } + policy := p.(*kuadrantv1.DNSPolicy) if err := isTargetRefsFound(topology, policy); err != nil { diff --git a/controllers/state_of_the_world.go b/controllers/state_of_the_world.go index e6e68c4e3..579caa8e5 100644 --- a/controllers/state_of_the_world.go +++ b/controllers/state_of_the_world.go @@ -39,6 +39,7 @@ import ( "github.com/kuadrant/kuadrant-operator/pkg/kuadrant" "github.com/kuadrant/kuadrant-operator/pkg/openshift" "github.com/kuadrant/kuadrant-operator/pkg/openshift/consoleplugin" + "github.com/kuadrant/kuadrant-operator/pkg/utils" ) var ( @@ -178,6 +179,7 @@ type BootOptionsBuilder struct { isIstioInstalled bool isCertManagerInstalled bool isConsolePluginInstalled bool + isDNSOperatorInstalled bool } func (b *BootOptionsBuilder) getOptions() []controller.ControllerOption { @@ -324,18 +326,24 @@ func (b *BootOptionsBuilder) getConsolePluginOptions() []controller.ControllerOp func (b *BootOptionsBuilder) getDNSOperatorOptions() []controller.ControllerOption { var opts []controller.ControllerOption - opts = append(opts, - controller.WithRunnable("dnsrecord watcher", controller.Watch( - &kuadrantdnsv1alpha1.DNSRecord{}, DNSRecordResource, metav1.NamespaceAll, - controller.FilterResourcesByLabel[*kuadrantdnsv1alpha1.DNSRecord](fmt.Sprintf("%s=%s", AppLabelKey, AppLabelValue)))), - controller.WithObjectKinds( - DNSRecordGroupKind, - ), - controller.WithObjectLinks( - LinkListenerToDNSRecord, - LinkDNSPolicyToDNSRecord, - ), - ) + var err error + b.isDNSOperatorInstalled, err = utils.IsCRDInstalled(b.manager.GetRESTMapper(), kuadrantdnsv1alpha1.GroupVersion.Group, "DNSRecord", kuadrantdnsv1alpha1.GroupVersion.Version) + if err != nil || !b.isDNSOperatorInstalled { + b.logger.Info("dns operator is not installed, skipping related watches and reconcilers", "err", err) + } else { + opts = append(opts, + controller.WithRunnable("dnsrecord watcher", controller.Watch( + &kuadrantdnsv1alpha1.DNSRecord{}, DNSRecordResource, metav1.NamespaceAll, + controller.FilterResourcesByLabel[*kuadrantdnsv1alpha1.DNSRecord](fmt.Sprintf("%s=%s", AppLabelKey, AppLabelValue)))), + controller.WithObjectKinds( + DNSRecordGroupKind, + ), + controller.WithObjectLinks( + LinkListenerToDNSRecord, + LinkDNSPolicyToDNSRecord, + ), + ) + } return opts } @@ -346,7 +354,7 @@ func (b *BootOptionsBuilder) Reconciler() controller.ReconcileFunc { Tasks: []controller.ReconcileFunc{ NewAuthorinoReconciler(b.client).Subscription().Reconcile, NewLimitadorReconciler(b.client).Subscription().Reconcile, - NewDNSWorkflow(b.client, b.manager.GetScheme()).Run, + NewDNSWorkflow(b.client, b.manager.GetScheme(), b.isDNSOperatorInstalled).Run, NewTLSWorkflow(b.client, b.manager.GetScheme(), b.isCertManagerInstalled).Run, NewDataPlanePoliciesWorkflow(b.client, b.isIstioInstalled, b.isEnvoyGatewayInstalled).Run, NewKuadrantStatusUpdater(b.client, b.isIstioInstalled, b.isEnvoyGatewayInstalled).Subscription().Reconcile,