@@ -191,6 +191,13 @@ func New(gatherProtoKubeConfig, metricsGatherKubeConfig, gatherKubeConfig *rest.
191
191
}
192
192
}
193
193
194
+ // GatheringRuleMetadata stores information about gathering rules
195
+ type GatheringRuleMetadata struct {
196
+ Rule GatheringRule `json:"rule"`
197
+ Errors []error `json:"errors"`
198
+ WasTriggered bool `json:"was_triggered"`
199
+ }
200
+
194
201
// GetName returns the name of the gatherer
195
202
func (g * Gatherer ) GetName () string {
196
203
return "conditional"
@@ -204,46 +211,53 @@ func (g *Gatherer) GetGatheringFunctions(ctx context.Context) (map[string]gather
204
211
return nil , fmt .Errorf ("got invalid config for conditional gatherer: %v" , utils .SumErrors (errs ))
205
212
}
206
213
207
- err := g .updateCache (ctx )
208
- if err != nil {
209
- return nil , fmt .Errorf ("conditional gatherer can't update the cache: %v" , err )
210
- }
214
+ g .updateCache (ctx )
211
215
212
216
gatheringFunctions := make (map [string ]gatherers.GatheringClosure )
213
217
214
- gatheringFunctions ["conditional_gatherer_rules" ] = gatherers.GatheringClosure {
215
- Run : g .GatherConditionalGathererRules ,
216
- CanFail : canConditionalGathererFail ,
217
- }
218
+ var metadata []GatheringRuleMetadata
218
219
219
220
for _ , conditionalGathering := range g .gatheringRules {
221
+ ruleMetadata := GatheringRuleMetadata {
222
+ Rule : conditionalGathering ,
223
+ }
224
+
220
225
allConditionsAreSatisfied , err := g .areAllConditionsSatisfied (conditionalGathering .Conditions )
221
226
if err != nil {
222
- return nil , err
227
+ klog .Errorf ("error checking conditions for a gathering rule: %v" , err )
228
+ ruleMetadata .Errors = append (ruleMetadata .Errors , err )
223
229
}
230
+
231
+ ruleMetadata .WasTriggered = allConditionsAreSatisfied
232
+
224
233
if allConditionsAreSatisfied {
225
234
functions , errs := g .createGatheringClosures (conditionalGathering .GatheringFunctions )
226
235
if len (errs ) > 0 {
227
- return nil , err
236
+ klog .Errorf ("error(s) creating a closure for a gathering rule: %v" , errs )
237
+ ruleMetadata .Errors = append (ruleMetadata .Errors , errs ... )
228
238
}
229
239
230
240
for funcName , function := range functions {
231
241
gatheringFunctions [funcName ] = function
232
242
}
233
243
}
234
- }
235
244
236
- return gatheringFunctions , nil
237
- }
245
+ metadata = append ( metadata , ruleMetadata )
246
+ }
238
247
239
- // GatherConditionalGathererRules stores the gathering rules in insights-operator/conditional-gatherer-rules.json
240
- func (g * Gatherer ) GatherConditionalGathererRules (context.Context ) ([]record.Record , []error ) {
241
- return []record.Record {
242
- {
243
- Name : "insights-operator/conditional-gatherer-rules" ,
244
- Item : record.JSONMarshaller {Object : g .gatheringRules },
248
+ gatheringFunctions ["conditional_gatherer_rules" ] = gatherers.GatheringClosure {
249
+ Run : func (context.Context ) ([]record.Record , []error ) {
250
+ return []record.Record {
251
+ {
252
+ Name : "insights-operator/conditional-gatherer-rules" ,
253
+ Item : record.JSONMarshaller {Object : metadata },
254
+ },
255
+ }, nil
245
256
},
246
- }, nil
257
+ CanFail : canConditionalGathererFail ,
258
+ }
259
+
260
+ return gatheringFunctions , nil
247
261
}
248
262
249
263
// areAllConditionsSatisfied returns true if all the conditions are satisfied, for example if the condition is
@@ -256,8 +270,8 @@ func (g *Gatherer) areAllConditionsSatisfied(conditions []ConditionWithParams) (
256
270
return false , fmt .Errorf ("alert field should not be nil" )
257
271
}
258
272
259
- if ! g .isAlertFiring (condition .Alert .Name ) {
260
- return false , nil
273
+ if firing , err := g .isAlertFiring (condition .Alert .Name ); ! firing || err != nil {
274
+ return false , err
261
275
}
262
276
case ClusterVersionMatches :
263
277
if condition .ClusterVersionMatches == nil {
@@ -276,26 +290,27 @@ func (g *Gatherer) areAllConditionsSatisfied(conditions []ConditionWithParams) (
276
290
}
277
291
278
292
// updateCache updates alerts and version caches
279
- func (g * Gatherer ) updateCache (ctx context.Context ) error {
293
+ func (g * Gatherer ) updateCache (ctx context.Context ) {
280
294
if g .metricsGatherKubeConfig == nil {
281
- return nil
295
+ return
282
296
}
283
297
284
298
metricsClient , err := rest .RESTClientFor (g .metricsGatherKubeConfig )
285
299
if err != nil {
286
- return err
287
- }
288
-
289
- if err := g .updateAlertsCache (ctx , metricsClient ); err != nil { //nolint:govet
290
- return err
300
+ klog .Errorf ("unable to update alerts cache: %v" , err )
301
+ } else if err := g .updateAlertsCache (ctx , metricsClient ); err != nil { //nolint:govet
302
+ klog .Errorf ("unable to update alerts cache: %v" , err )
303
+ g .firingAlerts = nil
291
304
}
292
305
293
306
configClient , err := configv1client .NewForConfig (g .gatherKubeConfig )
294
307
if err != nil {
295
- return err
308
+ klog .Errorf ("unable to update version cache: %v" , err )
309
+ } else if err := g .updateVersionCache (ctx , configClient ); err != nil {
310
+ klog .Errorf ("unable to update version cache: %v" , err )
311
+ g .clusterVersion = ""
296
312
}
297
313
298
- return g .updateVersionCache (ctx , configClient )
299
314
}
300
315
301
316
func (g * Gatherer ) updateAlertsCache (ctx context.Context , metricsClient rest.Interface ) error {
@@ -363,12 +378,20 @@ func (g *Gatherer) updateVersionCache(ctx context.Context, configClient configv1
363
378
}
364
379
365
380
// isAlertFiring using the cache it returns true if the alert is firing
366
- func (g * Gatherer ) isAlertFiring (alertName string ) bool {
381
+ func (g * Gatherer ) isAlertFiring (alertName string ) (bool , error ) {
382
+ if g .firingAlerts == nil {
383
+ return false , fmt .Errorf ("alerts cache is missing" )
384
+ }
385
+
367
386
_ , alertIsFiring := g .firingAlerts [alertName ]
368
- return alertIsFiring
387
+ return alertIsFiring , nil
369
388
}
370
389
371
390
func (g * Gatherer ) doesClusterVersionMatch (expectedVersionExpression string ) (bool , error ) {
391
+ if len (g .clusterVersion ) == 0 {
392
+ return false , fmt .Errorf ("cluster version is missing" )
393
+ }
394
+
372
395
clusterVersion , err := semver .Parse (g .clusterVersion )
373
396
if err != nil {
374
397
return false , err
0 commit comments