-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for validation of rule groups themselves (#46)
* feat: add a new Group validation rule scope and make the allowedSourceTenants validator only work for this scope Signed-off-by: Martin Chodur <[email protected]> * fix: lint Signed-off-by: Martin Chodur <[email protected]> --------- Signed-off-by: Martin Chodur <[email protected]>
- Loading branch information
Showing
17 changed files
with
136 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package validator | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/fusakla/promruval/v2/pkg/prometheus" | ||
"github.com/fusakla/promruval/v2/pkg/unmarshaler" | ||
"github.com/prometheus/prometheus/model/rulefmt" | ||
"golang.org/x/exp/slices" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func newHasAllowedSourceTenants(paramsConfig yaml.Node) (Validator, error) { | ||
params := struct { | ||
AllowedSourceTenants []string `yaml:"allowedSourceTenants"` | ||
}{} | ||
if err := paramsConfig.Decode(¶ms); err != nil { | ||
return nil, err | ||
} | ||
return &hasAllowedSourceTenants{allowedSourceTenants: params.AllowedSourceTenants}, nil | ||
} | ||
|
||
type hasAllowedSourceTenants struct { | ||
allowedSourceTenants []string | ||
} | ||
|
||
func (h hasAllowedSourceTenants) String() string { | ||
return fmt.Sprintf("does not have other `source_tenants` than: `%s`", strings.Join(h.allowedSourceTenants, "`, `")) | ||
} | ||
|
||
func (h hasAllowedSourceTenants) Validate(group unmarshaler.RuleGroup, _ rulefmt.Rule, _ *prometheus.Client) []error { | ||
var invalidTenants []string | ||
for _, tenant := range group.SourceTenants { | ||
if !slices.Contains(h.allowedSourceTenants, tenant) { | ||
invalidTenants = append(invalidTenants, tenant) | ||
} | ||
} | ||
if len(invalidTenants) == 0 { | ||
return []error{} | ||
} | ||
return []error{fmt.Errorf("group has invalid source_tenants: `%s`", strings.Join(invalidTenants, "`,`"))} | ||
} |
Oops, something went wrong.