Skip to content

Commit

Permalink
fix for OCPBUGS-31550-deleting_SMCP_breaks_Gateway_API
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhAgniRedhat committed Aug 9, 2024
1 parent 7cf86c7 commit 9fe51e2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
48 changes: 48 additions & 0 deletions pkg/operator/controller/gatewayclass/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ 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"

"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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/operator/controller/gatewayclass/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 9fe51e2

Please sign in to comment.