-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathrules.go
784 lines (661 loc) · 25.2 KB
/
rules.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
package commands
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/pkg/rulefmt"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
yamlv3 "gopkg.in/yaml.v3"
"github.com/grafana/cortex-tools/pkg/client"
"github.com/grafana/cortex-tools/pkg/printer"
"github.com/grafana/cortex-tools/pkg/rules"
"github.com/grafana/cortex-tools/pkg/rules/rwrulefmt"
)
const (
defaultPrepareAggregationLabel = "cluster"
)
var (
ruleLoadTimestamp = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "last_rule_load_timestamp_seconds",
Help: "The timestamp of the last rule load.",
})
ruleLoadSuccessTimestamp = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "last_rule_load_success_timestamp_seconds",
Help: "The timestamp of the last successful rule load.",
})
backends = []string{rules.CortexBackend, rules.LokiBackend} // list of supported backend types
formats = []string{"json", "yaml", "table"} // list of supported formats for the list command
)
// RuleCommand configures and executes rule related cortex operations
type RuleCommand struct {
ClientConfig client.Config
cli *client.CortexClient
// Backend type (cortex | loki)
Backend string
// Get Rule Groups Configs
Namespace string
RuleGroup string
// Load Rules Config
RuleFilesList []string
RuleFiles string
RuleFilesPath string
// Sync/Diff Rules Config
Namespaces string
namespacesMap map[string]struct{}
IgnoredNamespaces string
ignoredNamespacesMap map[string]struct{}
// Prepare Rules Config
InPlaceEdit bool
AggregationLabel string
AggregationLabelExcludedRuleGroups string
aggregationLabelExcludedRuleGroupsList map[string]struct{}
// Lint Rules Config
LintDryRun bool
// Rules check flags
Strict bool
// List Rules Config
Format string
DisableColor bool
// Diff Rules Config
Verbose bool
}
// Register rule related commands and flags with the kingpin application
func (r *RuleCommand) Register(app *kingpin.Application) {
rulesCmd := app.Command("rules", "View & edit rules stored in cortex.").PreAction(r.setup)
rulesCmd.Flag("authToken", "Authentication token for bearer token or JWT auth, alternatively set CORTEX_AUTH_TOKEN.").Default("").Envar("CORTEX_AUTH_TOKEN").StringVar(&r.ClientConfig.AuthToken)
rulesCmd.Flag("user", "API user to use when contacting cortex, alternatively set CORTEX_API_USER. If empty, CORTEX_TENANT_ID will be used instead.").Default("").Envar("CORTEX_API_USER").StringVar(&r.ClientConfig.User)
rulesCmd.Flag("key", "API key to use when contacting cortex, alternatively set CORTEX_API_KEY.").Default("").Envar("CORTEX_API_KEY").StringVar(&r.ClientConfig.Key)
rulesCmd.Flag("backend", "Backend type to interact with: <cortex|loki>").Default("cortex").EnumVar(&r.Backend, backends...)
// Register rule commands
listCmd := rulesCmd.
Command("list", "List the rules currently in the cortex ruler.").
Action(r.listRules)
printRulesCmd := rulesCmd.
Command("print", "Print the rules currently in the cortex ruler.").
Action(r.printRules)
getRuleGroupCmd := rulesCmd.
Command("get", "Retrieve a rulegroup from the ruler.").
Action(r.getRuleGroup)
deleteRuleGroupCmd := rulesCmd.
Command("delete", "Delete a rulegroup from the ruler.").
Action(r.deleteRuleGroup)
loadRulesCmd := rulesCmd.
Command("load", "load a set of rules to a designated cortex endpoint").
Action(r.loadRules)
diffRulesCmd := rulesCmd.
Command("diff", "diff a set of rules to a designated cortex endpoint").
Action(r.diffRules)
syncRulesCmd := rulesCmd.
Command("sync", "sync a set of rules to a designated cortex endpoint").
Action(r.syncRules)
prepareCmd := rulesCmd.
Command("prepare", "modifies a set of rules by including an specific label in aggregations.").
Action(r.prepare)
lintCmd := rulesCmd.
Command("lint", "formats a set of rule files. It reorders keys alphabetically, uses 4 spaces as indentantion, and formats PromQL expressions to a single line.").
Action(r.lint)
checkCmd := rulesCmd.
Command("check", "runs various best practice checks against rules.").
Action(r.checkRecordingRuleNames)
// Require Cortex cluster address and tentant ID on all these commands
for _, c := range []*kingpin.CmdClause{listCmd, printRulesCmd, getRuleGroupCmd, deleteRuleGroupCmd, loadRulesCmd, diffRulesCmd, syncRulesCmd} {
c.Flag("address", "Address of the cortex cluster, alternatively set CORTEX_ADDRESS.").
Envar("CORTEX_ADDRESS").
Required().
StringVar(&r.ClientConfig.Address)
c.Flag("id", "Cortex tenant id, alternatively set CORTEX_TENANT_ID.").
Envar("CORTEX_TENANT_ID").
Required().
StringVar(&r.ClientConfig.ID)
c.Flag("use-legacy-routes", "If set, API requests to cortex will use the legacy /api/prom/ routes, alternatively set CORTEX_USE_LEGACY_ROUTES.").
Default("false").
Envar("CORTEX_USE_LEGACY_ROUTES").
BoolVar(&r.ClientConfig.UseLegacyRoutes)
c.Flag("tls-ca-path", "TLS CA certificate to verify cortex API as part of mTLS, alternatively set CORTEX_TLS_CA_PATH.").
Default("").
Envar("CORTEX_TLS_CA_CERT").
StringVar(&r.ClientConfig.TLS.CAPath)
c.Flag("tls-cert-path", "TLS client certificate to authenticate with cortex API as part of mTLS, alternatively set CORTEX_TLS_CERT_PATH.").
Default("").
Envar("CORTEX_TLS_CLIENT_CERT").
StringVar(&r.ClientConfig.TLS.CertPath)
c.Flag("tls-key-path", "TLS client certificate private key to authenticate with cortex API as part of mTLS, alternatively set CORTEX_TLS_KEY_PATH.").
Default("").
Envar("CORTEX_TLS_CLIENT_KEY").
StringVar(&r.ClientConfig.TLS.KeyPath)
}
// Print Rules Command
printRulesCmd.Flag("disable-color", "disable colored output").BoolVar(&r.DisableColor)
// Get RuleGroup Command
getRuleGroupCmd.Arg("namespace", "Namespace of the rulegroup to retrieve.").Required().StringVar(&r.Namespace)
getRuleGroupCmd.Arg("group", "Name of the rulegroup ot retrieve.").Required().StringVar(&r.RuleGroup)
getRuleGroupCmd.Flag("disable-color", "disable colored output").BoolVar(&r.DisableColor)
// Delete RuleGroup Command
deleteRuleGroupCmd.Arg("namespace", "Namespace of the rulegroup to delete.").Required().StringVar(&r.Namespace)
deleteRuleGroupCmd.Arg("group", "Name of the rulegroup ot delete.").Required().StringVar(&r.RuleGroup)
// Load Rules Command
loadRulesCmd.Arg("rule-files", "The rule files to check.").Required().ExistingFilesVar(&r.RuleFilesList)
// Diff Command
diffRulesCmd.Arg("rule-files", "The rule files to check.").ExistingFilesVar(&r.RuleFilesList)
diffRulesCmd.Flag("namespaces", "comma-separated list of namespaces to check during a diff. Cannot be used together with --ignored-namespaces.").StringVar(&r.Namespaces)
diffRulesCmd.Flag("ignored-namespaces", "comma-separated list of namespaces to ignore during a diff. Cannot be used together with --namespaces.").StringVar(&r.IgnoredNamespaces)
diffRulesCmd.Flag("rule-files", "The rule files to check. Flag can be reused to load multiple files.").StringVar(&r.RuleFiles)
diffRulesCmd.Flag(
"rule-dirs",
"Comma separated list of paths to directories containing rules yaml files. Each file in a directory with a .yml or .yaml suffix will be parsed.",
).StringVar(&r.RuleFilesPath)
diffRulesCmd.Flag("disable-color", "disable colored output").BoolVar(&r.DisableColor)
diffRulesCmd.Flag("verbose", "show diff output with rules changes").BoolVar(&r.Verbose)
// Sync Command
syncRulesCmd.Arg("rule-files", "The rule files to check.").ExistingFilesVar(&r.RuleFilesList)
syncRulesCmd.Flag("namespaces", "comma-separated list of namespaces to check during a diff. Cannot be used together with --ignored-namespaces.").StringVar(&r.Namespaces)
syncRulesCmd.Flag("ignored-namespaces", "comma-separated list of namespaces to ignore during a sync. Cannot be used together with --namespaces.").StringVar(&r.IgnoredNamespaces)
syncRulesCmd.Flag("rule-files", "The rule files to check. Flag can be reused to load multiple files.").StringVar(&r.RuleFiles)
syncRulesCmd.Flag(
"rule-dirs",
"Comma separated list of paths to directories containing rules yaml files. Each file in a directory with a .yml or .yaml suffix will be parsed.",
).StringVar(&r.RuleFilesPath)
// Prepare Command
prepareCmd.Arg("rule-files", "The rule files to check.").ExistingFilesVar(&r.RuleFilesList)
prepareCmd.Flag("rule-files", "The rule files to check. Flag can be reused to load multiple files.").StringVar(&r.RuleFiles)
prepareCmd.Flag(
"rule-dirs",
"Comma separated list of paths to directories containing rules yaml files. Each file in a directory with a .yml or .yaml suffix will be parsed.",
).StringVar(&r.RuleFilesPath)
prepareCmd.Flag(
"in-place",
"edits the rule file in place",
).Short('i').BoolVar(&r.InPlaceEdit)
prepareCmd.Flag("label", "label to include as part of the aggregations.").Default(defaultPrepareAggregationLabel).Short('l').StringVar(&r.AggregationLabel)
prepareCmd.Flag("label-excluded-rule-groups", "Comma separated list of rule group names to exclude when including the configured label to aggregations.").StringVar(&r.AggregationLabelExcludedRuleGroups)
// Lint Command
lintCmd.Arg("rule-files", "The rule files to check.").ExistingFilesVar(&r.RuleFilesList)
lintCmd.Flag("rule-files", "The rule files to check. Flag can be reused to load multiple files.").StringVar(&r.RuleFiles)
lintCmd.Flag(
"rule-dirs",
"Comma separated list of paths to directories containing rules yaml files. Each file in a directory with a .yml or .yaml suffix will be parsed.",
).StringVar(&r.RuleFilesPath)
lintCmd.Flag("dry-run", "Performs a trial run that doesn't make any changes and (mostly) produces the same outpupt as a real run.").Short('n').BoolVar(&r.LintDryRun)
// Check Command
checkCmd.Arg("rule-files", "The rule files to check.").ExistingFilesVar(&r.RuleFilesList)
checkCmd.Flag("rule-files", "The rule files to check. Flag can be reused to load multiple files.").StringVar(&r.RuleFiles)
checkCmd.Flag(
"rule-dirs",
"Comma separated list of paths to directories containing rules yaml files. Each file in a directory with a .yml or .yaml suffix will be parsed.",
).StringVar(&r.RuleFilesPath)
checkCmd.Flag("strict", "fails rules checks that do not match best practices exactly").BoolVar(&r.Strict)
// List Command
listCmd.Flag("format", "Backend type to interact with: <json|yaml|table>").Default("table").EnumVar(&r.Format, formats...)
listCmd.Flag("disable-color", "disable colored output").BoolVar(&r.DisableColor)
}
func (r *RuleCommand) setup(k *kingpin.ParseContext) error {
prometheus.MustRegister(
ruleLoadTimestamp,
ruleLoadSuccessTimestamp,
)
// Loki's non-legacy route does not match Cortex, but the legacy one does.
if r.Backend == rules.LokiBackend {
r.ClientConfig.UseLegacyRoutes = true
}
cli, err := client.New(r.ClientConfig)
if err != nil {
return err
}
r.cli = cli
return nil
}
func (r *RuleCommand) setupFiles() error {
if r.Namespaces != "" && r.IgnoredNamespaces != "" {
return errors.New("--namespaces and --ignored-namespaces cannot be set at the same time")
}
// Set up ignored namespaces map for sync/diff command
if r.IgnoredNamespaces != "" {
r.ignoredNamespacesMap = map[string]struct{}{}
for _, ns := range strings.Split(r.IgnoredNamespaces, ",") {
if ns != "" {
r.ignoredNamespacesMap[ns] = struct{}{}
}
}
}
// Set up allowed namespaces map for sync/diff command
if r.Namespaces != "" {
r.namespacesMap = map[string]struct{}{}
for _, ns := range strings.Split(r.Namespaces, ",") {
if ns != "" {
r.namespacesMap[ns] = struct{}{}
}
}
}
// Set up rule groups excluded from label aggregation.
r.aggregationLabelExcludedRuleGroupsList = map[string]struct{}{}
for _, name := range strings.Split(r.AggregationLabelExcludedRuleGroups, ",") {
if name = strings.TrimSpace(name); name != "" {
r.aggregationLabelExcludedRuleGroupsList[name] = struct{}{}
}
}
for _, file := range strings.Split(r.RuleFiles, ",") {
if file != "" {
log.WithFields(log.Fields{
"file": file,
}).Debugf("adding file")
r.RuleFilesList = append(r.RuleFilesList, file)
}
}
for _, dir := range strings.Split(r.RuleFilesPath, ",") {
if dir != "" {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(info.Name(), ".yml") || strings.HasSuffix(info.Name(), ".yaml") {
log.WithFields(log.Fields{
"file": info.Name(),
"path": path,
}).Debugf("adding file in rule-path")
r.RuleFilesList = append(r.RuleFilesList, path)
return nil
}
log.WithFields(log.Fields{
"file": info.Name(),
"path": path,
}).Debugf("ignorings file in rule-path")
return nil
})
if err != nil {
return fmt.Errorf("error walking the path %q: %v", dir, err)
}
}
}
return nil
}
func (r *RuleCommand) listRules(k *kingpin.ParseContext) error {
rules, err := r.cli.ListRules(context.Background(), "")
if err != nil {
log.Fatalf("unable to read rules from cortex, %v", err)
}
p := printer.New(r.DisableColor)
return p.PrintRuleSet(rules, r.Format, os.Stdout)
}
func (r *RuleCommand) printRules(k *kingpin.ParseContext) error {
rules, err := r.cli.ListRules(context.Background(), "")
if err != nil {
if err == client.ErrResourceNotFound {
log.Infof("no rule groups currently exist for this user")
return nil
}
log.Fatalf("unable to read rules from cortex, %v", err)
}
p := printer.New(r.DisableColor)
return p.PrintRuleGroups(rules)
}
func (r *RuleCommand) getRuleGroup(k *kingpin.ParseContext) error {
group, err := r.cli.GetRuleGroup(context.Background(), r.Namespace, r.RuleGroup)
if err != nil {
if err == client.ErrResourceNotFound {
log.Infof("this rule group does not currently exist")
return nil
}
log.Fatalf("unable to read rules from cortex, %v", err)
}
p := printer.New(r.DisableColor)
return p.PrintRuleGroup(*group)
}
func (r *RuleCommand) deleteRuleGroup(k *kingpin.ParseContext) error {
err := r.cli.DeleteRuleGroup(context.Background(), r.Namespace, r.RuleGroup)
if err != nil && err != client.ErrResourceNotFound {
log.Fatalf("unable to delete rule group from cortex, %v", err)
}
return nil
}
func (r *RuleCommand) loadRules(k *kingpin.ParseContext) error {
nss, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
if err != nil {
return errors.Wrap(err, "load operation unsuccessful, unable to parse rules files")
}
ruleLoadTimestamp.SetToCurrentTime()
for _, ns := range nss {
for _, group := range ns.Groups {
fmt.Printf("group: '%v', ns: '%v'\n", group.Name, ns.Namespace)
curGroup, err := r.cli.GetRuleGroup(context.Background(), ns.Namespace, group.Name)
if err != nil && err != client.ErrResourceNotFound {
return errors.Wrap(err, "load operation unsuccessful, unable to contact cortex api")
}
if curGroup != nil {
err = rules.CompareGroups(*curGroup, group)
if err == nil {
log.WithFields(log.Fields{
"group": group.Name,
"namespace": ns.Namespace,
}).Infof("group already exists")
continue
}
log.WithFields(log.Fields{
"group": group.Name,
"namespace": ns.Namespace,
"difference": err,
}).Infof("updating group")
}
err = r.cli.CreateRuleGroup(context.Background(), ns.Namespace, group)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"group": group.Name,
"namespace": ns.Namespace,
}).Errorf("unable to load rule group")
return fmt.Errorf("load operation unsuccessful")
}
}
}
ruleLoadSuccessTimestamp.SetToCurrentTime()
return nil
}
// shouldCheckNamespace returns whether the namespace should be checked according to the allowed and ignored namespaces
func (r *RuleCommand) shouldCheckNamespace(namespace string) bool {
// when we have an allow list, only check those that we have explicitly defined.
if r.namespacesMap != nil {
_, allowed := r.namespacesMap[namespace]
return allowed
}
_, ignored := r.ignoredNamespacesMap[namespace]
return !ignored
}
func (r *RuleCommand) diffRules(k *kingpin.ParseContext) error {
err := r.setupFiles()
if err != nil {
return errors.Wrap(err, "diff operation unsuccessful, unable to load rules files")
}
nss, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
if err != nil {
return errors.Wrap(err, "diff operation unsuccessful, unable to parse rules files")
}
currentNamespaceMap, err := r.cli.ListRules(context.Background(), "")
//TODO: Skipping the 404s here might end up in an unsual scenario.
// If we're unable to reach the Cortex API due to a bad URL, we'll assume no rules are
// part of the namespace and provide a diff of the whole ruleset.
if err != nil && err != client.ErrResourceNotFound {
return errors.Wrap(err, "diff operation unsuccessful, unable to contact cortex api")
}
changes := []rules.NamespaceChange{}
for _, ns := range nss {
if !r.shouldCheckNamespace(ns.Namespace) {
continue
}
currentNamespace, exists := currentNamespaceMap[ns.Namespace]
if !exists {
changes = append(changes, rules.NamespaceChange{
State: rules.Created,
Namespace: ns.Namespace,
GroupsCreated: ns.Groups,
})
continue
}
origNamespace := rules.RuleNamespace{
Namespace: ns.Namespace,
Groups: currentNamespace,
}
changes = append(changes, rules.CompareNamespaces(origNamespace, ns))
// Remove namespace from temp map so namespaces that have been removed can easily be detected
delete(currentNamespaceMap, ns.Namespace)
}
for ns, deletedGroups := range currentNamespaceMap {
if !r.shouldCheckNamespace(ns) {
continue
}
changes = append(changes, rules.NamespaceChange{
State: rules.Deleted,
Namespace: ns,
GroupsDeleted: deletedGroups,
})
}
p := printer.New(r.DisableColor)
return p.PrintComparisonResult(changes, r.Verbose)
}
func (r *RuleCommand) syncRules(k *kingpin.ParseContext) error {
err := r.setupFiles()
if err != nil {
return errors.Wrap(err, "sync operation unsuccessful, unable to load rules files")
}
nss, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
if err != nil {
return errors.Wrap(err, "sync operation unsuccessful, unable to parse rules files")
}
currentNamespaceMap, err := r.cli.ListRules(context.Background(), "")
//TODO: Skipping the 404s here might end up in an unsual scenario.
// If we're unable to reach the Cortex API due to a bad URL, we'll assume no rules are
// part of the namespace and provide a diff of the whole ruleset.
if err != nil && err != client.ErrResourceNotFound {
return errors.Wrap(err, "sync operation unsuccessful, unable to contact cortex api")
}
changes := []rules.NamespaceChange{}
for _, ns := range nss {
if !r.shouldCheckNamespace(ns.Namespace) {
continue
}
currentNamespace, exists := currentNamespaceMap[ns.Namespace]
if !exists {
changes = append(changes, rules.NamespaceChange{
State: rules.Created,
Namespace: ns.Namespace,
GroupsCreated: ns.Groups,
})
continue
}
origNamespace := rules.RuleNamespace{
Namespace: ns.Namespace,
Groups: currentNamespace,
}
changes = append(changes, rules.CompareNamespaces(origNamespace, ns))
// Remove namespace from temp map so namespaces that have been removed can easily be detected
delete(currentNamespaceMap, ns.Namespace)
}
for ns, deletedGroups := range currentNamespaceMap {
if !r.shouldCheckNamespace(ns) {
continue
}
changes = append(changes, rules.NamespaceChange{
State: rules.Deleted,
Namespace: ns,
GroupsDeleted: deletedGroups,
})
}
err = r.executeChanges(context.Background(), changes)
if err != nil {
return errors.Wrap(err, "sync operation unsuccessful, unable to complete executing changes.")
}
return nil
}
func (r *RuleCommand) executeChanges(ctx context.Context, changes []rules.NamespaceChange) error {
var err error
for _, ch := range changes {
for _, g := range ch.GroupsCreated {
if !r.shouldCheckNamespace(ch.Namespace) {
continue
}
log.WithFields(log.Fields{
"group": g.Name,
"namespace": ch.Namespace,
}).Infof("creating group")
err = r.cli.CreateRuleGroup(ctx, ch.Namespace, g)
if err != nil {
return err
}
}
for _, g := range ch.GroupsUpdated {
if !r.shouldCheckNamespace(ch.Namespace) {
continue
}
log.WithFields(log.Fields{
"group": g.New.Name,
"namespace": ch.Namespace,
}).Infof("updating group")
err = r.cli.CreateRuleGroup(ctx, ch.Namespace, g.New)
if err != nil {
return err
}
}
for _, g := range ch.GroupsDeleted {
if !r.shouldCheckNamespace(ch.Namespace) {
continue
}
log.WithFields(log.Fields{
"group": g.Name,
"namespace": ch.Namespace,
}).Infof("deleting group")
err = r.cli.DeleteRuleGroup(ctx, ch.Namespace, g.Name)
if err != nil && err != client.ErrResourceNotFound {
return err
}
}
}
updated, created, deleted := rules.SummarizeChanges(changes)
fmt.Println()
fmt.Printf("Sync Summary: %v Groups Created, %v Groups Updated, %v Groups Deleted\n", created, updated, deleted)
return nil
}
func (r *RuleCommand) prepare(k *kingpin.ParseContext) error {
err := r.setupFiles()
if err != nil {
return errors.Wrap(err, "prepare operation unsuccessful, unable to load rules files")
}
namespaces, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
if err != nil {
return errors.Wrap(err, "prepare operation unsuccessful, unable to parse rules files")
}
// Do not apply the aggregation label to excluded rule groups.
applyTo := func(group rwrulefmt.RuleGroup, rule rulefmt.RuleNode) bool {
_, excluded := r.aggregationLabelExcludedRuleGroupsList[group.Name]
return !excluded
}
var count, mod int
for _, ruleNamespace := range namespaces {
c, m, err := ruleNamespace.AggregateBy(r.AggregationLabel, applyTo)
if err != nil {
return err
}
count += c
mod += m
}
// now, save all the files
if err := save(namespaces, r.InPlaceEdit); err != nil {
return err
}
log.Infof("SUCCESS: %d rules found, %d modified expressions", count, mod)
return nil
}
func (r *RuleCommand) lint(k *kingpin.ParseContext) error {
err := r.setupFiles()
if err != nil {
return errors.Wrap(err, "prepare operation unsuccessful, unable to load rules files")
}
namespaces, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
if err != nil {
return errors.Wrap(err, "prepare operation unsuccessful, unable to parse rules files")
}
var count, mod int
for _, ruleNamespace := range namespaces {
c, m, err := ruleNamespace.LintExpressions(r.Backend)
if err != nil {
return err
}
count += c
mod += m
}
if !r.LintDryRun {
// linting will always in-place edit unless is a dry-run.
if err := save(namespaces, true); err != nil {
return err
}
}
log.Infof("SUCCESS: %d rules found, %d linted expressions", count, mod)
return nil
}
func (r *RuleCommand) checkRecordingRuleNames(k *kingpin.ParseContext) error {
err := r.setupFiles()
if err != nil {
return errors.Wrap(err, "check operation unsuccessful, unable to load rules files")
}
namespaces, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
if err != nil {
return errors.Wrap(err, "check operation unsuccessful, unable to parse rules files")
}
for _, ruleNamespace := range namespaces {
n := ruleNamespace.CheckRecordingRules(r.Strict)
if n != 0 {
return fmt.Errorf("%d erroneous recording rule names", n)
}
duplicateRules := checkDuplicates(ruleNamespace.Groups)
if len(duplicateRules) != 0 {
fmt.Printf("%d duplicate rule(s) found.\n", len(duplicateRules))
for _, n := range duplicateRules {
fmt.Printf("Metric: %s\nLabel(s):\n", n.metric)
for i, l := range n.label {
fmt.Printf("\t%s: %s\n", i, l)
}
}
fmt.Println("Might cause inconsistency while recording expressions.")
}
}
return nil
}
// Taken from https://github.com/prometheus/prometheus/blob/8c8de46003d1800c9d40121b4a5e5de8582ef6e1/cmd/promtool/main.go#L403
type compareRuleType struct {
metric string
label map[string]string
}
func checkDuplicates(groups []rwrulefmt.RuleGroup) []compareRuleType {
var duplicates []compareRuleType
for _, group := range groups {
for index, rule := range group.Rules {
inst := compareRuleType{
metric: ruleMetric(rule),
label: rule.Labels,
}
for i := 0; i < index; i++ {
t := compareRuleType{
metric: ruleMetric(group.Rules[i]),
label: group.Rules[i].Labels,
}
if reflect.DeepEqual(t, inst) {
duplicates = append(duplicates, t)
}
}
}
}
return duplicates
}
func ruleMetric(rule rulefmt.RuleNode) string {
if rule.Alert.Value != "" {
return rule.Alert.Value
}
return rule.Record.Value
}
// End taken from https://github.com/prometheus/prometheus/blob/8c8de46003d1800c9d40121b4a5e5de8582ef6e1/cmd/promtool/main.go#L403
// save saves a set of rule files to to disk. You can specify whenever you want the
// file(s) to be edited in-place.
func save(nss map[string]rules.RuleNamespace, i bool) error {
for _, ns := range nss {
payload, err := yamlv3.Marshal(ns)
if err != nil {
return err
}
filepath := ns.Filepath
if !i {
filepath = filepath + ".result"
}
if err := ioutil.WriteFile(filepath, payload, 0644); err != nil {
return err
}
}
return nil
}