@@ -30,9 +30,7 @@ import (
3030 . "github.com/onsi/ginkgo/v2"
3131 . "github.com/onsi/gomega"
3232 corev1 "k8s.io/api/core/v1"
33- "k8s.io/apimachinery/pkg/api/meta"
3433 "k8s.io/apimachinery/pkg/runtime"
35- "k8s.io/apimachinery/pkg/runtime/schema"
3634 "k8s.io/apimachinery/pkg/util/sets"
3735 "k8s.io/client-go/kubernetes"
3836 "k8s.io/client-go/rest"
@@ -45,21 +43,12 @@ import (
4543 "sigs.k8s.io/cluster-api/test/infrastructure/container"
4644 "sigs.k8s.io/controller-runtime/pkg/cache"
4745 "sigs.k8s.io/controller-runtime/pkg/client"
48- "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
4946)
5047
5148func logf (format string , a ... interface {}) {
5249 fmt .Fprintf (GinkgoWriter , "INFO: " + format + "\n " , a ... )
5350}
5451
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-
6352// wrappedClusterProxy wraps framework.clusterProxy to add support for retrying if discovery times out
6453// when creating a client. This was needed because control plane upgrade tests
6554// where sometimes attempting to create the client before the elastic IP was migrated.
@@ -114,7 +103,7 @@ func (w *wrappedClusterProxy) GetClient() client.Client {
114103
115104 Eventually (func (g Gomega ) {
116105 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 ()})
118107 g .Expect (err ).NotTo (HaveOccurred ())
119108 }, "5m" , "10s" ).Should (Succeed ())
120109
@@ -161,6 +150,13 @@ func (w *wrappedClusterProxy) GetWorkloadCluster(ctx context.Context, namespace,
161150 w .fixConfig (ctx , name , config )
162151 }
163152
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+
164160 return newFromAPIConfig (name , config , w .GetScheme ())
165161}
166162
@@ -193,6 +189,19 @@ func (w *wrappedClusterProxy) isDockerCluster(ctx context.Context, namespace str
193189 return cluster .Spec .InfrastructureRef .Kind == "DockerCluster"
194190}
195191
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+
196205func (w * wrappedClusterProxy ) isEMLBCluster (ctx context.Context , namespace string , name string ) bool {
197206 cl := w .GetClient ()
198207
@@ -275,109 +284,3 @@ func (w *wrappedClusterProxy) Dispose(ctx context.Context) {
275284
276285 w .clusterProxy .Dispose (ctx )
277286}
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