Skip to content

Commit

Permalink
implement compression
Browse files Browse the repository at this point in the history
Signed-off-by: Huabing Zhao <[email protected]>
  • Loading branch information
zhaohuabing committed Jan 6, 2025
1 parent 10a31f1 commit 022f70b
Show file tree
Hide file tree
Showing 18 changed files with 1,023 additions and 1 deletion.
1 change: 0 additions & 1 deletion api/v1alpha1/backendtrafficpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type BackendTrafficPolicySpec struct {
// The compression config for the http streams.
//
// +optional
// +notImplementedHide
Compression []*Compression `json:"compression,omitempty"`

// ResponseOverride defines the configuration to override specific responses with a custom one.
Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha1/envoyproxy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ const (
// EnvoyFilterCustomResponse defines the Envoy HTTP custom response filter.
EnvoyFilterCustomResponse EnvoyFilter = "envoy.filters.http.custom_response"

// EnvoyFilterCompressor defines the Envoy HTTP compressor filter.
EnvoyFilterCompressor EnvoyFilter = "envoy.filters.http.compressor"

// EnvoyFilterRouter defines the Envoy HTTP router filter.
EnvoyFilterRouter EnvoyFilter = "envoy.filters.http.router"
)
Expand Down
17 changes: 17 additions & 0 deletions internal/gatewayapi/backendtrafficpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(
ds *ir.DNS
h2 *ir.HTTP2Settings
ro *ir.ResponseOverride
cp *ir.Compression
err, errs error
)

Expand Down Expand Up @@ -354,6 +355,7 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(
err = perr.WithMessage(err, "ResponseOverride")
errs = errors.Join(errs, err)
}
cp = buildCompression(policy.Spec.Compression)

ds = translateDNS(policy.Spec.ClusterSettings)

Expand Down Expand Up @@ -417,6 +419,7 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(
DNS: ds,
Timeout: to,
ResponseOverride: ro,
Compression: cp,
}

// Update the Host field in HealthCheck, now that we have access to the Route Hostname.
Expand Down Expand Up @@ -453,6 +456,7 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(
ds *ir.DNS
h2 *ir.HTTP2Settings
ro *ir.ResponseOverride
cp *ir.Compression
err, errs error
)

Expand Down Expand Up @@ -495,6 +499,7 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(
err = perr.WithMessage(err, "ResponseOverride")
errs = errors.Join(errs, err)
}
cp = buildCompression(policy.Spec.Compression)

ds = translateDNS(policy.Spec.ClusterSettings)

Expand Down Expand Up @@ -579,6 +584,7 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(
HTTP2: h2,
DNS: ds,
ResponseOverride: ro,
Compression: cp,
}

// Update the Host field in HealthCheck, now that we have access to the Route Hostname.
Expand Down Expand Up @@ -930,3 +936,14 @@ func defaultResponseOverrideRuleName(policy *egv1a1.BackendTrafficPolicy, index
irConfigName(policy),
strconv.Itoa(index))
}

func buildCompression(compression []*egv1a1.Compression) *ir.Compression {
if len(compression) == 0 {
return nil
}

// Only Gzip is supported for now, so we don't need to do anything special here
return &ir.Compression{
Type: "GZip",
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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: default
name: policy-for-route
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: httproute-1
compression:
- type: Gzip
169 changes: 169 additions & 0 deletions internal/gatewayapi/testdata/backendtrafficpolicy-compression.out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
backendTrafficPolicies:
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
creationTimestamp: null
name: policy-for-route
namespace: default
spec:
compression:
- type: Gzip
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: httproute-1
status:
ancestors:
- ancestorRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-1
namespace: envoy-gateway
sectionName: http
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
metadata:
kind: Gateway
name: gateway-1
namespace: envoy-gateway
sectionName: http
name: envoy-gateway/gateway-1/http
path:
escapedSlashesAction: UnescapeAndRedirect
mergeSlashes: true
port: 10080
routes:
- 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
metadata:
kind: HTTPRoute
name: httproute-1
namespace: default
name: httproute/default/httproute-1/rule/0/match/0/gateway_envoyproxy_io
pathMatch:
distinct: false
name: ""
prefix: /
traffic:
compression:
type: GZip
9 changes: 9 additions & 0 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,13 @@ type HeaderBasedSessionPersistence struct {
Name string `json:"name"`
}

// Compression holds the configuration for HTTP compression.
// Currently, only the default compressor(gzip) is supported.
// +k8s:deepcopy-gen=true
type Compression struct {
Type string `json:"type" yaml:"type"`
}

// TrafficFeatures holds the information associated with the Backend Traffic Policy.
// +k8s:deepcopy-gen=true
type TrafficFeatures struct {
Expand Down Expand Up @@ -762,6 +769,8 @@ type TrafficFeatures struct {
DNS *DNS `json:"dns,omitempty" yaml:"dns,omitempty"`
// ResponseOverride defines the schema for overriding the response.
ResponseOverride *ResponseOverride `json:"responseOverride,omitempty" yaml:"responseOverride,omitempty"`
// Compression settings for HTTP Response
Compression *Compression `json:"compression,omitempty" yaml:"compression,omitempty"`
}

func (b *TrafficFeatures) Validate() error {
Expand Down
20 changes: 20 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.

Loading

0 comments on commit 022f70b

Please sign in to comment.