-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
203 lines (166 loc) · 7.41 KB
/
controller.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package alert
import (
"context"
"fmt"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/norman/controller"
"github.com/rancher/rancher/pkg/controllers/user/alert/configsyncer"
"github.com/rancher/rancher/pkg/controllers/user/alert/deployer"
"github.com/rancher/rancher/pkg/controllers/user/alert/manager"
"github.com/rancher/rancher/pkg/controllers/user/alert/statesyncer"
"github.com/rancher/rancher/pkg/controllers/user/alert/watcher"
monitorutil "github.com/rancher/rancher/pkg/monitoring"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
)
var (
SeverityInfo = "info"
SeverityCritical = "critical"
SeverityWarning = "warning"
defaultTimingField = v3.TimingField{
GroupWaitSeconds: 180,
GroupIntervalSeconds: 180,
RepeatIntervalSeconds: 3600,
}
)
func Register(ctx context.Context, cluster *config.UserContext) {
alertmanager := manager.NewAlertManager(cluster)
prometheusCRDManager := manager.NewPrometheusCRDManager(ctx, cluster)
clusterAlertRules := cluster.Management.Management.ClusterAlertRules(cluster.ClusterName)
projectAlertRules := cluster.Management.Management.ProjectAlertRules("")
clusterAlertGroups := cluster.Management.Management.ClusterAlertGroups(cluster.ClusterName)
projectAlertGroups := cluster.Management.Management.ProjectAlertGroups("")
deploy := deployer.NewDeployer(cluster, alertmanager)
clusterAlertGroups.AddClusterScopedHandler(ctx, "cluster-alert-group-deployer", cluster.ClusterName, deploy.ClusterGroupSync)
projectAlertGroups.AddClusterScopedHandler(ctx, "project-alert-group-deployer", cluster.ClusterName, deploy.ProjectGroupSync)
clusterAlertRules.AddClusterScopedHandler(ctx, "cluster-alert-rule-deployer", cluster.ClusterName, deploy.ClusterRuleSync)
projectAlertRules.AddClusterScopedHandler(ctx, "project-alert-rule-deployer", cluster.ClusterName, deploy.ProjectRuleSync)
configSyncer := configsyncer.NewConfigSyncer(ctx, cluster, alertmanager, prometheusCRDManager)
clusterAlertGroups.AddClusterScopedHandler(ctx, "cluster-alert-group-controller", cluster.ClusterName, configSyncer.ClusterGroupSync)
projectAlertGroups.AddClusterScopedHandler(ctx, "project-alert-group-controller", cluster.ClusterName, configSyncer.ProjectGroupSync)
clusterAlertRules.AddClusterScopedHandler(ctx, "cluster-alert-rule-controller", cluster.ClusterName, configSyncer.ClusterRuleSync)
projectAlertRules.AddClusterScopedHandler(ctx, "project-alert-rule-controller", cluster.ClusterName, configSyncer.ProjectRuleSync)
cleaner := &alertGroupCleaner{
clusterName: cluster.ClusterName,
operatorCRDManager: prometheusCRDManager,
clusterAlertRules: clusterAlertRules,
projectAlertRules: projectAlertRules,
clusterAlertGroups: clusterAlertGroups,
projectAlertGroups: projectAlertGroups,
}
cl := &clusterAlertGroupLifecycle{cleaner: cleaner}
pl := &projectAlertGroupLifecycle{cleaner: cleaner}
clusterAlertGroups.AddClusterScopedLifecycle(ctx, "cluster-alert-group-lifecycle", cluster.ClusterName, cl)
projectAlertGroups.AddClusterScopedLifecycle(ctx, "project-alert-group-lifecycle", cluster.ClusterName, pl)
projectLifecycle := &ProjectLifecycle{
projectAlertRules: projectAlertRules,
projectAlertGroups: projectAlertGroups,
clusterName: cluster.ClusterName,
}
projects := cluster.Management.Management.Projects("")
projects.AddClusterScopedLifecycle(ctx, "project-precan-alert-controller", cluster.ClusterName, projectLifecycle)
statesyncer.StartStateSyncer(ctx, cluster, alertmanager)
initClusterPreCanAlerts(clusterAlertGroups, clusterAlertRules, cluster.ClusterName)
watcher.StartEventWatcher(ctx, cluster, alertmanager)
watcher.StartSysComponentWatcher(ctx, cluster, alertmanager)
watcher.StartPodWatcher(ctx, cluster, alertmanager)
watcher.StartWorkloadWatcher(ctx, cluster, alertmanager)
watcher.StartNodeWatcher(ctx, cluster, alertmanager)
}
type clusterAlertGroupLifecycle struct {
cleaner *alertGroupCleaner
}
type projectAlertGroupLifecycle struct {
cleaner *alertGroupCleaner
}
type alertGroupCleaner struct {
clusterName string
operatorCRDManager *manager.PromOperatorCRDManager
clusterAlertRules v3.ClusterAlertRuleInterface
projectAlertRules v3.ProjectAlertRuleInterface
clusterAlertGroups v3.ClusterAlertGroupInterface
projectAlertGroups v3.ProjectAlertGroupInterface
}
func (l *projectAlertGroupLifecycle) Create(obj *v3.ProjectAlertGroup) (runtime.Object, error) {
return obj, nil
}
func (l *projectAlertGroupLifecycle) Updated(obj *v3.ProjectAlertGroup) (runtime.Object, error) {
return obj, nil
}
func (l *projectAlertGroupLifecycle) Remove(obj *v3.ProjectAlertGroup) (runtime.Object, error) {
err := l.cleaner.Clean(nil, obj)
return obj, err
}
func (l *clusterAlertGroupLifecycle) Create(obj *v3.ClusterAlertGroup) (runtime.Object, error) {
return obj, nil
}
func (l *clusterAlertGroupLifecycle) Updated(obj *v3.ClusterAlertGroup) (runtime.Object, error) {
return obj, nil
}
func (l *clusterAlertGroupLifecycle) Remove(obj *v3.ClusterAlertGroup) (runtime.Object, error) {
err := l.cleaner.Clean(obj, nil)
return obj, err
}
func (l *alertGroupCleaner) Clean(clusterGroup *v3.ClusterAlertGroup, projectGroup *v3.ProjectAlertGroup) error {
if clusterGroup != nil {
groupName := fmt.Sprintf("%s:%s", clusterGroup.Namespace, clusterGroup.Name)
list, err := l.clusterAlertRules.List(metav1.ListOptions{})
if err != nil {
return err
}
for _, v := range list.Items {
if v.Spec.GroupName == groupName {
if err := l.clusterAlertRules.DeleteNamespaced(v.Namespace, v.Name, &metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return err
}
}
}
selector := fields.OneTermNotEqualSelector("metadata.name", clusterGroup.Name)
groups, err := l.clusterAlertGroups.List(metav1.ListOptions{FieldSelector: selector.String()})
if err != nil {
return fmt.Errorf("list cluster alert group failed while clean, %v", err)
}
if len(groups.Items) == 0 {
_, namespace := monitorutil.ClusterMonitoringInfo()
if err := l.operatorCRDManager.DeletePrometheusRule(namespace, l.clusterName); err != nil {
return err
}
}
return nil
}
if projectGroup != nil {
groupName := fmt.Sprintf("%s:%s", projectGroup.Namespace, projectGroup.Name)
list, err := l.projectAlertRules.List(metav1.ListOptions{})
if err != nil {
return err
}
for _, v := range list.Items {
if controller.ObjectInCluster(l.clusterName, v) {
if v.Spec.GroupName == groupName {
if err := l.projectAlertRules.DeleteNamespaced(v.Namespace, v.Name, &metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return err
}
}
}
}
_, projectName := ref.Parse(projectGroup.Spec.ProjectName)
s1 := fields.OneTermEqualSelector("metadata.namespace", projectName)
s2 := fields.OneTermNotEqualSelector("metadata.name", projectGroup.Name)
selector := fields.AndSelectors(s2, s1)
groups, err := l.projectAlertGroups.List(metav1.ListOptions{FieldSelector: selector.String()})
if err != nil {
return fmt.Errorf("list project alert group failed while clean, %v", err)
}
if len(groups.Items) == 0 {
_, namespace := monitorutil.ProjectMonitoringInfo(projectName)
if err := l.operatorCRDManager.DeletePrometheusRule(namespace, projectName); err != nil {
return err
}
}
}
return nil
}