-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
221 lines (198 loc) · 8.16 KB
/
main.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/alecthomas/kingpin/v2"
doublestar "github.com/bmatcuk/doublestar/v4"
"github.com/fusakla/promruval/v3/pkg/config"
"github.com/fusakla/promruval/v3/pkg/prometheus"
"github.com/fusakla/promruval/v3/pkg/report"
"github.com/fusakla/promruval/v3/pkg/unmarshaler"
"github.com/fusakla/promruval/v3/pkg/validate"
"github.com/fusakla/promruval/v3/pkg/validationrule"
"github.com/fusakla/promruval/v3/pkg/validator"
log "github.com/sirupsen/logrus"
)
var (
// Set using goreleaser ldflags during build, see https://goreleaser.com/environment/#using-the-mainversion
version = "dev"
commit = "none"
date = time.Now().Format("2006-01-02")
builtBy = os.Getenv("USER")
app = kingpin.New("promruval", "Prometheus rules validation tool.")
validateConfigFiles = app.Flag("config-file", "Path to validation config file. Can be passed multiple times, only validationRules will be reflected from the additional configs.").Short('c').ExistingFiles()
debug = app.Flag("debug", "Enable debug logging.").Bool()
versionCmd = app.Command("version", "Print version and build information.")
validateCmd = app.Command("validate", "Validate Prometheus rule files in YAML or jsonnet format using validation rules from config file(s).")
filePaths = validateCmd.Arg("path", "Rule file paths to be validated (.yaml, .yml or .jsonnet), can use even double star globs or ~. Will be expanded if not done by bash.").Required().Strings()
disabledRules = validateCmd.Flag("disable-rule", "Allows to disable any validation rules by it's name. Can be passed multiple times.").Short('d').Strings()
enabledRules = validateCmd.Flag("enable-rule", "Only enable these validation rules. Can be passed multiple times.").Short('e').Strings()
validationOutputFormat = validateCmd.Flag("output", "Format of the output.").Short('o').PlaceHolder("[text,json,yaml]").Default("text").Enum("text", "json", "yaml")
color = validateCmd.Flag("color", "Use color output.").Bool()
supportLoki = validateCmd.Flag("support-loki", "Support Loki rules format.").Bool()
supportMimir = validateCmd.Flag("support-mimir", "Support Mimir rules format.").Bool()
supportThanos = validateCmd.Flag("support-thanos", "Support Thanos rules format.").Bool()
docsCmd = app.Command("validation-docs", "Print human readable form of the validation rules from config file.")
docsOutputFormat = docsCmd.Flag("output", "Format of the output.").Short('o').PlaceHolder("[text,markdown,html]").Default("text").Enum("text", "markdown", "html")
)
func loadConfigFile(configFilePath string) (*config.Config, error) {
configLoader := config.NewLoader(configFilePath)
return configLoader.Load()
}
func validationRulesFromConfig(validationConfig *config.Config) ([]*validationrule.ValidationRule, error) {
var validationRules []*validationrule.ValidationRule
rulesIteration:
for _, validationRule := range validationConfig.ValidationRules {
if validationRule.Scope == "" {
return nil, fmt.Errorf("scope is missing in the validation rule `%s`", validationRule.Name)
}
for _, disabledRule := range *disabledRules {
if disabledRule == validationRule.Name {
continue rulesIteration
}
}
for _, enabledRule := range *enabledRules {
if enabledRule != validationRule.Name {
continue rulesIteration
}
}
newRule := validationrule.New(validationRule.Name, validationRule.Scope)
for _, validatorConfig := range validationRule.Validations {
if err := validator.KnownValidators(validationRule.Scope, []string{validatorConfig.ValidatorType}); err != nil {
return nil, fmt.Errorf("loading config for validator `%s` in the `%s` rule: %w", validatorConfig.ValidatorType, validationRule.Name, err)
}
newValidator, err := validator.NewFromConfig(validationRule.Scope, validatorConfig)
if err != nil {
return nil, fmt.Errorf("loading config for validator `%s` in the `%s` rule: %w", validatorConfig.ValidatorType, validationRule.Name, err)
}
if newValidator == nil {
continue
}
newRule.AddValidator(newValidator, validatorConfig.AdditionalDetails)
}
validationRules = append(validationRules, newRule)
}
return validationRules, nil
}
func exitWithError(err error) {
log.Error(err)
os.Exit(1)
}
func main() {
currentCommand := kingpin.MustParse(app.Parse(os.Args[1:]))
if currentCommand == versionCmd.FullCommand() {
fmt.Printf("Version: %s\nBuild date: %s\nBuild commit: %s\nBuilt by: %s\n", version, date, commit, builtBy)
return
}
if len(*validateConfigFiles) == 0 {
app.Fatalf("required flag --config-file not provided, try --help")
}
mainValidationConfig, err := loadConfigFile((*validateConfigFiles)[0])
if err != nil {
exitWithError(err)
}
for _, cf := range (*validateConfigFiles)[1:] {
validationConfig, err := loadConfigFile(cf)
if err != nil {
exitWithError(err)
}
if validationConfig.Prometheus.URL != "" {
mainValidationConfig.Prometheus = validationConfig.Prometheus
}
if validationConfig.CustomExcludeAnnotation != "" {
mainValidationConfig.CustomExcludeAnnotation = validationConfig.CustomExcludeAnnotation
}
if validationConfig.CustomDisableComment != "" {
mainValidationConfig.CustomDisableComment = validationConfig.CustomDisableComment
}
mainValidationConfig.ValidationRules = append(mainValidationConfig.ValidationRules, validationConfig.ValidationRules...)
}
validationRules, err := validationRulesFromConfig(mainValidationConfig)
if err != nil {
exitWithError(err)
}
switch currentCommand {
case docsCmd.FullCommand():
var reportRules []report.ValidationRule
for _, r := range validationRules {
reportRules = append(reportRules, r)
}
output, err := report.ValidationDocs(reportRules, *docsOutputFormat)
if err != nil {
exitWithError(err)
}
fmt.Println(output)
case validateCmd.FullCommand():
log.SetLevel(log.InfoLevel)
log.SetOutput(os.Stderr)
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableLevelTruncation: true})
if *debug {
log.SetLevel(log.DebugLevel)
}
var filesToBeValidated []string
for _, path := range *filePaths {
if strings.HasPrefix(path, "~/") {
home, err := os.UserHomeDir()
if err != nil {
exitWithError(fmt.Errorf("failed to get user home directory: %w", err))
}
path = filepath.Join(home, path[2:])
}
base, pattern := doublestar.SplitPattern(path)
paths, err := doublestar.Glob(os.DirFS(base), pattern, doublestar.WithFilesOnly(), doublestar.WithFailOnIOErrors(), doublestar.WithFailOnPatternNotExist())
if err != nil {
exitWithError(fmt.Errorf("failed expanding glob pattern `%s`: %w", path, err))
}
for _, p := range paths {
filesToBeValidated = append(filesToBeValidated, filepath.Join(base, p))
}
}
if *supportLoki {
unmarshaler.SupportLoki(true)
}
if *supportMimir {
unmarshaler.SupportMimir(true)
}
if *supportThanos {
unmarshaler.SupportThanos(true)
}
var prometheusClient *prometheus.Client
if mainValidationConfig.Prometheus.URL != "" {
prometheusClient, err = prometheus.NewClient(mainValidationConfig.Prometheus)
if err != nil {
exitWithError(fmt.Errorf("failed to initialize prometheus client: %w", err))
}
}
excludeAnnotation := "disabled_validation_rules"
if mainValidationConfig.CustomExcludeAnnotation != "" {
excludeAnnotation = mainValidationConfig.CustomExcludeAnnotation
}
disableValidatorsComment := "ignore_validations"
if mainValidationConfig.CustomDisableComment != "" {
disableValidatorsComment = mainValidationConfig.CustomDisableComment
}
validationReport := validate.Files(filesToBeValidated, validationRules, excludeAnnotation, disableValidatorsComment, prometheusClient)
if mainValidationConfig.Prometheus.URL != "" {
prometheusClient.DumpCache()
}
var output string
switch *validationOutputFormat {
case "text":
output, err = validationReport.AsText(2, *color)
case "json":
output, err = validationReport.AsJSON()
case "yaml":
output, err = validationReport.AsYaml()
}
if err != nil {
exitWithError(err)
}
fmt.Println(output)
if validationReport.Failed {
os.Exit(1)
}
}
}