diff --git a/controller/konnect/ops/ops_credentialapikey.go b/controller/konnect/ops/ops_credentialapikey.go index 2bac554b4..bebbf56b8 100644 --- a/controller/konnect/ops/ops_credentialapikey.go +++ b/controller/konnect/ops/ops_credentialapikey.go @@ -96,7 +96,7 @@ func deleteKongCredentialAPIKey( sdkkonnectops.DeleteKeyAuthWithConsumerRequest{ ControlPlaneID: cpID, ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(), - // BasicAuthID: id, + KeyAuthID: id, }) if errWrap := wrapErrIfKonnectOpFailed(err, DeleteOp, cred); errWrap != nil { // Service delete operation returns an SDKError instead of a NotFoundError. diff --git a/controller/konnect/reconciler_generic.go b/controller/konnect/reconciler_generic.go index aee6e90a7..759ce5f7d 100644 --- a/controller/konnect/reconciler_generic.go +++ b/controller/konnect/reconciler_generic.go @@ -854,12 +854,6 @@ func handleKongConsumerRef[T constraints.SupportedKonnectEntityType, TEnt constr client.ObjectKeyFromObject(&consumer), constraints.EntityTypeName[T](), client.ObjectKeyFromObject(ent), ) } - if cred, ok := any(ent).(*configurationv1alpha1.KongCredentialHMAC); ok { - if cred.Status.Konnect == nil { - cred.Status.Konnect = &konnectv1alpha1.KonnectEntityStatusWithControlPlaneAndConsumerRefs{} - } - cred.Status.Konnect.ConsumerID = consumer.Status.Konnect.GetKonnectID() - } if res, errStatus := updateStatusWithCondition( ctx, cl, ent, diff --git a/test/envtest/consts.go b/test/envtest/consts.go index d063cf5b6..37768aee6 100644 --- a/test/envtest/consts.go +++ b/test/envtest/consts.go @@ -7,9 +7,6 @@ const ( // sync period. It's set to 60m that is virtually infinite for the tests. konnectInfiniteSyncTime = time.Minute * 60 - // konnectSyncTime is used for tests that want to verify behavior of the reconcilers relying on the fixed sync. - konnectSyncTime = 100 * time.Millisecond - // waitTime is a generic wait time for the tests' eventual conditions. waitTime = 10 * time.Second diff --git a/test/envtest/kongconsumercredential_acl_test.go b/test/envtest/kongconsumercredential_acl_test.go index 663fc57e2..8a5b5474e 100644 --- a/test/envtest/kongconsumercredential_acl_test.go +++ b/test/envtest/kongconsumercredential_acl_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - "k8s.io/apimachinery/pkg/watch" + k8serrors "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/kong/gateway-operator/controller/konnect" @@ -35,10 +35,6 @@ func TestKongConsumerCredential_ACL(t *testing.T) { mgr, logs := NewManager(t, ctx, cfg, scheme.Get()) - clientWithWatch, err := client.NewWithWatch(mgr.GetConfig(), client.Options{ - Scheme: scheme.Get(), - }) - require.NoError(t, err) clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) @@ -114,7 +110,7 @@ func TestKongConsumerCredential_ACL(t *testing.T) { require.NoError(t, manager.SetupCacheIndicesForKonnectTypes(ctx, mgr, false)) reconcilers := []Reconciler{ konnect.NewKonnectEntityReconciler(factory, false, mgr.GetClient(), - konnect.WithKonnectEntitySyncPeriod[configurationv1alpha1.KongCredentialACL](konnectSyncTime), + konnect.WithKonnectEntitySyncPeriod[configurationv1alpha1.KongCredentialACL](konnectInfiniteSyncTime), ), } @@ -141,24 +137,16 @@ func TestKongConsumerCredential_ACL(t *testing.T) { ) require.NoError(t, clientNamespaced.Delete(ctx, kongCredentialACL)) + assert.EventuallyWithT(t, + func(c *assert.CollectT) { + assert.True(c, k8serrors.IsNotFound( + clientNamespaced.Get(ctx, client.ObjectKeyFromObject(kongCredentialACL), kongCredentialACL), + )) + }, waitTime, tickTime, + "KongCredentialACL wasn't deleted but it should have been", + ) + assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.KongCredentialsACLSDK.AssertExpectations(t)) }, waitTime, tickTime) - - w := setupWatch[configurationv1alpha1.KongCredentialACLList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - - kongCredentialACL = deploy.KongCredentialACL(t, ctx, clientNamespaced, consumer.Name, aclGroup) - t.Logf("redeployed %s KongCredentialACL resource", client.ObjectKeyFromObject(kongCredentialACL)) - t.Logf("checking if KongConsumer %s removal will delete the associated credentials %s", - client.ObjectKeyFromObject(consumer), - client.ObjectKeyFromObject(kongCredentialACL), - ) - - require.NoError(t, clientNamespaced.Delete(ctx, consumer)) - _ = watchFor(t, ctx, w, watch.Modified, - func(c *configurationv1alpha1.KongCredentialACL) bool { - return c.Name == kongCredentialACL.Name - }, - "KongCredentialACL wasn't deleted but it should have been", - ) } diff --git a/test/envtest/kongconsumercredential_apikey_test.go b/test/envtest/kongconsumercredential_apikey_test.go new file mode 100644 index 000000000..d442147ed --- /dev/null +++ b/test/envtest/kongconsumercredential_apikey_test.go @@ -0,0 +1,152 @@ +package envtest + +import ( + "context" + "testing" + + sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" + sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" + "github.com/google/uuid" + "github.com/samber/lo" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/kong/gateway-operator/controller/konnect" + "github.com/kong/gateway-operator/controller/konnect/ops" + "github.com/kong/gateway-operator/modules/manager" + "github.com/kong/gateway-operator/modules/manager/scheme" + "github.com/kong/gateway-operator/test/helpers/deploy" + + configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" + configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" + "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1" +) + +func TestKongConsumerCredential_APIKey(t *testing.T) { + t.Parallel() + ctx, cancel := Context(t, context.Background()) + defer cancel() + + // Setup up the envtest environment. + cfg, ns := Setup(t, ctx, scheme.Get()) + + mgr, logs := NewManager(t, ctx, cfg, scheme.Get()) + + clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) + + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + + consumerID := uuid.NewString() + consumer := deploy.KongConsumerWithProgrammed(t, ctx, clientNamespaced, &configurationv1.KongConsumer{ + Username: "username1", + Spec: configurationv1.KongConsumerSpec{ + ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ + Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, + KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ + Name: cp.Name, + }, + }, + }, + }) + consumer.Status.Konnect = &v1alpha1.KonnectEntityStatusWithControlPlaneRef{ + ControlPlaneID: cp.GetKonnectStatus().GetKonnectID(), + KonnectEntityStatus: v1alpha1.KonnectEntityStatus{ + ID: consumerID, + ServerURL: cp.GetKonnectStatus().GetServerURL(), + OrgID: cp.GetKonnectStatus().GetOrgID(), + }, + } + require.NoError(t, clientNamespaced.Status().Update(ctx, consumer)) + + kongCredentialAPIKey := deploy.KongCredentialAPIKey(t, ctx, clientNamespaced, consumer.Name) + keyID := uuid.NewString() + tags := []string{ + "k8s-generation:1", + "k8s-group:configuration.konghq.com", + "k8s-kind:KongCredentialAPIKey", + "k8s-name:" + kongCredentialAPIKey.Name, + "k8s-namespace:" + ns.Name, + "k8s-uid:" + string(kongCredentialAPIKey.GetUID()), + "k8s-version:v1alpha1", + } + + factory := ops.NewMockSDKFactory(t) + factory.SDK.KongCredentialsAPIKeySDK.EXPECT(). + CreateKeyAuthWithConsumer( + mock.Anything, + sdkkonnectops.CreateKeyAuthWithConsumerRequest{ + ControlPlaneID: cp.GetKonnectStatus().GetKonnectID(), + ConsumerIDForNestedEntities: consumerID, + KeyAuthWithoutParents: sdkkonnectcomp.KeyAuthWithoutParents{ + Key: lo.ToPtr("key"), + Tags: tags, + }, + }, + ). + Return( + &sdkkonnectops.CreateKeyAuthWithConsumerResponse{ + KeyAuth: &sdkkonnectcomp.KeyAuth{ + ID: lo.ToPtr(keyID), + }, + }, + nil, + ) + factory.SDK.KongCredentialsAPIKeySDK.EXPECT(). + UpsertKeyAuthWithConsumer(mock.Anything, mock.Anything, mock.Anything).Maybe(). + Return( + &sdkkonnectops.UpsertKeyAuthWithConsumerResponse{ + KeyAuth: &sdkkonnectcomp.KeyAuth{ + ID: lo.ToPtr(keyID), + }, + }, + nil, + ) + + require.NoError(t, manager.SetupCacheIndicesForKonnectTypes(ctx, mgr, false)) + reconcilers := []Reconciler{ + konnect.NewKonnectEntityReconciler(factory, false, mgr.GetClient(), + konnect.WithKonnectEntitySyncPeriod[configurationv1alpha1.KongCredentialAPIKey](konnectInfiniteSyncTime), + ), + } + + StartReconcilers(ctx, t, mgr, logs, reconcilers...) + + assert.EventuallyWithT(t, func(c *assert.CollectT) { + assert.True(c, factory.SDK.KongCredentialsAPIKeySDK.AssertExpectations(t)) + }, waitTime, tickTime) + + factory.SDK.KongCredentialsAPIKeySDK.EXPECT(). + DeleteKeyAuthWithConsumer( + mock.Anything, + sdkkonnectops.DeleteKeyAuthWithConsumerRequest{ + ControlPlaneID: cp.GetKonnectStatus().GetKonnectID(), + ConsumerIDForNestedEntities: consumerID, + KeyAuthID: keyID, + }, + ). + Return( + &sdkkonnectops.DeleteKeyAuthWithConsumerResponse{ + StatusCode: 200, + }, + nil, + ) + + require.NoError(t, clientNamespaced.Delete(ctx, kongCredentialAPIKey)) + + assert.EventuallyWithT(t, + func(c *assert.CollectT) { + assert.True(c, k8serrors.IsNotFound( + clientNamespaced.Get(ctx, client.ObjectKeyFromObject(kongCredentialAPIKey), kongCredentialAPIKey), + )) + }, waitTime, tickTime, + "KongCredentialAPIKey wasn't deleted but it should have been", + ) + + assert.EventuallyWithT(t, func(c *assert.CollectT) { + assert.True(c, factory.SDK.KongCredentialsAPIKeySDK.AssertExpectations(t)) + }, waitTime, tickTime) +} diff --git a/test/envtest/kongconsumercredential_basicauth_test.go b/test/envtest/kongconsumercredential_basicauth_test.go index b009db5bc..8ac53ae3c 100644 --- a/test/envtest/kongconsumercredential_basicauth_test.go +++ b/test/envtest/kongconsumercredential_basicauth_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - "k8s.io/apimachinery/pkg/watch" + k8serrors "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/kong/gateway-operator/controller/konnect" @@ -35,10 +35,6 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) { mgr, logs := NewManager(t, ctx, cfg, scheme.Get()) - clientWithWatch, err := client.NewWithWatch(mgr.GetConfig(), client.Options{ - Scheme: scheme.Get(), - }) - require.NoError(t, err) clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) @@ -116,7 +112,7 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) { require.NoError(t, manager.SetupCacheIndicesForKonnectTypes(ctx, mgr, false)) reconcilers := []Reconciler{ konnect.NewKonnectEntityReconciler(factory, false, mgr.GetClient(), - konnect.WithKonnectEntitySyncPeriod[configurationv1alpha1.KongCredentialBasicAuth](konnectSyncTime), + konnect.WithKonnectEntitySyncPeriod[configurationv1alpha1.KongCredentialBasicAuth](konnectInfiniteSyncTime), ), } @@ -143,24 +139,16 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) { ) require.NoError(t, clientNamespaced.Delete(ctx, kongCredentialBasicAuth)) + assert.EventuallyWithT(t, + func(c *assert.CollectT) { + assert.True(c, k8serrors.IsNotFound( + clientNamespaced.Get(ctx, client.ObjectKeyFromObject(kongCredentialBasicAuth), kongCredentialBasicAuth), + )) + }, waitTime, tickTime, + "KongCredentialBasicAuth wasn't deleted but it should have been", + ) + assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.KongCredentialsBasicAuthSDK.AssertExpectations(t)) }, waitTime, tickTime) - - w := setupWatch[configurationv1alpha1.KongCredentialBasicAuthList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - - kongCredentialBasicAuth = deploy.KongCredentialBasicAuth(t, ctx, clientNamespaced, consumer.Name, username, password) - t.Logf("redeployed %s KongCredentialBasicAuth resource", client.ObjectKeyFromObject(kongCredentialBasicAuth)) - t.Logf("checking if KongConsumer %s removal will delete the associated credentials %s", - client.ObjectKeyFromObject(consumer), - client.ObjectKeyFromObject(kongCredentialBasicAuth), - ) - - require.NoError(t, clientNamespaced.Delete(ctx, consumer)) - _ = watchFor(t, ctx, w, watch.Modified, - func(c *configurationv1alpha1.KongCredentialBasicAuth) bool { - return c.Name == kongCredentialBasicAuth.Name - }, - "KongCredentialBasicAuth wasn't deleted but it should have been", - ) } diff --git a/test/envtest/kongconsumercredential_hmac_test.go b/test/envtest/kongconsumercredential_hmac_test.go index 30d6abf0b..b261922b9 100644 --- a/test/envtest/kongconsumercredential_hmac_test.go +++ b/test/envtest/kongconsumercredential_hmac_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - "k8s.io/apimachinery/pkg/watch" + k8serrors "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/kong/gateway-operator/controller/konnect" @@ -35,10 +35,6 @@ func TestKongConsumerCredential_HMAC(t *testing.T) { mgr, logs := NewManager(t, ctx, cfg, scheme.Get()) - clientWithWatch, err := client.NewWithWatch(mgr.GetConfig(), client.Options{ - Scheme: scheme.Get(), - }) - require.NoError(t, err) clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) @@ -113,7 +109,7 @@ func TestKongConsumerCredential_HMAC(t *testing.T) { require.NoError(t, manager.SetupCacheIndicesForKonnectTypes(ctx, mgr, false)) reconcilers := []Reconciler{ konnect.NewKonnectEntityReconciler(factory, false, mgr.GetClient(), - konnect.WithKonnectEntitySyncPeriod[configurationv1alpha1.KongCredentialHMAC](konnectSyncTime), + konnect.WithKonnectEntitySyncPeriod[configurationv1alpha1.KongCredentialHMAC](konnectInfiniteSyncTime), ), } @@ -140,24 +136,16 @@ func TestKongConsumerCredential_HMAC(t *testing.T) { ) require.NoError(t, clientNamespaced.Delete(ctx, kongCredentialHMAC)) + assert.EventuallyWithT(t, + func(c *assert.CollectT) { + assert.True(c, k8serrors.IsNotFound( + clientNamespaced.Get(ctx, client.ObjectKeyFromObject(kongCredentialHMAC), kongCredentialHMAC), + )) + }, waitTime, tickTime, + "KongCredentialHMAC wasn't deleted but it should have been", + ) + assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.KongCredentialsHMACSDK.AssertExpectations(t)) }, waitTime, tickTime) - - w := setupWatch[configurationv1alpha1.KongCredentialHMACList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - - kongCredentialHMAC = deploy.KongCredentialHMAC(t, ctx, clientNamespaced, consumer.Name) - t.Logf("redeployed %s KongCredentialHMAC resource", client.ObjectKeyFromObject(kongCredentialHMAC)) - t.Logf("checking if KongConsumer %s removal will delete the associated credentials %s", - client.ObjectKeyFromObject(consumer), - client.ObjectKeyFromObject(kongCredentialHMAC), - ) - - require.NoError(t, clientNamespaced.Delete(ctx, consumer)) - _ = watchFor(t, ctx, w, watch.Modified, - func(c *configurationv1alpha1.KongCredentialHMAC) bool { - return c.Name == kongCredentialHMAC.Name - }, - "KongCredentialHMAC wasn't deleted but it should have been", - ) } diff --git a/test/envtest/kongconsumercredential_jwt_test.go b/test/envtest/kongconsumercredential_jwt_test.go index e72ab304d..752320df5 100644 --- a/test/envtest/kongconsumercredential_jwt_test.go +++ b/test/envtest/kongconsumercredential_jwt_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - "k8s.io/apimachinery/pkg/watch" + k8serrors "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/kong/gateway-operator/controller/konnect" @@ -35,10 +35,6 @@ func TestKongConsumerCredential_JWT(t *testing.T) { mgr, logs := NewManager(t, ctx, cfg, scheme.Get()) - clientWithWatch, err := client.NewWithWatch(mgr.GetConfig(), client.Options{ - Scheme: scheme.Get(), - }) - require.NoError(t, err) clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) @@ -114,7 +110,7 @@ func TestKongConsumerCredential_JWT(t *testing.T) { require.NoError(t, manager.SetupCacheIndicesForKonnectTypes(ctx, mgr, false)) reconcilers := []Reconciler{ konnect.NewKonnectEntityReconciler(factory, false, mgr.GetClient(), - konnect.WithKonnectEntitySyncPeriod[configurationv1alpha1.KongCredentialJWT](konnectSyncTime), + konnect.WithKonnectEntitySyncPeriod[configurationv1alpha1.KongCredentialJWT](konnectInfiniteSyncTime), ), } @@ -139,26 +135,19 @@ func TestKongConsumerCredential_JWT(t *testing.T) { }, nil, ) + require.NoError(t, clientNamespaced.Delete(ctx, kongCredentialJWT)) + assert.EventuallyWithT(t, + func(c *assert.CollectT) { + assert.True(c, k8serrors.IsNotFound( + clientNamespaced.Get(ctx, client.ObjectKeyFromObject(kongCredentialJWT), kongCredentialJWT), + )) + }, waitTime, tickTime, + "KongCredentialJWT wasn't deleted but it should have been", + ) + assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.KongCredentialsJWTSDK.AssertExpectations(t)) }, waitTime, tickTime) - - w := setupWatch[configurationv1alpha1.KongCredentialJWTList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - - kongCredentialJWT = deploy.KongCredentialJWT(t, ctx, clientNamespaced, consumer.Name) - t.Logf("redeployed %s KongCredentialJWT resource", client.ObjectKeyFromObject(kongCredentialJWT)) - t.Logf("checking if KongConsumer %s removal will delete the associated credentials %s", - client.ObjectKeyFromObject(consumer), - client.ObjectKeyFromObject(kongCredentialJWT), - ) - - require.NoError(t, clientNamespaced.Delete(ctx, consumer)) - _ = watchFor(t, ctx, w, watch.Modified, - func(c *configurationv1alpha1.KongCredentialJWT) bool { - return c.Name == kongCredentialJWT.Name - }, - "KongCredentialJWT wasn't deleted but it should have been", - ) } diff --git a/test/envtest/konnect_entities_gatewaycontrolplane_test.go b/test/envtest/konnect_entities_gatewaycontrolplane_test.go index 4bd8c2224..1b68f51db 100644 --- a/test/envtest/konnect_entities_gatewaycontrolplane_test.go +++ b/test/envtest/konnect_entities_gatewaycontrolplane_test.go @@ -121,7 +121,8 @@ var konnectGatewayControlPlaneTestCases = []konnectEntityReconcilerTestCase{ CreateControlPlane( mock.Anything, mock.MatchedBy(func(req sdkkonnectcomp.CreateControlPlaneRequest) bool { - return req.Name == "cp-2" + return req.Name == "cp-2" && + req.ClusterType != nil && *req.ClusterType == sdkkonnectcomp.ClusterTypeClusterTypeControlPlaneGroup }), ). Return( diff --git a/test/helpers/deploy/deploy_resources.go b/test/helpers/deploy/deploy_resources.go index 9e6fa74c0..ca6e53316 100644 --- a/test/helpers/deploy/deploy_resources.go +++ b/test/helpers/deploy/deploy_resources.go @@ -87,7 +87,7 @@ func KonnectAPIAuthConfiguration( opt(apiAuth) } require.NoError(t, cl.Create(ctx, apiAuth)) - t.Logf("deployed new %s KonnectAPIAuthConfiguration", client.ObjectKeyFromObject(apiAuth)) + logObjectCreate(t, apiAuth) return apiAuth } @@ -148,7 +148,7 @@ func KonnectGatewayControlPlane( opt(cp) } require.NoError(t, cl.Create(ctx, cp)) - t.Logf("deployed new %s KonnectGatewayControlPlane", client.ObjectKeyFromObject(cp)) + logObjectCreate(t, cp) return cp } @@ -215,7 +215,7 @@ func KongServiceAttachedToCP( opt(&kongService) } require.NoError(t, cl.Create(ctx, &kongService)) - t.Logf("deployed new %s KongService", client.ObjectKeyFromObject(&kongService)) + logObjectCreate(t, &kongService) return &kongService } @@ -251,7 +251,7 @@ func KongRouteAttachedToService( opt(&kongRoute) } require.NoError(t, cl.Create(ctx, &kongRoute)) - t.Logf("deployed new %s KongRoute", client.ObjectKeyFromObject(&kongRoute)) + logObjectCreate(t, &kongRoute) return &kongRoute } @@ -298,11 +298,40 @@ func KongPluginBinding( opt(kpb) } require.NoError(t, cl.Create(ctx, kpb)) - t.Logf("deployed new unmanaged KongPluginBinding %s", client.ObjectKeyFromObject(kpb)) + logObjectCreate(t, kpb) return kpb } +// KongCredentialAPIKey deploys a KongCredentialAPIKey resource and returns the resource. +func KongCredentialAPIKey( + t *testing.T, + ctx context.Context, + cl client.Client, + consumerName string, +) *configurationv1alpha1.KongCredentialAPIKey { + t.Helper() + + c := &configurationv1alpha1.KongCredentialAPIKey{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: "api-key-", + }, + Spec: configurationv1alpha1.KongCredentialAPIKeySpec{ + ConsumerRef: corev1.LocalObjectReference{ + Name: consumerName, + }, + KongCredentialAPIKeyAPISpec: configurationv1alpha1.KongCredentialAPIKeyAPISpec{ + Key: "key", + }, + }, + } + + require.NoError(t, cl.Create(ctx, c)) + logObjectCreate(t, c) + + return c +} + // KongCredentialBasicAuth deploys a KongCredentialBasicAuth resource and returns the resource. func KongCredentialBasicAuth( t *testing.T, @@ -330,7 +359,7 @@ func KongCredentialBasicAuth( } require.NoError(t, cl.Create(ctx, c)) - t.Logf("deployed new unmanaged KongCredentialBasicAuth %s", client.ObjectKeyFromObject(c)) + logObjectCreate(t, c) return c } @@ -360,7 +389,7 @@ func KongCredentialACL( } require.NoError(t, cl.Create(ctx, c)) - t.Logf("deployed new unmanaged KongCredentialACL %s", client.ObjectKeyFromObject(c)) + logObjectCreate(t, c) return c } @@ -389,7 +418,7 @@ func KongCredentialHMAC( } require.NoError(t, cl.Create(ctx, c)) - t.Logf("deployed new unmanaged KongCredentialHMAC %s", client.ObjectKeyFromObject(c)) + logObjectCreate(t, c) return c } @@ -418,7 +447,7 @@ func KongCredentialJWT( } require.NoError(t, cl.Create(ctx, c)) - t.Logf("deployed new unmanaged KongCredentialJWT %s", client.ObjectKeyFromObject(c)) + logObjectCreate(t, c) return c } @@ -449,7 +478,7 @@ func KongCACertificateAttachedToCP( }, } require.NoError(t, cl.Create(ctx, cert)) - t.Logf("deployed new KongCACertificate %s", client.ObjectKeyFromObject(cert)) + logObjectCreate(t, cert) return cert } @@ -481,7 +510,7 @@ func KongCertificateAttachedToCP( }, } require.NoError(t, cl.Create(ctx, cert)) - t.Logf("deployed new KongCertificate %s", client.ObjectKeyFromObject(cert)) + logObjectCreate(t, cert) return cert } @@ -514,7 +543,7 @@ func KongUpstreamAttachedToCP( } require.NoError(t, cl.Create(ctx, u)) - t.Logf("deployed new KongUpstream %s", client.ObjectKeyFromObject(u)) + logObjectCreate(t, u) return u } @@ -544,7 +573,7 @@ func KongTargetAttachedToUpstream( } require.NoError(t, cl.Create(ctx, u)) - t.Logf("deployed new KongTarget %s", client.ObjectKeyFromObject(u)) + logObjectCreate(t, u) return u } @@ -579,7 +608,7 @@ func KongConsumerAttachedToCP( } require.NoError(t, cl.Create(ctx, c)) - t.Logf("deployed new KongConsumer %s", client.ObjectKeyFromObject(c)) + logObjectCreate(t, c) return c } @@ -614,7 +643,7 @@ func KongConsumerGroupAttachedToCP( } require.NoError(t, cl.Create(ctx, &cg)) - t.Logf("deployed new KongConsumerGroup %s", client.ObjectKeyFromObject(&cg)) + logObjectCreate(t, &cg) return &cg } @@ -652,7 +681,7 @@ func KongVaultAttachedToCP( } require.NoError(t, cl.Create(ctx, vault)) - t.Logf("deployed new KongVault %s", client.ObjectKeyFromObject(vault)) + logObjectCreate(t, vault) return vault } @@ -692,7 +721,7 @@ func KongKeyAttachedToCP( opt(key) } require.NoError(t, cl.Create(ctx, key)) - t.Logf("deployed new KongKey %s", client.ObjectKeyFromObject(key)) + logObjectCreate(t, key) return key } @@ -769,7 +798,7 @@ func KongKeySetAttachedToCP( }, } require.NoError(t, cl.Create(ctx, keySet)) - t.Logf("deployed new KongKeySet %s", client.ObjectKeyFromObject(keySet)) + logObjectCreate(t, keySet) return keySet } @@ -834,7 +863,18 @@ func KongDataPlaneClientCertificateAttachedToCP( }, } require.NoError(t, cl.Create(ctx, cert)) - t.Logf("deployed new KongDataPlaneClientCertificate %s", client.ObjectKeyFromObject(cert)) + logObjectCreate(t, cert) return cert } + +func logObjectCreate[ + T interface { + client.Object + GetTypeName() string + }, +](t *testing.T, obj T) { + t.Helper() + + t.Logf("deployed new %s %s resource", obj.GetTypeName(), client.ObjectKeyFromObject(obj)) +}