forked from kubevirt/ssp-operator
/
resources.go
307 lines (287 loc) · 9.92 KB
/
resources.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
package metrics
import (
"errors"
"fmt"
"os"
"strings"
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
v1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
rbac "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
const (
PrometheusRuleName = "prometheus-k8s-rules-cnv"
MonitorNamespace = "openshift-monitoring"
defaultRunbookURLTemplate = "https://kubevirt.io/monitoring/runbooks/%s"
runbookURLTemplateEnv = "RUNBOOK_URL_TEMPLATE"
severityAlertLabelKey = "severity"
healthImpactAlertLabelKey = "operator_health_impact"
partOfAlertLabelKey = "kubernetes_operator_part_of"
partOfAlertLabelValue = "kubevirt"
componentAlertLabelKey = "kubernetes_operator_component"
componentAlertLabelValue = "ssp-operator"
PrometheusLabelKey = "prometheus.ssp.kubevirt.io"
PrometheusLabelValue = "true"
PrometheusClusterRoleName = "prometheus-k8s-ssp"
PrometheusServiceAccountName = "prometheus-k8s"
MetricsPortName = "metrics"
)
const (
CommonTemplatesRestoredIncreaseQuery = "sum(increase(kubevirt_ssp_common_templates_restored_total{pod=~'ssp-operator.*'}[1h]))"
TemplateValidatorRejectedIncreaseQuery = "sum(increase(kubevirt_ssp_template_validator_rejected_total{pod=~'virt-template-validator.*'}[1h]))"
)
// RecordRulesDesc represent SSP Operator Prometheus Record Rules
type RecordRulesDesc struct {
Name string
Expr intstr.IntOrString
Description string
Type string
}
// RecordRulesDescList lists all SSP Operator Prometheus Record Rules
var RecordRulesDescList = []RecordRulesDesc{
{
Name: "kubevirt_ssp_operator_up",
Expr: intstr.FromString("sum(up{pod=~'ssp-operator.*'}) OR on() vector(0)"),
Description: "The total number of running ssp-operator pods",
Type: "Gauge",
},
{
Name: "kubevirt_ssp_template_validator_up",
Expr: intstr.FromString("sum(up{pod=~'virt-template-validator.*'}) OR on() vector(0)"),
Description: "The total number of running virt-template-validator pods",
Type: "Gauge",
},
{
Name: "kubevirt_ssp_operator_reconcile_succeeded_aggregated",
Expr: intstr.FromString("sum(kubevirt_ssp_operator_reconcile_succeeded)"),
Description: "The total number of ssp-operator pods reconciling with no errors",
Type: "Gauge",
},
{
Name: "kubevirt_ssp_template_validator_rejected_increase",
Expr: intstr.FromString(TemplateValidatorRejectedIncreaseQuery + " OR on() vector(0)"),
Description: "The increase in the number of rejected template validators, over the last hour",
Type: "Gauge",
},
{
Name: "kubevirt_ssp_common_templates_restored_increase",
Expr: intstr.FromString(CommonTemplatesRestoredIncreaseQuery + " OR on() vector(0)"),
Description: "The increase in the number of common templates restored by the operator back to their original state, over the last hour",
Type: "Gauge",
},
}
func getAlertRules() ([]promv1.Rule, error) {
runbookURLTemplate, err := getRunbookURLTemplate()
if err != nil {
return nil, err
}
return []promv1.Rule{
{
Expr: intstr.FromString("sum(kubevirt_vmi_phase_count{phase=\"running\"}) by (node,os,workload,flavor)"),
Record: "cnv:vmi_status_running:count",
},
{
Alert: "SSPDown",
Expr: intstr.FromString("kubevirt_ssp_operator_up == 0"),
For: "5m",
Annotations: map[string]string{
"summary": "All SSP operator pods are down.",
"runbook_url": fmt.Sprintf(runbookURLTemplate, "SSPDown"),
},
Labels: map[string]string{
severityAlertLabelKey: "critical",
healthImpactAlertLabelKey: "critical",
partOfAlertLabelKey: partOfAlertLabelValue,
componentAlertLabelKey: componentAlertLabelValue,
},
},
{
Alert: "SSPTemplateValidatorDown",
Expr: intstr.FromString("kubevirt_ssp_template_validator_up == 0"),
For: "5m",
Annotations: map[string]string{
"summary": "All Template Validator pods are down.",
"runbook_url": fmt.Sprintf(runbookURLTemplate, "SSPTemplateValidatorDown"),
},
Labels: map[string]string{
severityAlertLabelKey: "critical",
healthImpactAlertLabelKey: "critical",
partOfAlertLabelKey: partOfAlertLabelValue,
componentAlertLabelKey: componentAlertLabelValue,
},
},
{
Alert: "SSPFailingToReconcile",
Expr: intstr.FromString("(kubevirt_ssp_operator_reconcile_succeeded_aggregated == 0) and (kubevirt_ssp_operator_up > 0)"),
For: "5m",
Annotations: map[string]string{
"summary": "The ssp-operator pod is up but failing to reconcile",
"runbook_url": fmt.Sprintf(runbookURLTemplate, "SSPFailingToReconcile"),
},
Labels: map[string]string{
severityAlertLabelKey: "critical",
healthImpactAlertLabelKey: "critical",
partOfAlertLabelKey: partOfAlertLabelValue,
componentAlertLabelKey: componentAlertLabelValue,
},
},
{
Alert: "SSPHighRateRejectedVms",
Expr: intstr.FromString("kubevirt_ssp_template_validator_rejected_increase > 5"),
For: "5m",
Annotations: map[string]string{
"summary": "High rate of rejected Vms",
"runbook_url": fmt.Sprintf(runbookURLTemplate, "SSPHighRateRejectedVms"),
},
Labels: map[string]string{
severityAlertLabelKey: "warning",
healthImpactAlertLabelKey: "warning",
partOfAlertLabelKey: partOfAlertLabelValue,
componentAlertLabelKey: componentAlertLabelValue,
},
},
{
Alert: "SSPCommonTemplatesModificationReverted",
Expr: intstr.FromString("kubevirt_ssp_common_templates_restored_increase > 0"),
For: "0m",
Annotations: map[string]string{
"summary": "Common Templates manual modifications were reverted by the operator",
"runbook_url": fmt.Sprintf(runbookURLTemplate, "SSPCommonTemplatesModificationReverted"),
},
Labels: map[string]string{
severityAlertLabelKey: "warning",
healthImpactAlertLabelKey: "none",
partOfAlertLabelKey: partOfAlertLabelValue,
componentAlertLabelKey: componentAlertLabelValue,
},
},
{
Alert: "VirtualMachineCRCErrors",
Expr: intstr.FromString("(count(kubevirt_ssp_vm_rbd_block_volume_without_rxbounce > 0) or vector(0)) > 0"),
Annotations: map[string]string{
"description": "{{ $value }} Virtual Machines are in risk of causing CRC errors and major service outages",
"summary": "When running VMs using ODF storage with 'rbd' mounter or 'rbd.csi.ceph.com provisioner', it will report bad crc/signature errors and cluster performance will be severely degraded if krbd:rxbounce is not set.",
"runbook_url": fmt.Sprintf(runbookURLTemplate, "VirtualMachineCRCErrors"),
},
Labels: map[string]string{
severityAlertLabelKey: "warning",
healthImpactAlertLabelKey: "none",
partOfAlertLabelKey: partOfAlertLabelValue,
componentAlertLabelKey: componentAlertLabelValue,
},
},
}, nil
}
func getRecordRules() []promv1.Rule {
var recordRules []promv1.Rule
for _, rrd := range RecordRulesDescList {
recordRules = append(recordRules, promv1.Rule{Record: rrd.Name, Expr: rrd.Expr})
}
return recordRules
}
func newMonitoringClusterRole() *rbac.ClusterRole {
return &rbac.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: PrometheusClusterRoleName,
},
Rules: []rbac.PolicyRule{{
APIGroups: []string{""},
Resources: []string{"services", "endpoints", "pods"},
Verbs: []string{"get", "list", "watch"},
}},
}
}
func newMonitoringClusterRoleBinding() *rbac.ClusterRoleBinding {
return &rbac.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: PrometheusClusterRoleName,
},
Subjects: []rbac.Subject{
{
Kind: "ServiceAccount",
Name: PrometheusServiceAccountName,
Namespace: MonitorNamespace,
},
},
RoleRef: rbac.RoleRef{
Kind: "ClusterRole",
Name: PrometheusClusterRoleName,
APIGroup: rbac.GroupName,
},
}
}
func ServiceMonitorLabels() map[string]string {
return map[string]string{
"openshift.io/cluster-monitoring": "true",
PrometheusLabelKey: PrometheusLabelValue,
"k8s-app": "kubevirt",
}
}
func newServiceMonitorCR(namespace string) *promv1.ServiceMonitor {
return &promv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: PrometheusRuleName,
Labels: ServiceMonitorLabels(),
},
Spec: promv1.ServiceMonitorSpec{
NamespaceSelector: v1.NamespaceSelector{
Any: true,
},
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
PrometheusLabelKey: PrometheusLabelValue,
},
},
Endpoints: []promv1.Endpoint{
{
Port: MetricsPortName,
Scheme: "https",
TLSConfig: &promv1.TLSConfig{
SafeTLSConfig: promv1.SafeTLSConfig{
InsecureSkipVerify: true,
},
},
HonorLabels: true,
},
},
},
}
}
func newPrometheusRule(namespace string) (*promv1.PrometheusRule, error) {
alertRules, err := getAlertRules()
if err != nil {
return nil, err
}
return &promv1.PrometheusRule{
ObjectMeta: metav1.ObjectMeta{
Name: PrometheusRuleName,
Namespace: namespace,
Labels: map[string]string{
"prometheus": "k8s",
"role": "alert-rules",
"kubevirt.io": "prometheus-rules",
PrometheusLabelKey: PrometheusLabelValue,
},
},
Spec: promv1.PrometheusRuleSpec{
Groups: []promv1.RuleGroup{
{
Name: "cnv.rules",
Rules: append(alertRules, getRecordRules()...),
},
},
},
}, nil
}
func getRunbookURLTemplate() (string, error) {
runbookURLTemplate, exists := os.LookupEnv(runbookURLTemplateEnv)
if !exists {
runbookURLTemplate = defaultRunbookURLTemplate
}
if strings.Count(runbookURLTemplate, "%s") != 1 || strings.Count(runbookURLTemplate, "%") != 1 {
return "", errors.New("runbook URL template must have exactly 1 %s substring")
}
return runbookURLTemplate, nil
}