@@ -30,9 +30,7 @@ import (
30
30
. "github.com/onsi/ginkgo/v2"
31
31
. "github.com/onsi/gomega"
32
32
corev1 "k8s.io/api/core/v1"
33
- "k8s.io/apimachinery/pkg/api/meta"
34
33
"k8s.io/apimachinery/pkg/runtime"
35
- "k8s.io/apimachinery/pkg/runtime/schema"
36
34
"k8s.io/apimachinery/pkg/util/sets"
37
35
"k8s.io/client-go/kubernetes"
38
36
"k8s.io/client-go/rest"
@@ -45,21 +43,12 @@ import (
45
43
"sigs.k8s.io/cluster-api/test/infrastructure/container"
46
44
"sigs.k8s.io/controller-runtime/pkg/cache"
47
45
"sigs.k8s.io/controller-runtime/pkg/client"
48
- "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
49
46
)
50
47
51
48
func logf (format string , a ... interface {}) {
52
49
fmt .Fprintf (GinkgoWriter , "INFO: " + format + "\n " , a ... )
53
50
}
54
51
55
- func (wc * wrappedClient ) GroupVersionKindFor (obj runtime.Object ) (gvk schema.GroupVersionKind , err error ) {
56
- return wc .client .GroupVersionKindFor (obj )
57
- }
58
-
59
- func (wc * wrappedClient ) IsObjectNamespaced (obj runtime.Object ) (namespaced bool , err error ) {
60
- return wc .client .IsObjectNamespaced (obj )
61
- }
62
-
63
52
// wrappedClusterProxy wraps framework.clusterProxy to add support for retrying if discovery times out
64
53
// when creating a client. This was needed because control plane upgrade tests
65
54
// where sometimes attempting to create the client before the elastic IP was migrated.
@@ -114,7 +103,7 @@ func (w *wrappedClusterProxy) GetClient() client.Client {
114
103
115
104
Eventually (func (g Gomega ) {
116
105
var err error
117
- resClient , err = NewWrappedClient (config , client.Options {Scheme : w .GetScheme ()}, w )
106
+ resClient , err = client . New (config , client.Options {Scheme : w .GetScheme ()})
118
107
g .Expect (err ).NotTo (HaveOccurred ())
119
108
}, "5m" , "10s" ).Should (Succeed ())
120
109
@@ -161,6 +150,13 @@ func (w *wrappedClusterProxy) GetWorkloadCluster(ctx context.Context, namespace,
161
150
w .fixConfig (ctx , name , config )
162
151
}
163
152
153
+ if w .isPacketCluster (ctx , namespace , name ) {
154
+ if ! w .isEMLBCluster (ctx , namespace , name ) {
155
+ logf ("Recording cluster %s for EIP Cleanup later" , name )
156
+ w .eipClusterNames .Insert (name )
157
+ }
158
+ }
159
+
164
160
return newFromAPIConfig (name , config , w .GetScheme ())
165
161
}
166
162
@@ -193,6 +189,19 @@ func (w *wrappedClusterProxy) isDockerCluster(ctx context.Context, namespace str
193
189
return cluster .Spec .InfrastructureRef .Kind == "DockerCluster"
194
190
}
195
191
192
+ func (w * wrappedClusterProxy ) isPacketCluster (ctx context.Context , namespace string , name string ) bool {
193
+ cl := w .GetClient ()
194
+
195
+ cluster := & clusterv1.Cluster {}
196
+ key := client.ObjectKey {
197
+ Name : name ,
198
+ Namespace : namespace ,
199
+ }
200
+ Expect (cl .Get (ctx , key , cluster )).To (Succeed (), "Failed to get %s" , key )
201
+
202
+ return cluster .Spec .InfrastructureRef .Kind == "PacketCluster"
203
+ }
204
+
196
205
func (w * wrappedClusterProxy ) isEMLBCluster (ctx context.Context , namespace string , name string ) bool {
197
206
cl := w .GetClient ()
198
207
@@ -275,109 +284,3 @@ func (w *wrappedClusterProxy) Dispose(ctx context.Context) {
275
284
276
285
w .clusterProxy .Dispose (ctx )
277
286
}
278
-
279
- func NewWrappedClient (config * rest.Config , options client.Options , clusterProxy * wrappedClusterProxy ) (* wrappedClient , error ) {
280
- client , err := client .New (config , options )
281
- if err != nil {
282
- return nil , err
283
- }
284
-
285
- return & wrappedClient {client : client , clusterProxy : clusterProxy }, nil
286
- }
287
-
288
- type wrappedClient struct {
289
- client client.Client
290
- clusterProxy * wrappedClusterProxy
291
- }
292
-
293
- func (wc * wrappedClient ) recordClusterNameForResource (ctx context.Context , obj client.Object ) error {
294
- var clusterName string
295
-
296
- gvk , err := apiutil .GVKForObject (obj , wc .client .Scheme ())
297
- if err != nil {
298
- return err
299
- }
300
-
301
- if gvk .Group == clusterv1 .GroupVersion .Group && gvk .Kind == "Cluster" {
302
- clusterName = obj .GetName ()
303
- }
304
-
305
- labeledCluster , ok := obj .GetLabels ()[clusterv1 .ClusterNameLabel ]
306
- if ok {
307
- clusterName = labeledCluster
308
- }
309
-
310
- if clusterName != "" {
311
- if ! wc .clusterProxy .isEMLBCluster (ctx , obj .GetName (), clusterName ) {
312
- logf ("Recording cluster %s for EIP Cleanup later" , clusterName )
313
- wc .clusterProxy .eipClusterNames .Insert (clusterName )
314
- }
315
- }
316
-
317
- return nil
318
- }
319
-
320
- func (wc * wrappedClient ) Create (ctx context.Context , obj client.Object , opts ... client.CreateOption ) error {
321
- err := wc .recordClusterNameForResource (ctx , obj )
322
- if err != nil {
323
- return err
324
- }
325
-
326
- return wc .client .Create (ctx , obj , opts ... )
327
- }
328
-
329
- func (wc * wrappedClient ) Delete (ctx context.Context , obj client.Object , opts ... client.DeleteOption ) error {
330
- return wc .client .Delete (ctx , obj , opts ... )
331
- }
332
-
333
- func (wc * wrappedClient ) Update (ctx context.Context , obj client.Object , opts ... client.UpdateOption ) error {
334
- return wc .client .Update (ctx , obj , opts ... )
335
- }
336
-
337
- func (wc * wrappedClient ) Patch (ctx context.Context , obj client.Object , patch client.Patch , opts ... client.PatchOption ) error {
338
- return wc .client .Patch (ctx , obj , patch , opts ... )
339
- }
340
-
341
- func (wc * wrappedClient ) DeleteAllOf (ctx context.Context , obj client.Object , opts ... client.DeleteAllOfOption ) error {
342
- // Nothing in the e2e framework appears to be using DeleteAllOf, so we can likely ignore it.
343
- return wc .client .DeleteAllOf (ctx , obj , opts ... )
344
- }
345
-
346
- func (wc * wrappedClient ) Get (ctx context.Context , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
347
- return wc .client .Get (ctx , key , obj , opts ... )
348
- }
349
-
350
- func (wc * wrappedClient ) List (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error {
351
- err := wc .client .List (ctx , list , opts ... )
352
- if err != nil {
353
- return err
354
- }
355
-
356
- if cl , ok := list .(* clusterv1.ClusterList ); ok {
357
- for _ , c := range cl .Items {
358
- if ! wc .clusterProxy .isEMLBCluster (ctx , c .Namespace , c .GetName ()) {
359
- logf ("Recording cluster %s for EIP Cleanup later" , c .GetName ())
360
- wc .clusterProxy .clusterNames .Insert (c .GetName ())
361
- }
362
- }
363
- }
364
-
365
- return nil
366
- }
367
-
368
- func (wc * wrappedClient ) RESTMapper () meta.RESTMapper {
369
- return wc .client .RESTMapper ()
370
- }
371
-
372
- // SubResource returns the sub resource this client is using.
373
- func (wc * wrappedClient ) SubResource (subResource string ) client.SubResourceClient {
374
- return wc .client .SubResource (subResource )
375
- }
376
-
377
- func (wc * wrappedClient ) Scheme () * runtime.Scheme {
378
- return wc .client .Scheme ()
379
- }
380
-
381
- func (wc * wrappedClient ) Status () client.StatusWriter {
382
- return wc .client .Status ()
383
- }
0 commit comments