Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement useClientProtocol #3254

Merged
merged 8 commits into from
Apr 25, 2024
10 changes: 10 additions & 0 deletions internal/gatewayapi/backendtrafficpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(policy *egv1a1.Backen
}
r.Timeout = to
}

if policy.Spec.UseClientProtocol != nil {
r.UseClientProtocol = policy.Spec.UseClientProtocol
}
}
}
}
Expand Down Expand Up @@ -560,6 +564,12 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
r.Timeout = ct
}
}

if policy.Spec.UseClientProtocol != nil {
if r.UseClientProtocol == nil {
r.UseClientProtocol = policy.Spec.UseClientProtocol
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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
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-1
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
useClientProtocol: true

Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
backendTrafficPolicies:
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
creationTimestamp: null
name: policy-for-gateway
namespace: envoy-gateway
spec:
targetRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-1
namespace: envoy-gateway
useClientProtocol: true
status:
ancestors:
- ancestorRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-1
namespace: envoy-gateway
conditions:
- lastTransitionTime: null
message: Policy has been accepted.
reason: Accepted
status: "True"
type: Accepted
controllerName: gateway.envoyproxy.io/gatewayclass-controller
gateways:
- apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
creationTimestamp: null
name: gateway-1
namespace: envoy-gateway
spec:
gatewayClassName: envoy-gateway-class
listeners:
- allowedRoutes:
namespaces:
from: All
name: http
port: 80
protocol: HTTP
status:
listeners:
- attachedRoutes: 1
conditions:
- lastTransitionTime: null
message: Sending translated listener configuration to the data plane
reason: Programmed
status: "True"
type: Programmed
- lastTransitionTime: null
message: Listener has been successfully translated
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: null
message: Listener references have been resolved
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
name: http
supportedKinds:
- group: gateway.networking.k8s.io
kind: HTTPRoute
- group: gateway.networking.k8s.io
kind: GRPCRoute
httpRoutes:
- apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
creationTimestamp: null
name: httproute-1
namespace: default
spec:
hostnames:
- gateway.envoyproxy.io
parentRefs:
- name: gateway-1
namespace: envoy-gateway
sectionName: http
rules:
- backendRefs:
- name: service-1
port: 8080
matches:
- path:
value: /
status:
parents:
- conditions:
- lastTransitionTime: null
message: Route is accepted
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: null
message: Resolved all the Object references for the Route
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
controllerName: gateway.envoyproxy.io/gatewayclass-controller
parentRef:
name: gateway-1
namespace: envoy-gateway
sectionName: http
infraIR:
envoy-gateway/gateway-1:
proxy:
listeners:
- address: null
name: envoy-gateway/gateway-1/http
ports:
- containerPort: 10080
name: http-80
protocol: HTTP
servicePort: 80
metadata:
labels:
gateway.envoyproxy.io/owning-gateway-name: gateway-1
gateway.envoyproxy.io/owning-gateway-namespace: envoy-gateway
name: envoy-gateway/gateway-1
xdsIR:
envoy-gateway/gateway-1:
accessLog:
text:
- path: /dev/stdout
http:
- address: 0.0.0.0
hostnames:
- '*'
isHTTP2: false
name: envoy-gateway/gateway-1/http
path:
escapedSlashesAction: UnescapeAndRedirect
mergeSlashes: true
port: 10080
routes:
- backendWeights:
invalid: 0
valid: 0
destination:
name: httproute/default/httproute-1/rule/0
settings:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
weight: 1
hostname: gateway.envoyproxy.io
isHTTP2: false
name: httproute/default/httproute-1/rule/0/match/0/gateway_envoyproxy_io
pathMatch:
distinct: false
name: ""
prefix: /
useClientProtocol: true
2 changes: 2 additions & 0 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ type HTTPRoute struct {

// Security holds the features associated with SecurityPolicy
Security *SecurityFeatures `json:"security,omitempty" yaml:"security,omitempty"`
// UseClientProtocol enables using the same protocol upstream that was used downstream
UseClientProtocol *bool `json:"useClientProtocol,omitempty" yaml:"useClientProtocol,omitempty"`
}

// SecurityFeatures holds the information associated with the Security Policy.
Expand Down
5 changes: 5 additions & 0 deletions internal/ir/zz_generated.deepcopy.go

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

69 changes: 39 additions & 30 deletions internal/xds/translator/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ const (
)

type xdsClusterArgs struct {
name string
settings []*ir.DestinationSetting
tSocket *corev3.TransportSocket
endpointType EndpointType
loadBalancer *ir.LoadBalancer
proxyProtocol *ir.ProxyProtocol
circuitBreaker *ir.CircuitBreaker
healthCheck *ir.HealthCheck
http1Settings *ir.HTTP1Settings
timeout *ir.Timeout
tcpkeepalive *ir.TCPKeepalive
metrics *ir.Metrics
name string
settings []*ir.DestinationSetting
tSocket *corev3.TransportSocket
endpointType EndpointType
loadBalancer *ir.LoadBalancer
proxyProtocol *ir.ProxyProtocol
circuitBreaker *ir.CircuitBreaker
healthCheck *ir.HealthCheck
http1Settings *ir.HTTP1Settings
timeout *ir.Timeout
tcpkeepalive *ir.TCPKeepalive
metrics *ir.Metrics
useClientProtocol bool
}

type EndpointType int
Expand Down Expand Up @@ -447,7 +448,7 @@ func buildTypedExtensionProtocolOptions(args *xdsClusterArgs) map[string]*anypb.

requiresHTTP1Options := args.http1Settings != nil && (args.http1Settings.EnableTrailers || args.http1Settings.PreserveHeaderCase || args.http1Settings.HTTP10 != nil)

if !(requiresCommonHTTPOptions || requiresHTTP1Options || requiresHTTP2Options) {
if !(requiresCommonHTTPOptions || requiresHTTP1Options || requiresHTTP2Options || args.useClientProtocol) {
return nil
}

Expand All @@ -473,25 +474,11 @@ func buildTypedExtensionProtocolOptions(args *xdsClusterArgs) map[string]*anypb.
Value: *args.circuitBreaker.MaxRequestsPerConnection,
}
}

}

// When setting any Typed Extension Protocol Options, UpstreamProtocolOptions are mandatory
// If translation requires HTTP2 enablement or HTTP1 trailers, set appropriate setting
// Default to http1 otherwise
// TODO: If the cluster is TLS enabled, use AutoHTTPConfig instead of ExplicitHttpConfig
// so that when ALPN is supported then enabling http1 options doesn't force HTTP/1.1
switch {
case requiresHTTP2Options:
protocolOptions.UpstreamProtocolOptions = &httpv3.HttpProtocolOptions_ExplicitHttpConfig_{
ExplicitHttpConfig: &httpv3.HttpProtocolOptions_ExplicitHttpConfig{
ProtocolConfig: &httpv3.HttpProtocolOptions_ExplicitHttpConfig_Http2ProtocolOptions{},
},
}
case requiresHTTP1Options:
http1opts := &corev3.Http1ProtocolOptions{
EnableTrailers: args.http1Settings.EnableTrailers,
}
http1opts := &corev3.Http1ProtocolOptions{}
if args.http1Settings != nil {
http1opts.EnableTrailers = args.http1Settings.EnableTrailers
if args.http1Settings.PreserveHeaderCase {
preservecaseAny, _ := anypb.New(&preservecasev3.PreserveCaseFormatterConfig{})
http1opts.HeaderKeyFormat = &corev3.Http1ProtocolOptions_HeaderKeyFormat{
Expand All @@ -507,6 +494,28 @@ func buildTypedExtensionProtocolOptions(args *xdsClusterArgs) map[string]*anypb.
http1opts.AcceptHttp_10 = true
http1opts.DefaultHostForHttp_10 = ptr.Deref(args.http1Settings.HTTP10.DefaultHost, "")
}
}

// When setting any Typed Extension Protocol Options, UpstreamProtocolOptions are mandatory
// If translation requires HTTP2 enablement or HTTP1 trailers, set appropriate setting
// Default to http1 otherwise
// TODO: If the cluster is TLS enabled, use AutoHTTPConfig instead of ExplicitHttpConfig
// so that when ALPN is supported then enabling http1 options doesn't force HTTP/1.1
switch {
case args.useClientProtocol:
protocolOptions.UpstreamProtocolOptions = &httpv3.HttpProtocolOptions_UseDownstreamProtocolConfig{
UseDownstreamProtocolConfig: &httpv3.HttpProtocolOptions_UseDownstreamHttpConfig{
HttpProtocolOptions: http1opts,
Http2ProtocolOptions: &corev3.Http2ProtocolOptions{},
},
}
case requiresHTTP2Options:
protocolOptions.UpstreamProtocolOptions = &httpv3.HttpProtocolOptions_ExplicitHttpConfig_{
ExplicitHttpConfig: &httpv3.HttpProtocolOptions_ExplicitHttpConfig{
ProtocolConfig: &httpv3.HttpProtocolOptions_ExplicitHttpConfig_Http2ProtocolOptions{},
},
}
case requiresHTTP1Options:
protocolOptions.UpstreamProtocolOptions = &httpv3.HttpProtocolOptions_ExplicitHttpConfig_{
ExplicitHttpConfig: &httpv3.HttpProtocolOptions_ExplicitHttpConfig{
ProtocolConfig: &httpv3.HttpProtocolOptions_ExplicitHttpConfig_HttpProtocolOptions{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
http:
- address: 0.0.0.0
hostnames:
- '*'
isHTTP2: false
name: envoy-gateway/gateway-1/http
path:
escapedSlashesAction: UnescapeAndRedirect
mergeSlashes: true
port: 10080
routes:
- backendWeights:
invalid: 0
valid: 0
destination:
name: httproute/default/httproute-1/rule/0
settings:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
protocol: HTTP
weight: 1
hostname: gateway.envoyproxy.io
isHTTP2: false
name: httproute/default/httproute-1/rule/0/match/0/gateway_envoyproxy_io
pathMatch:
distinct: false
name: ""
prefix: /
useClientProtocol: true
Loading