Skip to content

Commit

Permalink
pkg/prometheus/promcfg*.go: Fix nits
Browse files Browse the repository at this point in the history
  • Loading branch information
slashpai committed Sep 29, 2021
1 parent e33449f commit 929bbf5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
10 changes: 4 additions & 6 deletions pkg/prometheus/promcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,16 @@ func buildExternalLabels(p *v1.Prometheus) yaml.MapSlice {
return stringMapToMapSlice(m)
}

// ValidateConfigInputs validates if inputs passed are valid
// validateConfigInputs runs extra validation on the Prometheus fields which can't be done at the CRD schema validation level.
func validateConfigInputs(p *v1.Prometheus) error {
if p.Spec.EnforcedBodySizeLimit != "" {
err := validateBodySizeLimit(p.Spec.EnforcedBodySizeLimit)
if err != nil {
if err := validateBodySizeLimit(p.Spec.EnforcedBodySizeLimit); err != nil {
return err
}
}
return nil
}

// Validate EnforcedBodySizeLimit
func validateBodySizeLimit(enforcedLimit string) error {
// To validate if given value is parsable for the acceptable body_size_limit values
_, err := units.ParseBase2Bytes(enforcedLimit)
Expand All @@ -253,8 +251,8 @@ func (cg *ConfigGenerator) GenerateConfig(
// Validate Prometheus Config Inputs at Prometheus CRD level
err := validateConfigInputs(p)

if err != nil {
return nil, errors.Wrap(err, "invalid config value passed")
if err := validateConfigInputs(p); err != nil {
return nil, err
}

versionStr := p.Spec.Version
Expand Down
8 changes: 4 additions & 4 deletions pkg/prometheus/promcfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5807,22 +5807,22 @@ alerting:
{
version: "v2.28.0",
enforcedBodySizeLimit: "100",
expectedErr: errors.New("invalid config value passed: invalid enforcedBodySizeLimit value specified: units: unknown unit in 100"),
expectedErr: errors.New("invalid enforcedBodySizeLimit value specified: units: unknown unit in 100"),
},
{
version: "v2.28.0",
enforcedBodySizeLimit: "200kb",
expectedErr: errors.New("invalid config value passed: invalid enforcedBodySizeLimit value specified: units: unknown unit kb in 200kb"),
expectedErr: errors.New("invalid enforcedBodySizeLimit value specified: units: unknown unit kb in 200kb"),
},
{
version: "v2.28.0",
enforcedBodySizeLimit: "300 MB",
expectedErr: errors.New("invalid config value passed: invalid enforcedBodySizeLimit value specified: units: unknown unit MB in 300 MB"),
expectedErr: errors.New("invalid enforcedBodySizeLimit value specified: units: unknown unit MB in 300 MB"),
},
{
version: "v2.28.0",
enforcedBodySizeLimit: "150M",
expectedErr: errors.New("invalid config value passed: invalid enforcedBodySizeLimit value specified: units: unknown unit M in 150M"),
expectedErr: errors.New("invalid enforcedBodySizeLimit value specified: units: unknown unit M in 150M"),
},
} {
t.Run(fmt.Sprintf("%s enforcedBodySizeLimit(%s)", tc.version, tc.enforcedBodySizeLimit), func(t *testing.T) {
Expand Down

0 comments on commit 929bbf5

Please sign in to comment.