forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster_handler.go
83 lines (68 loc) · 1.99 KB
/
cluster_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package rbac
import (
"reflect"
"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
}