Skip to content

Commit

Permalink
feat(translator): Implement BTP TCPKeepAlive (#2581)
Browse files Browse the repository at this point in the history
Implement BTP TCPKeepAlive

Signed-off-by: Guy Daich <guy.daich@sap.com>
  • Loading branch information
guydc authored Feb 12, 2024
1 parent bb59484 commit 2c1b946
Show file tree
Hide file tree
Showing 13 changed files with 580 additions and 1 deletion.
44 changes: 44 additions & 0 deletions internal/gatewayapi/backendtrafficpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(policy *egv1a1.Backen
cb *ir.CircuitBreaker
fi *ir.FaultInjection
to *ir.Timeout
ka *ir.TCPKeepalive
)

// Build IR
Expand All @@ -272,6 +273,9 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(policy *egv1a1.Backen
if policy.Spec.FaultInjection != nil {
fi = t.buildFaultInjection(policy)
}
if policy.Spec.TCPKeepalive != nil {
ka = t.buildTCPKeepAlive(policy)
}
// Apply IR to all relevant routes
prefix := irRoutePrefix(route)
for _, ir := range xdsIR {
Expand All @@ -285,6 +289,7 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(policy *egv1a1.Backen
r.HealthCheck = hc
r.CircuitBreaker = cb
r.FaultInjection = fi
r.TCPKeepalive = ka

// some timeout setting originate from the route
if policy.Spec.Timeout != nil {
Expand All @@ -307,6 +312,7 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
cb *ir.CircuitBreaker
fi *ir.FaultInjection
ct *ir.Timeout
ka *ir.TCPKeepalive
)

// Build IR
Expand All @@ -328,6 +334,9 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
if policy.Spec.FaultInjection != nil {
fi = t.buildFaultInjection(policy)
}
if policy.Spec.TCPKeepalive != nil {
ka = t.buildTCPKeepAlive(policy)
}

// Apply IR to all the routes within the specific Gateway
// If the feature is already set, then skip it, since it must be have
Expand Down Expand Up @@ -357,6 +366,9 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
if r.FaultInjection == nil {
r.FaultInjection = fi
}
if r.TCPKeepalive == nil {
r.TCPKeepalive = ka
}

if policy.Spec.Timeout != nil {
ct = t.buildTimeout(policy, r)
Expand Down Expand Up @@ -940,3 +952,35 @@ func (t *Translator) buildFaultInjection(policy *egv1a1.BackendTrafficPolicy) *i
}
return fi
}

func (t *Translator) buildTCPKeepAlive(policy *egv1a1.BackendTrafficPolicy) *ir.TCPKeepalive {
var ka *ir.TCPKeepalive
if policy.Spec.TCPKeepalive != nil {
pka := policy.Spec.TCPKeepalive
ka = &ir.TCPKeepalive{}

if pka.Probes != nil {
ka.Probes = pka.Probes
}

if pka.IdleTime != nil {
d, err := time.ParseDuration(string(*pka.IdleTime))
if err != nil {
setBackendTrafficPolicyTranslationErrorCondition(policy, "TCP Keep Alive", fmt.Sprintf("invalid IdleTime value %s", *pka.IdleTime))
return nil
}
ka.IdleTime = ptr.To(uint32(d.Seconds()))
}

if pka.Interval != nil {
d, err := time.ParseDuration(string(*pka.Interval))
if err != nil {
setBackendTrafficPolicyTranslationErrorCondition(policy, "TCP Keep Alive", fmt.Sprintf("invalid Interval value %s", *pka.Interval))
return nil
}
ka.Interval = ptr.To(uint32(d.Seconds()))
}

}
return ka
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
gateways:
- apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
namespace: envoy-gateway
name: gateway-1
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
- apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
namespace: envoy-gateway
name: gateway-2
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
grpcRoutes:
- apiVersion: gateway.networking.k8s.io/v1alpha2
kind: GRPCRoute
metadata:
namespace: default
name: grpcroute-1
spec:
parentRefs:
- namespace: envoy-gateway
name: gateway-1
sectionName: http
rules:
- backendRefs:
- name: service-1
port: 8080
httpRoutes:
- apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: default
name: httproute-1
spec:
hostnames:
- gateway.envoyproxy.io
parentRefs:
- namespace: envoy-gateway
name: gateway-2
sectionName: http
rules:
- matches:
- path:
value: "/"
backendRefs:
- name: service-1
port: 8080
backendTrafficPolicies:
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: envoy-gateway
name: policy-for-gateway
spec:
targetRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-1
namespace: envoy-gateway
tcpKeepalive:
probes: 3
idleTime: 20m
interval: 60s
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: default
name: policy-for-route
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: httproute-1
namespace: default
tcpKeepalive:
probes: 6
idleTime: 10s
interval: 30m
Loading

0 comments on commit 2c1b946

Please sign in to comment.