Skip to content

Commit 0b1aabd

Browse files
sudiptob2rudrakhp
authored andcommitted
feat: add compressor field to configure compression setting in BackendTrafficPolicy (envoyproxy#6956)
* feat: add compressor field to configure compression setting in BackendTrafficPolicy Signed-off-by: sudipto baral <[email protected]> * docs: update response compression documentation to reflect the new compressor field and deprecate the old compression field Signed-off-by: sudipto baral <[email protected]> * test: update translator tests outputs Signed-off-by: sudipto baral <[email protected]> * test: re-generate outputs Signed-off-by: sudipto baral <[email protected]> * test: go mod tidy Signed-off-by: sudipto baral <[email protected]> * remove patchMerge from Compressor field definition Removed unnecessary patchMergeKey and patchStrategy from Compressor field. Signed-off-by: Rudrakh Panigrahi <[email protected]> * revert patchMergeKey changes Signed-off-by: Rudrakh Panigrahi <[email protected]> --------- Signed-off-by: sudipto baral <[email protected]> Signed-off-by: Rudrakh Panigrahi <[email protected]> Co-authored-by: Rudrakh Panigrahi <[email protected]> Signed-off-by: Adam Buran <[email protected]>
1 parent 5c0ee48 commit 0b1aabd

24 files changed

+2645
-11
lines changed

api/v1alpha1/backendtrafficpolicy_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type BackendTrafficPolicy struct {
4343
// +kubebuilder:validation:XValidation:rule="has(self.targetRefs) ? self.targetRefs.all(ref, ref.group == 'gateway.networking.k8s.io') : true ", message="this policy can only have a targetRefs[*].group of gateway.networking.k8s.io"
4444
// +kubebuilder:validation:XValidation:rule="has(self.targetRefs) ? self.targetRefs.all(ref, ref.kind in ['Gateway', 'HTTPRoute', 'GRPCRoute', 'UDPRoute', 'TCPRoute', 'TLSRoute']) : true ", message="this policy can only have a targetRefs[*].kind of Gateway/HTTPRoute/GRPCRoute/TCPRoute/UDPRoute/TLSRoute"
4545
// +kubebuilder:validation:XValidation:rule="has(self.targetRefs) ? self.targetRefs.all(ref, !has(ref.sectionName)) : true",message="this policy does not yet support the sectionName field"
46+
// +kubebuilder:validation:XValidation:rule="!has(self.compression) || !has(self.compressor)", message="either compression or compressor can be set, not both"
4647
type BackendTrafficPolicySpec struct {
4748
PolicyTargetReferences `json:",inline"`
4849
ClusterSettings `json:",inline"`
@@ -73,13 +74,23 @@ type BackendTrafficPolicySpec struct {
7374
UseClientProtocol *bool `json:"useClientProtocol,omitempty"`
7475

7576
// The compression config for the http streams.
77+
// Deprecated: Use Compressor instead.
7678
//
7779
// +patchMergeKey=type
7880
// +patchStrategy=merge
7981
//
8082
// +optional
8183
Compression []*Compression `json:"compression,omitempty" patchMergeKey:"type" patchStrategy:"merge"`
8284

85+
// The compressor config for the http streams.
86+
// This provides more granular control over compression configuration.
87+
//
88+
// +patchMergeKey=type
89+
// +patchStrategy=merge
90+
//
91+
// +optional
92+
Compressor []*Compression `json:"compressor,omitempty" patchMergeKey:"type" patchStrategy:"merge"`
93+
8394
// ResponseOverride defines the configuration to override specific responses with a custom one.
8495
// If multiple configurations are specified, the first one to match wins.
8596
//

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 11 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: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,35 @@ spec:
231231
type: object
232232
type: object
233233
compression:
234-
description: The compression config for the http streams.
234+
description: |-
235+
The compression config for the http streams.
236+
Deprecated: Use Compressor instead.
237+
items:
238+
description: |-
239+
Compression defines the config of enabling compression.
240+
This can help reduce the bandwidth at the expense of higher CPU.
241+
properties:
242+
brotli:
243+
description: The configuration for Brotli compressor.
244+
type: object
245+
gzip:
246+
description: The configuration for GZIP compressor.
247+
type: object
248+
type:
249+
description: CompressorType defines the compressor type to use
250+
for compression.
251+
enum:
252+
- Gzip
253+
- Brotli
254+
type: string
255+
required:
256+
- type
257+
type: object
258+
type: array
259+
compressor:
260+
description: |-
261+
The compressor config for the http streams.
262+
This provides more granular control over compression configuration.
235263
items:
236264
description: |-
237265
Compression defines the config of enabling compression.
@@ -2324,6 +2352,8 @@ spec:
23242352
- message: this policy does not yet support the sectionName field
23252353
rule: 'has(self.targetRefs) ? self.targetRefs.all(ref, !has(ref.sectionName))
23262354
: true'
2355+
- message: either compression or compressor can be set, not both
2356+
rule: '!has(self.compression) || !has(self.compressor)'
23272357
status:
23282358
description: status defines the current status of BackendTrafficPolicy.
23292359
properties:

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,35 @@ spec:
230230
type: object
231231
type: object
232232
compression:
233-
description: The compression config for the http streams.
233+
description: |-
234+
The compression config for the http streams.
235+
Deprecated: Use Compressor instead.
236+
items:
237+
description: |-
238+
Compression defines the config of enabling compression.
239+
This can help reduce the bandwidth at the expense of higher CPU.
240+
properties:
241+
brotli:
242+
description: The configuration for Brotli compressor.
243+
type: object
244+
gzip:
245+
description: The configuration for GZIP compressor.
246+
type: object
247+
type:
248+
description: CompressorType defines the compressor type to use
249+
for compression.
250+
enum:
251+
- Gzip
252+
- Brotli
253+
type: string
254+
required:
255+
- type
256+
type: object
257+
type: array
258+
compressor:
259+
description: |-
260+
The compressor config for the http streams.
261+
This provides more granular control over compression configuration.
234262
items:
235263
description: |-
236264
Compression defines the config of enabling compression.
@@ -2323,6 +2351,8 @@ spec:
23232351
- message: this policy does not yet support the sectionName field
23242352
rule: 'has(self.targetRefs) ? self.targetRefs.all(ref, !has(ref.sectionName))
23252353
: true'
2354+
- message: either compression or compressor can be set, not both
2355+
rule: '!has(self.compression) || !has(self.compressor)'
23262356
status:
23272357
description: status defines the current status of BackendTrafficPolicy.
23282358
properties:

internal/gatewayapi/backendtrafficpolicy.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ func (t *Translator) buildTrafficFeatures(policy *egv1a1.BackendTrafficPolicy, r
629629
errs = errors.Join(errs, err)
630630
}
631631

632-
cp = buildCompression(policy.Spec.Compression)
632+
cp = buildCompression(policy.Spec.Compression, policy.Spec.Compressor)
633633
httpUpgrade = buildHTTPProtocolUpgradeConfig(policy.Spec.HTTPUpgrade)
634634

635635
ac = buildAdmissionControl(policy.Spec.AdmissionControl)
@@ -1198,7 +1198,23 @@ func defaultResponseOverrideRuleName(policy *egv1a1.BackendTrafficPolicy, index
11981198
strconv.Itoa(index))
11991199
}
12001200

1201-
func buildCompression(compression []*egv1a1.Compression) []*ir.Compression {
1201+
func buildCompression(compression, compressor []*egv1a1.Compression) []*ir.Compression {
1202+
// Handle the Compressor field first (higher priority)
1203+
if len(compressor) > 0 {
1204+
irCompression := make([]*ir.Compression, 0, len(compressor))
1205+
for _, c := range compressor {
1206+
// Only add compression if the corresponding compressor not null
1207+
if (c.Type == egv1a1.GzipCompressorType && c.Gzip != nil) ||
1208+
(c.Type == egv1a1.BrotliCompressorType && c.Brotli != nil) {
1209+
irCompression = append(irCompression, &ir.Compression{
1210+
Type: c.Type,
1211+
})
1212+
}
1213+
}
1214+
return irCompression
1215+
}
1216+
1217+
// Fallback to the deprecated Compression field
12021218
if compression == nil {
12031219
return nil
12041220
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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: null
50+
- type: Gzip
51+
gzip: null

0 commit comments

Comments
 (0)