Skip to content

feat: automatically set first compressor with choose_first#7406

Merged
arkodg merged 5 commits intoenvoyproxy:mainfrom
buroa:buroa/compressor
Nov 19, 2025
Merged

feat: automatically set first compressor with choose_first#7406
arkodg merged 5 commits intoenvoyproxy:mainfrom
buroa:buroa/compressor

Conversation

@buroa
Copy link
Copy Markdown
Contributor

@buroa buroa commented Nov 1, 2025

This adds a new chooseFirst to each compressor.

When chooseFirst is true, the compressor is preferred when q-values in Accept-Encoding are equal. If multiple compressor filters set choose_first to true, the last one in the filter chain is chosen.

I also cleaned up some code in various places. There was some duplication happening.

chooseFirst is automatically set to true based on the first item in the compressor/compression list.

Closes #5796

@buroa buroa requested a review from a team as a code owner November 1, 2025 21:13
@codecov
Copy link
Copy Markdown

codecov bot commented Nov 1, 2025

Codecov Report

❌ Patch coverage is 70.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.21%. Comparing base (1e295b6) to head (be7cec7).
⚠️ Report is 46 commits behind head on main.

Files with missing lines Patch % Lines
internal/xds/translator/compressor.go 70.00% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7406      +/-   ##
==========================================
- Coverage   72.36%   71.21%   -1.15%     
==========================================
  Files         231      274      +43     
  Lines       34042    34815     +773     
==========================================
+ Hits        24633    24793     +160     
- Misses       7633     8238     +605     
- Partials     1776     1784       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@@ -113,26 +113,40 @@ static_resources:
envoy.filters.http.compression:
"@type": type.googleapis.com/envoy.extensions.filters.http.compressor.v3.CompressorPerRoute
overrides:
{{- if .PrometheusCompressionRemoveAcceptEncodingHeader }}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking this loud, should we move 19001 to dynamic listener
cc @envoyproxy/gateway-maintainers

buroa added a commit to buroa/k8s-gitops that referenced this pull request Nov 3, 2025
@buroa buroa requested review from jukie and zirain November 4, 2025 13:44
@buroa
Copy link
Copy Markdown
Contributor Author

buroa commented Nov 5, 2025

/retest

@buroa
Copy link
Copy Markdown
Contributor Author

buroa commented Nov 5, 2025

/retest

@buroa
Copy link
Copy Markdown
Contributor Author

buroa commented Nov 12, 2025

@arkodg Can I get a review please? :-)

Comment on lines +1575 to +1577
Type: c.Type,
ChooseFirst: ptr.Deref(&c.ChooseFirst, false),
RemoveAcceptEncodingHeader: ptr.Deref(&c.RemoveAcceptEncodingHeader, false),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update these set of tests (backendtrafficpolicy-compressor-*.in.yaml) and verify that the Compressor field can also properly handle new ChooseFirst and RemoveAcceptEncodingHeader during merging?

https://github.com/envoyproxy/gateway/blob/main/internal/gatewayapi/testdata/backendtrafficpolicy-compressor-json-merge-switch-compression.in.yaml

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChooseFirst and RemoveAcceptEncodingHeader are global, so you cannot just disable them per-route like that example.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, just for my understanding, does that mean we can not do the following?

backendTrafficPolicies:
  # Gateway-level policy - Enable Gzip only
  - apiVersion: gateway.envoyproxy.io/v1alpha1
    kind: BackendTrafficPolicy
    metadata:
      namespace: envoy-gateway
      name: gateway-compressor-policy
    spec:
      targetRef:
        group: gateway.networking.k8s.io
        kind: Gateway
        name: gateway-1
      compressor:
        - type: Gzip
          chooseFirst: true # enabled at gateway level
          gzip: {}
  # Route-level policy - Switch to Brotli and Zstd, disable Gzip
  - apiVersion: gateway.envoyproxy.io/v1alpha1
    kind: BackendTrafficPolicy
    metadata:
      namespace: default
      name: route-compressor-policy-switch-to-brotli-and-zstd
    spec:
      targetRef:
        group: gateway.networking.k8s.io
        kind: HTTPRoute
        name: httproute-1
      compressor:
        - type: Gzip
          gzip: null # disabled in route level
        - type: Brotli
          chooseFirst: true # using chooseFirst for Brotli in the route level
          brotli: {}
        - type: Zstd
          zstd: {}
      mergeType: JSONMerge

@arkodg
Copy link
Copy Markdown
Contributor

arkodg commented Nov 18, 2025

is this API really needed, can be leverage the list order and set the first value as choose first ?

@buroa
Copy link
Copy Markdown
Contributor Author

buroa commented Nov 18, 2025

is this API really needed, can be leverage the list order and set the first value as choose first ?

Do we want to assume that?

@arkodg
Copy link
Copy Markdown
Contributor

arkodg commented Nov 18, 2025

is this API really needed, can be leverage the list order and set the first value as choose first ?

Do we want to assume that?

are there any drawbacks to this approach ?

@buroa
Copy link
Copy Markdown
Contributor Author

buroa commented Nov 19, 2025

is this API really needed, can be leverage the list order and set the first value as choose first ?

Do we want to assume that?

are there any drawbacks to this approach ?

Just thought about it, not really. We can just assume the first item in the compression list is choosen first.

This also allows us to set removeAcceptEncodingHeader per individual route instead of global.

@arkodg
Copy link
Copy Markdown
Contributor

arkodg commented Nov 19, 2025

@buroa can we also split up the PR into 2 - one for chooseFirst and the other for removeAcceptEncodingHeader

@buroa
Copy link
Copy Markdown
Contributor Author

buroa commented Nov 19, 2025

@buroa can we also split up the PR into 2 - one for chooseFirst and the other for removeAcceptEncodingHeader

Can do.

Signed-off-by: Steven Kreitzer <skre@skre.me>
Signed-off-by: Steven Kreitzer <skre@skre.me>
@buroa buroa changed the title feat: allow more compressor overrides feat: automatically set first compressor with choose_first Nov 19, 2025
@arkodg
Copy link
Copy Markdown
Contributor

arkodg commented Nov 19, 2025

can we add a doc string in the API to highlight that order matters and if multiple values are present, we pick the one first in the list

Signed-off-by: Steven Kreitzer <skre@skre.me>
Signed-off-by: Steven Kreitzer <skre@skre.me>
Signed-off-by: Steven Kreitzer <skre@skre.me>
Comment on lines +163 to +165
compressorProto := compressorPerRouteConfig()
if compressorAny, err = proto.ToAnyWithValidation(compressorProto); err != nil {
return err
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create this outside of the for loop? Is there any advantage to doing it inside?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is see, it now takes *ir.Compression

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in the next PR it will use that to determine the accept encoding overrides.

Copy link
Copy Markdown
Member

@sudiptob2 sudiptob2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
It would be a good idea to add an end-to-end test to verify how we configure these fields using compressor. Created #7564 to track.

@buroa
Copy link
Copy Markdown
Contributor Author

buroa commented Nov 19, 2025

/retest

1 similar comment
@buroa
Copy link
Copy Markdown
Contributor Author

buroa commented Nov 19, 2025

/retest

Copy link
Copy Markdown
Contributor

@arkodg arkodg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks

@arkodg arkodg merged commit 6a99849 into envoyproxy:main Nov 19, 2025
76 of 81 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Priority order of Compressors (Brotli, Gzip)

5 participants