代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。