-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
186 lines (162 loc) · 4.42 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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"strings"
ghodssYaml "github.com/ghodss/yaml"
"github.com/globocom/slo-generator/kubernetes"
"github.com/globocom/slo-generator/slo"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/prometheus/prometheus/pkg/rulefmt"
yaml "gopkg.in/yaml.v3"
)
func main() {
var (
sloPath = ""
classesPath = ""
ruleOutput = ""
k8sLabels = ""
disableTicket = false
k8s = false
)
flag.StringVar(&sloPath, "slo.path", "", "A YML file describing SLOs")
flag.StringVar(&classesPath, "classes.path", "", "A YML file describing SLOs classes (optional)")
flag.StringVar(&ruleOutput, "rule.output", "", "Output to describe a prometheus rules")
flag.BoolVar(&disableTicket, "disable.ticket", false, "Disable generation of alerts of kind ticket")
flag.BoolVar(&k8s, "kubernetes", false, "Generates prometheus-operator YAML")
flag.StringVar(&k8sLabels, "kubernetes-labels", "", "Add some labels in generated resource")
flag.Parse()
if sloPath == "" {
log.Fatal("slo.path is a required param")
}
var output io.Writer
if ruleOutput == "" {
output = os.Stdout
} else {
targetFile, err := os.Create(ruleOutput)
if err != nil {
log.Fatal(err)
}
defer targetFile.Close()
output = targetFile
}
f, err := os.Open(sloPath)
if err != nil {
log.Fatal(err)
}
spec := &slo.SLOSpec{}
err = yaml.NewDecoder(f).Decode(spec)
if err != nil {
log.Fatal(err)
}
classesDefinition, err := readClassesDefinition(classesPath)
if err != nil {
log.Fatal(err)
}
if len(spec.Classes) > 0 && len(classesDefinition.Classes) > 0 {
log.Fatal("you can not define classes in slo and classes files")
} else if len(classesDefinition.Classes) > 0 {
spec.Classes = classesDefinition.Classes
}
ruleGroups := &rulefmt.RuleGroups{
Groups: []rulefmt.RuleGroup{},
}
if k8s {
labels := map[string]string{}
if k8sLabels != "" {
labels, err = parseLabels(k8sLabels)
if err != nil {
log.Fatal(err)
}
}
manifests := []monitoringv1.PrometheusRule{}
for _, slo := range spec.SLOS {
// try to use any slo class found
sloClass, err := spec.Classes.FindClass(slo.Class)
if err != nil {
log.Fatalf("Could not compile SLO: %q, err: %q", slo.Name, err.Error())
}
manifests = append(manifests, kubernetes.GenerateManifests(kubernetes.Opts{
SLO: slo,
Class: sloClass,
DisableTicket: disableTicket,
})...)
}
for i, manifest := range manifests {
if manifest.Labels == nil {
manifest.Labels = map[string]string{}
}
for key, value := range labels {
manifest.Labels[key] = value
}
b, err := ghodssYaml.Marshal(manifest)
if err != nil {
log.Fatal(err)
}
if i > 0 {
_, err = output.Write([]byte("---\n"))
if err != nil {
log.Fatal(err)
}
}
_, err = output.Write(b)
if err != nil {
log.Fatal(err)
}
}
if ruleOutput != "" {
log.Printf("generated a kubernetes manifest record in %q", ruleOutput)
}
return
}
for _, slo := range spec.SLOS {
// try to use any slo class found
sloClass, err := spec.Classes.FindClass(slo.Class)
if err != nil {
log.Fatalf("Could not compile SLO: %q, err: %q", slo.Name, err.Error())
}
ruleGroups.Groups = append(ruleGroups.Groups, slo.GenerateGroupRules(sloClass, disableTicket)...)
ruleGroups.Groups = append(ruleGroups.Groups, rulefmt.RuleGroup{
Name: "slo:" + slo.Name + ":alert",
Rules: slo.GenerateAlertRules(sloClass, disableTicket),
})
}
err = yaml.NewEncoder(output).Encode(ruleGroups)
if err != nil {
log.Fatal(err)
}
if ruleOutput != "" {
log.Printf("generated a SLO record in %q", ruleOutput)
}
}
func parseLabels(labels string) (map[string]string, error) {
result := map[string]string{}
for _, part := range strings.Split(labels, ",") {
parts := strings.Split(part, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid label %q", part)
}
result[parts[0]] = parts[1]
}
return result, nil
}
// readClassesDefinition read SLO classes from filesystem
func readClassesDefinition(classesPath string) (*slo.ClassesDefinition, error) {
classesDefinition := slo.ClassesDefinition{
Classes: []slo.Class{},
}
if classesPath != "" {
f, err := os.Open(classesPath)
if err != nil {
return nil, err
}
err = yaml.NewDecoder(f).Decode(&classesDefinition)
if err != nil {
return nil, err
}
}
return &classesDefinition, nil
}