@@ -3,20 +3,15 @@ package clusters
33import (
44 "context"
55 "fmt"
6- "strings"
76 "sync"
87
98 "golang.org/x/sync/errgroup"
109 corev1 "k8s.io/api/core/v1"
1110 "k8s.io/apimachinery/pkg/api/errors"
1211 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13- "k8s.io/apimachinery/pkg/runtime/schema "
12+ "k8s.io/apimachinery/pkg/runtime"
1413 "k8s.io/apimachinery/pkg/watch"
15- "k8s.io/client-go/dynamic"
1614 "sigs.k8s.io/controller-runtime/pkg/client"
17- gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
18- gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
19- gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
2015)
2116
2217// -----------------------------------------------------------------------------
@@ -27,15 +22,19 @@ import (
2722// used during integration tests to clean up test resources.
2823type Cleaner struct {
2924 cluster Cluster
25+ scheme * runtime.Scheme
3026 objects []client.Object
3127 manifests []string
3228 namespaces []* corev1.Namespace
3329 lock sync.RWMutex
3430}
3531
3632// NewCleaner provides a new initialized *Cleaner object.
37- func NewCleaner (cluster Cluster ) * Cleaner {
38- return & Cleaner {cluster : cluster }
33+ func NewCleaner (cluster Cluster , scheme * runtime.Scheme ) * Cleaner {
34+ return & Cleaner {
35+ cluster : cluster ,
36+ scheme : scheme ,
37+ }
3938}
4039
4140// -----------------------------------------------------------------------------
@@ -63,15 +62,16 @@ func (c *Cleaner) AddNamespace(namespace *corev1.Namespace) {
6362func (c * Cleaner ) Cleanup (ctx context.Context ) error {
6463 c .lock .RLock ()
6564 defer c .lock .RUnlock ()
66- dyn , err := dynamic .NewForConfig (c .cluster .Config ())
65+
66+ cl , err := client .New (c .cluster .Config (), client.Options {
67+ Scheme : c .scheme ,
68+ })
6769 if err != nil {
6870 return err
6971 }
7072
7173 for _ , obj := range c .objects {
72- resource := resourceDeleterForObj (dyn , obj )
73-
74- if err := resource .Delete (ctx , obj .GetName (), metav1.DeleteOptions {}); err != nil {
74+ if err := cl .Delete (ctx , obj ); err != nil {
7575 if ! errors .IsNotFound (err ) {
7676 return err
7777 }
@@ -128,100 +128,6 @@ func (c *Cleaner) Cleanup(ctx context.Context) error {
128128 return g .Wait ()
129129}
130130
131- // fixupObjKinds takes a client.Object and checks if it's of one of the gateway
132- // API types and if so then it adjusts that object's Kind and APIVersion.
133- // This possibly might also need other types to be included but those are enough
134- // for our needs for now especially since that will help cleaning up non-namespaced
135- // GatewayClasses which are not cleaned up on namespace removal also done in
136- // Cleanup().
137- //
138- // The reason we need this is that when decoding to go structs APIVersion and Kind
139- // are dropper because the type info is inherent in the object.
140- // Decoding to unstructured objects (like the dynamic client does) preserves that
141- // information.
142- // There should be a better way of doing this.
143- //
144- // Possibly related:
145- // - https://github.com/kubernetes/kubernetes/issues/3030
146- // - https://github.com/kubernetes/kubernetes/issues/80609
147- func fixupObjKinds (obj client.Object ) client.Object {
148- // If Kind and APIVersion are set then we're good.
149- if obj .GetObjectKind ().GroupVersionKind ().Kind != "" && obj .GetResourceVersion () != "" {
150- return obj
151- }
152-
153- // Otherwise try to fix that up by performing type assertions and filling
154- // those 2 fields accordingly.
155- switch o := obj .(type ) {
156- case * gatewayv1.GatewayClass :
157- o .Kind = "GatewayClass"
158- o .APIVersion = gatewayv1 .GroupVersion .String ()
159- return o
160- case * gatewayv1.Gateway :
161- o .Kind = "Gateway"
162- o .APIVersion = gatewayv1 .GroupVersion .String ()
163- return o
164- case * gatewayv1.HTTPRoute :
165- o .Kind = "HTTPRoute"
166- o .APIVersion = gatewayv1 .GroupVersion .String ()
167- return o
168-
169- case * gatewayv1alpha2.TCPRoute :
170- o .Kind = "TCPRoute"
171- o .APIVersion = gatewayv1alpha2 .GroupVersion .String ()
172- return o
173- case * gatewayv1alpha2.UDPRoute :
174- o .Kind = "UDPRoute"
175- o .APIVersion = gatewayv1alpha2 .GroupVersion .String ()
176- return o
177- case * gatewayv1alpha2.TLSRoute :
178- o .Kind = "TLSRoute"
179- o .APIVersion = gatewayv1alpha2 .GroupVersion .String ()
180- return o
181- case * gatewayv1beta1.ReferenceGrant :
182- o .Kind = "ReferenceGrant"
183- o .APIVersion = gatewayv1beta1 .GroupVersion .String ()
184- return o
185-
186- default :
187- return obj
188- }
189- }
190-
191- type deleter interface {
192- Delete (ctx context.Context , name string , options metav1.DeleteOptions , subresources ... string ) error
193- }
194-
195- func resourceDeleterForObj (dyn * dynamic.DynamicClient , obj client.Object ) deleter {
196- obj = fixupObjKinds (obj )
197-
198- var (
199- namespace = obj .GetNamespace ()
200- kind = obj .GetObjectKind ()
201- gvk = kind .GroupVersionKind ()
202- )
203-
204- var gvr schema.GroupVersionResource
205- switch gvk .Kind {
206- // GatewayClass is a special case because gatewayclass + "s" is not a plural
207- // of gatewayclass.
208- case "GatewayClass" :
209- gvr = schema.GroupVersionResource {
210- Group : gvk .Group ,
211- Version : gvk .Version ,
212- Resource : "gatewayclasses" ,
213- }
214- default :
215- res := strings .ToLower (gvk .Kind ) + "s"
216- gvr = gvk .GroupVersion ().WithResource (res )
217- }
218-
219- if namespace == "" {
220- return dyn .Resource (gvr )
221- }
222- return dyn .Resource (gvr ).Namespace (namespace )
223- }
224-
225131// DumpDiagnostics dumps diagnostics from the underlying cluster.
226132//
227133// Deprecated: Users should use Cluster.DumpDiagnostics().
0 commit comments