-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
115 lines (100 loc) · 5.11 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package monitoring
import (
"context"
"github.com/rancher/rancher/pkg/monitoring"
"github.com/rancher/rancher/pkg/systemaccount"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
)
// Register initializes the controllers and registers
func Register(ctx context.Context, agentContext *config.UserContext) {
clusterName := agentContext.ClusterName
logrus.Infof("Registering monitoring for cluster %q", clusterName)
cattleContext := agentContext.Management
mgmtContext := cattleContext.Management
cattleClustersClient := mgmtContext.Clusters(metav1.NamespaceAll)
cattleProjectsClient := mgmtContext.Projects(clusterName)
// app handler
ah := &appHandler{
cattleAppClient: cattleContext.Project.Apps(metav1.NamespaceAll),
cattleProjectClient: cattleProjectsClient,
cattleSecretClient: cattleContext.Core.Secrets(metav1.NamespaceAll),
cattleTemplateVersionClient: mgmtContext.CatalogTemplateVersions(metav1.NamespaceAll),
cattleClusterGraphClient: mgmtContext.ClusterMonitorGraphs(metav1.NamespaceAll),
cattleProjectGraphClient: mgmtContext.ProjectMonitorGraphs(metav1.NamespaceAll),
cattleMonitorMetricClient: mgmtContext.MonitorMetrics(metav1.NamespaceAll),
agentDeploymentClient: agentContext.Apps.Deployments(metav1.NamespaceAll),
agentDaemonSetClient: agentContext.Apps.DaemonSets(metav1.NamespaceAll),
agentStatefulSetClient: agentContext.Apps.StatefulSets(metav1.NamespaceAll),
agentServiceAccountClient: agentContext.Core.ServiceAccounts(metav1.NamespaceAll),
agentSecretClient: agentContext.Core.Secrets(metav1.NamespaceAll),
agentNodeClient: agentContext.Core.Nodes(metav1.NamespaceAll),
agentNamespaceClient: agentContext.Core.Namespaces(metav1.NamespaceAll),
systemAccountManager: systemaccount.NewManager(agentContext.Management),
}
// operator handler
oh := &operatorHandler{
clusterName: clusterName,
cattleClusterClient: cattleClustersClient,
app: ah,
}
cattleClustersClient.AddHandler(ctx, "prometheus-operator-handler", oh.syncCluster)
cattleProjectsClient.Controller().AddClusterScopedHandler(ctx, "prometheus-operator-handler", clusterName, oh.syncProject)
_, clusterMonitoringNamespace := monitoring.ClusterMonitoringInfo()
agentClusterMonitoringEndpointClient := agentContext.Core.Endpoints(clusterMonitoringNamespace)
// cluster handler
ch := &clusterHandler{
clusterName: clusterName,
cattleClustersClient: cattleClustersClient,
agentEndpointsLister: agentClusterMonitoringEndpointClient.Controller().Lister(),
app: ah,
}
cattleClustersClient.AddHandler(ctx, "cluster-monitoring-handler", ch.sync)
// cluster monitoring enabled handler
cattleClusterController := cattleClustersClient.Controller()
cmeh := &clusterMonitoringEnabledHandler{
clusterName: clusterName,
cattleClusterController: cattleClusterController,
cattleClusterLister: cattleClusterController.Lister(),
}
agentClusterMonitoringEndpointClient.AddHandler(ctx, "cluster-monitoring-enabled-handler", cmeh.sync)
prtbInformer := mgmtContext.ProjectRoleTemplateBindings("").Controller().Informer()
prtbInformer.AddIndexers(map[string]cache.IndexFunc{
prtbBySA: prtbBySAFunc,
})
// project handler
ph := &projectHandler{
clusterName: clusterName,
cattleClusterClient: cattleClustersClient,
cattleProjectClient: cattleProjectsClient,
prtbIndexer: prtbInformer.GetIndexer(),
prtbClient: mgmtContext.ProjectRoleTemplateBindings(""),
app: ah,
}
cattleProjectsClient.Controller().AddClusterScopedHandler(ctx, "project-monitoring-handler", clusterName, ph.sync)
}
func RegisterAgent(ctx context.Context, agentContext *config.UserOnlyContext) {
cp := &ControlPlaneEndpointController{
Endpoints: agentContext.Core.Endpoints("cattle-prometheus"),
EndpointLister: agentContext.Core.Endpoints("cattle-prometheus").Controller().Lister(),
EndpointsController: agentContext.Core.Endpoints("cattle-prometheus").Controller(),
NodeLister: agentContext.Core.Nodes("").Controller().Lister(),
ServiceLister: agentContext.Core.Services("cattle-prometheus").Controller().Lister(),
}
agentContext.Core.Nodes("").AddHandler(ctx, "control-plane-endpoint", cp.sync)
agentContext.Core.Endpoints("cattle-prometheus").AddHandler(ctx, "control-plane-endpoint", cp.syncEndpoints)
promIndexes := cache.Indexers{
promByMemberNamespaceIndex: promsByMemberNamespace,
}
promInformer := agentContext.Monitoring.Prometheuses("").Controller().Informer()
promInformer.AddIndexers(promIndexes)
cr := ConfigRefreshHandler{
prometheusClient: agentContext.Monitoring.Prometheuses(""),
nsLister: agentContext.Core.Namespaces("").Controller().Lister(),
prometheusIndexer: promInformer.GetIndexer(),
}
agentContext.Core.Namespaces("").AddHandler(ctx, "project-monitoring-config-refresh", cr.syncNamespace)
agentContext.Monitoring.Prometheuses("").AddHandler(ctx, "project-monitoring-config-refresh", cr.syncPrometheus)
}