Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 07dc7c2

Browse files
author
Serhii Zakharov
committedDec 2, 2021
changed logic creating conditional gathering functions
1 parent 80b17c5 commit 07dc7c2

File tree

2 files changed

+62
-54
lines changed

2 files changed

+62
-54
lines changed
 

Diff for: ‎pkg/gatherers/conditional/conditional_gatherer.go

+56-33
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ func New(gatherProtoKubeConfig, metricsGatherKubeConfig, gatherKubeConfig *rest.
170170
}
171171
}
172172

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+
173180
// GetName returns the name of the gatherer
174181
func (g *Gatherer) GetName() string {
175182
return "conditional"
@@ -183,46 +190,53 @@ func (g *Gatherer) GetGatheringFunctions(ctx context.Context) (map[string]gather
183190
return nil, fmt.Errorf("got invalid config for conditional gatherer: %v", utils.SumErrors(errs))
184191
}
185192

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

191195
gatheringFunctions := make(map[string]gatherers.GatheringClosure)
192196

193-
gatheringFunctions["conditional_gatherer_rules"] = gatherers.GatheringClosure{
194-
Run: g.GatherConditionalGathererRules,
195-
CanFail: canConditionalGathererFail,
196-
}
197+
var metadata []GatheringRuleMetadata
197198

198199
for _, conditionalGathering := range g.gatheringRules {
200+
ruleMetadata := GatheringRuleMetadata{
201+
Rule: conditionalGathering,
202+
}
203+
199204
allConditionsAreSatisfied, err := g.areAllConditionsSatisfied(conditionalGathering.Conditions)
200205
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)
202208
}
209+
210+
ruleMetadata.WasTriggered = allConditionsAreSatisfied
211+
203212
if allConditionsAreSatisfied {
204213
functions, errs := g.createGatheringClosures(conditionalGathering.GatheringFunctions)
205214
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...)
207217
}
208218

209219
for funcName, function := range functions {
210220
gatheringFunctions[funcName] = function
211221
}
212222
}
213-
}
214223

215-
return gatheringFunctions, nil
216-
}
224+
metadata = append(metadata, ruleMetadata)
225+
}
217226

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
224235
},
225-
}, nil
236+
CanFail: canConditionalGathererFail,
237+
}
238+
239+
return gatheringFunctions, nil
226240
}
227241

228242
// 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) (
235249
return false, fmt.Errorf("alert field should not be nil")
236250
}
237251

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
240254
}
241255
case ClusterVersionMatches:
242256
if condition.ClusterVersionMatches == nil {
@@ -255,26 +269,27 @@ func (g *Gatherer) areAllConditionsSatisfied(conditions []ConditionWithParams) (
255269
}
256270

257271
// updateCache updates alerts and version caches
258-
func (g *Gatherer) updateCache(ctx context.Context) error {
272+
func (g *Gatherer) updateCache(ctx context.Context) {
259273
if g.metricsGatherKubeConfig == nil {
260-
return nil
274+
return
261275
}
262276

263277
metricsClient, err := rest.RESTClientFor(g.metricsGatherKubeConfig)
264278
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
270283
}
271284

272285
configClient, err := configv1client.NewForConfig(g.gatherKubeConfig)
273286
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 = ""
275291
}
276292

277-
return g.updateVersionCache(ctx, configClient)
278293
}
279294

280295
func (g *Gatherer) updateAlertsCache(ctx context.Context, metricsClient rest.Interface) error {
@@ -342,12 +357,20 @@ func (g *Gatherer) updateVersionCache(ctx context.Context, configClient configv1
342357
}
343358

344359
// 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+
346365
_, alertIsFiring := g.firingAlerts[alertName]
347-
return alertIsFiring
366+
return alertIsFiring, nil
348367
}
349368

350369
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+
351374
clusterVersion, err := semver.Parse(g.clusterVersion)
352375
if err != nil {
353376
return false, err

Diff for: ‎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, 4)
170-
}
171-
172157
func newFakeClientWithMetrics(metrics string) *fake.RESTClient {
173158
fakeClient := &fake.RESTClient{
174159
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),

0 commit comments

Comments
 (0)
Please sign in to comment.