@@ -18,9 +18,11 @@ package hostpathprovisioner
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
22
23
"os"
23
24
"reflect"
25
+ "strings"
24
26
25
27
promv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
26
28
"github.com/go-logr/logr"
@@ -29,7 +31,7 @@ import (
29
31
"sigs.k8s.io/controller-runtime/pkg/client"
30
32
"sigs.k8s.io/controller-runtime/pkg/reconcile"
31
33
32
- "k8s.io/apimachinery/pkg/api/errors"
34
+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
33
35
"k8s.io/apimachinery/pkg/api/meta"
34
36
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
35
37
"k8s.io/apimachinery/pkg/util/intstr"
@@ -41,7 +43,8 @@ const (
41
43
rbacName = "hostpath-provisioner-monitoring"
42
44
monitorName = "service-monitor-hpp"
43
45
defaultMonitoringNs = "monitoring"
44
- runbookURLBasePath = "https://kubevirt.io/monitoring/runbooks/"
46
+ defaultRunbookURLTemplate = "https://kubevirt.io/monitoring/runbooks/%s"
47
+ runbookURLTemplateEnv = "RUNBOOK_URL_TEMPLATE"
45
48
severityAlertLabelKey = "severity"
46
49
healthImpactAlertLabelKey = "operator_health_impact"
47
50
partOfAlertLabelKey = "kubernetes_operator_part_of"
@@ -76,7 +79,7 @@ func (r *ReconcileHostPathProvisioner) reconcilePrometheusResource(reqLogger log
76
79
setLastAppliedConfiguration (desired )
77
80
// Check if this PrometheusRule already exists
78
81
err := r .client .Get (context .TODO (), client .ObjectKeyFromObject (found ), found )
79
- if err != nil && errors .IsNotFound (err ) {
82
+ if err != nil && k8serrors .IsNotFound (err ) {
80
83
reqLogger .Info ("Creating a new PrometheusResource" , "Name" , found .GetName ())
81
84
err = r .client .Create (context .TODO (), desired )
82
85
if err != nil {
@@ -131,7 +134,7 @@ func (r *ReconcileHostPathProvisioner) deletePrometheusResources(namespace strin
131
134
Namespace : namespace ,
132
135
},
133
136
}
134
- if err := r .client .Delete (context .TODO (), rule ); err != nil && ! errors .IsNotFound (err ) {
137
+ if err := r .client .Delete (context .TODO (), rule ); err != nil && ! k8serrors .IsNotFound (err ) {
135
138
return err
136
139
}
137
140
@@ -141,7 +144,7 @@ func (r *ReconcileHostPathProvisioner) deletePrometheusResources(namespace strin
141
144
Namespace : namespace ,
142
145
},
143
146
}
144
- if err := r .client .Delete (context .TODO (), role ); err != nil && ! errors .IsNotFound (err ) {
147
+ if err := r .client .Delete (context .TODO (), role ); err != nil && ! k8serrors .IsNotFound (err ) {
145
148
return err
146
149
}
147
150
@@ -151,7 +154,7 @@ func (r *ReconcileHostPathProvisioner) deletePrometheusResources(namespace strin
151
154
Namespace : namespace ,
152
155
},
153
156
}
154
- if err := r .client .Delete (context .TODO (), roleBinding ); err != nil && ! errors .IsNotFound (err ) {
157
+ if err := r .client .Delete (context .TODO (), roleBinding ); err != nil && ! k8serrors .IsNotFound (err ) {
155
158
return err
156
159
}
157
160
@@ -161,7 +164,7 @@ func (r *ReconcileHostPathProvisioner) deletePrometheusResources(namespace strin
161
164
Namespace : namespace ,
162
165
},
163
166
}
164
- if err := r .client .Delete (context .TODO (), monitor ); err != nil && ! errors .IsNotFound (err ) {
167
+ if err := r .client .Delete (context .TODO (), monitor ); err != nil && ! k8serrors .IsNotFound (err ) {
165
168
return err
166
169
}
167
170
@@ -171,7 +174,7 @@ func (r *ReconcileHostPathProvisioner) deletePrometheusResources(namespace strin
171
174
Namespace : namespace ,
172
175
},
173
176
}
174
- if err := r .client .Delete (context .TODO (), service ); err != nil && ! errors .IsNotFound (err ) {
177
+ if err := r .client .Delete (context .TODO (), service ); err != nil && ! k8serrors .IsNotFound (err ) {
175
178
return err
176
179
}
177
180
@@ -208,15 +211,15 @@ func getRecordRules(namespace string) []promv1.Rule {
208
211
return recordRules
209
212
}
210
213
211
- func getAlertRules () []promv1.Rule {
214
+ func getAlertRules (runbookURLTemplate string ) []promv1.Rule {
212
215
return []promv1.Rule {
213
216
generateAlertRule (
214
217
"HPPOperatorDown" ,
215
218
"kubevirt_hpp_operator_up_total == 0" ,
216
219
"5m" ,
217
220
map [string ]string {
218
221
"summary" : "Hostpath Provisioner operator is down" ,
219
- "runbook_url" : runbookURLBasePath + "HPPOperatorDown" ,
222
+ "runbook_url" : fmt . Sprintf ( runbookURLTemplate , "HPPOperatorDown" ) ,
220
223
},
221
224
map [string ]string {
222
225
severityAlertLabelKey : "warning" ,
@@ -231,7 +234,7 @@ func getAlertRules() []promv1.Rule {
231
234
"5m" ,
232
235
map [string ]string {
233
236
"summary" : "Hostpath Provisioner is not available to use" ,
234
- "runbook_url" : runbookURLBasePath + "HPPNotReady" ,
237
+ "runbook_url" : fmt . Sprintf ( runbookURLTemplate , "HPPNotReady" ) ,
235
238
},
236
239
map [string ]string {
237
240
severityAlertLabelKey : "warning" ,
@@ -246,7 +249,7 @@ func getAlertRules() []promv1.Rule {
246
249
"1m" ,
247
250
map [string ]string {
248
251
"summary" : "HPP pool path sharing a filesystem with OS, fix to prevent HPP PVs from causing disk pressure and affecting node operation" ,
249
- "runbook_url" : runbookURLBasePath + "HPPSharingPoolPathWithOS" ,
252
+ "runbook_url" : fmt . Sprintf ( runbookURLTemplate , "HPPSharingPoolPathWithOS" ) ,
250
253
},
251
254
map [string ]string {
252
255
severityAlertLabelKey : "warning" ,
@@ -262,6 +265,8 @@ func createPrometheusRule(namespace string) *promv1.PrometheusRule {
262
265
labels := getRecommendedLabels ()
263
266
labels [PrometheusLabelKey ] = PrometheusLabelValue
264
267
268
+ runbookURLTemplate := getRunbookURLTemplate ()
269
+
265
270
return & promv1.PrometheusRule {
266
271
TypeMeta : metav1.TypeMeta {
267
272
APIVersion : promv1 .SchemeGroupVersion .String (),
@@ -276,7 +281,7 @@ func createPrometheusRule(namespace string) *promv1.PrometheusRule {
276
281
Groups : []promv1.RuleGroup {
277
282
{
278
283
Name : "hpp.rules" ,
279
- Rules : append (getRecordRules (namespace ), getAlertRules ()... ),
284
+ Rules : append (getRecordRules (namespace ), getAlertRules (runbookURLTemplate )... ),
280
285
},
281
286
},
282
287
},
@@ -438,3 +443,16 @@ func (r *ReconcileHostPathProvisioner) checkPrometheusUsed() (bool, error) {
438
443
}
439
444
return true , nil
440
445
}
446
+
447
+ func getRunbookURLTemplate () string {
448
+ runbookURLTemplate , exists := os .LookupEnv (runbookURLTemplateEnv )
449
+ if ! exists {
450
+ runbookURLTemplate = defaultRunbookURLTemplate
451
+ }
452
+
453
+ if strings .Count (runbookURLTemplate , "%s" ) != 1 {
454
+ panic (errors .New ("runbook URL template must have exactly 1 %s substring" ))
455
+ }
456
+
457
+ return runbookURLTemplate
458
+ }
0 commit comments