From 71aaabab570a97029f97d01d53f8cb710d8d5633 Mon Sep 17 00:00:00 2001 From: Rahul-D78 Date: Fri, 11 Nov 2022 21:01:45 +0530 Subject: [PATCH 1/3] feat: filter out Gateways before reconciliation Signed-off-by: Rahul-D78 --- controllers/gateway_controller.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/controllers/gateway_controller.go b/controllers/gateway_controller.go index 47a7b1ce..20c5ef55 100644 --- a/controllers/gateway_controller.go +++ b/controllers/gateway_controller.go @@ -5,6 +5,7 @@ import ( "fmt" "reflect" + "github.com/go-logr/logr" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -15,6 +16,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" @@ -36,11 +38,25 @@ const gatewayServiceLabel = "konghq.com/owned-by-gateway" type GatewayReconciler struct { client.Client Scheme *runtime.Scheme + Log logr.Logger } func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&gatewayv1beta1.Gateway{}). + WithEventFilter(predicate.NewPredicateFuncs(func(obj client.Object) bool { + gateway, ok := obj.(*gatewayv1beta1.Gateway) + if !ok { + r.Log.Error(fmt.Errorf("unexpected object type in gateway watch predicates"), "expected", "*gatewayv1beta1.Gateway", "found", reflect.TypeOf(obj)) + return false + } + gatewayClass := &gatewayv1beta1.GatewayClass{} + if err := r.Client.Get(context.Background(), client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil { + r.Log.Error(err, "could not retrieve gatewayclass", "gatewayclass", gateway.Spec.GatewayClassName) + return false + } + return gatewayClass.Spec.ControllerName != GatewayClassControllerName + })). Watches( &source.Kind{Type: &corev1.Service{}}, handler.EnqueueRequestsFromMapFunc(mapServiceToGateway), From 8e174a374b6cfe7896edc9e40cb54c358911ed96 Mon Sep 17 00:00:00 2001 From: Rahul-D78 Date: Fri, 11 Nov 2022 22:54:00 +0530 Subject: [PATCH 2/3] putting predicate as the variadic argument Signed-off-by: Rahul-D78 --- controllers/gateway_controller.go | 32 +++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/controllers/gateway_controller.go b/controllers/gateway_controller.go index 20c5ef55..b0e0e648 100644 --- a/controllers/gateway_controller.go +++ b/controllers/gateway_controller.go @@ -13,6 +13,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/utils/pointer" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/log" @@ -43,20 +44,9 @@ type GatewayReconciler struct { func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). - For(&gatewayv1beta1.Gateway{}). - WithEventFilter(predicate.NewPredicateFuncs(func(obj client.Object) bool { - gateway, ok := obj.(*gatewayv1beta1.Gateway) - if !ok { - r.Log.Error(fmt.Errorf("unexpected object type in gateway watch predicates"), "expected", "*gatewayv1beta1.Gateway", "found", reflect.TypeOf(obj)) - return false - } - gatewayClass := &gatewayv1beta1.GatewayClass{} - if err := r.Client.Get(context.Background(), client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil { - r.Log.Error(err, "could not retrieve gatewayclass", "gatewayclass", gateway.Spec.GatewayClassName) - return false - } - return gatewayClass.Spec.ControllerName != GatewayClassControllerName - })). + For(&gatewayv1beta1.Gateway{}, + builder.WithPredicates(predicate.NewPredicateFuncs(r.gatewayHasMatchingGatewayClass)), + ). Watches( &source.Kind{Type: &corev1.Service{}}, handler.EnqueueRequestsFromMapFunc(mapServiceToGateway), @@ -64,6 +54,20 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error { Complete(r) } +func (r *GatewayReconciler) gatewayHasMatchingGatewayClass(obj client.Object) bool { + gateway, ok := obj.(*gatewayv1beta1.Gateway) + if !ok { + r.Log.Error(fmt.Errorf("unexpected object type in gateway watch predicates"), "expected", "*gatewayv1beta1.Gateway", "found", reflect.TypeOf(obj)) + return false + } + gatewayClass := &gatewayv1beta1.GatewayClass{} + if err := r.Client.Get(context.Background(), client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil { + r.Log.Error(err, "could not retrieve gatewayclass", "gatewayclass", gateway.Spec.GatewayClassName) + return false + } + return gatewayClass.Spec.ControllerName != GatewayClassControllerName +} + func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx) From ae8e303c2c5a4538b0e69e204a2be68141340d57 Mon Sep 17 00:00:00 2001 From: Rahul-D78 Date: Sat, 12 Nov 2022 00:10:05 +0530 Subject: [PATCH 3/3] optimizing the predicate function Signed-off-by: Rahul-D78 --- controllers/gateway_controller.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/controllers/gateway_controller.go b/controllers/gateway_controller.go index b0e0e648..38c63b86 100644 --- a/controllers/gateway_controller.go +++ b/controllers/gateway_controller.go @@ -62,10 +62,12 @@ func (r *GatewayReconciler) gatewayHasMatchingGatewayClass(obj client.Object) bo } gatewayClass := &gatewayv1beta1.GatewayClass{} if err := r.Client.Get(context.Background(), client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil { - r.Log.Error(err, "could not retrieve gatewayclass", "gatewayclass", gateway.Spec.GatewayClassName) + if errors.IsNotFound(err) { + r.Log.Error(err, "gatewayclass not found", "gatewayclass", gateway.Spec.GatewayClassName) + } return false } - return gatewayClass.Spec.ControllerName != GatewayClassControllerName + return true // some other problem retrieving the object, enqueue anyway to avoid dropping } func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {