Skip to content

Commit fab3f6b

Browse files
committed
use SetAdoptOptions to configure adoption of CRDs
1 parent 8b8ee4a commit fab3f6b

File tree

6 files changed

+52
-146
lines changed

6 files changed

+52
-146
lines changed

test/envtest/kongpluginbinding_adoption_test.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
apiwatch "k8s.io/apimachinery/pkg/watch"
1313
"sigs.k8s.io/controller-runtime/pkg/client"
1414

15+
commonv1alpha1 "github.com/kong/kong-operator/api/common/v1alpha1"
1516
configurationv1alpha1 "github.com/kong/kong-operator/api/configuration/v1alpha1"
1617
konnectv1alpha1 "github.com/kong/kong-operator/api/konnect/v1alpha1"
1718
"github.com/kong/kong-operator/controller/konnect"
@@ -85,10 +86,10 @@ func TestKongPluginBindingAdoption(t *testing.T) {
8586
WithPluginRef(proxyCacheKongPlugin.Name).
8687
WithScope(configurationv1alpha1.KongPluginBindingScopeGlobalInControlPlane).
8788
Build(),
88-
setKongPluginBindingAdoptOptions(t, pluginID),
89+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongPluginBinding](commonv1alpha1.AdoptModeOverride, pluginID),
8990
)
9091

91-
t.Log("Waiting for KongPluginBinding being Programmed and set Konnect ID")
92+
t.Logf("Waiting for KongPluginBinding %s/%s being Programmed and set Konnect ID", ns.Name, kpbGlobal.Name)
9293
watchFor(t, ctx, w,
9394
apiwatch.Modified,
9495
func(kpb *configurationv1alpha1.KongPluginBinding) bool {
@@ -100,7 +101,7 @@ func TestKongPluginBindingAdoption(t *testing.T) {
100101
t.Log("Setting up SDK expectation for plugin deletion")
101102
sdk.PluginSDK.EXPECT().DeletePlugin(mock.Anything, cp.GetKonnectID(), pluginID).Return(nil, nil)
102103

103-
t.Log("Deleting the KongPluginBinding")
104+
t.Logf("Deleting the KongPluginBinding %s/%s", ns.Name, kpbGlobal.Name)
104105
require.NoError(t, clientNamespaced.Delete(ctx, kpbGlobal))
105106
eventually.WaitForObjectToNotExist(t, ctx, cl, kpbGlobal, waitTime, tickTime)
106107
})
@@ -141,10 +142,10 @@ func TestKongPluginBindingAdoption(t *testing.T) {
141142
WithPluginRef(proxyCacheKongPlugin.Name).
142143
WithServiceTarget(kongService.Name).
143144
Build(),
144-
setKongPluginBindingAdoptOptions(t, pluginID),
145+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongPluginBinding](commonv1alpha1.AdoptModeOverride, pluginID),
145146
)
146147

147-
t.Log("Waiting for KongPluginBinding being Programmed and set Konnect ID")
148+
t.Logf("Waiting for KongPluginBinding %s/%s being Programmed and set Konnect ID", ns.Name, kpbService.Name)
148149
watchFor(t, ctx, w,
149150
apiwatch.Modified,
150151
func(kpb *configurationv1alpha1.KongPluginBinding) bool {
@@ -156,7 +157,7 @@ func TestKongPluginBindingAdoption(t *testing.T) {
156157
t.Log("Setting up SDK expectation for plugin deletion")
157158
sdk.PluginSDK.EXPECT().DeletePlugin(mock.Anything, cp.GetKonnectID(), pluginID).Return(nil, nil)
158159

159-
t.Log("Deleting the KongPluginBinding")
160+
t.Logf("Deleting the KongPluginBinding %s/%s", ns.Name, kpbService.Name)
160161
require.NoError(t, clientNamespaced.Delete(ctx, kpbService))
161162
eventually.WaitForObjectToNotExist(t, ctx, cl, kpbService, waitTime, tickTime)
162163
})
@@ -183,10 +184,10 @@ func TestKongPluginBindingAdoption(t *testing.T) {
183184
WithPluginRef("non-exist-plugin").
184185
WithScope(configurationv1alpha1.KongPluginBindingScopeGlobalInControlPlane).
185186
Build(),
186-
setKongPluginBindingAdoptOptions(t, pluginID),
187+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongPluginBinding](commonv1alpha1.AdoptModeOverride, pluginID),
187188
)
188189

189-
t.Log("Waiting for the KongPluginBinding to be marked as not programmed and not adopted")
190+
t.Logf("Waiting for the KongPluginBinding %s/%s to be marked as not programmed and not adopted", ns.Name, kpbGlobal.Name)
190191
watchFor(t, ctx, w,
191192
apiwatch.Modified,
192193
func(kpb *configurationv1alpha1.KongPluginBinding) bool {
@@ -198,14 +199,3 @@ func TestKongPluginBindingAdoption(t *testing.T) {
198199
)
199200
})
200201
}
201-
202-
// setKongPluginBindingAdoptOptions returns a function to set the adopt options on a KongPluginBinding
203-
// to adopt a plugin from the existing one with the given ID.
204-
// TODO: Use a more generic way to set adopt options: https://github.com/Kong/kong-operator/issues/2660
205-
func setKongPluginBindingAdoptOptions(t *testing.T, id string) func(obj client.Object) {
206-
return func(obj client.Object) {
207-
kpb, ok := obj.(*configurationv1alpha1.KongPluginBinding)
208-
require.True(t, ok)
209-
kpb.Spec.Adopt = deploy.AdoptOptionsOverrideModeWithID(id)
210-
}
211-
}

test/envtest/konnect_entities_kongroute_test.go

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,10 @@ func TestKongRoute(t *testing.T) {
185185
t.Logf("Creating a KongRoute to adopt the existing route (ID: %s)", routeID)
186186
createdRoute := deploy.KongRoute(t, ctx, clientNamespaced,
187187
deploy.WithNamespacedKongServiceRef(svc),
188-
func(obj client.Object) {
189-
r, ok := obj.(*configurationv1alpha1.KongRoute)
190-
require.True(t, ok)
191-
r.Spec.Adopt = &commonv1alpha1.AdoptOptions{
192-
From: commonv1alpha1.AdoptSourceKonnect,
193-
Mode: commonv1alpha1.AdoptModeOverride,
194-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
195-
ID: routeID,
196-
},
197-
}
198-
r.Spec.Name = &routeName
199-
r.Spec.Paths = []string{"/path-2"}
200-
},
188+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongRoute](commonv1alpha1.AdoptModeOverride, routeID),
201189
)
202190

203-
t.Log("Waiting for the KongRoute to get KonnectID and marked as programmed")
191+
t.Logf("Waiting for the KongRoute %s/%s to get KonnectID and marked as programmed", ns.Name, createdRoute.Name)
204192
watchFor(t, ctx, w, apiwatch.Modified, func(r *configurationv1alpha1.KongRoute) bool {
205193
return r.Name == createdRoute.Name && r.GetKonnectID() == routeID && k8sutils.IsProgrammed(r)
206194
},
@@ -210,7 +198,7 @@ func TestKongRoute(t *testing.T) {
210198
t.Log("Setting up SDK expectations for route deletion")
211199
sdk.RoutesSDK.EXPECT().DeleteRoute(mock.Anything, cp.GetKonnectID(), routeID).Return(nil, nil)
212200

213-
t.Log("Deleting KongRoute")
201+
t.Logf("Deleting KongRoute %s/%s", ns.Name, createdRoute.Name)
214202
require.NoError(t, clientNamespaced.Delete(ctx, createdRoute))
215203

216204
eventually.WaitForObjectToNotExist(t, ctx, cl, createdRoute, waitTime, tickTime)
@@ -249,22 +237,10 @@ func TestKongRoute(t *testing.T) {
249237
t.Logf("Creating a KongRoute to adopt the existing route (ID: %s)", routeID)
250238
createdRoute := deploy.KongRoute(t, ctx, clientNamespaced,
251239
deploy.WithKonnectNamespacedRefControlPlaneRef(cp),
252-
func(obj client.Object) {
253-
r, ok := obj.(*configurationv1alpha1.KongRoute)
254-
require.True(t, ok)
255-
r.Spec.Adopt = &commonv1alpha1.AdoptOptions{
256-
From: commonv1alpha1.AdoptSourceKonnect,
257-
Mode: commonv1alpha1.AdoptModeOverride,
258-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
259-
ID: routeID,
260-
},
261-
}
262-
r.Spec.Name = &routeName
263-
r.Spec.Paths = []string{"/path-2"}
264-
},
240+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongRoute](commonv1alpha1.AdoptModeMatch, routeID),
265241
)
266242

267-
t.Log("Waiting for the KongRoute to get KonnectID and marked as programmed")
243+
t.Logf("Waiting for the KongRoute %s/%s to get KonnectID and marked as programmed", ns.Name, createdRoute.Name)
268244
watchFor(t, ctx, w, apiwatch.Modified, func(r *configurationv1alpha1.KongRoute) bool {
269245
return r.Name == createdRoute.Name && r.GetKonnectID() == routeID && k8sutils.IsProgrammed(r)
270246
},
@@ -300,22 +276,10 @@ func TestKongRoute(t *testing.T) {
300276
t.Logf("Creating a KongRoute to adopt the existing route (ID: %s)", routeID)
301277
createdRoute := deploy.KongRoute(t, ctx, clientNamespaced,
302278
deploy.WithNamespacedKongServiceRef(svc),
303-
func(obj client.Object) {
304-
r, ok := obj.(*configurationv1alpha1.KongRoute)
305-
require.True(t, ok)
306-
r.Spec.Adopt = &commonv1alpha1.AdoptOptions{
307-
From: commonv1alpha1.AdoptSourceKonnect,
308-
Mode: commonv1alpha1.AdoptModeOverride,
309-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
310-
ID: routeID,
311-
},
312-
}
313-
r.Spec.Name = &routeName
314-
r.Spec.Paths = []string{"/path-2"}
315-
},
279+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongRoute](commonv1alpha1.AdoptModeMatch, routeID),
316280
)
317281

318-
t.Log("Waiting for the KongRoute to be marked as not programmed and not adopted")
282+
t.Logf("Waiting for the KongRoute %s/%s to be marked as not programmed and not adopted", ns.Name, createdRoute.Name)
319283
watchFor(t, ctx, w, apiwatch.Modified, func(r *configurationv1alpha1.KongRoute) bool {
320284
return r.Name == createdRoute.Name &&
321285
conditionsContainProgrammedFalse(r.GetConditions()) &&

test/envtest/konnect_entities_kongservice_test.go

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -447,20 +447,10 @@ func TestKongService(t *testing.T) {
447447

448448
t.Log("Creating a KongService to adopt the existing service")
449449
createdService := deploy.KongService(t, t.Context(), clientNamespaced, deploy.WithKonnectNamespacedRefControlPlaneRef(cp),
450-
func(obj client.Object) {
451-
ks, ok := obj.(*configurationv1alpha1.KongService)
452-
require.True(t, ok)
453-
ks.Spec.URL = lo.ToPtr("https://example.com/example")
454-
ks.Spec.Adopt = &commonv1alpha1.AdoptOptions{
455-
From: commonv1alpha1.AdoptSourceKonnect,
456-
Mode: commonv1alpha1.AdoptModeOverride,
457-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
458-
ID: serviceKonnectID,
459-
},
460-
}
461-
})
450+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongService](commonv1alpha1.AdoptModeOverride, serviceKonnectID),
451+
)
462452

463-
t.Log("Waiting for the service to be programmed and get Konnect ID")
453+
t.Logf("Waiting for the KongService %s/%s to be programmed and get Konnect ID", ns.Name, createdService.Name)
464454
watchFor(t, t.Context(), w, apiwatch.Modified, func(ks *configurationv1alpha1.KongService) bool {
465455
return ks.Name == createdService.Name &&
466456
ks.GetKonnectID() == serviceKonnectID && k8sutils.IsProgrammed(ks)
@@ -475,7 +465,7 @@ func TestKongService(t *testing.T) {
475465
serviceKonnectID,
476466
).Return(&sdkkonnectops.DeleteServiceResponse{}, nil)
477467

478-
t.Log("Deleting the KongService")
468+
t.Logf("Deleting the KongService %s/%s", ns.Name, createdService.Name)
479469
require.NoError(t, clientNamespaced.Delete(t.Context(), createdService))
480470

481471
t.Log("Waiting for the SDK's DeleteService to be called")
@@ -502,20 +492,10 @@ func TestKongService(t *testing.T) {
502492

503493
t.Log("Creating a KongService to adopt the existing service")
504494
createdService := deploy.KongService(t, t.Context(), clientNamespaced, deploy.WithKonnectNamespacedRefControlPlaneRef(cp),
505-
func(obj client.Object) {
506-
ks, ok := obj.(*configurationv1alpha1.KongService)
507-
require.True(t, ok)
508-
ks.Spec.URL = lo.ToPtr("https://example.com/example")
509-
ks.Spec.Adopt = &commonv1alpha1.AdoptOptions{
510-
From: commonv1alpha1.AdoptSourceKonnect,
511-
Mode: commonv1alpha1.AdoptModeOverride,
512-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
513-
ID: serviceKonnectID,
514-
},
515-
}
516-
})
495+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongService](commonv1alpha1.AdoptModeOverride, serviceKonnectID),
496+
)
517497

518-
t.Log("Waiting for the KongService to be marked as not programmed")
498+
t.Logf("Waiting for the KongService %s/%s to be marked as not programmed", ns.Name, createdService.Name)
519499
watchFor(t, t.Context(), w, apiwatch.Modified, func(ks *configurationv1alpha1.KongService) bool {
520500
return ks.Name == createdService.Name &&
521501
conditionsContainProgrammedFalse(ks.GetConditions()) &&
@@ -555,20 +535,10 @@ func TestKongService(t *testing.T) {
555535

556536
t.Log("Creating a KongService to adopt the existing service")
557537
createdService := deploy.KongService(t, t.Context(), clientNamespaced, deploy.WithKonnectNamespacedRefControlPlaneRef(cp),
558-
func(obj client.Object) {
559-
ks, ok := obj.(*configurationv1alpha1.KongService)
560-
require.True(t, ok)
561-
ks.Spec.URL = lo.ToPtr("https://example.com/example")
562-
ks.Spec.Adopt = &commonv1alpha1.AdoptOptions{
563-
From: commonv1alpha1.AdoptSourceKonnect,
564-
Mode: commonv1alpha1.AdoptModeOverride,
565-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
566-
ID: serviceKonnectID,
567-
},
568-
}
569-
})
538+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongService](commonv1alpha1.AdoptModeOverride, serviceKonnectID),
539+
)
570540

571-
t.Log("Waiting for the KongService to be marked as not programmed")
541+
t.Logf("Waiting for the KongService %s/%s to be marked as not programmed", ns.Name, createdService.Name)
572542
watchFor(t, t.Context(), w, apiwatch.Modified, func(ks *configurationv1alpha1.KongService) bool {
573543
return ks.Name == createdService.Name &&
574544
conditionsContainProgrammedFalse(ks.GetConditions()) &&

test/envtest/konnect_entities_kongtarget_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,10 @@ func TestKongTarget(t *testing.T) {
163163

164164
t.Logf("Creating a KongTarget to adopt the existing target (ID:%s)", targetID)
165165
createdTarget := deploy.KongTargetAttachedToUpstream(t, ctx, clientNamespaced, upstream,
166-
func(obj client.Object) {
167-
kt, ok := obj.(*configurationv1alpha1.KongTarget)
168-
require.True(t, ok)
169-
kt.Spec.Adopt = &commonv1alpha1.AdoptOptions{
170-
From: commonv1alpha1.AdoptSourceKonnect,
171-
Mode: commonv1alpha1.AdoptModeOverride,
172-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
173-
ID: targetID,
174-
},
175-
}
176-
})
177-
178-
t.Log("Waiting for KongTarget to set Konnect ID and programmed condition")
166+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongTarget](commonv1alpha1.AdoptModeOverride, targetID),
167+
)
168+
169+
t.Logf("Waiting for KongTarget %s/%s to set Konnect ID and programmed condition", ns.Name, createdTarget.Name)
179170
watchFor(t, ctx, w, apiwatch.Modified, func(kt *configurationv1alpha1.KongTarget) bool {
180171
return createdTarget.Name == kt.Name &&
181172
kt.GetKonnectID() == targetID && k8sutils.IsProgrammed(kt)

test/envtest/konnect_entities_kongupstream_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,10 @@ func TestKongUpstream(t *testing.T) {
254254
t.Logf("Creating a KongUpstream to adopt existing upstream (ID:%s)", upstreamID)
255255
createdUpstream := deploy.KongUpstream(t, ctx, clientNamespaced,
256256
deploy.WithKonnectNamespacedRefControlPlaneRef(cp),
257-
func(obj client.Object) {
258-
kup, ok := obj.(*configurationv1alpha1.KongUpstream)
259-
require.True(t, ok)
260-
kup.Spec.Name = upstreamName
261-
kup.Spec.Adopt = &commonv1alpha1.AdoptOptions{
262-
From: commonv1alpha1.AdoptSourceKonnect,
263-
Mode: commonv1alpha1.AdoptModeOverride,
264-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
265-
ID: upstreamID,
266-
},
267-
}
268-
})
257+
deploy.WithKonnectAdoptOptions[*configurationv1alpha1.KongUpstream](commonv1alpha1.AdoptModeOverride, upstreamID),
258+
)
269259

270-
t.Log("Waiting for KongUpstream to set Konnect ID and programmed condition")
260+
t.Logf("Waiting for KongUpstream %s/%s to set Konnect ID and programmed condition", ns.Name, createdUpstream.Name)
271261
watchFor(t, ctx, w, apiwatch.Modified, func(u *configurationv1alpha1.KongUpstream) bool {
272262
return createdUpstream.Name == u.Name &&
273263
u.GetKonnectID() == upstreamID && k8sutils.IsProgrammed(u)
@@ -278,7 +268,7 @@ func TestKongUpstream(t *testing.T) {
278268
t.Log("Setting up SDK expectations for upstream deletion")
279269
sdk.UpstreamsSDK.EXPECT().DeleteUpstream(mock.Anything, cp.GetKonnectID(), upstreamID).Return(nil, nil)
280270

281-
t.Log("Deleting KongUpstream")
271+
t.Logf("Deleting KongUpstream %s/%s", ns.Name, createdUpstream.Name)
282272
require.NoError(t, clientNamespaced.Delete(ctx, createdUpstream))
283273
eventually.WaitForObjectToNotExist(t, ctx, cl, createdUpstream, waitTime, tickTime)
284274

test/helpers/deploy/deploy_resources.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,25 +1243,26 @@ func WithKonnectConfiguration[T ObjectSupportingKonnectConfiguration](
12431243
}
12441244
}
12451245

1246-
// AdoptOptionsOverrideModeWithID returns the adopt option with the override mode and the given ID.
1247-
func AdoptOptionsOverrideModeWithID(id string) *commonv1alpha1.AdoptOptions {
1248-
return &commonv1alpha1.AdoptOptions{
1249-
From: commonv1alpha1.AdoptSourceKonnect,
1250-
Mode: commonv1alpha1.AdoptModeOverride,
1251-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
1252-
ID: id,
1253-
},
1254-
}
1246+
// ObjectSupportingAdoption defines the interface of types supporting adoption.
1247+
type ObjectSupportingAdoption interface {
1248+
client.Object
1249+
SetAdoptOptions(*commonv1alpha1.AdoptOptions)
12551250
}
12561251

1257-
// AdoptOptionsMatchModeWithID returns the adopt option with the match mode and the given ID.
1258-
func AdoptOptionsMatchModeWithID(id string) *commonv1alpha1.AdoptOptions {
1259-
return &commonv1alpha1.AdoptOptions{
1260-
From: commonv1alpha1.AdoptSourceKonnect,
1261-
Mode: commonv1alpha1.AdoptModeMatch,
1262-
Konnect: &commonv1alpha1.AdoptKonnectOptions{
1263-
ID: id,
1264-
},
1252+
// WithKonnectAdoptOptions returns an option function that sets the adopt options to adopt from Konnect.
1253+
func WithKonnectAdoptOptions[T ObjectSupportingAdoption](mode commonv1alpha1.AdoptMode, id string) ObjOption {
1254+
return func(obj client.Object) {
1255+
ent, ok := obj.(T)
1256+
if ok {
1257+
opts := &commonv1alpha1.AdoptOptions{
1258+
From: commonv1alpha1.AdoptSourceKonnect,
1259+
Mode: mode,
1260+
Konnect: &commonv1alpha1.AdoptKonnectOptions{
1261+
ID: id,
1262+
},
1263+
}
1264+
ent.SetAdoptOptions(opts)
1265+
}
12651266
}
12661267
}
12671268

0 commit comments

Comments
 (0)