Skip to content

Commit

Permalink
refactor(konnect): add generic objectListToReconcileRequests (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek authored Oct 2, 2024
1 parent 0a2c4d5 commit 820f2c8
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 64 deletions.
25 changes: 25 additions & 0 deletions controller/konnect/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"

"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -106,3 +107,27 @@ func controlPlaneIsRefKonnectNamespacedRef[
}
return cpRef, cpRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef
}

// objectListToReconcileRequests converts a list of objects to a list of reconcile requests.
func objectListToReconcileRequests[
T any,
TPtr interface {
*T
client.Object
},
](
items []T,
) []ctrl.Request {
ret := make([]ctrl.Request, 0, len(items))
for _, item := range items {
var e TPtr = &item
ret = append(ret, ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: e.GetNamespace(),
Name: e.GetName(),
},
})
}

return ret
}
12 changes: 1 addition & 11 deletions controller/konnect/watch_credentialacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,6 @@ func kongCredentialACLForKongConsumer(
return nil
}

var ret []reconcile.Request
for _, cred := range l.Items {
ret = append(ret, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: cred.Namespace,
Name: cred.Name,
},
},
)
}
return ret
return objectListToReconcileRequests(l.Items)
}
}
12 changes: 1 addition & 11 deletions controller/konnect/watch_credentialapikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,6 @@ func kongCredentialAPIKeyForKongConsumer(
return nil
}

var ret []reconcile.Request
for _, cred := range l.Items {
ret = append(ret, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: cred.Namespace,
Name: cred.Name,
},
},
)
}
return ret
return objectListToReconcileRequests(l.Items)
}
}
12 changes: 1 addition & 11 deletions controller/konnect/watch_credentialbasicauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,6 @@ func kongCredentialBasicAuthForKongConsumer(
return nil
}

var ret []reconcile.Request
for _, cred := range l.Items {
ret = append(ret, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: cred.Namespace,
Name: cred.Name,
},
},
)
}
return ret
return objectListToReconcileRequests(l.Items)
}
}
12 changes: 1 addition & 11 deletions controller/konnect/watch_credentialjwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,6 @@ func kongCredentialJWTForKongConsumer(
return nil
}

var ret []reconcile.Request
for _, cred := range l.Items {
ret = append(ret, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: cred.Namespace,
Name: cred.Name,
},
},
)
}
return ret
return objectListToReconcileRequests(l.Items)
}
}
11 changes: 2 additions & 9 deletions controller/konnect/watch_kongconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/samber/lo"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand Down Expand Up @@ -215,13 +214,7 @@ func enqueueKongConsumerForKongConsumerGroup(
}); err != nil {
return nil
}
return lo.Map(l.Items, func(consumer configurationv1.KongConsumer, _ int) reconcile.Request {
return reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: consumer.Namespace,
Name: consumer.Name,
},
}
})

return objectListToReconcileRequests(l.Items)
}
}
13 changes: 2 additions & 11 deletions controller/konnect/watch_kongsni.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func kongSNIRefersToKonnectGatewayControlPlane(
return true
}

return cert.Spec.ControlPlaneRef != nil && cert.Spec.ControlPlaneRef.Type == configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef
return objHasControlPlaneRefKonnectNamespacedRef(&cert)
}
}

Expand All @@ -82,15 +82,6 @@ func enqueueKongSNIForKongCertificate(
return nil
}

ret := make([]reconcile.Request, 0, len(sniList.Items))
for _, sni := range sniList.Items {
ret = append(ret, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: sni.Namespace,
Name: sni.Name,
},
})
}
return ret
return objectListToReconcileRequests(sniList.Items)
}
}
39 changes: 39 additions & 0 deletions controller/konnect/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

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

"github.com/kong/gateway-operator/controller/konnect/constraints"
Expand Down Expand Up @@ -41,3 +42,41 @@ func testReconciliationWatchOptionsForEntity[
_ = watchOptions
})
}

func TestObjectListToReconcileRequests(t *testing.T) {
t.Run("KongConsumer", func(t *testing.T) {
tests := []struct {
name string
list []configurationv1.KongConsumer
}{
{
name: "KongConsumer",
list: []configurationv1.KongConsumer{
{
ObjectMeta: metav1.ObjectMeta{
Name: "consumer1",
Namespace: "default",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "consumer2",
Namespace: "default",
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
requests := objectListToReconcileRequests(tt.list)
require.Len(t, requests, len(tt.list))
for i, item := range tt.list {
require.Equal(t, item.GetName(), requests[i].Name)
require.Equal(t, item.GetNamespace(), requests[i].Namespace)
}
})
}
})
}

0 comments on commit 820f2c8

Please sign in to comment.