Skip to content

Commit 8297b39

Browse files
author
Serhii Zakharov
committed
changed logic creating conditional gathering functions
1 parent 379d58d commit 8297b39

File tree

2 files changed

+62
-54
lines changed

2 files changed

+62
-54
lines changed

pkg/gatherers/conditional/conditional_gatherer.go

+56-33
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ func New(gatherProtoKubeConfig, metricsGatherKubeConfig, gatherKubeConfig *rest.
191191
}
192192
}
193193

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+
194201
// GetName returns the name of the gatherer
195202
func (g *Gatherer) GetName() string {
196203
return "conditional"
@@ -204,46 +211,53 @@ func (g *Gatherer) GetGatheringFunctions(ctx context.Context) (map[string]gather
204211
return nil, fmt.Errorf("got invalid config for conditional gatherer: %v", utils.SumErrors(errs))
205212
}
206213

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)
211215

212216
gatheringFunctions := make(map[string]gatherers.GatheringClosure)
213217

214-
gatheringFunctions["conditional_gatherer_rules"] = gatherers.GatheringClosure{
215-
Run: g.GatherConditionalGathererRules,
216-
CanFail: canConditionalGathererFail,
217-
}
218+
var metadata []GatheringRuleMetadata
218219

219220
for _, conditionalGathering := range g.gatheringRules {
221+
ruleMetadata := GatheringRuleMetadata{
222+
Rule: conditionalGathering,
223+
}
224+
220225
allConditionsAreSatisfied, err := g.areAllConditionsSatisfied(conditionalGathering.Conditions)
221226
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)
223229
}
230+
231+
ruleMetadata.WasTriggered = allConditionsAreSatisfied
232+
224233
if allConditionsAreSatisfied {
225234
functions, errs := g.createGatheringClosures(conditionalGathering.GatheringFunctions)
226235
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...)
228238
}
229239

230240
for funcName, function := range functions {
231241
gatheringFunctions[funcName] = function
232242
}
233243
}
234-
}
235244

236-
return gatheringFunctions, nil
237-
}
245+
metadata = append(metadata, ruleMetadata)
246+
}
238247

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
245256
},
246-
}, nil
257+
CanFail: canConditionalGathererFail,
258+
}
259+
260+
return gatheringFunctions, nil
247261
}
248262

249263
// 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) (
256270
return false, fmt.Errorf("alert field should not be nil")
257271
}
258272

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
261275
}
262276
case ClusterVersionMatches:
263277
if condition.ClusterVersionMatches == nil {
@@ -276,26 +290,27 @@ func (g *Gatherer) areAllConditionsSatisfied(conditions []ConditionWithParams) (
276290
}
277291

278292
// updateCache updates alerts and version caches
279-
func (g *Gatherer) updateCache(ctx context.Context) error {
293+
func (g *Gatherer) updateCache(ctx context.Context) {
280294
if g.metricsGatherKubeConfig == nil {
281-
return nil
295+
return
282296
}
283297

284298
metricsClient, err := rest.RESTClientFor(g.metricsGatherKubeConfig)
285299
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
291304
}
292305

293306
configClient, err := configv1client.NewForConfig(g.gatherKubeConfig)
294307
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 = ""
296312
}
297313

298-
return g.updateVersionCache(ctx, configClient)
299314
}
300315

301316
func (g *Gatherer) updateAlertsCache(ctx context.Context, metricsClient rest.Interface) error {
@@ -363,12 +378,20 @@ func (g *Gatherer) updateVersionCache(ctx context.Context, configClient configv1
363378
}
364379

365380
// 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+
367386
_, alertIsFiring := g.firingAlerts[alertName]
368-
return alertIsFiring
387+
return alertIsFiring, nil
369388
}
370389

371390
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+
372395
clusterVersion, err := semver.Parse(g.clusterVersion)
373396
if err != nil {
374397
return false, err

pkg/gatherers/conditional/conditional_gatherer_test.go

+6-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package conditional
22

33
import (
44
"context"
5-
"encoding/json"
65
"io"
76
"net/http"
87
"strings"
@@ -116,7 +115,9 @@ func Test_Gatherer_GetGatheringFunctions_ConditionIsSatisfied(t *testing.T) {
116115
_, found = gatheringFunctions["image_streams_of_namespace/namespace=openshift-cluster-samples-operator"]
117116
assert.True(t, found)
118117

119-
assert.True(t, gatherer.isAlertFiring("SamplesImagestreamImportFailing"))
118+
firing, err := gatherer.isAlertFiring("SamplesImagestreamImportFailing")
119+
assert.NoError(t, err)
120+
assert.True(t, firing)
120121

121122
err = gatherer.updateAlertsCache(context.TODO(), newFakeClientWithMetrics(
122123
"ALERTS{alertname=\"OtherAlert\",alertstate=\"firing\"} 1 1621618110163\n",
@@ -137,7 +138,9 @@ func Test_Gatherer_GetGatheringFunctions_ConditionIsSatisfied(t *testing.T) {
137138
_, found = gatheringFunctions["image_streams_of_namespace/namespace=openshift-cluster-samples-operator"]
138139
assert.False(t, found)
139140

140-
assert.False(t, gatherer.isAlertFiring("SamplesImagestreamImportFailing"))
141+
firing, err = gatherer.isAlertFiring("SamplesImagestreamImportFailing")
142+
assert.NoError(t, err)
143+
assert.False(t, firing)
141144
}
142145

143146
func Test_getConditionalGatheringFunctionName(t *testing.T) {
@@ -151,24 +154,6 @@ func Test_getConditionalGatheringFunctionName(t *testing.T) {
151154
assert.Equal(t, "func/param1=test,param2=5,param3=9", res)
152155
}
153156

154-
func Test_Gatherer_GatherConditionalGathererRules(t *testing.T) {
155-
gatherer := newEmptyGatherer()
156-
records, errs := gatherer.GatherConditionalGathererRules(context.TODO())
157-
assert.Empty(t, errs)
158-
159-
assert.Len(t, records, 1)
160-
assert.Equal(t, "insights-operator/conditional-gatherer-rules", records[0].Name)
161-
162-
item, err := records[0].Item.Marshal(context.TODO())
163-
assert.NoError(t, err)
164-
165-
var gotGatheringRules []GatheringRule
166-
err = json.Unmarshal(item, &gotGatheringRules)
167-
assert.NoError(t, err)
168-
169-
assert.Len(t, gotGatheringRules, 5)
170-
}
171-
172157
func newFakeClientWithMetrics(metrics string) *fake.RESTClient {
173158
fakeClient := &fake.RESTClient{
174159
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),

0 commit comments

Comments
 (0)