Skip to content

Commit

Permalink
fix: fix enqueueObjectForKonnectGatewayControlPlane type parameters (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek authored Oct 29, 2024
1 parent 6a80ea6 commit 3a4ad19
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 13 deletions.
15 changes: 13 additions & 2 deletions controller/konnect/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func objectListToReconcileRequests[
items []T,
filters ...func(TPtr) bool,
) []ctrl.Request {
if len(items) == 0 {
return nil
}

ret := make([]ctrl.Request, 0, len(items))
for _, item := range items {
var e TPtr = &item
Expand All @@ -151,6 +155,10 @@ func objectListToReconcileRequests[
// as the object.
func enqueueObjectForKonnectGatewayControlPlane[
TList interface {
GetItems() []T
},
TListPtr interface {
*TList
client.ObjectList
GetItems() []T
},
Expand All @@ -165,8 +173,11 @@ func enqueueObjectForKonnectGatewayControlPlane[
if !ok {
return nil
}
var l TList
if err := cl.List(ctx, l,
var (
l TList
lPtr TListPtr = &l
)
if err := cl.List(ctx, lPtr,
// TODO: change this when cross namespace refs are allowed.
client.InNamespace(cp.GetNamespace()),
client.MatchingFields{
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongcacertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func KongCACertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongCACertificateList](
enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongCACertificateList](
cl, IndexFieldKongCACertificateOnKonnectGatewayControlPlane,
),
),
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongcertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func KongCertificateReconciliationWatchOptions(cl client.Client) []func(*ctrl.Bu
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongCertificateList](
enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongCertificateList](
cl, IndexFieldKongCertificateOnKonnectGatewayControlPlane,
),
),
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func KongConsumerReconciliationWatchOptions(
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueObjectForKonnectGatewayControlPlane[*configurationv1.KongConsumerList](
enqueueObjectForKonnectGatewayControlPlane[configurationv1.KongConsumerList](
cl, IndexFieldKongConsumerOnKonnectGatewayControlPlane,
),
),
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongconsumergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func KongConsumerGroupReconciliationWatchOptions(
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueObjectForKonnectGatewayControlPlane[*configurationv1beta1.KongConsumerGroupList](
enqueueObjectForKonnectGatewayControlPlane[configurationv1beta1.KongConsumerGroupList](
cl, IndexFieldKongConsumerGroupOnKonnectGatewayControlPlane,
),
),
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongdataplanecertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func KongDataPlaneClientCertificateReconciliationWatchOptions(cl client.Client)
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongDataPlaneClientCertificateList](
enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongDataPlaneClientCertificateList](
cl, IndexFieldKongDataPlaneClientCertificateOnKonnectGatewayControlPlane,
),
),
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func KongKeyReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder) *
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongKeyList](
enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongKeyList](
cl, IndexFieldKongKeyOnKonnectGatewayControlPlane,
),
),
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongkeyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func KongKeySetReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongKeySetList](
enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongKeySetList](
cl, IndexFieldKongKeySetOnKonnectGatewayControlPlane,
),
),
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongpluginbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func KongPluginBindingReconciliationWatchOptions(
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongPluginBindingList](
enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongPluginBindingList](
cl, IndexFieldKongPluginBindingKonnectGatewayControlPlane,
),
),
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func KongServiceReconciliationWatchOptions(
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongServiceList](
enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongServiceList](
cl, IndexFieldKongServiceOnKonnectGatewayControlPlane,
),
),
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongupstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func KongUpstreamReconciliationWatchOptions(
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongUpstreamList](
enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongUpstreamList](
cl, IndexFieldKongUpstreamOnKonnectGatewayControlPlane,
),
),
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongvault.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func KongVaultReconciliationWatchOptions(cl client.Client) []func(*ctrl.Builder)
return b.Watches(
&konnectv1alpha1.KonnectGatewayControlPlane{},
handler.EnqueueRequestsFromMapFunc(
enqueueObjectForKonnectGatewayControlPlane[*configurationv1alpha1.KongVaultList](
enqueueObjectForKonnectGatewayControlPlane[configurationv1alpha1.KongVaultList](
cl, IndexFieldKongVaultOnKonnectGatewayControlPlane,
),
),
Expand Down
120 changes: 120 additions & 0 deletions controller/konnect/watch_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package konnect

import (
"context"
"testing"

"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
fakectrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/kong/gateway-operator/controller/konnect/constraints"
"github.com/kong/gateway-operator/modules/manager/scheme"

configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1"
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
Expand Down Expand Up @@ -80,3 +85,118 @@ func TestObjectListToReconcileRequests(t *testing.T) {
}
})
}

func TestEnqueueObjectForKonnectGatewayControlPlane(t *testing.T) {
cp := &konnectv1alpha1.KonnectGatewayControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "test1",
Namespace: "default",
},
}
t.Run("KongConsumer", func(t *testing.T) {
tests := []struct {
name string
index string
list []client.Object
extractFunc client.IndexerFunc
expected []ctrl.Request
}{
{
name: "no ControlPlane reference",
index: IndexFieldKongConsumerOnKonnectGatewayControlPlane,
extractFunc: kongConsumerReferencesKonnectGatewayControlPlane,
list: []client.Object{
&configurationv1.KongConsumer{
ObjectMeta: metav1.ObjectMeta{
Name: "consumer1",
Namespace: "default",
},
},
&configurationv1.KongConsumer{
ObjectMeta: metav1.ObjectMeta{
Name: "consumer2",
Namespace: "default",
},
},
},
},
{
name: "1 KongConumser refers to KonnectGatewayControlPlane",
index: IndexFieldKongConsumerOnKonnectGatewayControlPlane,
extractFunc: kongConsumerReferencesKonnectGatewayControlPlane,
list: []client.Object{
&configurationv1.KongConsumer{
ObjectMeta: metav1.ObjectMeta{
Name: "consumer1",
Namespace: "default",
},
Spec: configurationv1.KongConsumerSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: cp.Name,
},
},
},
},
&configurationv1.KongConsumer{
ObjectMeta: metav1.ObjectMeta{
Name: "consumer2",
Namespace: "default",
},
},
},
expected: []ctrl.Request{
{
NamespacedName: types.NamespacedName{
Name: "consumer1",
Namespace: "default",
},
},
},
},
{
name: "1 KongConumser refers to a different KonnectGatewayControlPlane",
index: IndexFieldKongConsumerOnKonnectGatewayControlPlane,
extractFunc: kongConsumerReferencesKonnectGatewayControlPlane,
list: []client.Object{
&configurationv1.KongConsumer{
ObjectMeta: metav1.ObjectMeta{
Name: "consumer1",
Namespace: "default",
},
Spec: configurationv1.KongConsumerSpec{
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: "different-cp",
},
},
},
},
&configurationv1.KongConsumer{
ObjectMeta: metav1.ObjectMeta{
Name: "consumer2",
Namespace: "default",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cl := fakectrlruntimeclient.NewClientBuilder().
WithScheme(scheme.Get()).
WithObjects(tt.list...).
WithIndex(&configurationv1.KongConsumer{}, tt.index, tt.extractFunc).
Build()
require.NotNil(t, cl)

f := enqueueObjectForKonnectGatewayControlPlane[configurationv1.KongConsumerList](cl, tt.index)
requests := f(context.Background(), cp)
require.Len(t, requests, len(tt.expected))
require.Equal(t, tt.expected, requests)
})
}
})
}

0 comments on commit 3a4ad19

Please sign in to comment.