Skip to content

Commit 6d86c0b

Browse files
author
Serhii Zakharov
committed
unified conditional gatherer api with targeted update edge blocking api
1 parent 2b6697e commit 6d86c0b

File tree

5 files changed

+35
-54
lines changed

5 files changed

+35
-54
lines changed

pkg/gatherers/conditional/conditional_gatherer.go

+9-13
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ var gatheringFunctionBuilders = map[GatheringFunctionName]GathererFunctionBuilde
3737
// "conditions": [
3838
// {
3939
// "type": "alert_is_firing",
40-
// "params": {
40+
// "alert": {
4141
// "name": "ClusterVersionOperatorIsDown"
4242
// }
4343
// },
4444
// {
45-
// "type": "cluster_version_equals",
46-
// "params": {
47-
// "version": "4.8"
45+
// "type": "cluster_version_matches",
46+
// "cluster_version": {
47+
// "version": "4.8.x"
4848
// }
4949
// }
5050
// ],
@@ -63,7 +63,7 @@ var defaultGatheringRules = []GatheringRule{
6363
Conditions: []ConditionWithParams{
6464
{
6565
Type: AlertIsFiring,
66-
Params: AlertIsFiringConditionParams{
66+
Alert: &AlertConditionParams{
6767
Name: "SamplesImagestreamImportFailing",
6868
},
6969
},
@@ -82,7 +82,7 @@ var defaultGatheringRules = []GatheringRule{
8282
Conditions: []ConditionWithParams{
8383
{
8484
Type: AlertIsFiring,
85-
Params: AlertIsFiringConditionParams{
85+
Alert: &AlertConditionParams{
8686
Name: "APIRemovedInNextEUSReleaseInUse",
8787
},
8888
},
@@ -186,15 +186,11 @@ func (g *Gatherer) areAllConditionsSatisfied(conditions []ConditionWithParams) (
186186
for _, condition := range conditions {
187187
switch condition.Type {
188188
case AlertIsFiring:
189-
params, ok := condition.Params.(AlertIsFiringConditionParams)
190-
if !ok {
191-
return false, fmt.Errorf(
192-
"invalid params type, expected %T, got %T",
193-
AlertIsFiringConditionParams{}, condition.Params,
194-
)
189+
if condition.Alert == nil {
190+
return false, fmt.Errorf("alert field should not be nil")
195191
}
196192

197-
if !g.isAlertFiring(params.Name) {
193+
if !g.isAlertFiring(condition.Alert.Name) {
198194
return false, nil
199195
}
200196
default:

pkg/gatherers/conditional/conditional_gatherer_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Test_Gatherer_GetGatheringFunctions_InvalidConfig(t *testing.T) {
5454
Conditions: []ConditionWithParams{
5555
{
5656
Type: AlertIsFiring,
57-
Params: AlertIsFiringConditionParams{
57+
Alert: &AlertConditionParams{
5858
Name: "SamplesImagestreamImportFailing",
5959
},
6060
},
+5-20
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package conditional
22

3-
import (
4-
"encoding/json"
5-
"fmt"
6-
)
7-
83
// ConditionWithParams is a type holding a condition with its params
94
type ConditionWithParams struct {
10-
Type ConditionType `json:"type"`
11-
Params interface{} `json:"params"`
5+
Type ConditionType `json:"type"`
6+
Alert *AlertConditionParams `json:"alert,omitempty"`
127
}
138

149
// condition types:
@@ -17,23 +12,13 @@ type ConditionWithParams struct {
1712
type ConditionType string
1813

1914
// AlertIsFiring is a condition to check that alert is firing
15+
// the params are in the field `alert`
2016
const AlertIsFiring ConditionType = "alert_is_firing"
2117

22-
// NewParams creates an instance of params type for this condition type
23-
func (ct ConditionType) NewParams(jsonParam []byte) (interface{}, error) {
24-
switch ct { //nolint:gocritic
25-
case AlertIsFiring:
26-
var result AlertIsFiringConditionParams
27-
err := json.Unmarshal(jsonParam, &result)
28-
return result, err
29-
}
30-
return nil, fmt.Errorf("unable to create params for %T: %v", ct, ct)
31-
}
32-
3318
// params:
3419

35-
// AlertIsFiringConditionParams is a type holding params for alert_is_firing condition
36-
type AlertIsFiringConditionParams struct {
20+
// AlertConditionParams is a type holding params for alert_is_firing condition
21+
type AlertConditionParams struct {
3722
// Name of the alert
3823
Name string `json:"name"`
3924
}

pkg/gatherers/conditional/gathering_rule.schema.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"conditions": [
99
{
1010
"type": "alert_is_firing",
11-
"params": {
11+
"alert": {
1212
"name": "SamplesImagestreamImportFailing"
1313
}
1414
}
@@ -48,7 +48,7 @@
4848
"description": "alert_is_firing condition",
4949
"required": [
5050
"type",
51-
"params"
51+
"alert"
5252
],
5353
"properties": {
5454
"type": {
@@ -57,9 +57,9 @@
5757
"description": "Type of the condition alert_is_firing",
5858
"const": "alert_is_firing"
5959
},
60-
"params": {
60+
"alert": {
6161
"type": "object",
62-
"title": "AlertIsFiringConditionParams",
62+
"title": "AlertConditionParams",
6363
"description": "Parameters of the condition alert_is_firing",
6464
"required": [
6565
"name"

pkg/gatherers/conditional/validation_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ func Test_Validation(t *testing.T) {
1414
Conditions: []ConditionWithParams{
1515
{
1616
Type: AlertIsFiring,
17-
Params: AlertIsFiringConditionParams{
17+
Alert: &AlertConditionParams{
1818
Name: "test1",
1919
},
2020
},
2121
{
2222
Type: AlertIsFiring,
23-
Params: AlertIsFiringConditionParams{
23+
Alert: &AlertConditionParams{
2424
Name: "test2",
2525
},
2626
},
@@ -53,7 +53,7 @@ func Test_Validation_InvalidGatheringRules(t *testing.T) {
5353
Conditions: []ConditionWithParams{
5454
{
5555
Type: AlertIsFiring,
56-
Params: AlertIsFiringConditionParams{
56+
Alert: &AlertConditionParams{
5757
Name: "test" + fmt.Sprint(i),
5858
},
5959
},
@@ -81,7 +81,7 @@ func Test_Validation_InvalidGatheringRules(t *testing.T) {
8181
Conditions: []ConditionWithParams{
8282
{
8383
Type: AlertIsFiring,
84-
Params: AlertIsFiringConditionParams{
84+
Alert: &AlertConditionParams{
8585
Name: "test1",
8686
},
8787
},
@@ -96,7 +96,7 @@ func Test_Validation_InvalidGatheringRules(t *testing.T) {
9696
Conditions: []ConditionWithParams{
9797
{
9898
Type: AlertIsFiring,
99-
Params: AlertIsFiringConditionParams{
99+
Alert: &AlertConditionParams{
100100
Name: "test1",
101101
},
102102
},
@@ -140,11 +140,11 @@ func Test_Validation_InvalidConditions(t *testing.T) {
140140
}},
141141
Errors: []string{
142142
`0.conditions.0: Must validate at least one schema (anyOf)`,
143+
`0.conditions.0: alert is required`,
143144
`0.conditions.0.type: 0.conditions.0.type does not match: "alert_is_firing"`,
144-
`0.conditions.0.params: Invalid type. Expected: object, given: null`,
145145
`0.conditions.1: Must validate at least one schema (anyOf)`,
146+
`0.conditions.1: alert is required`,
146147
`0.conditions.1.type: 0.conditions.1.type does not match: "alert_is_firing"`,
147-
`0.conditions.1.params: Invalid type. Expected: object, given: null`,
148148
`0.gathering_functions: Invalid type. Expected: object, given: null`,
149149
},
150150
},
@@ -153,14 +153,14 @@ func Test_Validation_InvalidConditions(t *testing.T) {
153153
Rules: []GatheringRule{{
154154
Conditions: []ConditionWithParams{
155155
{
156-
Type: AlertIsFiring,
157-
Params: "using incorrect type for params",
156+
Type: AlertIsFiring,
157+
Alert: nil,
158158
},
159159
},
160160
}},
161161
Errors: []string{
162162
`0.conditions.0: Must validate at least one schema (anyOf)`,
163-
`0.conditions.0.params: Invalid type. Expected: object, given: string`,
163+
`0.conditions.0: alert is required`,
164164
`0.gathering_functions: Invalid type. Expected: object, given: null`,
165165
},
166166
},
@@ -170,15 +170,15 @@ func Test_Validation_InvalidConditions(t *testing.T) {
170170
Conditions: []ConditionWithParams{
171171
{
172172
Type: AlertIsFiring,
173-
Params: AlertIsFiringConditionParams{
173+
Alert: &AlertConditionParams{
174174
Name: "contains invalid characters $^#!@$%&",
175175
},
176176
},
177177
},
178178
}},
179179
Errors: []string{
180180
`0.conditions.0: Must validate at least one schema (anyOf)`,
181-
`0.conditions.0.params.name: Does not match pattern '^[a-zA-Z0-9_]{1,128}$'`,
181+
`0.conditions.0.alert.name: Does not match pattern '^[a-zA-Z0-9_]{1,128}$'`,
182182
`0.gathering_functions: Invalid type. Expected: object, given: null`,
183183
},
184184
},
@@ -188,7 +188,7 @@ func Test_Validation_InvalidConditions(t *testing.T) {
188188
Conditions: []ConditionWithParams{
189189
{
190190
Type: AlertIsFiring,
191-
Params: AlertIsFiringConditionParams{
191+
Alert: &AlertConditionParams{
192192
Name: "", // empty
193193
},
194194
},
@@ -197,7 +197,7 @@ func Test_Validation_InvalidConditions(t *testing.T) {
197197
Errors: []string{
198198
`0.gathering_functions: Invalid type. Expected: object, given: null`,
199199
`0.conditions.0: Must validate at least one schema (anyOf)`,
200-
`0.conditions.0.params.name: Does not match pattern '^[a-zA-Z0-9_]{1,128}$'`,
200+
`0.conditions.0.alert.name: Does not match pattern '^[a-zA-Z0-9_]{1,128}$'`,
201201
},
202202
},
203203
{
@@ -206,15 +206,15 @@ func Test_Validation_InvalidConditions(t *testing.T) {
206206
Conditions: []ConditionWithParams{
207207
{
208208
Type: AlertIsFiring,
209-
Params: AlertIsFiringConditionParams{
209+
Alert: &AlertConditionParams{
210210
Name: rand.String(1024), // too long
211211
},
212212
},
213213
},
214214
}},
215215
Errors: []string{
216216
`0.conditions.0: Must validate at least one schema (anyOf)`,
217-
`0.conditions.0.params.name: Does not match pattern '^[a-zA-Z0-9_]{1,128}$'`,
217+
`0.conditions.0.alert.name: Does not match pattern '^[a-zA-Z0-9_]{1,128}$'`,
218218
`0.gathering_functions: Invalid type. Expected: object, given: null`,
219219
},
220220
},

0 commit comments

Comments
 (0)