Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add max query delay validation per data source #560

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions manifest/v1alpha/agent/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var specValidation = govy.New[Spec](
Rules(exactlyOneDataSourceTypeValidationRule).
Rules(
historicalDataRetrievalValidationRule,
queryDelayGreaterThanOrEqualToDefaultValidationRule),
queryDelayValidationRule),
govy.For(func(s Spec) v1alpha.ReleaseChannel { return s.ReleaseChannel }).
WithName("releaseChannel").
OmitEmpty().
Expand Down Expand Up @@ -239,8 +239,8 @@ var (
)

const (
errCodeExactlyOneDataSourceType = "exactly_one_data_source_type"
errCodeQueryDelayGreaterThanOrEqualToDefault = "query_delay_greater_than_or_equal_to_default"
errCodeExactlyOneDataSourceType = "exactly_one_data_source_type"
errCodeQueryDelayOutOfBounds = "query_delay_out_of_bounds"
)

var exactlyOneDataSourceTypeValidationRule = govy.NewRule(func(spec Spec) error {
Expand Down Expand Up @@ -420,11 +420,23 @@ var historicalDataRetrievalValidationRule = govy.NewRule(func(spec Spec) error {
return nil
})

var queryDelayGreaterThanOrEqualToDefaultValidationRule = govy.NewRule(func(spec Spec) error {
var queryDelayValidationRule = govy.NewRule(func(spec Spec) error {
if spec.QueryDelay == nil {
return nil
}
typ, _ := spec.GetType()
maxQueryDelay := v1alpha.GetQueryDelayMax(typ)
maxQueryDelayAllowed := v1alpha.Duration{
Value: maxQueryDelay.Value,
Unit: maxQueryDelay.Unit,
}
if spec.QueryDelay.Duration.GreaterThan(maxQueryDelayAllowed) {
return govy.NewPropertyError(
"queryDelay",
spec.QueryDelay,
errors.Errorf("must be less than or equal to %d %s",
*maxQueryDelayAllowed.Value, maxQueryDelayAllowed.Unit))
}
agentDefault := v1alpha.GetQueryDelayDefaults()[typ]
if spec.QueryDelay.LessThan(agentDefault) {
return govy.NewPropertyError(
Expand All @@ -434,7 +446,7 @@ var queryDelayGreaterThanOrEqualToDefaultValidationRule = govy.NewRule(func(spec
)
}
return nil
}).WithErrorCode(errCodeQueryDelayGreaterThanOrEqualToDefault)
}).WithErrorCode(errCodeQueryDelayOutOfBounds)

// newURLValidator is a helper construct for Agent which only have a simple 'url' field govy.
func newURLValidator[S any](getter govy.PropertyGetter[string, S]) govy.Validator[S] {
Expand Down
21 changes: 17 additions & 4 deletions manifest/v1alpha/agent/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,29 @@ func TestValidateSpec_QueryDelay(t *testing.T) {
})
}
})
t.Run("delay larger than max query delay", func(t *testing.T) {
t.Run("delay larger than default max query delay", func(t *testing.T) {
agent := validAgent(v1alpha.Prometheus)

agent.Spec.QueryDelay = &v1alpha.QueryDelay{Duration: v1alpha.Duration{
Value: ptr(1441),
Unit: v1alpha.Minute,
}}
err := validate(agent)
testutils.AssertContainsErrors(t, agent, err, 1, testutils.ExpectedError{
Prop: "spec.queryDelay",
Message: "must be less than or equal to 1440m",
Prop: "spec.queryDelay",
Code: errCodeQueryDelayOutOfBounds,
})
})
t.Run("delay larger than data source max query delay", func(t *testing.T) {
agent := validAgent(v1alpha.SplunkObservability)
agent.Spec.QueryDelay = &v1alpha.QueryDelay{Duration: v1alpha.Duration{
Value: ptr(16),
Unit: v1alpha.Minute,
}}
err := validate(agent)
testutils.AssertContainsErrors(t, agent, err, 1, testutils.ExpectedError{
Prop: "spec.queryDelay",
Code: errCodeQueryDelayOutOfBounds,
})
})
t.Run("delay less than default", func(t *testing.T) {
Expand All @@ -203,7 +216,7 @@ func TestValidateSpec_QueryDelay(t *testing.T) {
err := validate(agent)
testutils.AssertContainsErrors(t, agent, err, 1, testutils.ExpectedError{
Prop: "spec.queryDelay",
Code: errCodeQueryDelayGreaterThanOrEqualToDefault,
Code: errCodeQueryDelayOutOfBounds,
})
})
}
Expand Down
Loading
Loading