forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_scoped_gc.go
84 lines (71 loc) · 2.17 KB
/
cluster_scoped_gc.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
package clustergc
import (
"context"
"strings"
"github.com/rancher/norman/lifecycle"
"github.com/rancher/norman/resource"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
)
func Register(ctx context.Context, management *config.ManagementContext) {
gc := &gcLifecycle{
mgmt: management,
}
management.Management.Clusters("").AddLifecycle(ctx, "cluster-scoped-gc", gc)
}
type gcLifecycle struct {
mgmt *config.ManagementContext
}
func (c *gcLifecycle) Create(obj *v3.Cluster) (runtime.Object, error) {
return obj, nil
}
func (c *gcLifecycle) Updated(obj *v3.Cluster) (runtime.Object, error) {
return nil, nil
}
func cleanFinalizers(clusterName string, object *unstructured.Unstructured, dynamicClient dynamic.ResourceInterface) (*unstructured.Unstructured, error) {
object = object.DeepCopy()
modified := false
md, err := meta.Accessor(object)
if err != nil {
return object, err
}
finalizers := md.GetFinalizers()
for i := len(finalizers) - 1; i >= 0; i-- {
f := finalizers[i]
if strings.HasPrefix(f, lifecycle.ScopedFinalizerKey) && strings.HasSuffix(f, "_"+clusterName) {
finalizers = append(finalizers[:i], finalizers[i+1:]...)
modified = true
}
}
if modified {
md.SetFinalizers(finalizers)
obj, e := dynamicClient.Update(object, metav1.UpdateOptions{})
return obj, e
}
return object, nil
}
func (c *gcLifecycle) Remove(cluster *v3.Cluster) (runtime.Object, error) {
for key := range resource.Get() {
objList, err := c.mgmt.DynamicClient.Resource(key).List(metav1.ListOptions{})
if err != nil {
if errors.IsNotFound(err) {
// skip this iteration, no objects were initialized of this type for the cluster
continue
}
return cluster, err
}
for _, obj := range objList.Items {
_, err = cleanFinalizers(cluster.Name, &obj, c.mgmt.DynamicClient.Resource(key).Namespace(obj.GetNamespace()))
if err != nil {
return cluster, err
}
}
}
return nil, nil
}