forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_scoped_gc.go
204 lines (181 loc) · 5.94 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
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
package clustergc
import (
"context"
"strings"
"github.com/rancher/norman/lifecycle"
"github.com/rancher/norman/objectclient"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)
const (
prtbByClusterIndex = "managment.cattle.io/prtb-by-cluster"
)
func Register(ctx context.Context, management *config.ManagementContext) {
informer := management.Management.ProjectRoleTemplateBindings("").Controller().Informer()
indexers := map[string]cache.IndexFunc{
prtbByClusterIndex: prtbByCluster,
}
informer.AddIndexers(indexers)
gc := &gcLifecycle{
rtLister: management.Management.RoleTemplates("").Controller().Lister(),
grbLister: management.Management.GlobalRoleBindings("").Controller().Lister(),
projectLister: management.Management.Projects("").Controller().Lister(),
crtbLister: management.Management.ClusterRoleTemplateBindings("").Controller().Lister(),
projectAlertLister: management.Management.ProjectAlertGroups("").Controller().Lister(),
nodeLister: management.Management.Nodes("").Controller().Lister(),
psptLister: management.Management.PodSecurityPolicyTemplates("").Controller().Lister(),
secretsLister: management.Core.Secrets("").Controller().Lister(),
prtbIndexer: informer.GetIndexer(),
mgmt: management,
}
management.Management.Clusters("").AddLifecycle(ctx, "cluster-scoped-gc", gc)
}
type gcLifecycle struct {
projectLister v3.ProjectLister
crtbLister v3.ClusterRoleTemplateBindingLister
projectAlertLister v3.ProjectAlertGroupLister
prtbIndexer cache.Indexer
nodeLister v3.NodeLister
rtLister v3.RoleTemplateLister
grbLister v3.GlobalRoleBindingLister
psptLister v3.PodSecurityPolicyTemplateLister
secretsLister v1.SecretLister
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 runtime.Object, client *objectclient.ObjectClient) error {
object = object.DeepCopyObject()
modified := false
md, err := meta.Accessor(object)
if err != nil {
return 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)
_, err := client.Update(md.GetName(), object)
return err
}
return nil
}
func (c *gcLifecycle) Remove(cluster *v3.Cluster) (runtime.Object, error) {
rts, err := c.rtLister.List("", labels.Everything())
if err != nil {
return cluster, err
}
oClient := c.mgmt.Management.RoleTemplates("").ObjectClient()
for _, rt := range rts {
if err := cleanFinalizers(cluster.Name, rt, oClient); err != nil {
return cluster, err
}
}
grbs, err := c.grbLister.List("", labels.Everything())
if err != nil {
return cluster, err
}
oClient = c.mgmt.Management.GlobalRoleBindings("").ObjectClient()
for _, grb := range grbs {
if err := cleanFinalizers(cluster.Name, grb, oClient); err != nil {
return cluster, err
}
}
projects, err := c.projectLister.List(cluster.Name, labels.Everything())
if err != nil {
return cluster, err
}
oClient = c.mgmt.Management.Projects(cluster.Name).ObjectClient()
for _, p := range projects {
if err := cleanFinalizers(cluster.Name, p, oClient); err != nil {
return cluster, err
}
}
crtbs, err := c.crtbLister.List(cluster.Name, labels.Everything())
if err != nil {
return cluster, err
}
oClient = c.mgmt.Management.ClusterRoleTemplateBindings("").ObjectClient()
for _, p := range crtbs {
if err := cleanFinalizers(cluster.Name, p, oClient); err != nil {
return cluster, err
}
}
alerts, err := c.projectAlertLister.List("", labels.Everything())
if err != nil {
return cluster, err
}
oClient = c.mgmt.Management.ProjectAlertGroups("").ObjectClient()
for _, p := range alerts {
if err := cleanFinalizers(cluster.Name, p, oClient); err != nil {
return cluster, err
}
}
secrets, err := c.secretsLister.List("", labels.Everything())
if err != nil {
return cluster, err
}
oClient = c.mgmt.Core.Secrets("").ObjectClient()
for _, p := range secrets {
if err := cleanFinalizers(cluster.Name, p, oClient); err != nil {
return cluster, err
}
}
nodes, err := c.nodeLister.List(cluster.Name, labels.Everything())
if err != nil {
return cluster, err
}
oClient = c.mgmt.Management.Nodes("").ObjectClient()
for _, p := range nodes {
if err := cleanFinalizers(cluster.Name, p, oClient); err != nil {
return cluster, err
}
}
pspts, err := c.psptLister.List("", labels.Everything())
if err != nil {
return cluster, err
}
oClient = c.mgmt.Management.PodSecurityPolicyTemplates("").ObjectClient()
for _, pspt := range pspts {
if err := cleanFinalizers(cluster.Name, pspt, oClient); err != nil {
return cluster, err
}
}
prtbs, err := c.prtbIndexer.ByIndex(prtbByClusterIndex, cluster.Name)
if err != nil {
return cluster, err
}
for _, pr := range prtbs {
prtb, _ := pr.(*v3.ProjectRoleTemplateBinding)
oClient = c.mgmt.Management.ProjectRoleTemplateBindings(prtb.Namespace).ObjectClient()
if err := cleanFinalizers(cluster.Name, prtb, oClient); err != nil {
return cluster, err
}
}
return nil, nil
}
func prtbByCluster(obj interface{}) ([]string, error) {
prtb, ok := obj.(*v3.ProjectRoleTemplateBinding)
if !ok {
return []string{}, nil
}
if parts := strings.SplitN(prtb.ProjectName, ":", 2); len(parts) == 2 && len(parts[1]) > 0 {
return []string{parts[0]}, nil
}
return []string{}, nil
}