Skip to content

Commit 792b3a2

Browse files
committed
feat: add contentTypes config for BackendTrafficPolicy compression
Signed-off-by: zhoujinyu <2319109590@qq.com>
1 parent 265fdeb commit 792b3a2

20 files changed

Lines changed: 501 additions & 31 deletions

api/v1alpha1/compression_types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,13 @@ type Compression struct {
6868
// +kubebuilder:validation:XIntOrString
6969
// +kubebuilder:validation:Pattern="^[1-9]+[0-9]*([EPTGMK]i|[EPTGMk])?$"
7070
MinContentLength *resource.Quantity `json:"minContentLength,omitempty"`
71+
72+
// ContentTypes defines the set of response Content-Types that will trigger compression.
73+
// When set, only responses whose Content-Type matches one of these values are compressed.
74+
// If not specified, Envoy's default content types are used:
75+
// application/javascript, application/json, application/xhtml+xml, image/svg+xml,
76+
// text/css, text/html, text/plain, text/xml.
77+
//
78+
// +optional
79+
ContentTypes []string `json:"contentTypes,omitempty"`
7180
}

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/gateway-crds-helm/templates/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ spec:
369369
brotli:
370370
description: The configuration for Brotli compressor.
371371
type: object
372+
contentTypes:
373+
description: |-
374+
ContentTypes defines the set of response Content-Types that will trigger compression.
375+
When set, only responses whose Content-Type matches one of these values are compressed.
376+
If not specified, Envoy's default content types are used:
377+
application/javascript, application/json, application/xhtml+xml, image/svg+xml,
378+
text/css, text/html, text/plain, text/xml.
379+
items:
380+
type: string
381+
type: array
372382
gzip:
373383
description: The configuration for GZIP compressor.
374384
type: object
@@ -414,6 +424,16 @@ spec:
414424
brotli:
415425
description: The configuration for Brotli compressor.
416426
type: object
427+
contentTypes:
428+
description: |-
429+
ContentTypes defines the set of response Content-Types that will trigger compression.
430+
When set, only responses whose Content-Type matches one of these values are compressed.
431+
If not specified, Envoy's default content types are used:
432+
application/javascript, application/json, application/xhtml+xml, image/svg+xml,
433+
text/css, text/html, text/plain, text/xml.
434+
items:
435+
type: string
436+
type: array
417437
gzip:
418438
description: The configuration for GZIP compressor.
419439
type: object

charts/gateway-crds-helm/templates/generated/gateway.envoyproxy.io_envoyproxies.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14692,6 +14692,16 @@ spec:
1469214692
brotli:
1469314693
description: The configuration for Brotli compressor.
1469414694
type: object
14695+
contentTypes:
14696+
description: |-
14697+
ContentTypes defines the set of response Content-Types that will trigger compression.
14698+
When set, only responses whose Content-Type matches one of these values are compressed.
14699+
If not specified, Envoy's default content types are used:
14700+
application/javascript, application/json, application/xhtml+xml, image/svg+xml,
14701+
text/css, text/html, text/plain, text/xml.
14702+
items:
14703+
type: string
14704+
type: array
1469514705
gzip:
1469614706
description: The configuration for GZIP compressor.
1469714707
type: object

charts/gateway-helm/charts/crds/crds/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,16 @@ spec:
368368
brotli:
369369
description: The configuration for Brotli compressor.
370370
type: object
371+
contentTypes:
372+
description: |-
373+
ContentTypes defines the set of response Content-Types that will trigger compression.
374+
When set, only responses whose Content-Type matches one of these values are compressed.
375+
If not specified, Envoy's default content types are used:
376+
application/javascript, application/json, application/xhtml+xml, image/svg+xml,
377+
text/css, text/html, text/plain, text/xml.
378+
items:
379+
type: string
380+
type: array
371381
gzip:
372382
description: The configuration for GZIP compressor.
373383
type: object
@@ -413,6 +423,16 @@ spec:
413423
brotli:
414424
description: The configuration for Brotli compressor.
415425
type: object
426+
contentTypes:
427+
description: |-
428+
ContentTypes defines the set of response Content-Types that will trigger compression.
429+
When set, only responses whose Content-Type matches one of these values are compressed.
430+
If not specified, Envoy's default content types are used:
431+
application/javascript, application/json, application/xhtml+xml, image/svg+xml,
432+
text/css, text/html, text/plain, text/xml.
433+
items:
434+
type: string
435+
type: array
416436
gzip:
417437
description: The configuration for GZIP compressor.
418438
type: object

charts/gateway-helm/charts/crds/crds/generated/gateway.envoyproxy.io_envoyproxies.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14691,6 +14691,16 @@ spec:
1469114691
brotli:
1469214692
description: The configuration for Brotli compressor.
1469314693
type: object
14694+
contentTypes:
14695+
description: |-
14696+
ContentTypes defines the set of response Content-Types that will trigger compression.
14697+
When set, only responses whose Content-Type matches one of these values are compressed.
14698+
If not specified, Envoy's default content types are used:
14699+
application/javascript, application/json, application/xhtml+xml, image/svg+xml,
14700+
text/css, text/html, text/plain, text/xml.
14701+
items:
14702+
type: string
14703+
type: array
1469414704
gzip:
1469514705
description: The configuration for GZIP compressor.
1469614706
type: object

internal/gatewayapi/backendtrafficpolicy.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,17 +2092,7 @@ func buildCompression(compression, compressor []*egv1a1.Compression) []*ir.Compr
20922092
if (c.Type == egv1a1.GzipCompressorType && c.Gzip != nil) ||
20932093
(c.Type == egv1a1.BrotliCompressorType && c.Brotli != nil) ||
20942094
(c.Type == egv1a1.ZstdCompressorType && c.Zstd != nil) {
2095-
irCompression := ir.Compression{
2096-
Type: c.Type,
2097-
ChooseFirst: i == 0, // only the first compressor is marked as ChooseFirst
2098-
}
2099-
if c.MinContentLength != nil {
2100-
minContentLength, ok := c.MinContentLength.AsInt64()
2101-
if ok {
2102-
irCompression.MinContentLength = new(uint32(minContentLength))
2103-
}
2104-
}
2105-
result = append(result, &irCompression)
2095+
result = append(result, buildIRCompression(c, i == 0))
21062096
}
21072097
}
21082098
return result
@@ -2114,22 +2104,29 @@ func buildCompression(compression, compressor []*egv1a1.Compression) []*ir.Compr
21142104
}
21152105
result := make([]*ir.Compression, 0, len(compression))
21162106
for i, c := range compression {
2117-
irCompression := ir.Compression{
2118-
Type: c.Type,
2119-
ChooseFirst: i == 0, // only the first compressor is marked as ChooseFirst
2120-
}
2121-
if c.MinContentLength != nil {
2122-
minContentLength, ok := c.MinContentLength.AsInt64()
2123-
if ok {
2124-
irCompression.MinContentLength = new(uint32(minContentLength))
2125-
}
2126-
}
2127-
result = append(result, &irCompression)
2107+
result = append(result, buildIRCompression(c, i == 0))
21282108
}
21292109

21302110
return result
21312111
}
21322112

2113+
func buildIRCompression(c *egv1a1.Compression, chooseFirst bool) *ir.Compression {
2114+
irCompression := &ir.Compression{
2115+
Type: c.Type,
2116+
ChooseFirst: chooseFirst, // only the first compressor is marked as ChooseFirst
2117+
}
2118+
if c.MinContentLength != nil {
2119+
minContentLength, ok := c.MinContentLength.AsInt64()
2120+
if ok {
2121+
irCompression.MinContentLength = new(uint32(minContentLength))
2122+
}
2123+
}
2124+
if len(c.ContentTypes) > 0 {
2125+
irCompression.ContentTypes = append([]string{}, c.ContentTypes...)
2126+
}
2127+
return irCompression
2128+
}
2129+
21332130
func buildHTTPProtocolUpgradeConfig(cfgs []*egv1a1.ProtocolUpgradeConfig) []ir.HTTPUpgradeConfig {
21342131
if len(cfgs) == 0 {
21352132
return nil

internal/gatewayapi/backendtrafficpolicy_test.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,23 @@ func TestBuildCompression(t *testing.T) {
396396
},
397397
},
398398
},
399+
{
400+
name: "compressor with contentTypes",
401+
compressor: []*egv1a1.Compression{
402+
{
403+
Type: egv1a1.GzipCompressorType,
404+
Gzip: &egv1a1.GzipCompressor{},
405+
ContentTypes: []string{"application/json", "text/html"},
406+
},
407+
},
408+
expected: []*ir.Compression{
409+
{
410+
Type: egv1a1.GzipCompressorType,
411+
ChooseFirst: true,
412+
ContentTypes: []string{"application/json", "text/html"},
413+
},
414+
},
415+
},
399416
{
400417
name: "multiple compressors with different minContentLength",
401418
compressor: []*egv1a1.Compression{
@@ -424,21 +441,40 @@ func TestBuildCompression(t *testing.T) {
424441
},
425442
},
426443
{
427-
name: "compressor takes priority over compression",
428-
compression: []*egv1a1.Compression{
444+
name: "compressor with minContentLength and contentTypes",
445+
compressor: []*egv1a1.Compression{
429446
{
430-
Type: egv1a1.GzipCompressorType,
431-
Gzip: &egv1a1.GzipCompressor{},
432-
MinContentLength: new(resource.MustParse("100")),
447+
Type: egv1a1.BrotliCompressorType,
448+
Brotli: &egv1a1.BrotliCompressor{},
449+
MinContentLength: new(resource.MustParse("200")),
450+
ContentTypes: []string{"application/json"},
433451
},
434452
},
453+
expected: []*ir.Compression{
454+
{
455+
Type: egv1a1.BrotliCompressorType,
456+
ChooseFirst: true,
457+
MinContentLength: new(uint32(200)),
458+
ContentTypes: []string{"application/json"},
459+
},
460+
},
461+
},
462+
{
463+
name: "compressor takes priority over compression",
435464
compressor: []*egv1a1.Compression{
436465
{
437466
Type: egv1a1.BrotliCompressorType,
438467
Brotli: &egv1a1.BrotliCompressor{},
439468
MinContentLength: new(resource.MustParse("200")),
440469
},
441470
},
471+
compression: []*egv1a1.Compression{
472+
{
473+
Type: egv1a1.GzipCompressorType,
474+
Gzip: &egv1a1.GzipCompressor{},
475+
MinContentLength: new(resource.MustParse("100")),
476+
},
477+
},
442478
expected: []*ir.Compression{
443479
{
444480
Type: egv1a1.BrotliCompressorType,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
gateways:
2+
- apiVersion: gateway.networking.k8s.io/v1
3+
kind: Gateway
4+
metadata:
5+
namespace: envoy-gateway
6+
name: gateway-1
7+
spec:
8+
gatewayClassName: envoy-gateway-class
9+
listeners:
10+
- name: http
11+
protocol: HTTP
12+
port: 80
13+
allowedRoutes:
14+
namespaces:
15+
from: All
16+
httpRoutes:
17+
- apiVersion: gateway.networking.k8s.io/v1
18+
kind: HTTPRoute
19+
metadata:
20+
namespace: default
21+
name: httproute-1
22+
spec:
23+
hostnames:
24+
- gateway.envoyproxy.io
25+
parentRefs:
26+
- namespace: envoy-gateway
27+
name: gateway-1
28+
sectionName: http
29+
rules:
30+
- matches:
31+
- path:
32+
value: "/"
33+
backendRefs:
34+
- name: service-1
35+
port: 8080
36+
backendTrafficPolicies:
37+
- apiVersion: gateway.envoyproxy.io/v1alpha1
38+
kind: BackendTrafficPolicy
39+
metadata:
40+
namespace: default
41+
name: policy-for-route
42+
spec:
43+
targetRef:
44+
group: gateway.networking.k8s.io
45+
kind: HTTPRoute
46+
name: httproute-1
47+
compressor:
48+
- type: Brotli
49+
brotli: {}
50+
minContentLength: 100
51+
contentTypes:
52+
- application/json
53+
- text/html
54+
- type: Gzip
55+
gzip: {}
56+
contentTypes:
57+
- application/json

0 commit comments

Comments
 (0)