diff --git a/pkg/operator/controller/gatewayclass/controller.go b/pkg/operator/controller/gatewayclass/controller.go index 842df6efb..8f8e9d4d6 100644 --- a/pkg/operator/controller/gatewayclass/controller.go +++ b/pkg/operator/controller/gatewayclass/controller.go @@ -4,9 +4,13 @@ import ( "context" logf "github.com/openshift/cluster-ingress-operator/pkg/log" + operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller" "k8s.io/client-go/tools/record" + maistrav2 "github.com/maistra/istio-operator/pkg/apis/maistra/v2" + operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" + gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" utilerrors "k8s.io/apimachinery/pkg/util/errors" @@ -14,6 +18,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/predicate" @@ -61,6 +66,46 @@ func NewUnmanaged(mgr manager.Manager, config Config) (controller.Controller, er if err := c.Watch(source.Kind[client.Object](operatorCache, &gatewayapiv1beta1.GatewayClass{}, &handler.EnqueueRequestForObject{}, isOurGatewayClass, predicate.Not(isIstioGatewayClass))); err != nil { return nil, err } + + toServiceMeshControlPlane := func(ctx context.Context, _ client.Object) []reconcile.Request { + return []reconcile.Request{{ + NamespacedName: operatorcontroller.ServiceMeshControlPlaneName(config.OperandNamespace), + }} + } + + isOurSMCP := predicate.NewPredicateFuncs(func(o client.Object) bool { + return o.GetName() == operatorcontroller.ServiceMeshControlPlaneName(config.OperandNamespace).Name + }) + + if err = c.Watch(source.Kind[client.Object](operatorCache, &maistrav2.ServiceMeshControlPlane{}, + handler.EnqueueRequestsFromMapFunc(toServiceMeshControlPlane), isOurSMCP, predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { return false }, + DeleteFunc: func(e event.DeleteEvent) bool { return true }, + UpdateFunc: func(e event.UpdateEvent) bool { return false }, + GenericFunc: func(e event.GenericEvent) bool { return false }, + })); err != nil { + return nil, err + } + + toServiceMeshSubscription := func(ctx context.Context, _ client.Object) []reconcile.Request { + return []reconcile.Request{{ + NamespacedName: operatorcontroller.ServiceMeshSubscriptionName(), + }} + } + + isServiceMeshSubscription := predicate.NewPredicateFuncs(func(o client.Object) bool { + return o.GetName() == operatorcontroller.ServiceMeshSubscriptionName().Name && + o.GetNamespace() == operatorcontroller.ServiceMeshSubscriptionName().Namespace + }) + if err = c.Watch(source.Kind[client.Object](operatorCache, &operatorsv1alpha1.Subscription{}, + handler.EnqueueRequestsFromMapFunc(toServiceMeshSubscription), isServiceMeshSubscription, predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { return false }, + DeleteFunc: func(e event.DeleteEvent) bool { return true }, + UpdateFunc: func(e event.UpdateEvent) bool { return false }, + GenericFunc: func(e event.GenericEvent) bool { return false }, + })); err != nil { + return nil, err + } return c, nil } @@ -90,14 +135,17 @@ func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) ( var gatewayclass gatewayapiv1beta1.GatewayClass if err := r.cache.Get(ctx, request.NamespacedName, &gatewayclass); err != nil { + log.Error(err, "failed to get gatewayclass", "request", request) return reconcile.Result{}, err } var errs []error if _, _, err := r.ensureServiceMeshOperatorSubscription(ctx); err != nil { + log.Error(err, "failed to ensure ServiceMeshOperatorSubscription", "request", request) errs = append(errs, err) } if _, _, err := r.ensureServiceMeshControlPlane(ctx, &gatewayclass); err != nil { + log.Error(err, "failed to ensure ServiceMeshControlPlane", "request", request) errs = append(errs, err) } return reconcile.Result{}, utilerrors.NewAggregate(errs) diff --git a/pkg/operator/controller/gatewayclass/servicemeshcontrolplane.go b/pkg/operator/controller/gatewayclass/servicemeshcontrolplane.go index 29083939e..97151008b 100644 --- a/pkg/operator/controller/gatewayclass/servicemeshcontrolplane.go +++ b/pkg/operator/controller/gatewayclass/servicemeshcontrolplane.go @@ -24,7 +24,7 @@ import ( func (r *reconciler) ensureServiceMeshControlPlane(ctx context.Context, gatewayclass *gatewayapiv1beta1.GatewayClass) (bool, *maistrav2.ServiceMeshControlPlane, error) { name := controller.ServiceMeshControlPlaneName(r.config.OperandNamespace) have, current, err := r.currentServiceMeshControlPlane(ctx, name) - if err != nil { + if err != nil && !errors.IsNotFound(err) { return false, nil, err } @@ -162,7 +162,7 @@ func (r *reconciler) currentServiceMeshControlPlane(ctx context.Context, name ty var smcp maistrav2.ServiceMeshControlPlane if err := r.cache.Get(ctx, name, &smcp); err != nil { if errors.IsNotFound(err) { - return false, nil, nil + return false, nil, err } return false, nil, fmt.Errorf("failed to get ServiceMeshControlPlane %s: %w", name, err) } diff --git a/pkg/operator/controller/gatewayclass/subscription.go b/pkg/operator/controller/gatewayclass/subscription.go index a37388fa4..fb59d7292 100644 --- a/pkg/operator/controller/gatewayclass/subscription.go +++ b/pkg/operator/controller/gatewayclass/subscription.go @@ -22,7 +22,7 @@ import ( func (r *reconciler) ensureServiceMeshOperatorSubscription(ctx context.Context) (bool, *operatorsv1alpha1.Subscription, error) { name := operatorcontroller.ServiceMeshSubscriptionName() have, current, err := r.currentSubscription(ctx, name) - if err != nil { + if err != nil && !errors.IsNotFound(err) { return false, nil, err } @@ -70,7 +70,7 @@ func (r *reconciler) currentSubscription(ctx context.Context, name types.Namespa var subscription operatorsv1alpha1.Subscription if err := r.client.Get(ctx, name, &subscription); err != nil { if errors.IsNotFound(err) { - return false, nil, nil + return false, nil, err } return false, nil, fmt.Errorf("failed to get subscription %s: %w", name, err) }