Skip to content

Commit

Permalink
feat Route Timeouts
Browse files Browse the repository at this point in the history
Fixes: envoyproxy#877

Signed-off-by: Arko Dasgupta <arko@tetrate.io>
  • Loading branch information
arkodg committed Sep 19, 2023
1 parent edaeefc commit d8f1487
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
23 changes: 23 additions & 0 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ package gatewayapi
import (
"fmt"
"strings"
"time"

"google.golang.org/protobuf/types/known/durationpb"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/gateway-api/apis/v1alpha2"
"sigs.k8s.io/gateway-api/apis/v1beta1"
Expand Down Expand Up @@ -185,6 +187,22 @@ func (t *Translator) processHTTPRouteRules(httpRoute *HTTPRouteContext, parentRe
return routeRoutes
}

func processTimeout(irRoute *ir.HTTPRoute, rule v1beta1.HTTPRouteRule) {
if rule.Timeouts != nil {
if rule.Timeouts.Request != nil {
// TODO: handle parse errors
d, _ := time.ParseDuration(string(*rule.Timeouts.Request))
irRoute.Timeout = durationpb.New(d)
}

if rule.Timeouts.BackendRequest != nil {
// TODO: handle parse errors
d, _ := time.ParseDuration(string(*rule.Timeouts.BackendRequest))
irRoute.PerTryTimeout = durationpb.New(d)
}
}
}

func (t *Translator) processHTTPRouteRule(httpRoute *HTTPRouteContext, ruleIdx int, httpFiltersContext *HTTPFiltersContext, rule v1beta1.HTTPRouteRule) []*ir.HTTPRoute {
var ruleRoutes []*ir.HTTPRoute

Expand All @@ -193,6 +211,7 @@ func (t *Translator) processHTTPRouteRule(httpRoute *HTTPRouteContext, ruleIdx i
irRoute := &ir.HTTPRoute{
Name: irRouteName(httpRoute, ruleIdx, -1),
}
processTimeout(irRoute, rule)
applyHTTPFiltersContextToIRRoute(httpFiltersContext, irRoute)
ruleRoutes = append(ruleRoutes, irRoute)
}
Expand All @@ -204,6 +223,7 @@ func (t *Translator) processHTTPRouteRule(httpRoute *HTTPRouteContext, ruleIdx i
irRoute := &ir.HTTPRoute{
Name: irRouteName(httpRoute, ruleIdx, matchIdx),
}
processTimeout(irRoute, rule)

if match.Path != nil {
switch PathMatchTypeDerefOr(match.Path.Type, v1beta1.PathMatchPathPrefix) {
Expand Down Expand Up @@ -295,6 +315,7 @@ func applyHTTPFiltersContextToIRRoute(httpFiltersContext *HTTPFiltersContext, ir
if httpFiltersContext.RateLimit != nil {
irRoute.RateLimit = httpFiltersContext.RateLimit
}

if len(httpFiltersContext.ExtensionRefs) > 0 {
irRoute.ExtensionRefs = httpFiltersContext.ExtensionRefs
}
Expand Down Expand Up @@ -536,6 +557,8 @@ func (t *Translator) processHTTPRouteParentRefListener(route RouteContext, route
Mirrors: routeRoute.Mirrors,
RequestAuthentication: routeRoute.RequestAuthentication,
RateLimit: routeRoute.RateLimit,
Timeout: routeRoute.Timeout,
PerTryTimeout: routeRoute.PerTryTimeout,
ExtensionRefs: routeRoute.ExtensionRefs,
}
// Don't bother copying over the weights unless the route has invalid backends.
Expand Down
5 changes: 5 additions & 0 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/tetratelabs/multierror"
"golang.org/x/exp/slices"

"google.golang.org/protobuf/types/known/durationpb"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

Expand Down Expand Up @@ -273,6 +274,10 @@ type HTTPRoute struct {
RateLimit *RateLimit `json:"rateLimit,omitempty" yaml:"rateLimit,omitempty"`
// RequestAuthentication defines the schema for authenticating HTTP requests.
RequestAuthentication *RequestAuthentication `json:"requestAuthentication,omitempty" yaml:"requestAuthentication,omitempty"`
// Timeout is the time until which entire response is received from the upstream.
Timeout *durationpb.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
// PerTryTimeout is the timeout per retry attempt. Number of retries defaults to 1.
PerTryTimeout *durationpb.Duration `json:"perTryTimeout,omitempty" yaml:"perTryTimeout,omitempty"`
// ExtensionRefs holds unstructured resources that were introduced by an extension and used on the HTTPRoute as extensionRef filters
ExtensionRefs []*UnstructuredRef `json:"extensionRefs,omitempty" yaml:"extensionRefs,omitempty"`
}
Expand Down
11 changes: 11 additions & 0 deletions internal/xds/translator/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ func buildXdsRoute(httpRoute *ir.HTTPRoute) *routev3.Route {
}
}

// Timeouts
if httpRoute.Timeout != nil {
router.GetRoute().Timeout = httpRoute.Timeout
}

if httpRoute.PerTryTimeout != nil {
router.GetRoute().RetryPolicy = &routev3.RetryPolicy{
PerTryTimeout: httpRoute.PerTryTimeout,
}
}

// TODO: Convert this into a generic interface for API Gateway features.
// https://github.com/envoyproxy/gateway/issues/882
if err := patchRouteWithRateLimit(router.GetRoute(), httpRoute); err != nil {
Expand Down

0 comments on commit d8f1487

Please sign in to comment.