-
Notifications
You must be signed in to change notification settings - Fork 10
/
cleanup.go
231 lines (201 loc) · 6.31 KB
/
cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package clusters
import (
"context"
"fmt"
"strings"
"sync"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)
// -----------------------------------------------------------------------------
// Cleaner - Public
// -----------------------------------------------------------------------------
// Cleaner holds namespaces and objects for later cleanup. This is generally
// used during integration tests to clean up test resources.
type Cleaner struct {
cluster Cluster
objects []client.Object
manifests []string
namespaces []*corev1.Namespace
lock sync.RWMutex
}
// NewCleaner provides a new initialized *Cleaner object.
func NewCleaner(cluster Cluster) *Cleaner {
return &Cleaner{cluster: cluster}
}
// -----------------------------------------------------------------------------
// Cleaner - Public
// -----------------------------------------------------------------------------
func (c *Cleaner) Add(obj client.Object) {
c.lock.Lock()
defer c.lock.Unlock()
c.objects = append([]client.Object{obj}, c.objects...)
}
func (c *Cleaner) AddManifest(manifest string) {
c.lock.Lock()
defer c.lock.Unlock()
c.manifests = append(c.manifests, manifest)
}
func (c *Cleaner) AddNamespace(namespace *corev1.Namespace) {
c.lock.Lock()
defer c.lock.Unlock()
c.namespaces = append(c.namespaces, namespace)
}
func (c *Cleaner) Cleanup(ctx context.Context) error {
c.lock.RLock()
defer c.lock.RUnlock()
dyn, err := dynamic.NewForConfig(c.cluster.Config())
if err != nil {
return err
}
for _, obj := range c.objects {
resource := resourceDeleterForObj(dyn, obj)
if err := resource.Delete(ctx, obj.GetName(), metav1.DeleteOptions{}); err != nil {
if !errors.IsNotFound(err) {
return err
}
}
}
for _, manifest := range c.manifests {
err := DeleteManifestByYAML(ctx, c.cluster, manifest)
if err != nil {
return err
}
}
g, ctx := errgroup.WithContext(ctx)
// Limit the concurrency level to not overwhelm the API server.
g.SetLimit(8) //nolint:mnd
for _, namespace := range c.namespaces {
namespace := namespace
g.Go(func() error {
namespaceClient := c.cluster.Client().CoreV1().Namespaces()
if err := namespaceClient.Delete(ctx, namespace.Name, metav1.DeleteOptions{}); err != nil {
if errors.IsNotFound(err) {
// If the namespace cannot be found then we're good to go.
return nil
}
return err
}
w, err := namespaceClient.Watch(ctx, metav1.ListOptions{
LabelSelector: "kubernetes.io/metadata.name=" + namespace.Name,
})
if err != nil {
return err
}
defer w.Stop()
for {
select {
case event := <-w.ResultChan():
if event.Type == watch.Deleted {
return nil
}
case <-ctx.Done():
return fmt.Errorf(
"failed to delete namespace %q because the context is done", namespace.Name,
)
}
}
})
}
return g.Wait()
}
// fixupObjKinds takes a client.Object and checks if it's of one of the gateway
// API types and if so then it adjusts that object's Kind and APIVersion.
// This possibly might also need other types to be included but those are enough
// for our needs for now especially since that will help cleaning up non-namespaced
// GatewayClasses which are not cleaned up on namespace removal also done in
// Cleanup().
//
// The reason we need this is that when decoding to go structs APIVersion and Kind
// are dropper because the type info is inherent in the object.
// Decoding to unstructured objects (like the dynamic client does) preserves that
// information.
// There should be a better way of doing this.
//
// Possibly related:
// - https://github.com/kubernetes/kubernetes/issues/3030
// - https://github.com/kubernetes/kubernetes/issues/80609
func fixupObjKinds(obj client.Object) client.Object {
// If Kind and APIVersion are set then we're good.
if obj.GetObjectKind().GroupVersionKind().Kind != "" && obj.GetResourceVersion() != "" {
return obj
}
// Otherwise try to fix that up by performing type assertions and filling
// those 2 fields accordingly.
switch o := obj.(type) {
case *gatewayv1.GatewayClass:
o.Kind = "GatewayClass"
o.APIVersion = gatewayv1.GroupVersion.String()
return o
case *gatewayv1.Gateway:
o.Kind = "Gateway"
o.APIVersion = gatewayv1.GroupVersion.String()
return o
case *gatewayv1.HTTPRoute:
o.Kind = "HTTPRoute"
o.APIVersion = gatewayv1.GroupVersion.String()
return o
case *gatewayv1alpha2.TCPRoute:
o.Kind = "TCPRoute"
o.APIVersion = gatewayv1alpha2.GroupVersion.String()
return o
case *gatewayv1alpha2.UDPRoute:
o.Kind = "UDPRoute"
o.APIVersion = gatewayv1alpha2.GroupVersion.String()
return o
case *gatewayv1alpha2.TLSRoute:
o.Kind = "TLSRoute"
o.APIVersion = gatewayv1alpha2.GroupVersion.String()
return o
case *gatewayv1beta1.ReferenceGrant:
o.Kind = "ReferenceGrant"
o.APIVersion = gatewayv1beta1.GroupVersion.String()
return o
default:
return obj
}
}
type deleter interface {
Delete(ctx context.Context, name string, options metav1.DeleteOptions, subresources ...string) error
}
func resourceDeleterForObj(dyn *dynamic.DynamicClient, obj client.Object) deleter {
obj = fixupObjKinds(obj)
var (
namespace = obj.GetNamespace()
kind = obj.GetObjectKind()
gvk = kind.GroupVersionKind()
)
var gvr schema.GroupVersionResource
switch gvk.Kind {
// GatewayClass is a special case because gatewayclass + "s" is not a plural
// of gatewayclass.
case "GatewayClass":
gvr = schema.GroupVersionResource{
Group: gvk.Group,
Version: gvk.Version,
Resource: "gatewayclasses",
}
default:
res := strings.ToLower(gvk.Kind) + "s"
gvr = gvk.GroupVersion().WithResource(res)
}
if namespace == "" {
return dyn.Resource(gvr)
}
return dyn.Resource(gvr).Namespace(namespace)
}
// DumpDiagnostics dumps diagnostics from the underlying cluster.
//
// Deprecated: Users should use Cluster.DumpDiagnostics().
func (c *Cleaner) DumpDiagnostics(ctx context.Context, meta string) (string, error) {
return c.cluster.DumpDiagnostics(ctx, meta)
}