forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initdata.go
500 lines (460 loc) · 14.6 KB
/
initdata.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package alert
import (
"fmt"
"github.com/rancher/rancher/pkg/controllers/user/alert/common"
"github.com/rancher/rancher/pkg/controllers/user/alert/manager"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
func initClusterPreCanAlerts(clusterAlertGroups v3.ClusterAlertGroupInterface, clusterAlertRules v3.ClusterAlertRuleInterface, clusterName string) {
name := "etcd-alert"
group := &v3.ClusterAlertGroup{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterGroupSpec{
ClusterName: clusterName,
CommonGroupField: v3.CommonGroupField{
Description: "Alert for etcd leader existence, db size",
DisplayName: "A set of alerts for etcd",
TimingField: defaultTimingField,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertGroups.Create(group); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan alert group for etcd: %v", err)
}
name = "no-leader"
rule := &v3.ClusterAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterAlertRuleSpec{
ClusterName: clusterName,
GroupName: common.GetGroupID(clusterName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityCritical,
DisplayName: "Etcd member has no leader",
TimingField: defaultTimingField,
},
MetricRule: &v3.MetricRule{
Description: "Etcd member has no leader",
Expression: `etcd_server_has_leader`,
Comparison: manager.ComparisonNotEqual,
Duration: "3m",
ThresholdValue: 1,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
name = "high-number-of-leader-changes"
rule = &v3.ClusterAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterAlertRuleSpec{
ClusterName: clusterName,
GroupName: common.GetGroupID(clusterName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityWarning,
DisplayName: "A high number of leader changes within the etcd cluster are happening",
TimingField: defaultTimingField,
},
MetricRule: &v3.MetricRule{
Description: "Etcd instance has seen high number of leader changes within the last hour",
Expression: `increase(etcd_server_leader_changes_seen_total[1h])`,
Comparison: manager.ComparisonGreaterThan,
Duration: "3m",
ThresholdValue: 3,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
name = "db-over-size"
rule = &v3.ClusterAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterAlertRuleSpec{
ClusterName: clusterName,
GroupName: common.GetGroupID(clusterName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityWarning,
DisplayName: "Database usage close to the quota 500M",
TimingField: defaultTimingField,
},
MetricRule: &v3.MetricRule{
Description: "Shows the etcd database size including free space waiting for defragmentation close to the quota",
Expression: `sum(etcd_debugging_mvcc_db_total_size_in_bytes)`,
Comparison: manager.ComparisonGreaterThan,
Duration: "3m",
ThresholdValue: 524288000,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
name = "etcd-system-service"
rule = &v3.ClusterAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterAlertRuleSpec{
ClusterName: clusterName,
GroupName: common.GetGroupID(clusterName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityCritical,
DisplayName: "Etcd is unavailable",
TimingField: defaultTimingField,
},
SystemServiceRule: &v3.SystemServiceRule{
Condition: "etcd",
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
name = "kube-components-alert"
group = &v3.ClusterAlertGroup{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterGroupSpec{
ClusterName: clusterName,
CommonGroupField: v3.CommonGroupField{
Description: "Alert for kube components api server, scheduler, controller manager",
DisplayName: "A set of alerts for kube components",
TimingField: defaultTimingField,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertGroups.Create(group); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for controller manager, scheduler: %v", err)
}
name = "scheduler-system-service"
rule = &v3.ClusterAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterAlertRuleSpec{
ClusterName: clusterName,
GroupName: common.GetGroupID(clusterName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityCritical,
DisplayName: "Scheduler is unavailable",
TimingField: defaultTimingField,
},
SystemServiceRule: &v3.SystemServiceRule{
Condition: "scheduler",
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
name = "controllermanager-system-service"
rule = &v3.ClusterAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterAlertRuleSpec{
ClusterName: clusterName,
GroupName: common.GetGroupID(clusterName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityCritical,
DisplayName: "Controller Manager is unavailable",
TimingField: defaultTimingField,
},
SystemServiceRule: &v3.SystemServiceRule{
Condition: "controller-manager",
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
name = "node-alert"
group = &v3.ClusterAlertGroup{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterGroupSpec{
ClusterName: clusterName,
CommonGroupField: v3.CommonGroupField{
Description: "Alert for Node Memory, CPU, Disk Usage",
DisplayName: "A set of alerts for node",
TimingField: defaultTimingField,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertGroups.Create(group); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for node: %v", err)
}
name = "node-disk-running-full"
rule = &v3.ClusterAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterAlertRuleSpec{
ClusterName: clusterName,
GroupName: common.GetGroupID(clusterName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityCritical,
DisplayName: "Node disk is running full within 24 hours",
TimingField: defaultTimingField,
},
MetricRule: &v3.MetricRule{
Description: "Device on node is running full within the next 24 hours",
Expression: `predict_linear(node_filesystem_files_free{mountpoint!~"^/etc/(?:resolv.conf|hosts|hostname)$"}[6h], 3600 * 24)`,
Comparison: manager.ComparisonLessOrEqual,
Duration: "10m",
ThresholdValue: 1,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
name = "high-memmory"
rule = &v3.ClusterAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterAlertRuleSpec{
ClusterName: clusterName,
GroupName: common.GetGroupID(clusterName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityWarning,
DisplayName: "High node memory utilization",
TimingField: defaultTimingField,
},
MetricRule: &v3.MetricRule{
Description: "Node memory utilization is over 80%",
Expression: `1 - sum(node_memory_MemAvailable_bytes) by (instance) / sum(node_memory_MemTotal_bytes) by (instance)`,
Comparison: manager.ComparisonGreaterOrEqual,
Duration: "3m",
ThresholdValue: 80,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
name = "high-cpu-load"
rule = &v3.ClusterAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterAlertRuleSpec{
ClusterName: clusterName,
GroupName: common.GetGroupID(clusterName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityWarning,
DisplayName: "High cpu load",
TimingField: defaultTimingField,
},
MetricRule: &v3.MetricRule{
Description: "The cpu load is higher than 100",
Expression: `sum(node_load1) by (instance) / count(node_cpu_seconds_total{mode="system"}) by (instance) * 100`,
Comparison: manager.ComparisonGreaterThan,
Duration: "3m",
ThresholdValue: 100,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
name = "event-alert"
group = &v3.ClusterAlertGroup{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterGroupSpec{
ClusterName: clusterName,
CommonGroupField: v3.CommonGroupField{
Description: "Alert for receiving resource event",
DisplayName: "A set of alerts when event happened",
TimingField: defaultTimingField,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertGroups.Create(group); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for event: %v", err)
}
name = "deployment-event-alert"
rule = &v3.ClusterAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v3.ClusterAlertRuleSpec{
ClusterName: clusterName,
GroupName: common.GetGroupID(clusterName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityWarning,
DisplayName: "Get warning deployment event",
TimingField: defaultTimingField,
},
EventRule: &v3.EventRule{
EventType: "Warning",
ResourceKind: "Deployment",
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := clusterAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
}
type ProjectLifecycle struct {
projectAlertGroups v3.ProjectAlertGroupInterface
projectAlertRules v3.ProjectAlertRuleInterface
clusterName string
}
//Create built-in project alerts
func (l *ProjectLifecycle) Create(obj *v3.Project) (runtime.Object, error) {
name := "projectalert-workload-alert"
projectName := obj.Name
projectID := fmt.Sprintf("%s:%s", obj.Namespace, obj.Name)
group := &v3.ProjectAlertGroup{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: projectName,
},
Spec: v3.ProjectGroupSpec{
ProjectName: projectID,
CommonGroupField: v3.CommonGroupField{
DisplayName: "A set of alerts for workload, pod, container",
Description: "Alert for cpu, memory, disk, network",
TimingField: defaultTimingField,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := l.projectAlertGroups.Create(group); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create built-in rules for deployment: %v", err)
}
name = "less-than-half-workload-available"
rule := &v3.ProjectAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: projectName,
},
Spec: v3.ProjectAlertRuleSpec{
ProjectName: projectID,
GroupName: common.GetGroupID(projectName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityCritical,
DisplayName: "Less than half workload available",
TimingField: defaultTimingField,
},
WorkloadRule: &v3.WorkloadRule{
Selector: map[string]string{
"app": "workload",
},
AvailablePercentage: 50,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := l.projectAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
name = "memory-close-to-resource-limited"
rule = &v3.ProjectAlertRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: projectName,
},
Spec: v3.ProjectAlertRuleSpec{
ProjectName: projectID,
GroupName: common.GetGroupID(projectName, group.Name),
CommonRuleField: v3.CommonRuleField{
Severity: SeverityWarning,
DisplayName: "Memory usage close to the quota",
TimingField: defaultTimingField,
},
MetricRule: &v3.MetricRule{
Description: "Container using memory close to the quota",
Expression: `sum(container_memory_working_set_bytes) by (pod_name, container_name) / sum(label_join(label_join(kube_pod_container_resource_limits_memory_bytes,"pod_name", "", "pod"),"container_name", "", "container")) by (pod_name, container_name)`,
Comparison: manager.ComparisonGreaterThan,
Duration: "3m",
ThresholdValue: 1,
},
},
Status: v3.AlertStatus{
AlertState: "active",
},
}
if _, err := l.projectAlertRules.Create(rule); err != nil && !apierrors.IsAlreadyExists(err) {
logrus.Warnf("Failed to create precan rules for %s: %v", name, err)
}
return obj, nil
}
func (l *ProjectLifecycle) Updated(obj *v3.Project) (runtime.Object, error) {
return obj, nil
}
func (l *ProjectLifecycle) Remove(obj *v3.Project) (runtime.Object, error) {
return obj, nil
}
func getCommonRuleField(groupID, displayName, severity string) v3.CommonRuleField {
return v3.CommonRuleField{
DisplayName: displayName,
Severity: severity,
TimingField: defaultTimingField,
}
}