37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cluster_handler.go 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package rbac
import (
"reflect"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)
const (
grbByRoleIndex = "management.cattle.io/grb-by-role"
)
func newClusterHandler(workload *config.UserContext) v3.ClusterHandlerFunc { //*clusterHandler {
informer := workload.Management.Management.GlobalRoleBindings("").Controller().Informer()
indexers := map[string]cache.IndexFunc{
grbByRoleIndex: grbByRole,
}
informer.AddIndexers(indexers)
ch := &clusterHandler{
grbIndexer: informer.GetIndexer(),
grbController: workload.Management.Management.GlobalRoleBindings("").Controller(),
clusters: workload.Management.Management.Clusters(""),
clusterName: workload.ClusterName,
}
return ch.sync
}
type clusterHandler struct {
grbController v3.GlobalRoleBindingController
grbIndexer cache.Indexer
clusters v3.ClusterInterface
clusterName string
}
func (h *clusterHandler) sync(key string, cluster *v3.Cluster) (runtime.Object, error) {
if key == "" || cluster == nil || cluster.Name != h.clusterName {
return nil, nil
}
original := cluster
cluster = original.DeepCopy()
var updateErr error
err := h.doSync(cluster)
if cluster != nil && !reflect.DeepEqual(cluster, original) {
_, updateErr = h.clusters.Update(cluster)
}
if err != nil {
return nil, err
}
return nil, updateErr
}
func (h *clusterHandler) doSync(cluster *v3.Cluster) error {
_, err := v3.ClusterConditionGlobalAdminsSynced.DoUntilTrue(cluster, func() (runtime.Object, error) {
grbs, err := h.grbIndexer.ByIndex(grbByRoleIndex, "admin")
if err != nil {
return cluster, err
}
for _, x := range grbs {
grb, _ := x.(*v3.GlobalRoleBinding)
h.grbController.Enqueue("", grb.Name)
}
return cluster, nil
})
return err
}
func grbByRole(obj interface{}) ([]string, error) {
grb, ok := obj.(*v3.GlobalRoleBinding)
if !ok {
return []string{}, nil
}
return []string{grb.GlobalRoleName}, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.10-rc1

搜索帮助

0d507c66 1850385 C8b1a773 1850385