Skip to content

Commit 8fe7ce2

Browse files
committed
add goreleaser and differentiate between print and listing rules
Signed-off-by: Jacob Lisi <[email protected]>
1 parent d8f6ed8 commit 8fe7ce2

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

.goreleaser.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This is an example goreleaser.yaml file with some sane defaults.
2+
# Make sure to check the documentation at http://goreleaser.com
3+
before:
4+
hooks:
5+
# you may remove this if you don't use vgo
6+
- go mod tidy
7+
# you may remove this if you don't need go generate
8+
- go generate ./...
9+
builds:
10+
- env:
11+
- CGO_ENABLED=0
12+
archives:
13+
- replacements:
14+
darwin: Darwin
15+
linux: Linux
16+
windows: Windows
17+
386: i386
18+
amd64: x86_64
19+
checksum:
20+
name_template: 'checksums.txt'
21+
snapshot:
22+
name_template: "{{ .Tag }}-next"
23+
changelog:
24+
sort: asc
25+
filters:
26+
exclude:
27+
- '^docs:'
28+
- '^test:'

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Changelog

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,32 @@ The following commands are used by users to interact with their cortex ruler con
3232

3333
#### Rules List
3434

35+
This command will retrieve all of the rule groups stored in the specified cortex instance and print each one by rule group name and namespace to the terminal.
36+
3537
cortex-cli rules list
3638

37-
#### RulesGet
39+
#### Rules Print
40+
41+
This command will retrieve all of the rule groups stored in the specified cortex instance and print them to the terminal.
42+
43+
cortex-cli rules print
44+
45+
#### Rules Get
46+
47+
This command will retrieve the specified rule group from cortex and print it to the terminal.
3848

39-
cortex-cli rules get example_rule_group
49+
cortex-cli rules get example_namespace example_rule_group
50+
51+
#### Rules Delete
52+
53+
This command will retrieve the specified rule group from cortex and print it to the terminal.
54+
55+
cortex-cli rules delete example_namespace example_rule_group
4056

4157
#### Rules Load
4258

59+
This command will load each rule group in the specified files and load them into cortex. If a rule already exists in cortex it will be overwritten if a diff is found.
60+
4361
cortex-cli rules load ./example_rules_one.yaml ./example_rules_two.yaml ...
4462

4563
## Chunks

pkg/client/rules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (r *CortexClient) CreateRuleGroup(ctx context.Context, namespace string, rg
3030

3131
// DeleteRuleGroup creates a new rule group
3232
func (r *CortexClient) DeleteRuleGroup(ctx context.Context, namespace, groupName string) error {
33-
res, err := r.doRequest("/api/prom/rules/"+namespace, "DELETE", nil)
33+
res, err := r.doRequest("/api/prom/rules/"+namespace+"/"+groupName, "DELETE", nil)
3434
if err != nil {
3535
return err
3636
}

pkg/commands/rules.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"text/tabwriter"
78

89
"github.com/alecthomas/chroma/quick"
910
"github.com/pkg/errors"
@@ -53,11 +54,19 @@ func (r *RuleCommand) Register(app *kingpin.Application) {
5354
// List Rules Command
5455
rulesCmd.Command("list", "List the rules currently in the cortex ruler.").Action(r.listRules)
5556

57+
// Print Rules Command
58+
rulesCmd.Command("print", "Print the rules currently in the cortex ruler.").Action(r.printRules)
59+
5660
// Get RuleGroup Command
5761
getRuleGroupCmd := rulesCmd.Command("get", "Retreive a rulegroup from the ruler.").Action(r.getRuleGroup)
5862
getRuleGroupCmd.Arg("namespace", "Namespace of the rulegroup to retrieve.").Required().StringVar(&r.Namespace)
5963
getRuleGroupCmd.Arg("group", "Name of the rulegroup ot retrieve.").Required().StringVar(&r.RuleGroup)
6064

65+
// Delete RuleGroup Command
66+
deleteRuleGroupCmd := rulesCmd.Command("delete", "Delete a rulegroup from the ruler.").Action(r.deleteRuleGroup)
67+
deleteRuleGroupCmd.Arg("namespace", "Namespace of the rulegroup to delete.").Required().StringVar(&r.Namespace)
68+
deleteRuleGroupCmd.Arg("group", "Name of the rulegroup ot delete.").Required().StringVar(&r.RuleGroup)
69+
6170
loadRulesCmd := rulesCmd.Command("load", "load a set of rules to a designated cortex endpoint").Action(r.loadRules)
6271
loadRulesCmd.Arg("rule-files", "The rule files to check.").Required().ExistingFilesVar(&r.RuleFiles)
6372
}
@@ -78,6 +87,27 @@ func (r *RuleCommand) setup(k *kingpin.ParseContext) error {
7887
}
7988

8089
func (r *RuleCommand) listRules(k *kingpin.ParseContext) error {
90+
rules, err := r.cli.ListRules(context.Background(), "")
91+
if err != nil {
92+
log.Fatalf("unable to read rules from cortex, %v", err)
93+
94+
}
95+
96+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug)
97+
98+
fmt.Fprintln(w, "Namespace\t Rule Group")
99+
for ns, rulegroups := range rules {
100+
for _, rg := range rulegroups {
101+
fmt.Fprintf(w, "%s\t %s\n", ns, rg.Name)
102+
}
103+
}
104+
105+
w.Flush()
106+
107+
return nil
108+
}
109+
110+
func (r *RuleCommand) printRules(k *kingpin.ParseContext) error {
81111
rules, err := r.cli.ListRules(context.Background(), "")
82112
if err != nil {
83113
log.Fatalf("unable to read rules from cortex, %v", err)
@@ -113,6 +143,14 @@ func (r *RuleCommand) getRuleGroup(k *kingpin.ParseContext) error {
113143
return nil
114144
}
115145

146+
func (r *RuleCommand) deleteRuleGroup(k *kingpin.ParseContext) error {
147+
err := r.cli.DeleteRuleGroup(context.Background(), r.Namespace, r.RuleGroup)
148+
if err != nil {
149+
log.Fatalf("unable to delete rule group from cortex, %v", err)
150+
}
151+
return nil
152+
}
153+
116154
func (r *RuleCommand) loadRules(k *kingpin.ParseContext) error {
117155
nss, err := rules.ParseFiles(r.RuleFiles)
118156
if err != nil {

0 commit comments

Comments
 (0)