-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
74 lines (62 loc) · 2.8 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
package resourcequota
import (
"context"
"github.com/rancher/types/config"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"
)
const (
nsByProjectIndex = "resourcequota.cluster.cattle.io/ns-by-project"
)
func Register(ctx context.Context, cluster *config.UserContext) {
// Index for looking up Namespaces by projectID annotation
nsInformer := cluster.Core.Namespaces("").Controller().Informer()
nsIndexers := map[string]cache.IndexFunc{
nsByProjectIndex: nsByProjectID,
}
nsInformer.AddIndexers(nsIndexers)
sync := &SyncController{
Namespaces: cluster.Core.Namespaces(""),
NamespaceLister: cluster.Core.Namespaces("").Controller().Lister(),
NsIndexer: nsInformer.GetIndexer(),
ResourceQuotas: cluster.Core.ResourceQuotas(""),
ResourceQuotaLister: cluster.Core.ResourceQuotas("").Controller().Lister(),
ProjectLister: cluster.Management.Management.Projects(cluster.ClusterName).Controller().Lister(),
}
cluster.Core.Namespaces("").AddHandler("resourceQuotaSyncController", sync.syncResourceQuota)
reconcile := &reconcileController{
namespaces: cluster.Core.Namespaces(""),
nsIndexer: nsInformer.GetIndexer(),
}
cluster.Management.Management.Projects(cluster.ClusterName).AddHandler("resourceQuotaNamespacesReconcileController", reconcile.reconcileNamespaces)
cleanup := &cleanupController{
projectLister: cluster.Management.Management.Projects(cluster.ClusterName).Controller().Lister(),
namespaceLister: cluster.Core.Namespaces("").Controller().Lister(),
resourceQuotas: cluster.Core.ResourceQuotas(""),
resourceQuotaLister: cluster.Core.ResourceQuotas("").Controller().Lister(),
}
cluster.Core.ResourceQuotas("").AddHandler("resourceQuotaCleanupController", cleanup.cleanup)
calculate := &calculateLimitController{
nsIndexer: nsInformer.GetIndexer(),
projectLister: cluster.Management.Management.Projects(cluster.ClusterName).Controller().Lister(),
projects: cluster.Management.Management.Projects(cluster.ClusterName),
clusterName: cluster.ClusterName,
}
cluster.Core.Namespaces("").AddHandler("resourceQuotaUsedLimitController", calculate.calculateResourceQuotaUsed)
cluster.Management.Management.Projects(cluster.ClusterName).AddHandler("resourceQuotaProjectUsedLimitController", calculate.calculateResourceQuotaUsedProject)
reset := "aResetController{
nsIndexer: nsInformer.GetIndexer(),
namespaces: cluster.Core.Namespaces(""),
}
cluster.Management.Management.Projects(cluster.ClusterName).AddHandler("namespaceResourceQuotaResetController", reset.resetNamespaceQuota)
}
func nsByProjectID(obj interface{}) ([]string, error) {
ns, ok := obj.(*corev1.Namespace)
if !ok {
return []string{}, nil
}
if id, ok := ns.Annotations[projectIDAnnotation]; ok {
return []string{id}, nil
}
return []string{}, nil
}