Skip to content

Commit

Permalink
feat: filter out Gateways before reconciliation (#20)
Browse files Browse the repository at this point in the history
Signed-off-by: Rahul-D78 <[email protected]>
  • Loading branch information
Rahul-D78 authored Nov 11, 2022
1 parent 69655b4 commit 510b8aa
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion controllers/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ 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"
"k8s.io/apimachinery/pkg/runtime"
"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"
"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"
Expand All @@ -36,18 +39,37 @@ 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{}).
For(&gatewayv1beta1.Gateway{},
builder.WithPredicates(predicate.NewPredicateFuncs(r.gatewayHasMatchingGatewayClass)),
).
Watches(
&source.Kind{Type: &corev1.Service{}},
handler.EnqueueRequestsFromMapFunc(mapServiceToGateway),
).
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 {
if errors.IsNotFound(err) {
r.Log.Error(err, "gatewayclass not found", "gatewayclass", gateway.Spec.GatewayClassName)
}
return false
}
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) {
log := log.FromContext(ctx)

Expand Down

0 comments on commit 510b8aa

Please sign in to comment.