Skip to content

Commit

Permalink
feat: implement response compression (#5001)
Browse files Browse the repository at this point in the history
* implement compression

Signed-off-by: Huabing Zhao <[email protected]>

* fix lint

Signed-off-by: Huabing Zhao <[email protected]>

* fix gen

Signed-off-by: Huabing Zhao <[email protected]>

* fix gen

Signed-off-by: Huabing Zhao <[email protected]>

* fix gen

Signed-off-by: Huabing Zhao <[email protected]>

* fix lint

Signed-off-by: Huabing Zhao <[email protected]>

* add release note

Signed-off-by: Huabing Zhao <[email protected]>

* fix test

Signed-off-by: Huabing Zhao <[email protected]>

* support brotli

Signed-off-by: Huabing Zhao <[email protected]>
  • Loading branch information
zhaohuabing authored Jan 14, 2025
1 parent f6bb7f8 commit 743e5e0
Show file tree
Hide file tree
Showing 27 changed files with 1,035 additions and 9 deletions.
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
18 changes: 17 additions & 1 deletion api/v1alpha1/compression_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@ package v1alpha1

// CompressorType defines the types of compressor library supported by Envoy Gateway.
//
// +kubebuilder:validation:Enum=Gzip
// +kubebuilder:validation:Enum=Gzip;Brotli
type CompressorType string

const (
GzipCompressorType CompressorType = "Gzip"

BrotliCompressorType CompressorType = "Brotli"
)

// GzipCompressor defines the config for the Gzip compressor.
// The default values can be found here:
// https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/compression/gzip/compressor/v3/gzip.proto#extension-envoy-compression-gzip-compressor
type GzipCompressor struct{}

// BrotliCompressor defines the config for the Brotli compressor.
// The default values can be found here:
// https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/compression/brotli/compressor/v3/brotli.proto#extension-envoy-compression-brotli-compressor
type BrotliCompressor struct{}

// Compression defines the config of enabling compression.
// This can help reduce the bandwidth at the expense of higher CPU.
type Compression struct {
Expand All @@ -23,6 +34,11 @@ type Compression struct {
// +required
Type CompressorType `json:"type"`

// The configuration for Brotli compressor.
//
// +optional
Brotli *BrotliCompressor `json:"brotli,omitempty"`

// The configuration for GZIP compressor.
//
// +optional
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 @@ -245,6 +245,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
20 changes: 20 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 @@ -106,6 +106,9 @@ spec:
Compression defines the config of enabling compression.
This can help reduce the bandwidth at the expense of higher CPU.
properties:
brotli:
description: The configuration for Brotli compressor.
type: object
gzip:
description: The configuration for GZIP compressor.
type: object
Expand All @@ -114,6 +117,7 @@ spec:
for compression.
enum:
- Gzip
- Brotli
type: string
required:
- type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12497,6 +12497,9 @@ spec:
scarce and large payloads can be effectively compressed
at the expense of higher CPU load.
properties:
brotli:
description: The configuration for Brotli compressor.
type: object
gzip:
description: The configuration for GZIP compressor.
type: object
Expand All @@ -12505,6 +12508,7 @@ spec:
type to use for compression.
enum:
- Gzip
- Brotli
type: string
required:
- type
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
fortio.org/fortio v1.68.0
fortio.org/log v1.17.1
github.com/Masterminds/semver/v3 v3.3.1
github.com/andybalholm/brotli v1.0.1
github.com/cenkalti/backoff/v4 v4.3.0
github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alessio/shellescape v1.2.2/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
Expand Down
20 changes: 20 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,17 @@ func defaultResponseOverrideRuleName(policy *egv1a1.BackendTrafficPolicy, index
irConfigName(policy),
strconv.Itoa(index))
}

func buildCompression(compression []*egv1a1.Compression) []*ir.Compression {
if compression == nil {
return nil
}
irCompression := make([]*ir.Compression, 0, len(compression))
for _, c := range compression {
irCompression = append(irCompression, &ir.Compression{
Type: c.Type,
})
}

return irCompression
}
3 changes: 2 additions & 1 deletion internal/gatewayapi/clustersettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"math"
"math/big"
"net/http"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -76,7 +77,7 @@ func translateTrafficFeatures(policy *egv1a1.ClusterSettings) (*ir.TrafficFeatur
// If nothing was set in any of the above calls, return nil instead of an empty
// container
var empty ir.TrafficFeatures
if empty == *ret {
if reflect.DeepEqual(empty, *ret) {
ret = nil
}

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: default
name: policy-for-route
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: httproute-1
compression:
- type: Brotli
- type: Gzip
Loading

0 comments on commit 743e5e0

Please sign in to comment.