Skip to content

Commit

Permalink
validate timeouts and retries
Browse files Browse the repository at this point in the history
  • Loading branch information
rboyer committed Aug 8, 2023
1 parent 7b79030 commit bdb0089
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 31 deletions.
31 changes: 15 additions & 16 deletions internal/mesh/internal/types/grpc_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package types

import (
"errors"
"fmt"

"github.com/hashicorp/go-multierror"

Expand Down Expand Up @@ -187,23 +186,23 @@ func ValidateGRPCRoute(res *pbresource.Resource) error {
}

if rule.Timeouts != nil {
// TODO(rb): validate timeouts
for _, err := range validateHTTPTimeouts(rule.Timeouts) {
merr = multierror.Append(merr, wrapRuleErr(
resource.ErrInvalidField{
Name: "timeouts",
Wrapped: err,
},
))
}
}
if rule.Retries != nil {
// TODO(rb): validate retries
for j, condition := range rule.Retries.OnConditions {
if !isValidRetryCondition(condition) {
merr = multierror.Append(merr, wrapRuleErr(
resource.ErrInvalidListElement{
Name: "retries",
Index: j,
Wrapped: resource.ErrInvalidField{
Name: "on_conditions",
Wrapped: fmt.Errorf("not a valid retry condition: %q", condition),
},
},
))
}
for _, err := range validateHTTPRetries(rule.Retries) {
merr = multierror.Append(merr, wrapRuleErr(
resource.ErrInvalidField{
Name: "retries",
Wrapped: err,
},
))
}
}
}
Expand Down
88 changes: 73 additions & 15 deletions internal/mesh/internal/types/http_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,30 +290,88 @@ func ValidateHTTPRoute(res *pbresource.Resource) error {
}

if rule.Timeouts != nil {
// TODO(rb): validate timeouts
for _, err := range validateHTTPTimeouts(rule.Timeouts) {
merr = multierror.Append(merr, wrapRuleErr(
resource.ErrInvalidField{
Name: "timeouts",
Wrapped: err,
},
))
}
}
if rule.Retries != nil {
// TODO(rb): validate retries
for j, condition := range rule.Retries.OnConditions {
if !isValidRetryCondition(condition) {
merr = multierror.Append(merr, wrapRuleErr(
resource.ErrInvalidListElement{
Name: "retries",
Index: j,
Wrapped: resource.ErrInvalidField{
Name: "on_conditions",
Wrapped: fmt.Errorf("not a valid retry condition: %q", condition),
},
},
))
}
for _, err := range validateHTTPRetries(rule.Retries) {
merr = multierror.Append(merr, wrapRuleErr(
resource.ErrInvalidField{
Name: "retries",
Wrapped: err,
},
))
}
}
}

return merr
}

func validateHTTPTimeouts(timeouts *pbmesh.HTTPRouteTimeouts) []error {
if timeouts == nil {
return nil
}

var errs []error

if timeouts.Request != nil {
val := timeouts.Request.AsDuration()
if val < 0 {
errs = append(errs, resource.ErrInvalidField{
Name: "request",
Wrapped: fmt.Errorf("timeout cannot be negative: %v", val),
})
}
}
if timeouts.BackendRequest != nil {
val := timeouts.BackendRequest.AsDuration()
if val < 0 {
errs = append(errs, resource.ErrInvalidField{
Name: "request",
Wrapped: fmt.Errorf("timeout cannot be negative: %v", val),
})
}
}
if timeouts.Idle != nil {
val := timeouts.Idle.AsDuration()
if val < 0 {
errs = append(errs, resource.ErrInvalidField{
Name: "idle",
Wrapped: fmt.Errorf("timeout cannot be negative: %v", val),
})
}
}

return errs
}

func validateHTTPRetries(retries *pbmesh.HTTPRouteRetries) []error {
if retries == nil {
return nil
}

var errs []error

for i, condition := range retries.OnConditions {
if !isValidRetryCondition(condition) {
errs = append(errs, resource.ErrInvalidListElement{
Name: "on_conditions",
Index: i,
Wrapped: fmt.Errorf("not a valid retry condition: %q", condition),
})
}
}

return errs
}

func validateBackendRef(backendRef *pbmesh.BackendReference) []error {
var errs []error
if backendRef == nil {
Expand Down

0 comments on commit bdb0089

Please sign in to comment.