37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
crtb_handler.go 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2018-07-24 14:45 . Add imported cluster cleanup
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"
)
func newCRTBLifecycle(m *manager) *crtbLifecycle {
return &crtbLifecycle{m: m}
}
type crtbLifecycle struct {
m *manager
}
func (c *crtbLifecycle) Create(obj *v3.ClusterRoleTemplateBinding) (*v3.ClusterRoleTemplateBinding, error) {
err := c.syncCRTB(obj)
return obj, err
}
func (c *crtbLifecycle) Updated(obj *v3.ClusterRoleTemplateBinding) (*v3.ClusterRoleTemplateBinding, error) {
err := c.syncCRTB(obj)
return obj, err
}
func (c *crtbLifecycle) Remove(obj *v3.ClusterRoleTemplateBinding) (*v3.ClusterRoleTemplateBinding, 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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.9-rc2

搜索帮助