Skip to content

Commit

Permalink
feat: implement RequestTimeout in BackendTrafficPolicy (#4329)
Browse files Browse the repository at this point in the history
* feat: implement RequestTimeout in BackendTrafficPolicy

Signed-off-by: Kensei Nakada <handbomusic@gmail.com>

* fix: the timeout on HTTPRoute overwrites the timeout on BTP

Signed-off-by: Kensei Nakada <handbomusic@gmail.com>

* fix: regenerate from the latest api spec

Signed-off-by: Kensei Nakada <handbomusic@gmail.com>

---------

Signed-off-by: Kensei Nakada <handbomusic@gmail.com>
  • Loading branch information
sanposhiho authored Oct 15, 2024
1 parent 8fc4ecb commit 5880d6b
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 9 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/timeout_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ type HTTPTimeout struct {
//
// +optional
MaxConnectionDuration *gwapiv1.Duration `json:"maxConnectionDuration,omitempty"`

// RequestTimeout is the time until which entire response is received from the upstream.
//
// +optional
RequestTimeout *gwapiv1.Duration `json:"requestTimeout,omitempty" yaml:"requestTimeout,omitempty"`
}

type ClientTimeout struct {
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,11 @@ spec:
Default: unlimited.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
requestTimeout:
description: RequestTimeout is the time until which entire
response is received from the upstream.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
type: object
tcp:
description: Timeout settings for TCP.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,11 @@ spec:
Default: unlimited.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
requestTimeout:
description: RequestTimeout is the time until which
entire response is received from the upstream.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
type: object
tcp:
description: Timeout settings for TCP.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11220,6 +11220,13 @@ spec:
Default: unlimited.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
requestTimeout:
description: RequestTimeout is
the time until which entire
response is received from the
upstream.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
type: object
tcp:
description: Timeout settings for
Expand Down Expand Up @@ -12190,6 +12197,13 @@ spec:
Default: unlimited.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
requestTimeout:
description: RequestTimeout is
the time until which entire
response is received from the
upstream.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
type: object
tcp:
description: Timeout settings for
Expand Down Expand Up @@ -13220,6 +13234,12 @@ spec:
Default: unlimited.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
requestTimeout:
description: RequestTimeout is the time
until which entire response is received
from the upstream.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
type: object
tcp:
description: Timeout settings for TCP.
Expand Down Expand Up @@ -14195,6 +14215,12 @@ spec:
Default: unlimited.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
requestTimeout:
description: RequestTimeout is the time until
which entire response is received from the
upstream.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
type: object
tcp:
description: Timeout settings for TCP.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,11 @@ spec:
Default: unlimited.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
requestTimeout:
description: RequestTimeout is the time until
which entire response is received from the upstream.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
type: object
tcp:
description: Timeout settings for TCP.
Expand Down Expand Up @@ -2060,6 +2065,11 @@ spec:
Default: unlimited.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
requestTimeout:
description: RequestTimeout is the time until
which entire response is received from the upstream.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
type: object
tcp:
description: Timeout settings for TCP.
Expand Down Expand Up @@ -3214,6 +3224,11 @@ spec:
Default: unlimited.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
requestTimeout:
description: RequestTimeout is the time until
which entire response is received from the upstream.
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
type: object
tcp:
description: Timeout settings for TCP.
Expand Down
27 changes: 18 additions & 9 deletions internal/gatewayapi/clustersettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ func translateTrafficFeatures(policy *egv1a1.ClusterSettings) (*ir.TrafficFeatur
return ret, nil
}

func buildClusterSettingsTimeout(policy egv1a1.ClusterSettings, traffic *ir.TrafficFeatures) (*ir.Timeout, error) {
func buildClusterSettingsTimeout(policy egv1a1.ClusterSettings, routeTrafficFeatures *ir.TrafficFeatures) (*ir.Timeout, error) {
if policy.Timeout == nil {
if traffic != nil {
if routeTrafficFeatures != nil {
// Don't lose any existing timeout definitions.
return mergeTimeoutSettings(nil, traffic.Timeout), nil
return mergeTimeoutSettings(nil, routeTrafficFeatures.Timeout), nil
}
return nil, nil
}
Expand All @@ -109,6 +109,7 @@ func buildClusterSettingsTimeout(policy egv1a1.ClusterSettings, traffic *ir.Traf
if pto.HTTP != nil {
var cit *metav1.Duration
var mcd *metav1.Duration
var rt *metav1.Duration

if pto.HTTP.ConnectionIdleTimeout != nil {
d, err := time.ParseDuration(string(*pto.HTTP.ConnectionIdleTimeout))
Expand All @@ -128,19 +129,27 @@ func buildClusterSettingsTimeout(policy egv1a1.ClusterSettings, traffic *ir.Traf
}
}

if pto.HTTP.RequestTimeout != nil {
d, err := time.ParseDuration(string(*pto.HTTP.RequestTimeout))
if err != nil {
errs = errors.Join(errs, fmt.Errorf("invalid RequestTimeout value %s", *pto.HTTP.RequestTimeout))
} else {
rt = ptr.To(metav1.Duration{Duration: d})
}
}

to.HTTP = &ir.HTTPTimeout{
ConnectionIdleTimeout: cit,
MaxConnectionDuration: mcd,
RequestTimeout: rt,
}
}

// http request timeout is translated during the gateway-api route resource translation
// merge route timeout setting with backendtrafficpolicy timeout settings.
// Merging is done after the clustersettings definitions are translated so that
// clustersettings will override previous settings.
if traffic != nil {
to = mergeTimeoutSettings(to, traffic.Timeout)
// The timeout from route's TrafficFeatures takes precedence over the timeout in BTP
if routeTrafficFeatures != nil {
to = mergeTimeoutSettings(routeTrafficFeatures.Timeout, to)
}

return to, errs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ httpRoutes:
backendRefs:
- name: service-1
port: 8080
timeouts:
request: 1s
backendTrafficPolicies:
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
Expand All @@ -79,6 +81,7 @@ backendTrafficPolicies:
http:
connectionIdleTimeout: 16s
maxConnectionDuration: 17s
requestTimeout: 18s
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
Expand All @@ -95,3 +98,4 @@ backendTrafficPolicies:
http:
connectionIdleTimeout: 21s
maxConnectionDuration: 22s
requestTimeout: 23s
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ backendTrafficPolicies:
http:
connectionIdleTimeout: 21s
maxConnectionDuration: 22s
requestTimeout: 23s
tcp:
connectTimeout: 20s
status:
Expand Down Expand Up @@ -46,6 +47,7 @@ backendTrafficPolicies:
http:
connectionIdleTimeout: 16s
maxConnectionDuration: 17s
requestTimeout: 18s
tcp:
connectTimeout: 15s
status:
Expand Down Expand Up @@ -198,6 +200,8 @@ httpRoutes:
matches:
- path:
value: /
timeouts:
request: 1s
status:
parents:
- conditions:
Expand Down Expand Up @@ -289,6 +293,7 @@ xdsIR:
http:
connectionIdleTimeout: 16s
maxConnectionDuration: 17s
requestTimeout: 18s
tcp:
connectTimeout: 15s
envoy-gateway/gateway-2:
Expand Down Expand Up @@ -336,5 +341,6 @@ xdsIR:
http:
connectionIdleTimeout: 21s
maxConnectionDuration: 22s
requestTimeout: 1s
tcp:
connectTimeout: 20s
1 change: 1 addition & 0 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,7 @@ _Appears in:_
| --- | --- | --- | --- |
| `connectionIdleTimeout` | _[Duration](https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.Duration)_ | false | The idle timeout for an HTTP connection. Idle time is defined as a period in which there are no active requests in the connection.<br />Default: 1 hour. |
| `maxConnectionDuration` | _[Duration](https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.Duration)_ | false | The maximum duration of an HTTP connection.<br />Default: unlimited. |
| `requestTimeout` | _[Duration](https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.Duration)_ | false | RequestTimeout is the time until which entire response is received from the upstream. |


#### HTTPURLRewriteFilter
Expand Down
1 change: 1 addition & 0 deletions site/content/zh/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,7 @@ _Appears in:_
| --- | --- | --- | --- |
| `connectionIdleTimeout` | _[Duration](https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.Duration)_ | false | The idle timeout for an HTTP connection. Idle time is defined as a period in which there are no active requests in the connection.<br />Default: 1 hour. |
| `maxConnectionDuration` | _[Duration](https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.Duration)_ | false | The maximum duration of an HTTP connection.<br />Default: unlimited. |
| `requestTimeout` | _[Duration](https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.Duration)_ | false | RequestTimeout is the time until which entire response is received from the upstream. |


#### HTTPURLRewriteFilter
Expand Down

0 comments on commit 5880d6b

Please sign in to comment.