37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
globalrole_handler.go 2.96 KB
一键复制 编辑 原始数据 按行查看 历史
Craig Jellick 提交于 2018-06-20 15:10 . Improve auth logging
package auth
import (
"reflect"
"github.com/pkg/errors"
"github.com/rancher/types/apis/management.cattle.io/v3"
rbacv1 "github.com/rancher/types/apis/rbac.authorization.k8s.io/v1"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
globalRoleLabel = map[string]string{"authz.management.cattle.io/globalrole": "true"}
crNameAnnotation = "authz.management.cattle.io/cr-name"
clusterRoleKind = "ClusterRole"
grController = "mgmt-auth-gr-controller"
)
func newGlobalRoleLifecycle(management *config.ManagementContext) *globalRoleLifecycle {
return &globalRoleLifecycle{
crLister: management.RBAC.ClusterRoles("").Controller().Lister(),
crClient: management.RBAC.ClusterRoles(""),
}
}
type globalRoleLifecycle struct {
crLister rbacv1.ClusterRoleLister
crClient rbacv1.ClusterRoleInterface
}
func (gr *globalRoleLifecycle) Create(obj *v3.GlobalRole) (*v3.GlobalRole, error) {
err := gr.reconcileGlobalRole(obj)
return obj, err
}
func (gr *globalRoleLifecycle) Updated(obj *v3.GlobalRole) (*v3.GlobalRole, error) {
err := gr.reconcileGlobalRole(obj)
return nil, err
}
func (gr *globalRoleLifecycle) Remove(obj *v3.GlobalRole) (*v3.GlobalRole, error) {
// Don't need to delete the created ClusterRole because owner reference will take care of that
return nil, nil
}
func (gr *globalRoleLifecycle) reconcileGlobalRole(globalRole *v3.GlobalRole) error {
crName := getCRName(globalRole)
clusterRole, _ := gr.crLister.Get("", crName)
if clusterRole != nil {
if !reflect.DeepEqual(globalRole.Rules, clusterRole.Rules) {
clusterRole.Rules = globalRole.Rules
logrus.Infof("[%v] Updating clusterRole %v. GlobalRole rules have changed. Have: %+v. Want: %+v", grController, clusterRole.Name, clusterRole.Rules, globalRole.Rules)
if _, err := gr.crClient.Update(clusterRole); err != nil {
return errors.Wrapf(err, "couldn't update ClusterRole %v", clusterRole.Name)
}
}
return nil
}
logrus.Infof("[%v] Creating clusterRole %v for corresponding GlobalRole", grController, crName)
_, err := gr.crClient.Create(&v1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: crName,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: globalRole.TypeMeta.APIVersion,
Kind: globalRole.TypeMeta.Kind,
Name: globalRole.Name,
UID: globalRole.UID,
},
},
Labels: globalRoleLabel,
},
Rules: globalRole.Rules,
})
if err != nil {
return err
}
// Add an annotation to the globalrole indicating the name we used for future updates
if globalRole.Annotations == nil {
globalRole.Annotations = map[string]string{}
}
globalRole.Annotations[crNameAnnotation] = crName
return nil
}
func getCRName(globalRole *v3.GlobalRole) string {
if crName, ok := globalRole.Annotations[crNameAnnotation]; ok {
return crName
}
return generateCRName(globalRole.Name)
}
func generateCRName(name string) string {
return "cattle-globalrole-" + name
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.7-rc1

搜索帮助

0d507c66 1850385 C8b1a773 1850385