Skip to content

Commit

Permalink
group xds backend traffic features for BackendTrafficPolicy
Browse files Browse the repository at this point in the history
Signed-off-by: shawnh2 <shawnhxh@outlook.com>
  • Loading branch information
shawnh2 committed Apr 14, 2024
1 parent 231b3b4 commit a8d5173
Show file tree
Hide file tree
Showing 54 changed files with 1,084 additions and 948 deletions.
100 changes: 42 additions & 58 deletions internal/gatewayapi/backendtrafficpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(policy *egv1a1.Backen
}
}

for _, ir := range xdsIR {
for _, tcp := range ir.TCP {
for _, x := range xdsIR {
for _, tcp := range x.TCP {
if strings.HasPrefix(tcp.Destination.Name, prefix) {
tcp.LoadBalancer = lb
tcp.ProxyProtocol = pp
Expand All @@ -360,34 +360,37 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(policy *egv1a1.Backen
}
}

for _, udp := range ir.UDP {
for _, udp := range x.UDP {
if strings.HasPrefix(udp.Destination.Name, prefix) {
udp.LoadBalancer = lb
udp.Timeout = to
}
}

for _, http := range ir.HTTP {
for _, http := range x.HTTP {
for _, r := range http.Routes {
// Apply if there is a match
if strings.HasPrefix(r.Name, prefix) {
r.RateLimit = rl
r.LoadBalancer = lb
r.ProxyProtocol = pp
r.HealthCheck = hc
r.BackendTraffic = &ir.BackendTrafficFeatures{
RateLimit: rl,
LoadBalancer: lb,
ProxyProtocol: pp,
HealthCheck: hc,
CircuitBreaker: cb,
FaultInjection: fi,
TCPKeepalive: ka,
Retry: rt,
}

// Update the Host field in HealthCheck, now that we have access to the Route Hostname.
r.HealthCheck.SetHTTPHostIfAbsent(r.Hostname)
r.CircuitBreaker = cb
r.FaultInjection = fi
r.TCPKeepalive = ka
r.Retry = rt
r.BackendTraffic.HealthCheck.SetHTTPHostIfAbsent(r.Hostname)

// some timeout setting originate from the route
// Some timeout setting originate from the route
if policy.Spec.Timeout != nil {
if to, err = t.buildTimeout(policy, r); err != nil {
return errors.Wrap(err, "Timeout")
}
r.Timeout = to
r.BackendTraffic.Timeout = to
}
}
}
Expand Down Expand Up @@ -448,7 +451,7 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
// set by a policy attaching to the route
irKey := t.getIRKey(gateway.Gateway)
// Should exist since we've validated this
ir := xdsIR[irKey]
x := xdsIR[irKey]

policyTarget := irStringKey(
string(ptr.Deref(policy.Spec.TargetRef.Namespace, gwv1a2.Namespace(policy.Namespace))),
Expand All @@ -461,7 +464,7 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
}
}

for _, tcp := range ir.TCP {
for _, tcp := range x.TCP {
gatewayName := tcp.Name[0:strings.LastIndex(tcp.Name, "/")]
if t.MergeGateways && gatewayName != policyTarget {
continue
Expand All @@ -485,7 +488,7 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
}
}

for _, udp := range ir.UDP {
for _, udp := range x.UDP {
gatewayName := udp.Name[0:strings.LastIndex(udp.Name, "/")]
if t.MergeGateways && gatewayName != policyTarget {
continue
Expand All @@ -502,7 +505,7 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
}
}

for _, http := range ir.HTTP {
for _, http := range x.HTTP {
gatewayName := http.Name[0:strings.LastIndex(http.Name, "/")]
if t.MergeGateways && gatewayName != policyTarget {
continue
Expand All @@ -513,52 +516,29 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
for _, r := range http.Routes {
// If any of the features are already set, it means that a more specific
// policy(targeting xRoute) has already set it, so we skip it.
// TODO: zhaohuabing group the features into a struct and check if all of them are set
if r.RateLimit != nil || r.LoadBalancer != nil ||
r.ProxyProtocol != nil || r.HealthCheck != nil ||
r.CircuitBreaker != nil || r.FaultInjection != nil ||
r.TCPKeepalive != nil || r.Retry != nil ||
r.Timeout != nil {
if r.BackendTraffic != nil {
continue
}

// Apply if not already set
if r.RateLimit == nil {
r.RateLimit = rl
}
if r.LoadBalancer == nil {
r.LoadBalancer = lb
}
if r.ProxyProtocol == nil {
r.ProxyProtocol = pp
}
if r.HealthCheck == nil {
r.HealthCheck = hc
// Update the Host field in HealthCheck, now that we have access to the Route Hostname.
r.HealthCheck.SetHTTPHostIfAbsent(r.Hostname)
r.BackendTraffic = &ir.BackendTrafficFeatures{
RateLimit: rl,
LoadBalancer: lb,
ProxyProtocol: pp,
HealthCheck: hc,
CircuitBreaker: cb,
FaultInjection: fi,
TCPKeepalive: ka,
Retry: rt,
}

if r.CircuitBreaker == nil {
r.CircuitBreaker = cb
}
if r.FaultInjection == nil {
r.FaultInjection = fi
}
if r.TCPKeepalive == nil {
r.TCPKeepalive = ka
}
if r.Retry == nil {
r.Retry = rt
}
// Update the Host field in HealthCheck, now that we have access to the Route Hostname.
r.BackendTraffic.HealthCheck.SetHTTPHostIfAbsent(r.Hostname)

if policy.Spec.Timeout != nil {
if ct, err = t.buildTimeout(policy, r); err != nil {
return errors.Wrap(err, "Timeout")
}

if r.Timeout == nil {
r.Timeout = ct
}
r.BackendTraffic.Timeout = ct
}
}
}
Expand Down Expand Up @@ -1051,13 +1031,17 @@ func (t *Translator) buildTimeout(policy *egv1a1.BackendTrafficPolicy, r *ir.HTT

// http request timeout is translated during the gateway-api route resource translation
// merge route timeout setting with backendtrafficpolicy timeout settings
if r != nil && r.Timeout != nil && r.Timeout.HTTP != nil && r.Timeout.HTTP.RequestTimeout != nil {
if r != nil &&
r.BackendTraffic != nil &&
r.BackendTraffic.Timeout != nil &&
r.BackendTraffic.Timeout.HTTP != nil &&
r.BackendTraffic.Timeout.HTTP.RequestTimeout != nil {
if hto == nil {
hto = &ir.HTTPTimeout{
RequestTimeout: r.Timeout.HTTP.RequestTimeout,
RequestTimeout: r.BackendTraffic.Timeout.HTTP.RequestTimeout,
}
} else {
hto.RequestTimeout = r.Timeout.HTTP.RequestTimeout
hto.RequestTimeout = r.BackendTraffic.Timeout.HTTP.RequestTimeout
}
}

Expand Down
16 changes: 11 additions & 5 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ func processTimeout(irRoute *ir.HTTPRoute, rule gwapiv1.HTTPRouteRule) {
var rto *ir.Timeout

// Timeout is translated from multiple resources and may already be partially set
if irRoute.Timeout != nil {
rto = irRoute.Timeout.DeepCopy()
if irRoute.BackendTraffic != nil && irRoute.BackendTraffic.Timeout != nil {
rto = irRoute.BackendTraffic.Timeout.DeepCopy()
} else {
rto = &ir.Timeout{}
}
Expand All @@ -257,7 +257,9 @@ func processTimeout(irRoute *ir.HTTPRoute, rule gwapiv1.HTTPRouteRule) {
setRequestTimeout(rto, metav1.Duration{Duration: d})
}

irRoute.Timeout = rto
irRoute.BackendTraffic = &ir.BackendTrafficFeatures{
Timeout: rto,
}
}
}

Expand Down Expand Up @@ -655,14 +657,18 @@ func (t *Translator) processHTTPRouteParentRefListener(route RouteContext, route
URLRewrite: routeRoute.URLRewrite,
Mirrors: routeRoute.Mirrors,
ExtensionRefs: routeRoute.ExtensionRefs,
Timeout: routeRoute.Timeout,
Retry: routeRoute.Retry,
IsHTTP2: routeRoute.IsHTTP2,
}
// Don't bother copying over the weights unless the route has invalid backends.
if routeRoute.BackendWeights.Invalid > 0 {
hostRoute.BackendWeights = routeRoute.BackendWeights
}
if routeRoute.BackendTraffic != nil {
hostRoute.BackendTraffic = &ir.BackendTrafficFeatures{
Timeout: routeRoute.BackendTraffic.Timeout,
Retry: routeRoute.BackendTraffic.Retry,
}
}
perHostRoutes = append(perHostRoutes, hostRoute)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ xdsIR:
mergeSlashes: true
port: 10080
routes:
- backendWeights:
- backendTraffic:
loadBalancer:
consistentHash:
sourceIP: true
backendWeights:
invalid: 0
valid: 0
destination:
Expand All @@ -234,15 +238,21 @@ xdsIR:
weight: 1
hostname: gateway.envoyproxy.io
isHTTP2: false
loadBalancer:
consistentHash:
sourceIP: true
name: httproute/default/httproute-1/rule/0/match/0/gateway_envoyproxy_io
pathMatch:
distinct: false
name: ""
prefix: /foo
- backendWeights:
- backendTraffic:
loadBalancer:
random: {}
timeout:
http:
connectionIdleTimeout: 21s
maxConnectionDuration: 22s
tcp:
connectTimeout: 20s
backendWeights:
invalid: 0
valid: 0
destination:
Expand All @@ -256,16 +266,8 @@ xdsIR:
weight: 1
hostname: gateway.envoyproxy.io
isHTTP2: false
loadBalancer:
random: {}
name: httproute/default/httproute-2/rule/0/match/0/gateway_envoyproxy_io
pathMatch:
distinct: false
name: ""
prefix: /bar
timeout:
http:
connectionIdleTimeout: 21s
maxConnectionDuration: 22s
tcp:
connectTimeout: 20s
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ xdsIR:
mergeSlashes: true
port: 10080
routes:
- backendWeights:
- backendTraffic: {}
backendWeights:
invalid: 1
valid: 0
directResponse:
Expand All @@ -607,7 +608,8 @@ xdsIR:
mergeSlashes: true
port: 10080
routes:
- backendWeights:
- backendTraffic: {}
backendWeights:
invalid: 1
valid: 0
directResponse:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,15 @@ xdsIR:
mergeSlashes: true
port: 10080
routes:
- backendWeights:
- backendTraffic:
faultInjection:
abort:
grpcStatus: 14
percentage: 100
delay:
fixedDelay: 5.4s
percentage: 80
backendWeights:
invalid: 0
valid: 0
destination:
Expand All @@ -354,13 +362,6 @@ xdsIR:
port: 8080
protocol: GRPC
weight: 1
faultInjection:
abort:
grpcStatus: 14
percentage: 100
delay:
fixedDelay: 5.4s
percentage: 80
hostname: '*'
isHTTP2: true
name: grpcroute/default/grpcroute-1/rule/0/match/-1/*
Expand All @@ -379,7 +380,12 @@ xdsIR:
mergeSlashes: true
port: 10080
routes:
- backendWeights:
- backendTraffic:
faultInjection:
abort:
httpStatus: 14
percentage: 0.01
backendWeights:
invalid: 0
valid: 0
destination:
Expand All @@ -391,18 +397,22 @@ xdsIR:
port: 8080
protocol: HTTP
weight: 1
faultInjection:
abort:
httpStatus: 14
percentage: 0.01
hostname: gateway.envoyproxy.io
isHTTP2: false
name: httproute/default/httproute-2/rule/0/match/0/gateway_envoyproxy_io
pathMatch:
distinct: false
name: ""
prefix: /route2
- backendWeights:
- backendTraffic:
faultInjection:
abort:
httpStatus: 500
percentage: 100
delay:
fixedDelay: 5.4s
percentage: 80
backendWeights:
invalid: 0
valid: 0
destination:
Expand All @@ -414,13 +424,6 @@ xdsIR:
port: 8080
protocol: HTTP
weight: 1
faultInjection:
abort:
httpStatus: 500
percentage: 100
delay:
fixedDelay: 5.4s
percentage: 80
hostname: gateway.envoyproxy.io
isHTTP2: false
name: httproute/default/httproute-1/rule/0/match/0/gateway_envoyproxy_io
Expand Down
Loading

0 comments on commit a8d5173

Please sign in to comment.