forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
86 lines (77 loc) · 2.95 KB
/
register.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
84
85
86
package clusterauthtoken
import (
"context"
"fmt"
"github.com/rancher/rancher/pkg/controllers/user/clusterauthtoken/common"
managementv3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/cache"
)
const tokenByUserAndClusterIndex = "auth.management.cattle.io/token-by-user-and-cluster"
func Register(ctx context.Context, cluster *config.UserContext) {
tokenInformer := cluster.Management.Management.Tokens("").Controller().Informer()
tokenIndexers := map[string]cache.IndexFunc{
tokenByUserAndClusterIndex: tokenByUserAndCluster,
}
if err := tokenInformer.AddIndexers(tokenIndexers); err != nil {
logrus.Error(err)
return
}
namespace := common.DefaultNamespace
clusterName := cluster.ClusterName
clusterAuthToken := cluster.Cluster.ClusterAuthTokens(namespace)
clusterAuthTokenLister := cluster.Cluster.ClusterAuthTokens(namespace).Controller().Lister()
clusterUserAttribute := cluster.Cluster.ClusterUserAttributes(namespace)
clusterUserAttributeLister := cluster.Cluster.ClusterUserAttributes(namespace).Controller().Lister()
clusterConfigMap := cluster.Core.ConfigMaps(namespace)
clusterConfigMapLister := cluster.Core.ConfigMaps(namespace).Controller().Lister()
tokenIndexer := tokenInformer.GetIndexer()
userLister := cluster.Management.Management.Users("").Controller().Lister()
userAttribute := cluster.Management.Management.UserAttributes("")
userAttributeLister := cluster.Management.Management.UserAttributes("").Controller().Lister()
settingInterface := cluster.Management.Management.Settings("")
cluster.Management.Management.Settings("").AddHandler(ctx, "cat-setting-controller", (&settingHandler{
namespace,
clusterConfigMap,
clusterConfigMapLister,
settingInterface,
}).Sync)
cluster.Management.Management.Tokens("").AddClusterScopedLifecycle(ctx,
"cat-token-controller",
clusterName,
&tokenHandler{
namespace,
clusterAuthToken,
clusterAuthTokenLister,
clusterUserAttribute,
clusterUserAttributeLister,
tokenIndexer,
userLister,
userAttributeLister,
})
cluster.Management.Management.Users("").AddHandler(ctx, "cat-user-controller", (&userHandler{
namespace,
clusterUserAttribute,
clusterUserAttributeLister,
}).Sync)
cluster.Management.Management.UserAttributes("").AddHandler(ctx, "cat-user-attribute-controller", (&userAttributeHandler{
namespace,
clusterUserAttribute,
clusterUserAttributeLister,
}).Sync)
cluster.Cluster.ClusterUserAttributes(namespace).AddHandler(ctx, "cat-cluster-user-attribute-controller", (&clusterUserAttributeHandler{
userAttribute,
userAttributeLister,
}).Sync)
}
func tokenUserClusterKey(token *managementv3.Token) string {
return fmt.Sprintf("%s/%s", token.UserID, token.ClusterName)
}
func tokenByUserAndCluster(obj interface{}) ([]string, error) {
t, ok := obj.(*managementv3.Token)
if !ok {
return []string{}, nil
}
return []string{tokenUserClusterKey(t)}, nil
}