Skip to content

Commit

Permalink
refactor(konnect): add generic watch func for objects referring direc…
Browse files Browse the repository at this point in the history
…tly to KonnectGatewayControlPlane (#658)
  • Loading branch information
pmalek authored Sep 26, 2024
1 parent 2b37971 commit a8465d5
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (r *KonnectEntityPluginBindingFinalizerReconciler[T, TEnt]) setControllerBu
b.
For(&configurationv1alpha1.KongService{},
builder.WithPredicates(
predicate.NewPredicateFuncs(kongServiceRefersToKonnectGatewayControlPlane),
predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongService]),
kongPluginsAnnotationChangedPredicate,
),
).
Expand Down
27 changes: 27 additions & 0 deletions controller/konnect/watch.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package konnect

import (
"context"
"fmt"
"reflect"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kong/gateway-operator/controller/konnect/constraints"
operatorerrors "github.com/kong/gateway-operator/internal/errors"

configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1"
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand Down Expand Up @@ -56,3 +60,26 @@ func ReconciliationWatchOptionsForEntity[
panic(fmt.Sprintf("unsupported entity type %T", ent))
}
}

// objRefersToKonnectGatewayControlPlane returns true if the object
// refers to a KonnectGatewayControlPlane.
func objRefersToKonnectGatewayControlPlane[
T constraints.SupportedKonnectEntityType,
TEnt constraints.EntityType[T],
](obj client.Object) bool {
ent, ok := obj.(TEnt)
if !ok {
ctrllog.FromContext(context.Background()).Error(
operatorerrors.ErrUnexpectedObject,
"failed to run predicate function",
"expected", constraints.EntityTypeName[T](), "found", reflect.TypeOf(obj),
)
return false
}

cpRef, ok := getControlPlaneRef(ent).Get()
if !ok {
return false
}
return cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef
}
18 changes: 1 addition & 17 deletions controller/konnect/watch_kongcacertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package konnect
import (
"context"
"fmt"
"reflect"

"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -14,7 +13,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

operatorerrors "github.com/kong/gateway-operator/internal/errors"
"github.com/kong/gateway-operator/modules/manager/logging"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -27,7 +25,7 @@ func KongCACertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.
func(b *ctrl.Builder) *ctrl.Builder {
return b.For(&configurationv1alpha1.KongCACertificate{},
builder.WithPredicates(
predicate.NewPredicateFuncs(kongCACertificateRefersToKonnectControlPlane),
predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongCACertificate]),
),
)
},
Expand All @@ -50,20 +48,6 @@ func KongCACertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.
}
}

func kongCACertificateRefersToKonnectControlPlane(obj client.Object) bool {
kongCACertificate, ok := obj.(*configurationv1alpha1.KongCACertificate)
if !ok {
ctrllog.FromContext(context.Background()).Error(
operatorerrors.ErrUnexpectedObject,
"failed to run predicate function",
"expected", "KongCACertificate", "found", reflect.TypeOf(obj),
)
return false
}
return kongCACertificate.Spec.ControlPlaneRef != nil &&
kongCACertificate.Spec.ControlPlaneRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef
}

func enqueueKongCACertificateForKonnectAPIAuthConfiguration(cl client.Client) handler.MapFunc {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
auth, ok := obj.(*konnectv1alpha1.KonnectAPIAuthConfiguration)
Expand Down
18 changes: 1 addition & 17 deletions controller/konnect/watch_kongcertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package konnect
import (
"context"
"fmt"
"reflect"

"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -14,7 +13,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

operatorerrors "github.com/kong/gateway-operator/internal/errors"
"github.com/kong/gateway-operator/modules/manager/logging"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -27,7 +25,7 @@ func KongCertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.Bu
func(b *ctrl.Builder) *ctrl.Builder {
return b.For(&configurationv1alpha1.KongCertificate{},
builder.WithPredicates(
predicate.NewPredicateFuncs(kongCertificateRefersToKonnectControlPlane),
predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongCertificate]),
),
)
},
Expand All @@ -50,20 +48,6 @@ func KongCertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.Bu
}
}

func kongCertificateRefersToKonnectControlPlane(obj client.Object) bool {
kongCertificate, ok := obj.(*configurationv1alpha1.KongCertificate)
if !ok {
ctrllog.FromContext(context.Background()).Error(
operatorerrors.ErrUnexpectedObject,
"failed to run predicate function",
"expected", "KongCertificate", "found", reflect.TypeOf(obj),
)
return false
}
return kongCertificate.Spec.ControlPlaneRef != nil &&
kongCertificate.Spec.ControlPlaneRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef
}

func enqueueKongCertificateForKonnectAPIAuthConfiguration(cl client.Client) handler.MapFunc {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
auth, ok := obj.(*konnectv1alpha1.KonnectAPIAuthConfiguration)
Expand Down
21 changes: 1 addition & 20 deletions controller/konnect/watch_kongconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package konnect
import (
"context"
"fmt"
"reflect"

"github.com/samber/lo"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -15,7 +14,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

operatorerrors "github.com/kong/gateway-operator/internal/errors"
"github.com/kong/gateway-operator/modules/manager/logging"

configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1"
Expand All @@ -41,7 +39,7 @@ func KongConsumerReconciliationWatchOptions(
func(b *ctrl.Builder) *ctrl.Builder {
return b.For(&configurationv1.KongConsumer{},
builder.WithPredicates(
predicate.NewPredicateFuncs(kongConsumerRefersToKonnectGatewayControlPlane),
predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1.KongConsumer]),
),
)
},
Expand Down Expand Up @@ -72,23 +70,6 @@ func KongConsumerReconciliationWatchOptions(
}
}

// kongConsumerRefersToKonnectGatewayControlPlane returns true if the KongConsumer
// refers to a KonnectGatewayControlPlane.
func kongConsumerRefersToKonnectGatewayControlPlane(obj client.Object) bool {
kongConsumer, ok := obj.(*configurationv1.KongConsumer)
if !ok {
ctrllog.FromContext(context.Background()).Error(
operatorerrors.ErrUnexpectedObject,
"failed to run predicate function",
"expected", "KongConsumer", "found", reflect.TypeOf(obj),
)
return false
}

cpRef := kongConsumer.Spec.ControlPlaneRef
return cpRef != nil && cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef
}

func enqueueKongConsumerForKonnectAPIAuthConfiguration(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
Expand Down
21 changes: 1 addition & 20 deletions controller/konnect/watch_kongconsumergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package konnect
import (
"context"
"fmt"
"reflect"

"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -14,7 +13,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

operatorerrors "github.com/kong/gateway-operator/internal/errors"
"github.com/kong/gateway-operator/modules/manager/logging"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -39,7 +37,7 @@ func KongConsumerGroupReconciliationWatchOptions(
func(b *ctrl.Builder) *ctrl.Builder {
return b.For(&configurationv1beta1.KongConsumerGroup{},
builder.WithPredicates(
predicate.NewPredicateFuncs(kongConsumerGroupRefersToKonnectGatewayControlPlane),
predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1beta1.KongConsumerGroup]),
),
)
},
Expand All @@ -62,23 +60,6 @@ func KongConsumerGroupReconciliationWatchOptions(
}
}

// kongConsumerGroupRefersToKonnectGatewayControlPlane returns true if the KongConsumerGroup
// refers to a KonnectGatewayControlPlane.
func kongConsumerGroupRefersToKonnectGatewayControlPlane(obj client.Object) bool {
kongConsumerGroup, ok := obj.(*configurationv1beta1.KongConsumerGroup)
if !ok {
ctrllog.FromContext(context.Background()).Error(
operatorerrors.ErrUnexpectedObject,
"failed to run predicate function",
"expected", "KongConsumerGroup", "found", reflect.TypeOf(obj),
)
return false
}

cpRef := kongConsumerGroup.Spec.ControlPlaneRef
return cpRef != nil && cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef
}

func enqueueKongConsumerGroupForKonnectAPIAuthConfiguration(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
Expand Down
18 changes: 1 addition & 17 deletions controller/konnect/watch_kongkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package konnect
import (
"context"
"fmt"
"reflect"

"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -14,7 +13,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

operatorerrors "github.com/kong/gateway-operator/internal/errors"
"github.com/kong/gateway-operator/modules/manager/logging"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -27,7 +25,7 @@ func KongKeyReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) *
func(b *ctrl.Builder) *ctrl.Builder {
return b.For(&configurationv1alpha1.KongKey{},
builder.WithPredicates(
predicate.NewPredicateFuncs(kongKeyRefersToKonnectControlPlane),
predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongKey]),
),
)
},
Expand All @@ -50,20 +48,6 @@ func KongKeyReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) *
}
}

func kongKeyRefersToKonnectControlPlane(obj client.Object) bool {
kongKey, ok := obj.(*configurationv1alpha1.KongKey)
if !ok {
ctrllog.FromContext(context.Background()).Error(
operatorerrors.ErrUnexpectedObject,
"failed to run predicate function",
"expected", "KongKey", "found", reflect.TypeOf(obj),
)
return false
}
return kongKey.Spec.ControlPlaneRef != nil &&
kongKey.Spec.ControlPlaneRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef
}

func enqueueKongKeyForKonnectAPIAuthConfiguration(cl client.Client) handler.MapFunc {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
auth, ok := obj.(*konnectv1alpha1.KonnectAPIAuthConfiguration)
Expand Down
25 changes: 1 addition & 24 deletions controller/konnect/watch_kongpluginbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package konnect
import (
"context"
"fmt"
"reflect"

"github.com/samber/lo"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -15,7 +14,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

operatorerrors "github.com/kong/gateway-operator/internal/errors"
"github.com/kong/gateway-operator/modules/manager/logging"

configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1"
Expand Down Expand Up @@ -45,7 +43,7 @@ func KongPluginBindingReconciliationWatchOptions(
func(b *ctrl.Builder) *ctrl.Builder {
return b.For(&configurationv1alpha1.KongPluginBinding{},
builder.WithPredicates(
predicate.NewPredicateFuncs(kongPluginBindingRefersToKonnectGatewayControlPlane),
predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongPluginBinding]),
),
)
},
Expand Down Expand Up @@ -108,27 +106,6 @@ func KongPluginBindingReconciliationWatchOptions(
}
}

// -----------------------------------------------------------------------------
// KongPluginBinding reconciler - Watch Predicates
// -----------------------------------------------------------------------------

// kongPluginBindingRefersToKonnectGatewayControlPlane returns true if the KongPluginBinding
// refers to a KonnectGatewayControlPlane.
func kongPluginBindingRefersToKonnectGatewayControlPlane(obj client.Object) bool {
kongPB, ok := obj.(*configurationv1alpha1.KongPluginBinding)
if !ok {
ctrllog.FromContext(context.Background()).Error(
operatorerrors.ErrUnexpectedObject,
"failed to run predicate function",
"expected", "KongPluginBinding", "found", reflect.TypeOf(obj),
)
return false
}

cpRef := kongPB.Spec.ControlPlaneRef
return cpRef != nil && cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef
}

// -----------------------------------------------------------------------------
// KongPluginBinding reconciler - Watch Mappers
// -----------------------------------------------------------------------------
Expand Down
21 changes: 1 addition & 20 deletions controller/konnect/watch_kongservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package konnect
import (
"context"
"fmt"
"reflect"

"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -14,7 +13,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

operatorerrors "github.com/kong/gateway-operator/internal/errors"
"github.com/kong/gateway-operator/modules/manager/logging"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand All @@ -38,7 +36,7 @@ func KongServiceReconciliationWatchOptions(
func(b *ctrl.Builder) *ctrl.Builder {
return b.For(&configurationv1alpha1.KongService{},
builder.WithPredicates(
predicate.NewPredicateFuncs(kongServiceRefersToKonnectGatewayControlPlane),
predicate.NewPredicateFuncs(objRefersToKonnectGatewayControlPlane[configurationv1alpha1.KongService]),
),
)
},
Expand All @@ -61,23 +59,6 @@ func KongServiceReconciliationWatchOptions(
}
}

// kongServiceRefersToKonnectGatewayControlPlane returns true if the KongService
// refers to a KonnectGatewayControlPlane.
func kongServiceRefersToKonnectGatewayControlPlane(obj client.Object) bool {
kongSvc, ok := obj.(*configurationv1alpha1.KongService)
if !ok {
ctrllog.FromContext(context.Background()).Error(
operatorerrors.ErrUnexpectedObject,
"failed to run predicate function",
"expected", "KongService", "found", reflect.TypeOf(obj),
)
return false
}

cpRef := kongSvc.Spec.ControlPlaneRef
return cpRef != nil && cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef
}

func enqueueKongServiceForKonnectAPIAuthConfiguration(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
Expand Down
Loading

0 comments on commit a8465d5

Please sign in to comment.