Skip to content

Commit

Permalink
Allow to define classes also on SLO file
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Nov 7, 2022
1 parent 2833537 commit 6344ee4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func main() {
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{},
}
Expand All @@ -82,7 +88,7 @@ func main() {
manifests := []monitoringv1.PrometheusRule{}
for _, slo := range spec.SLOS {
// try to use any slo class found
sloClass, err := classesDefinition.FindClass(slo.Class)
sloClass, err := spec.Classes.FindClass(slo.Class)
if err != nil {
log.Fatalf("Could not compile SLO: %q, err: %q", slo.Name, err.Error())
}
Expand Down Expand Up @@ -126,7 +132,7 @@ func main() {

for _, slo := range spec.SLOS {
// try to use any slo class found
sloClass, err := classesDefinition.FindClass(slo.Class)
sloClass, err := spec.Classes.FindClass(slo.Class)
if err != nil {
log.Fatalf("Could not compile SLO: %q, err: %q", slo.Name, err.Error())
}
Expand Down
8 changes: 5 additions & 3 deletions slo/classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ type Class struct {
}

type ClassesDefinition struct {
Classes []Class `yaml:"classes"`
Classes Classes `yaml:"classes"`
}

type Classes []Class

// FindClass finds for a given name, if not found return an error
func (c *ClassesDefinition) FindClass(name string) (*Class, error) {
func (c Classes) FindClass(name string) (*Class, error) {
if name == "" {
return nil, nil
}
for _, class := range c.Classes {
for _, class := range c {
if class.Name == name {
return &class, nil
}
Expand Down
6 changes: 3 additions & 3 deletions slo/classes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ func TestFindClass(t *testing.T) {
},
}

class, err := definition.FindClass("HIGH")
class, err := definition.Classes.FindClass("HIGH")
assert.NoError(t, err)
assert.Equal(t, &definition.Classes[0], class)

class, err = definition.FindClass("NOTFOUND")
class, err = definition.Classes.FindClass("NOTFOUND")
assert.EqualError(t, err, "SLO class \"NOTFOUND\" is not found")
assert.Nil(t, class)

class, err = definition.FindClass("")
class, err = definition.Classes.FindClass("")
assert.Nil(t, err)
assert.Nil(t, class)
}
3 changes: 2 additions & 1 deletion slo/slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var quantiles = []struct {
}

type SLOSpec struct {
SLOS []SLO
SLOS []SLO `yaml:"slos"`
Classes Classes `yaml:"classes"`
}

type ExprBlock struct {
Expand Down

0 comments on commit 6344ee4

Please sign in to comment.