forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crtb_handler.go
84 lines (68 loc) · 2.31 KB
/
crtb_handler.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 rbac
import (
"github.com/pkg/errors"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
func newCRTBLifecycle(m *manager) *crtbLifecycle {
return &crtbLifecycle{m: m}
}
type crtbLifecycle struct {
m *manager
}
func (c *crtbLifecycle) Create(obj *v3.ClusterRoleTemplateBinding) (runtime.Object, error) {
err := c.syncCRTB(obj)
return obj, err
}
func (c *crtbLifecycle) Updated(obj *v3.ClusterRoleTemplateBinding) (runtime.Object, error) {
err := c.syncCRTB(obj)
return obj, err
}
func (c *crtbLifecycle) Remove(obj *v3.ClusterRoleTemplateBinding) (runtime.Object, error) {
err := c.ensureCRTBDelete(obj)
return obj, err
}
func (c *crtbLifecycle) syncCRTB(binding *v3.ClusterRoleTemplateBinding) error {
if binding.RoleTemplateName == "" {
logrus.Warnf("ClusterRoleTemplateBinding %v has no role template set. Skipping.", binding.Name)
return nil
}
if binding.UserName == "" && binding.GroupPrincipalName == "" && binding.GroupName == "" {
return nil
}
rt, err := c.m.rtLister.Get("", binding.RoleTemplateName)
if err != nil {
return errors.Wrapf(err, "couldn't get role template %v", binding.RoleTemplateName)
}
roles := map[string]*v3.RoleTemplate{}
if err := c.m.gatherRoles(rt, roles); err != nil {
return err
}
if err := c.m.ensureRoles(roles); err != nil {
return errors.Wrap(err, "couldn't ensure roles")
}
if err := c.m.ensureClusterBindings(roles, binding); err != nil {
return errors.Wrapf(err, "couldn't ensure cluster bindings %v", binding)
}
return nil
}
func (c *crtbLifecycle) ensureCRTBDelete(binding *v3.ClusterRoleTemplateBinding) error {
set := labels.Set(map[string]string{rtbOwnerLabel: string(binding.UID)})
bindingCli := c.m.workload.RBAC.ClusterRoleBindings("")
rbs, err := c.m.crbLister.List("", set.AsSelector())
if err != nil {
return errors.Wrapf(err, "couldn't list clusterrolebindings with selector %s", set.AsSelector())
}
for _, rb := range rbs {
if err := bindingCli.Delete(rb.Name, &metav1.DeleteOptions{}); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "error deleting clusterrolebinding %v", rb.Name)
}
}
}
return nil
}