Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds glob support for filepath in tools command #4105

Merged
merged 5 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmd/thanos/testdata/rules-files/valid_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
groups:
- name: test-alert-group
partial_response_strategy: "warn"
interval: 2m
rules:
- alert: TestAlert
expr: 1
labels:
key: value
annotations:
key: value

- name: test-rule-group
partial_response_strategy: "warn"
interval: 2m
rules:
- record: test_metric
expr: 1
18 changes: 18 additions & 0 deletions cmd/thanos/testdata/rules-files/valid_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
groups:
- name: test-alert-group
partial_response_strategy: "warn"
interval: 2m
rules:
- alert: TestAlert
expr: 1
labels:
key: value
annotations:
key: value

- name: test-rule-group
partial_response_strategy: "warn"
interval: 2m
rules:
- record: test_metric
expr: 1
45 changes: 29 additions & 16 deletions cmd/thanos/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package main

import (
"os"
"path/filepath"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/oklog/run"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/thanos-io/thanos/pkg/errutil"
"github.com/thanos-io/thanos/pkg/extkingpin"
Expand All @@ -25,7 +27,7 @@ func registerTools(app *extkingpin.App) {

func registerCheckRules(app extkingpin.AppClause) {
cmd := app.Command("rules-check", "Check if the rule files are valid or not.")
ruleFiles := cmd.Flag("rules", "The rule files glob to check (repeated).").Required().ExistingFiles()
ruleFiles := cmd.Flag("rules", "The rule files glob to check (repeated).").Required().Strings()

cmd.Setup(func(g *run.Group, logger log.Logger, reg *prometheus.Registry, _ opentracing.Tracer, _ <-chan struct{}, _ bool) error {
// Dummy actor to immediately kill the group after the run function returns.
Expand All @@ -34,31 +36,42 @@ func registerCheckRules(app extkingpin.AppClause) {
})
}

func checkRulesFiles(logger log.Logger, files *[]string) error {
func checkRulesFiles(logger log.Logger, patterns *[]string) error {
var failed errutil.MultiError

for _, fn := range *files {
level.Info(logger).Log("msg", "checking", "filename", fn)
f, err := os.Open(fn)
if err != nil {
for _, p := range *patterns {
level.Info(logger).Log("msg", "checking", "pattern", p)
matches, err := filepath.Glob(p)
if err != nil || matches == nil {
err = errors.New("matching file not found")
level.Error(logger).Log("result", "FAILED", "error", err)
level.Info(logger).Log()
failed.Add(err)
continue
}
defer func() { _ = f.Close() }()
for _, fn := range matches {
level.Info(logger).Log("msg", "checking", "filename", fn)
f, er := os.Open(fn)
if er != nil {
level.Error(logger).Log("result", "FAILED", "error", er)
level.Info(logger).Log()
failed.Add(err)
continue
}
defer func() { _ = f.Close() }()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically we'd use rununtil.CloseWithLogOnErr but since at the end the process closes immediately, this is fine. Just something to keep in mind for future contributions 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, Thanks 🙌


n, errs := rules.ValidateAndCount(f)
if errs.Err() != nil {
level.Error(logger).Log("result", "FAILED")
for _, e := range errs {
level.Error(logger).Log("error", e.Error())
failed.Add(e)
n, errs := rules.ValidateAndCount(f)
if errs.Err() != nil {
level.Error(logger).Log("result", "FAILED")
for _, e := range errs {
level.Error(logger).Log("error", e.Error())
failed.Add(e)
}
level.Info(logger).Log()
continue
}
level.Info(logger).Log()
continue
level.Info(logger).Log("result", "SUCCESS", "rules found", n)
}
level.Info(logger).Log("result", "SUCCESS", "rules found", n)
}
return failed.Err()
}
15 changes: 15 additions & 0 deletions cmd/thanos/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ func Test_CheckRules(t *testing.T) {
testutil.NotOk(t, checkRulesFiles(logger, &fn), "expected err for file %s", fn)
}
}

func Test_CheckRules_Glob(t *testing.T) {
// regex path
files := &[]string{"./testdata/rules-files/valid*.yaml"}
logger := log.NewNopLogger()
testutil.Ok(t, checkRulesFiles(logger, files))

// direct path
files = &[]string{"./testdata/rules-files/valid.yaml"}
testutil.Ok(t, checkRulesFiles(logger, files))

// invalid path
files = &[]string{"./testdata/rules-files/*.yamlaaa"}
testutil.NotOk(t, checkRulesFiles(logger, files), "expected err for file %s", files)
}