Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(konnect): add generic objectListToReconcileRequests #680

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
})
}
Loading