-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy_prtb_cleaner.go
72 lines (55 loc) · 1.41 KB
/
legacy_prtb_cleaner.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
package rbac
import (
"regexp"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var crbNamePattern = regexp.MustCompile("clusterrolebinding-.+")
var namespaceRoleRefPattern = regexp.MustCompile(".+-namespaces-.+")
var promotedRoleRefPattern = regexp.MustCompile(".+-promoted")
const createNSRoleRef = "create-ns"
func newLegacyCRBCleaner(m *manager) *crbCleaner {
return &crbCleaner{
m: m,
}
}
type crbCleaner struct {
m *manager
}
func (p *crbCleaner) sync(key string, obj *rbacv1.ClusterRoleBinding) error {
if key == "" || obj == nil {
return nil
}
eligible, err := p.eligibleForDeletion(obj)
if err != nil {
return err
}
if eligible {
if err := p.m.workload.RBAC.ClusterRoleBindings("").Delete(obj.Name, &metav1.DeleteOptions{}); err != nil {
if !errors.IsNotFound(err) {
return err
}
}
}
return nil
}
func (p *crbCleaner) eligibleForDeletion(crb *rbacv1.ClusterRoleBinding) (bool, error) {
if !crbNamePattern.MatchString(crb.Name) {
return false, nil
}
if noOwners, err := p.m.noRemainingOwnerLabels(crb); !noOwners || err != nil {
return false, err
}
roleRefName := crb.RoleRef.Name
if roleRefName == createNSRoleRef {
return true, nil
}
if namespaceRoleRefPattern.MatchString(roleRefName) {
return true, nil
}
if promotedRoleRefPattern.MatchString(roleRefName) {
return true, nil
}
return false, nil
}