Skip to content

Commit

Permalink
refactor(konnect): add enqueueObjectForKonnectGatewayControlPlane to …
Browse files Browse the repository at this point in the history
…generically list objects that refer to KonnectGatewayControlPlane (#816)
  • Loading branch information
pmalek authored Oct 28, 2024
1 parent 0d20781 commit ae44640
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 268 deletions.
44 changes: 39 additions & 5 deletions controller/konnect/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/kong/gateway-operator/controller/konnect/constraints"
operatorerrors "github.com/kong/gateway-operator/internal/errors"
Expand Down Expand Up @@ -118,11 +119,8 @@ func controlPlaneRefIsKonnectNamespacedRef[

// objectListToReconcileRequests converts a list of objects to a list of reconcile requests.
func objectListToReconcileRequests[
T any,
TPtr interface {
*T
client.Object
},
T constraints.SupportedKonnectEntityType,
TPtr constraints.EntityType[T],
](
items []T,
filters ...func(TPtr) bool,
Expand All @@ -145,3 +143,39 @@ func objectListToReconcileRequests[

return ret
}

// enqueueObjectForKonnectGatewayControlPlane returns a function that enqueues
// reconcile requests for objects matching the provided list type, so for example
// providing KongConsumerList, this function will enqueue reconcile requests for
// KongConsumers that refer to the KonnectGatewayControlPlane that was provided
// as the object.
func enqueueObjectForKonnectGatewayControlPlane[
TList interface {
client.ObjectList
GetItems() []T
},
T constraints.SupportedKonnectEntityType,
TT constraints.EntityType[T],
](
cl client.Client,
index string,
) func(context.Context, client.Object) []reconcile.Request {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane)
if !ok {
return nil
}
var l TList
if err := cl.List(ctx, l,
// TODO: change this when cross namespace refs are allowed.
client.InNamespace(cp.GetNamespace()),
client.MatchingFields{
index: cp.GetNamespace() + "/" + cp.GetName(),
},
); err != nil {
return nil
}

return objectListToReconcileRequests[T, TT](l.GetItems())
}
}
27 changes: 3 additions & 24 deletions controller/konnect/watch_kongcacertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func KongCACertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueKongCACertificateForKonnectControlPlane(cl),
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongCACertificateList](
cl, IndexFieldKongCACertificateOnKonnectGatewayControlPlane,
),
),
)
},
Expand Down Expand Up @@ -120,26 +122,3 @@ func enqueueKongCACertificateForKonnectAPIAuthConfiguration(cl client.Client) ha
return ret
}
}

func enqueueKongCACertificateForKonnectControlPlane(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane)
if !ok {
return nil
}
var l configurationv1alpha1.KongCACertificateList
if err := cl.List(ctx, &l,
// TODO: change this when cross namespace refs are allowed.
client.InNamespace(cp.GetNamespace()),
client.MatchingFields{
IndexFieldKongCACertificateOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name,
},
); err != nil {
return nil
}

return objectListToReconcileRequests(l.Items)
}
}
27 changes: 3 additions & 24 deletions controller/konnect/watch_kongcertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func KongCertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.Bu
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueKongCertificateForKonnectControlPlane(cl),
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongCertificateList](
cl, IndexFieldKongCertificateOnKonnectGatewayControlPlane,
),
),
)
},
Expand Down Expand Up @@ -120,26 +122,3 @@ func enqueueKongCertificateForKonnectAPIAuthConfiguration(cl client.Client) hand
return ret
}
}

func enqueueKongCertificateForKonnectControlPlane(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane)
if !ok {
return nil
}
var l configurationv1alpha1.KongCertificateList
if err := cl.List(ctx, &l,
// TODO: change this when cross namespace refs are allowed.
client.InNamespace(cp.GetNamespace()),
client.MatchingFields{
IndexFieldKongCertificateOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name,
},
); err != nil {
return nil
}

return objectListToReconcileRequests(l.Items)
}
}
27 changes: 3 additions & 24 deletions controller/konnect/watch_kongconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func KongConsumerReconciliationWatchOptions(
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueKongConsumerForKonnectGatewayControlPlane(cl),
enqueueObjectForKonnectGatewayControlPlane[*configurationv1.KongConsumerList](
cl, IndexFieldKongConsumerOnKonnectGatewayControlPlane,
),
),
)
},
Expand Down Expand Up @@ -144,29 +146,6 @@ func enqueueKongConsumerForKonnectAPIAuthConfiguration(
}
}

func enqueueKongConsumerForKonnectGatewayControlPlane(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane)
if !ok {
return nil
}
var l configurationv1.KongConsumerList
if err := cl.List(ctx, &l,
// TODO: change this when cross namespace refs are allowed.
client.InNamespace(cp.GetNamespace()),
client.MatchingFields{
IndexFieldKongConsumerOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name,
},
); err != nil {
return nil
}

return objectListToReconcileRequests(l.Items)
}
}

func enqueueKongConsumerForKongConsumerGroup(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
Expand Down
27 changes: 3 additions & 24 deletions controller/konnect/watch_kongconsumergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func KongConsumerGroupReconciliationWatchOptions(
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueKongConsumerGroupForKonnectGatewayControlPlane(cl),
enqueueObjectForKonnectGatewayControlPlane[*configurationv1beta1.KongConsumerGroupList](
cl, IndexFieldKongConsumerGroupOnKonnectGatewayControlPlane,
),
),
)
},
Expand Down Expand Up @@ -134,26 +136,3 @@ func enqueueKongConsumerGroupForKonnectAPIAuthConfiguration(
return ret
}
}

func enqueueKongConsumerGroupForKonnectGatewayControlPlane(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane)
if !ok {
return nil
}
var l configurationv1beta1.KongConsumerGroupList
if err := cl.List(ctx, &l,
// TODO: change this when cross namespace refs are allowed.
client.InNamespace(cp.GetNamespace()),
client.MatchingFields{
IndexFieldKongConsumerGroupOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name,
},
); err != nil {
return nil
}

return objectListToReconcileRequests(l.Items)
}
}
25 changes: 3 additions & 22 deletions controller/konnect/watch_kongdataplanecertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,15 @@ func KongDataPlaneClientCertificateReconciliationWatchOptions(cl client.Client)
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueKongDataPlaneClientCertificateForKonnectControlPlane(cl),
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongDataPlaneClientCertificateList](
cl, IndexFieldKongDataPlaneClientCertificateOnKonnectGatewayControlPlane,
),
),
)
},
}
}

func enqueueKongDataPlaneClientCertificateForKonnectControlPlane(cl client.Client) handler.MapFunc {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
auth, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane)
if !ok {
return nil
}
var l configurationv1alpha1.KongDataPlaneClientCertificateList
if err := cl.List(ctx, &l,
// TODO: change this when cross namespace refs are allowed.
client.InNamespace(auth.GetNamespace()),
client.MatchingFields{
IndexFieldKongDataPlaneClientCertificateOnKonnectGatewayControlPlane: client.ObjectKeyFromObject(auth).String(),
},
); err != nil {
return nil
}

return objectListToReconcileRequests(l.Items)
}
}

func enqueueKongDataPlaneClientCertificateForKonnectAPIAuthConfiguration(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
Expand Down
27 changes: 3 additions & 24 deletions controller/konnect/watch_kongkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ func KongKeyReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) *
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueKongKeyForKonnectControlPlane(cl),
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongKeyList](
cl, IndexFieldKongKeyOnKonnectGatewayControlPlane,
),
),
)
},
Expand Down Expand Up @@ -129,29 +131,6 @@ func enqueueKongKeyForKonnectAPIAuthConfiguration(cl client.Client) handler.MapF
}
}

func enqueueKongKeyForKonnectControlPlane(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane)
if !ok {
return nil
}
var l configurationv1alpha1.KongKeyList
if err := cl.List(ctx, &l,
// TODO: change this when cross namespace refs are allowed.
client.InNamespace(cp.GetNamespace()),
client.MatchingFields{
IndexFieldKongKeyOnKonnectGatewayControlPlane: cp.GetNamespace() + "/" + cp.GetName(),
},
); err != nil {
return nil
}

return objectListToReconcileRequests(l.Items)
}
}

func enqueueKongKeyForKongKeySet(cl client.Client) handler.MapFunc {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
keySet, ok := obj.(*configurationv1alpha1.KongKeySet)
Expand Down
27 changes: 3 additions & 24 deletions controller/konnect/watch_kongkeyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func KongKeySetReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueKongKeySetForKonnectControlPlane(cl),
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongKeySetList](
cl, IndexFieldKongKeySetOnKonnectGatewayControlPlane,
),
),
)
},
Expand Down Expand Up @@ -120,26 +122,3 @@ func enqueueKongKeySetForKonnectAPIAuthConfiguration(cl client.Client) handler.M
return ret
}
}

func enqueueKongKeySetForKonnectControlPlane(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane)
if !ok {
return nil
}
var l configurationv1alpha1.KongKeySetList
if err := cl.List(ctx, &l,
// TODO: change this when cross namespace refs are allowed.
client.InNamespace(cp.GetNamespace()),
client.MatchingFields{
IndexFieldKongKeySetOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name,
},
); err != nil {
return nil
}

return objectListToReconcileRequests(l.Items)
}
}
27 changes: 3 additions & 24 deletions controller/konnect/watch_kongpluginbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func KongPluginBindingReconciliationWatchOptions(
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueKongPluginBindingForKonnectGatewayControlPlane(cl),
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongPluginBindingList](
cl, IndexFieldKongPluginBindingKonnectGatewayControlPlane,
),
),
)
},
Expand Down Expand Up @@ -186,29 +188,6 @@ func enqueueKongPluginBindingForKonnectAPIAuthConfiguration(
}
}

func enqueueKongPluginBindingForKonnectGatewayControlPlane(
cl client.Client,
) func(ctx context.Context, obj client.Object) []reconcile.Request {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
cp, ok := obj.(*konnectv1alpha1.KonnectGatewayControlPlane)
if !ok {
return nil
}
var l configurationv1alpha1.KongPluginBindingList
if err := cl.List(ctx, &l,
// TODO: change this when cross namespace refs are allowed.
client.InNamespace(cp.GetNamespace()),
client.MatchingFields{
IndexFieldKongPluginBindingKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name,
},
); err != nil {
return nil
}

return objectListToReconcileRequests(l.Items)
}
}

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

0 comments on commit ae44640

Please sign in to comment.