37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
roletemplate_handler.go 2.40 KB
一键复制 编辑 原始数据 按行查看 历史
Craig Jellick 提交于 2018-03-19 15:43 . Sync RoleTemplate and ClusterRole
package authz
import (
"github.com/pkg/errors"
"github.com/rancher/types/apis/management.cattle.io/v3"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func newRTLifecycle(m *manager) *rtLifecycle {
return &rtLifecycle{m: m}
}
// rtLifecycle is responsible for ensuring that roleTemplates and their corresponding clusterRoles stay in sync.
// This means that if a roleTemplate's rules change, this handler will ensure the corresponding clusterRole's rules are changed
// and if a roleTemplate is removed, the corresponding clusterROle is removed.
// This handler does not create new clusterRoles. They are created on the fly when a ProjectRoleTemplateBinding or
// ClusterRoleTemplateBinding references the roleTemplates. This handler only ensures they remain in-sync after being created
type rtLifecycle struct {
m *manager
}
func (c *rtLifecycle) Create(obj *v3.RoleTemplate) (*v3.RoleTemplate, error) {
return obj, nil
}
func (c *rtLifecycle) Updated(obj *v3.RoleTemplate) (*v3.RoleTemplate, error) {
// checky if there are any PRTBs/CRTBs referencing this RoleTemplate for this cluster
prtbs, err := c.m.prtbIndexer.ByIndex(rtbByClusterAndRoleTemplateIndex, c.m.workload.ClusterName+"-"+obj.Name)
if err != nil {
return obj, err
}
hasRTBs := len(prtbs) > 0
if !hasRTBs {
crtbs, err := c.m.crtbIndexer.ByIndex(rtbByClusterAndRoleTemplateIndex, c.m.workload.ClusterName+"-"+obj.Name)
if err != nil {
return obj, err
}
hasRTBs = len(crtbs) > 0
}
// No RTBs referencing this RoleTemplate in this cluster, do not attempt to sync
if !hasRTBs {
return nil, nil
}
err = c.syncRT(obj)
return nil, err
}
func (c *rtLifecycle) Remove(obj *v3.RoleTemplate) (*v3.RoleTemplate, error) {
err := c.ensureRTDelete(obj)
return obj, err
}
func (c *rtLifecycle) syncRT(template *v3.RoleTemplate) error {
roles := map[string]*v3.RoleTemplate{}
if err := c.m.gatherRoles(template, roles); err != nil {
return err
}
if err := c.m.ensureRoles(roles); err != nil {
return errors.Wrapf(err, "couldn't ensure roles")
}
return nil
}
func (c *rtLifecycle) ensureRTDelete(template *v3.RoleTemplate) error {
roleCli := c.m.workload.K8sClient.RbacV1().ClusterRoles()
if err := roleCli.Delete(template.Name, &metav1.DeleteOptions{}); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "error deleting clusterrole %v", template.Name)
}
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.0-alpha24

搜索帮助