-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
bindings.go
58 lines (54 loc) · 2.06 KB
/
bindings.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
package reaper
import (
"github.com/golang/glog"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kapi "k8s.io/kubernetes/pkg/apis/core"
authclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset"
)
// reapClusterBindings removes the subject from cluster-level role bindings
func reapClusterBindings(removedSubject kapi.ObjectReference, c authclient.Interface) error {
clusterBindings, err := c.Authorization().ClusterRoleBindings().List(metav1.ListOptions{})
if err != nil {
return err
}
for _, binding := range clusterBindings.Items {
retainedSubjects := []kapi.ObjectReference{}
for _, subject := range binding.Subjects {
if subject != removedSubject {
retainedSubjects = append(retainedSubjects, subject)
}
}
if len(retainedSubjects) != len(binding.Subjects) {
updatedBinding := binding
updatedBinding.Subjects = retainedSubjects
if _, err := c.Authorization().ClusterRoleBindings().Update(&updatedBinding); err != nil && !kerrors.IsNotFound(err) {
glog.Infof("Cannot update clusterrolebinding/%s: %v", binding.Name, err)
}
}
}
return nil
}
// reapNamespacedBindings removes the subject from namespaced role bindings
func reapNamespacedBindings(removedSubject kapi.ObjectReference, c authclient.Interface) error {
namespacedBindings, err := c.Authorization().RoleBindings(metav1.NamespaceAll).List(metav1.ListOptions{})
if err != nil {
return err
}
for _, binding := range namespacedBindings.Items {
retainedSubjects := []kapi.ObjectReference{}
for _, subject := range binding.Subjects {
if subject != removedSubject {
retainedSubjects = append(retainedSubjects, subject)
}
}
if len(retainedSubjects) != len(binding.Subjects) {
updatedBinding := binding
updatedBinding.Subjects = retainedSubjects
if _, err := c.Authorization().RoleBindings(binding.Namespace).Update(&updatedBinding); err != nil && !kerrors.IsNotFound(err) {
glog.Infof("Cannot update rolebinding/%s in %s: %v", binding.Name, binding.Namespace, err)
}
}
}
return nil
}