Skip to content

Commit 60789e6

Browse files
committed
move functions for deploying plugins to deploy package
1 parent e49b0a1 commit 60789e6

File tree

2 files changed

+127
-75
lines changed

2 files changed

+127
-75
lines changed

test/helpers/deploy/deploy_resources.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,51 @@ func RateLimitingPlugin(
10781078
return plugin
10791079
}
10801080

1081+
// RequestTransformerPlugin deploys the request-transformer KongPlugin resource and returns the resource.
1082+
// The provided client should be namespaced, i.e. created with `client.NewNamespacedClient(client, ns)`
1083+
func RequestTransformerPlugin(
1084+
t *testing.T,
1085+
ctx context.Context,
1086+
cl client.Client,
1087+
) *configurationv1.KongPlugin {
1088+
t.Helper()
1089+
1090+
plugin := &configurationv1.KongPlugin{
1091+
ObjectMeta: metav1.ObjectMeta{
1092+
GenerateName: "request-transformer-kp-",
1093+
},
1094+
PluginName: "request-transformer",
1095+
Config: apiextensionsv1.JSON{
1096+
Raw: []byte(`{"add":{"headers":["X-Kong-Test:test"]}}`),
1097+
},
1098+
}
1099+
require.NoError(t, cl.Create(ctx, plugin))
1100+
t.Logf("deployed new %s KongPlugin (%s)", client.ObjectKeyFromObject(plugin), plugin.PluginName)
1101+
return plugin
1102+
}
1103+
1104+
// ResponseTransformerPlugin deploys the response-transformer KongPlugin resource and returns the resource.
1105+
// The provided client should be namespaced, i.e. created with `client.NewNamespacedClient(client, ns)`
1106+
func ResponseTransformerPlugin(t *testing.T,
1107+
ctx context.Context,
1108+
cl client.Client,
1109+
) *configurationv1.KongPlugin {
1110+
t.Helper()
1111+
1112+
plugin := &configurationv1.KongPlugin{
1113+
ObjectMeta: metav1.ObjectMeta{
1114+
GenerateName: "response-transformer-kp-",
1115+
},
1116+
PluginName: "response-transformer",
1117+
Config: apiextensionsv1.JSON{
1118+
Raw: []byte(`{"add":{"headers":["X-Kong-Test:test"]}}`),
1119+
},
1120+
}
1121+
require.NoError(t, cl.Create(ctx, plugin))
1122+
t.Logf("deployed new %s KongPlugin (%s)", client.ObjectKeyFromObject(plugin), plugin.PluginName)
1123+
return plugin
1124+
}
1125+
10811126
// KongKeySet deploys a KongKeySet resource and returns the resource.
10821127
func KongKeySet(
10831128
t *testing.T,
@@ -1266,6 +1311,55 @@ func WithKonnectAdoptOptions[T ObjectSupportingAdoption](mode commonv1alpha1.Ado
12661311
}
12671312
}
12681313

1314+
// ObjectSupportingBindingPlugins defines the interface of types supporting to be set as the target of KongPluginBinding.
1315+
type ObjectSupportingBindingPlugins interface {
1316+
*configurationv1alpha1.KongService |
1317+
*configurationv1alpha1.KongRoute |
1318+
*configurationv1.KongConsumer |
1319+
*configurationv1beta1.KongConsumerGroup
1320+
GetName() string
1321+
}
1322+
1323+
// WithKongPluginBindingTarget returns an option function that sets the binding target of the KongPluginBinding.
1324+
// The option function also sets the scope of the KongPluginBinding to "OnlyTargets".
1325+
func WithKongPluginBindingTarget[T ObjectSupportingBindingPlugins](
1326+
bindTarget T,
1327+
) ObjOption {
1328+
return func(obj client.Object) {
1329+
kpb, ok := obj.(*configurationv1alpha1.KongPluginBinding)
1330+
if !ok {
1331+
return
1332+
}
1333+
kpb.Spec.Scope = configurationv1alpha1.KongPluginBindingScopeOnlyTargets
1334+
if kpb.Spec.Targets == nil {
1335+
kpb.Spec.Targets = &configurationv1alpha1.KongPluginBindingTargets{}
1336+
}
1337+
// Set the target into the spec.targets of the KongPluginBinding.
1338+
switch any(bindTarget).(type) {
1339+
case *configurationv1alpha1.KongService:
1340+
kpb.Spec.Targets.ServiceReference = &configurationv1alpha1.TargetRefWithGroupKind{
1341+
Group: configurationv1alpha1.GroupVersion.Group,
1342+
Kind: "KongService",
1343+
Name: bindTarget.GetName(),
1344+
}
1345+
case *configurationv1alpha1.KongRoute:
1346+
kpb.Spec.Targets.RouteReference = &configurationv1alpha1.TargetRefWithGroupKind{
1347+
Group: configurationv1alpha1.GroupVersion.Group,
1348+
Kind: "KongRoute",
1349+
Name: bindTarget.GetName(),
1350+
}
1351+
case *configurationv1.KongConsumer:
1352+
kpb.Spec.Targets.ConsumerReference = &configurationv1alpha1.TargetRef{
1353+
Name: bindTarget.GetName(),
1354+
}
1355+
case *configurationv1beta1.KongConsumerGroup:
1356+
kpb.Spec.Targets.ConsumerGroupReference = &configurationv1alpha1.TargetRef{
1357+
Name: bindTarget.GetName(),
1358+
}
1359+
}
1360+
}
1361+
}
1362+
12691363
func logObjectCreate[
12701364
T interface {
12711365
client.Object

test/integration/konnect_entity_adoption_test.go

Lines changed: 33 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/kong/kong-operator/test/helpers"
2727
"github.com/kong/kong-operator/test/helpers/conditions"
2828
"github.com/kong/kong-operator/test/helpers/deploy"
29+
"github.com/kong/kong-operator/test/helpers/eventually"
2930
)
3031

3132
const (
@@ -244,11 +245,7 @@ func TestKonnectEntityAdoption_Plugin(t *testing.T) {
244245
t.Cleanup(deleteObjectAndWaitForDeletionFn(t, cp.DeepCopy()))
245246

246247
t.Logf("Waiting for Konnect ID to be assigned to ControlPlane %s/%s", cp.Namespace, cp.Name)
247-
require.EventuallyWithT(t, func(t *assert.CollectT) {
248-
err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: cp.Name, Namespace: cp.Namespace}, cp)
249-
require.NoError(t, err)
250-
assertKonnectEntityProgrammed(t, cp)
251-
}, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick)
248+
cp = eventually.KonnectEntityGetsProgrammed(t, ctx, clientNamespaced, cp)
252249

253250
cpKonnectID := cp.GetKonnectID()
254251

@@ -277,6 +274,7 @@ func TestKonnectEntityAdoption_Plugin(t *testing.T) {
277274
require.NotNil(t, resp.Plugin, "Should get a non-nil plugin in the response")
278275
globalRequestTransformerPlugin := resp.Plugin
279276
require.NotNil(t, globalRequestTransformerPlugin.ID, "Should receive a non-nil plugin ID")
277+
globalPluginID := *globalRequestTransformerPlugin.ID
280278

281279
buf, err := json.Marshal(globalRequestTransformerPlugin.Config)
282280
require.NoError(t, err, "Should marshal plugin configuration in JSON successfully")
@@ -300,48 +298,35 @@ func TestKonnectEntityAdoption_Plugin(t *testing.T) {
300298
PluginReference: configurationv1alpha1.PluginRef{
301299
Name: kongPluginReqTransformer.Name,
302300
},
303-
Adopt: &commonv1alpha1.AdoptOptions{
304-
From: commonv1alpha1.AdoptSourceKonnect,
305-
Mode: commonv1alpha1.AdoptModeOverride,
306-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
307-
ID: *globalRequestTransformerPlugin.ID,
308-
},
309-
},
310301
Scope: configurationv1alpha1.KongPluginBindingScopeGlobalInControlPlane,
311302
},
312-
}, deploy.WithKonnectNamespacedRefControlPlaneRef(cp))
303+
},
304+
deploy.WithKonnectNamespacedRefControlPlaneRef(cp),
305+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongPluginBinding](commonv1alpha1.AdoptModeOverride, globalPluginID),
306+
)
313307
t.Cleanup(deleteObjectAndWaitForDeletionFn(t, kpbGlobal.DeepCopy()))
314308

315309
t.Log("Waiting for KongPluginBinding to be programmed and set Konnect ID")
316-
require.EventuallyWithT(t, func(collect *assert.CollectT) {
317-
err := clientNamespaced.Get(GetCtx(), client.ObjectKeyFromObject(kpbGlobal), kpbGlobal)
318-
require.NoError(t, err)
319-
320-
assertKonnectEntityProgrammed(collect, kpbGlobal)
321-
assert.Equalf(collect, *globalRequestTransformerPlugin.ID, kpbGlobal.GetKonnectID(),
322-
"KongPluginBinding should set Konnect ID %s as the adopted plugin in status",
323-
*globalRequestTransformerPlugin.ID,
324-
)
325-
}, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick,
326-
"Did not see KongPluginBinding set Konnect ID and Programmed condition to True",
310+
eventually.KonnectEntityGetsProgrammed(
311+
t, ctx, clientNamespaced, kpbGlobal,
312+
func(t *assert.CollectT, kpb *configurationv1alpha1.KongPluginBinding) {
313+
require.Equalf(t, globalPluginID, kpb.GetKonnectID(),
314+
"KongPluginBinding %s/%s should set Konnect ID %s as the adopted plugin in status",
315+
kpb.Namespace, kpb.Name, globalPluginID,
316+
)
317+
},
327318
)
328319

329320
t.Log("Creating a KongService to attach plugins to")
330321
ks := deploy.KongService(t, GetCtx(), clientNamespaced, deploy.WithKonnectNamespacedRefControlPlaneRef(cp))
331322
t.Cleanup(deleteObjectAndWaitForDeletionFn(t, ks.DeepCopy()))
332323

333324
t.Log("Waiting for the KongService to get a Konnect ID")
334-
var serviceKonnectID string
335-
require.EventuallyWithT(t, func(collect *assert.CollectT) {
336-
err := clientNamespaced.Get(GetCtx(), client.ObjectKeyFromObject(ks), ks)
337-
require.NoError(t, err)
338-
339-
assertKonnectEntityProgrammed(collect, ks)
340-
assert.NotEmpty(collect, ks.GetKonnectID())
341-
serviceKonnectID = ks.GetKonnectID()
342-
}, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick,
343-
"Did not see KongService set Konnect ID and Programmed condition to True",
325+
ks = eventually.KonnectEntityGetsProgrammed(
326+
t, ctx, clientNamespaced, ks,
344327
)
328+
require.NotEmpty(t, ks.GetKonnectID(), "KongService should get Konnect ID when programmed")
329+
serviceKonnectID := ks.GetKonnectID()
345330

346331
t.Log("Creating a plugin by SDK attached to the service for adopting")
347332
resp, err = sdk.GetPluginSDK().CreatePlugin(
@@ -364,59 +349,32 @@ func TestKonnectEntityAdoption_Plugin(t *testing.T) {
364349
require.NotNil(t, resp.Plugin, "Should get a non-nil plugin in the response")
365350
serviceResponseTransformerPlugin := resp.Plugin
366351
require.NotNil(t, serviceResponseTransformerPlugin.ID, "Should receive a non-nil plugin ID")
367-
368-
buf, err = json.Marshal(serviceResponseTransformerPlugin.Config)
369-
require.NoError(t, err, "Should marshal plugin configuration in JSON successfully")
352+
pluginServiceID := *serviceResponseTransformerPlugin.ID
370353

371354
t.Log("Creating a KongPlugin and a KongPluginBinding for adopting the plugin")
372-
kongPluginResponseTransformer := &configurationv1.KongPlugin{
373-
ObjectMeta: metav1.ObjectMeta{
374-
Namespace: ns.Name,
375-
Name: "kongplugin-service-response-transformer",
376-
},
377-
PluginName: "response-transformer",
378-
Config: apiextensionsv1.JSON{
379-
Raw: buf,
380-
},
381-
}
382-
require.NoError(t, clientNamespaced.Create(GetCtx(), kongPluginResponseTransformer))
355+
kongPluginResponseTransformer := deploy.ResponseTransformerPlugin(t, ctx, clientNamespaced)
383356
t.Cleanup(deleteObjectAndWaitForDeletionFn(t, kongPluginResponseTransformer.DeepCopy()))
384357

385358
kpbService := deploy.KongPluginBinding(t, GetCtx(), clientNamespaced, &configurationv1alpha1.KongPluginBinding{
386359
Spec: configurationv1alpha1.KongPluginBindingSpec{
387360
PluginReference: configurationv1alpha1.PluginRef{
388361
Name: kongPluginResponseTransformer.Name,
389362
},
390-
Adopt: &commonv1alpha1.AdoptOptions{
391-
From: commonv1alpha1.AdoptSourceKonnect,
392-
Mode: commonv1alpha1.AdoptModeOverride,
393-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
394-
ID: *serviceResponseTransformerPlugin.ID,
395-
},
396-
},
397-
Scope: configurationv1alpha1.KongPluginBindingScopeOnlyTargets,
398-
Targets: &configurationv1alpha1.KongPluginBindingTargets{
399-
ServiceReference: &configurationv1alpha1.TargetRefWithGroupKind{
400-
Name: ks.Name,
401-
Kind: "KongService",
402-
Group: configurationv1alpha1.GroupVersion.Group,
403-
},
404-
},
405363
},
406-
}, deploy.WithKonnectNamespacedRefControlPlaneRef(cp))
364+
},
365+
deploy.WithKonnectNamespacedRefControlPlaneRef(cp),
366+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongPluginBinding](commonv1alpha1.AdoptModeOverride, pluginServiceID),
367+
deploy.WithKongPluginBindingTarget(ks),
368+
)
407369
t.Cleanup(deleteObjectAndWaitForDeletionFn(t, kpbService.DeepCopy()))
408370

409371
t.Log("Waiting for KongPluginBinding to be programmed and set Konnect ID")
410-
require.EventuallyWithT(t, func(collect *assert.CollectT) {
411-
err := clientNamespaced.Get(GetCtx(), client.ObjectKeyFromObject(kpbService), kpbService)
412-
require.NoError(t, err)
413-
414-
assertKonnectEntityProgrammed(collect, kpbService)
415-
assert.Equalf(collect, *serviceResponseTransformerPlugin.ID, kpbService.GetKonnectID(),
416-
"KongPluginBinding should set Konnect ID %s as the adopted plugin in status",
417-
*serviceResponseTransformerPlugin.ID,
418-
)
419-
}, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick,
420-
"Did not see KongPluginBinding set Konnect ID and Programmed condition to True",
372+
eventually.KonnectEntityGetsProgrammed(t, ctx, clientNamespaced, kpbService,
373+
func(t *assert.CollectT, kpb *configurationv1alpha1.KongPluginBinding) {
374+
require.Equalf(t, pluginServiceID, kpb.GetKonnectID(),
375+
"KongPluginBinding %s/%s should set Konnect ID %s as the adopted plugin in status",
376+
kpbService.Namespace, kpbService.Name, pluginServiceID,
377+
)
378+
},
421379
)
422380
}

0 commit comments

Comments
 (0)