@@ -170,6 +170,13 @@ func New(gatherProtoKubeConfig, metricsGatherKubeConfig, gatherKubeConfig *rest.
170
170
}
171
171
}
172
172
173
+ // GatheringRuleMetadata stores information about gathering rules
174
+ type GatheringRuleMetadata struct {
175
+ Rule GatheringRule `json:"rule"`
176
+ Errors []error `json:"errors"`
177
+ WasTriggered bool `json:"was_triggered"`
178
+ }
179
+
173
180
// GetName returns the name of the gatherer
174
181
func (g * Gatherer ) GetName () string {
175
182
return "conditional"
@@ -183,46 +190,53 @@ func (g *Gatherer) GetGatheringFunctions(ctx context.Context) (map[string]gather
183
190
return nil , fmt .Errorf ("got invalid config for conditional gatherer: %v" , utils .SumErrors (errs ))
184
191
}
185
192
186
- err := g .updateCache (ctx )
187
- if err != nil {
188
- return nil , fmt .Errorf ("conditional gatherer can't update the cache: %v" , err )
189
- }
193
+ g .updateCache (ctx )
190
194
191
195
gatheringFunctions := make (map [string ]gatherers.GatheringClosure )
192
196
193
- gatheringFunctions ["conditional_gatherer_rules" ] = gatherers.GatheringClosure {
194
- Run : g .GatherConditionalGathererRules ,
195
- CanFail : canConditionalGathererFail ,
196
- }
197
+ var metadata []GatheringRuleMetadata
197
198
198
199
for _ , conditionalGathering := range g .gatheringRules {
200
+ ruleMetadata := GatheringRuleMetadata {
201
+ Rule : conditionalGathering ,
202
+ }
203
+
199
204
allConditionsAreSatisfied , err := g .areAllConditionsSatisfied (conditionalGathering .Conditions )
200
205
if err != nil {
201
- return nil , err
206
+ klog .Errorf ("error checking conditions for a gathering rule: %v" , err )
207
+ ruleMetadata .Errors = append (ruleMetadata .Errors , err )
202
208
}
209
+
210
+ ruleMetadata .WasTriggered = allConditionsAreSatisfied
211
+
203
212
if allConditionsAreSatisfied {
204
213
functions , errs := g .createGatheringClosures (conditionalGathering .GatheringFunctions )
205
214
if len (errs ) > 0 {
206
- return nil , err
215
+ klog .Errorf ("error(s) creating a closure for a gathering rule: %v" , errs )
216
+ ruleMetadata .Errors = append (ruleMetadata .Errors , errs ... )
207
217
}
208
218
209
219
for funcName , function := range functions {
210
220
gatheringFunctions [funcName ] = function
211
221
}
212
222
}
213
- }
214
223
215
- return gatheringFunctions , nil
216
- }
224
+ metadata = append ( metadata , ruleMetadata )
225
+ }
217
226
218
- // GatherConditionalGathererRules stores the gathering rules in insights-operator/conditional-gatherer-rules.json
219
- func (g * Gatherer ) GatherConditionalGathererRules (context.Context ) ([]record.Record , []error ) {
220
- return []record.Record {
221
- {
222
- Name : "insights-operator/conditional-gatherer-rules" ,
223
- Item : record.JSONMarshaller {Object : g .gatheringRules },
227
+ gatheringFunctions ["conditional_gatherer_rules" ] = gatherers.GatheringClosure {
228
+ Run : func (context.Context ) ([]record.Record , []error ) {
229
+ return []record.Record {
230
+ {
231
+ Name : "insights-operator/conditional-gatherer-rules" ,
232
+ Item : record.JSONMarshaller {Object : metadata },
233
+ },
234
+ }, nil
224
235
},
225
- }, nil
236
+ CanFail : canConditionalGathererFail ,
237
+ }
238
+
239
+ return gatheringFunctions , nil
226
240
}
227
241
228
242
// areAllConditionsSatisfied returns true if all the conditions are satisfied, for example if the condition is
@@ -235,8 +249,8 @@ func (g *Gatherer) areAllConditionsSatisfied(conditions []ConditionWithParams) (
235
249
return false , fmt .Errorf ("alert field should not be nil" )
236
250
}
237
251
238
- if ! g .isAlertFiring (condition .Alert .Name ) {
239
- return false , nil
252
+ if firing , err := g .isAlertFiring (condition .Alert .Name ); ! firing || err != nil {
253
+ return false , err
240
254
}
241
255
case ClusterVersionMatches :
242
256
if condition .ClusterVersionMatches == nil {
@@ -255,26 +269,27 @@ func (g *Gatherer) areAllConditionsSatisfied(conditions []ConditionWithParams) (
255
269
}
256
270
257
271
// updateCache updates alerts and version caches
258
- func (g * Gatherer ) updateCache (ctx context.Context ) error {
272
+ func (g * Gatherer ) updateCache (ctx context.Context ) {
259
273
if g .metricsGatherKubeConfig == nil {
260
- return nil
274
+ return
261
275
}
262
276
263
277
metricsClient , err := rest .RESTClientFor (g .metricsGatherKubeConfig )
264
278
if err != nil {
265
- return err
266
- }
267
-
268
- if err := g .updateAlertsCache (ctx , metricsClient ); err != nil { //nolint:govet
269
- return err
279
+ klog .Errorf ("unable to update alerts cache: %v" , err )
280
+ } else if err := g .updateAlertsCache (ctx , metricsClient ); err != nil { //nolint:govet
281
+ klog .Errorf ("unable to update alerts cache: %v" , err )
282
+ g .firingAlerts = nil
270
283
}
271
284
272
285
configClient , err := configv1client .NewForConfig (g .gatherKubeConfig )
273
286
if err != nil {
274
- return err
287
+ klog .Errorf ("unable to update version cache: %v" , err )
288
+ } else if err := g .updateVersionCache (ctx , configClient ); err != nil {
289
+ klog .Errorf ("unable to update version cache: %v" , err )
290
+ g .clusterVersion = ""
275
291
}
276
292
277
- return g .updateVersionCache (ctx , configClient )
278
293
}
279
294
280
295
func (g * Gatherer ) updateAlertsCache (ctx context.Context , metricsClient rest.Interface ) error {
@@ -342,12 +357,20 @@ func (g *Gatherer) updateVersionCache(ctx context.Context, configClient configv1
342
357
}
343
358
344
359
// isAlertFiring using the cache it returns true if the alert is firing
345
- func (g * Gatherer ) isAlertFiring (alertName string ) bool {
360
+ func (g * Gatherer ) isAlertFiring (alertName string ) (bool , error ) {
361
+ if g .firingAlerts == nil {
362
+ return false , fmt .Errorf ("alerts cache is missing" )
363
+ }
364
+
346
365
_ , alertIsFiring := g .firingAlerts [alertName ]
347
- return alertIsFiring
366
+ return alertIsFiring , nil
348
367
}
349
368
350
369
func (g * Gatherer ) doesClusterVersionMatch (expectedVersionExpression string ) (bool , error ) {
370
+ if len (g .clusterVersion ) == 0 {
371
+ return false , fmt .Errorf ("cluster version is missing" )
372
+ }
373
+
351
374
clusterVersion , err := semver .Parse (g .clusterVersion )
352
375
if err != nil {
353
376
return false , err
0 commit comments