-
Notifications
You must be signed in to change notification settings - Fork 221
/
schema_alert_rule.go
282 lines (269 loc) · 7.85 KB
/
schema_alert_rule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package rancher2
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
const (
alertRuleSeverityCritical = "critical"
alertRuleSeverityInfo = "info"
alertRuleSeverityWarning = "warning"
eventRuleTypeWarning = "Warning"
eventRuleTypeNormal = "Normal"
eventRuleResourceKindDaemondSet = "DaemonSet"
eventRuleResourceKindDeployment = "Deployment"
eventRuleResourceKindNode = "Node"
eventRuleResourceKindPod = "Pod"
eventRuleResourceKindStatefulSet = "StatefulSet"
metricRuleComparisonEqual = "equal"
metricRuleComparisonGreaterOrEqual = "greater-or-equal"
metricRuleComparisonGreaterThan = "greater-than"
metricRuleComparisonLessOrEqual = "less-or-equal"
metricRuleComparisonLessThan = "less-than"
metricRuleComparisonNotEqual = "not-equal"
metricRuleComparisonNotNull = "has-value"
nodeRuleConditionCPU = "cpu"
nodeRuleConditionMem = "mem"
nodeRuleConditionNotReady = "notready"
podRuleConditionNotRunning = "notrunning"
podRuleConditionNotScheduled = "notscheduled"
podRuleConditionRestarts = "restarts"
systemServiceRuleConditionControllerManager = "controller-manager"
systemServiceRuleConditionEtcd = "etcd"
systemServiceRuleConditionScheduler = "scheduler"
)
var (
alertRuleSeverityTypes = []string{alertRuleSeverityCritical, alertRuleSeverityInfo, alertRuleSeverityWarning}
eventRuleTypes = []string{eventRuleTypeNormal, eventRuleTypeWarning}
eventRuleResourceKinds = []string{
eventRuleResourceKindDaemondSet,
eventRuleResourceKindDeployment,
eventRuleResourceKindNode,
eventRuleResourceKindPod,
eventRuleResourceKindStatefulSet,
}
metricRuleComparisons = []string{
metricRuleComparisonEqual,
metricRuleComparisonGreaterOrEqual,
metricRuleComparisonGreaterThan,
metricRuleComparisonLessOrEqual,
metricRuleComparisonLessThan,
metricRuleComparisonNotEqual,
metricRuleComparisonNotNull,
}
nodeRuleConditions = []string{
nodeRuleConditionCPU,
nodeRuleConditionMem,
nodeRuleConditionNotReady,
}
podRuleConditions = []string{
podRuleConditionNotRunning,
podRuleConditionNotScheduled,
podRuleConditionRestarts,
}
systemServiceRuleConditions = []string{
systemServiceRuleConditionControllerManager,
systemServiceRuleConditionEtcd,
systemServiceRuleConditionScheduler,
}
)
//Schemas
func alertRuleFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"group_id": {
Type: schema.TypeString,
Required: true,
Description: "Alert rule group ID",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Alert rule name",
},
"group_interval_seconds": {
Type: schema.TypeInt,
Optional: true,
Default: 180,
Description: "Alert rule interval seconds",
},
"group_wait_seconds": {
Type: schema.TypeInt,
Optional: true,
Default: 180,
Description: "Alert rule wait seconds",
},
"inherited": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Alert rule inherited",
},
"repeat_interval_seconds": {
Type: schema.TypeInt,
Optional: true,
Default: 3600,
Description: "Alert rule repeat interval seconds",
},
"severity": {
Type: schema.TypeString,
Optional: true,
Default: alertRuleSeverityCritical,
Description: "Alert rule severity",
ValidateFunc: validation.StringInSlice(alertRuleSeverityTypes, true),
},
}
for k, v := range commonAnnotationLabelFields() {
s[k] = v
}
return s
}
func eventRuleFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"event_type": {
Type: schema.TypeString,
Optional: true,
Default: eventRuleTypeWarning,
Description: "Event type",
ValidateFunc: validation.StringInSlice(eventRuleTypes, true),
},
"resource_kind": {
Type: schema.TypeString,
Required: true,
Description: "Resource kind",
ValidateFunc: validation.StringInSlice(eventRuleResourceKinds, true),
},
}
return s
}
func metricRuleFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"comparison": {
Type: schema.TypeString,
Optional: true,
Default: metricRuleComparisonEqual,
Description: "Metric rule comparison",
ValidateFunc: validation.StringInSlice(metricRuleComparisons, true),
},
"duration": {
Type: schema.TypeString,
Required: true,
Description: "Metric rule duration",
},
"expression": {
Type: schema.TypeString,
Required: true,
Description: "Metric rule expression",
},
"threshold_value": {
Type: schema.TypeFloat,
Required: true,
Description: "Metric rule threshold value",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Metric rule description",
},
}
return s
}
func nodeRuleFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"cpu_threshold": {
Type: schema.TypeInt,
Optional: true,
Default: 70,
Description: "Node rule cpu threshold",
ValidateFunc: validation.IntBetween(1, 100),
},
"condition": {
Type: schema.TypeString,
Optional: true,
Default: nodeRuleConditionNotReady,
Description: "Node rule condition",
ValidateFunc: validation.StringInSlice(nodeRuleConditions, true),
},
"mem_threshold": {
Type: schema.TypeInt,
Optional: true,
Default: 70,
Description: "Node rule mem threshold",
ValidateFunc: validation.IntBetween(1, 100),
},
"node_id": {
Type: schema.TypeString,
Optional: true,
Description: "Node ID",
},
"selector": {
Type: schema.TypeMap,
Optional: true,
Description: "Node rule selector",
},
}
return s
}
func podRuleFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"pod_id": {
Type: schema.TypeString,
Required: true,
Description: "Pod ID",
},
"condition": {
Type: schema.TypeString,
Optional: true,
Default: podRuleConditionNotRunning,
Description: "Pod rule condition",
ValidateFunc: validation.StringInSlice(podRuleConditions, true),
},
"restart_interval_seconds": {
Type: schema.TypeInt,
Optional: true,
Default: 300,
Description: "Pod rule restart interval seconds",
ValidateFunc: validation.IntAtLeast(1),
},
"restart_times": {
Type: schema.TypeInt,
Optional: true,
Default: 3,
Description: "Pod rule restart times",
ValidateFunc: validation.IntAtLeast(1),
},
}
return s
}
func systemServiceRuleFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"condition": {
Type: schema.TypeString,
Optional: true,
Default: systemServiceRuleConditionScheduler,
Description: "System service rule condition",
ValidateFunc: validation.StringInSlice(systemServiceRuleConditions, true),
},
}
return s
}
func workloadRuleFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"available_percentage": {
Type: schema.TypeInt,
Optional: true,
Default: 70,
Description: "Workload rule available percentage",
ValidateFunc: validation.IntBetween(1, 100),
},
"selector": {
Type: schema.TypeMap,
Optional: true,
Description: "Workload rule selector",
},
"workload_id": {
Type: schema.TypeString,
Optional: true,
Description: "Workload ID",
},
}
return s
}