Skip to content

Conversation

@michalskalski
Copy link

What type of PR is this?

Feature - adds new API field and functionality

What this PR does / why we need it:

This PR adds support for the addIfAbsent header action in HTTPHeaderFilter, which adds headers only if they don't already exist. This maps to Envoy's ADD_IF_ABSENT action.

Use Case:

Set default headers (e.g., Content-Security-Policy, X-Frame-Options) at the gateway level that can be overridden by backend responses. Unlike add which appends to existing headers, or set which overwrites them, addIfAbsent is a no-op if the header is already present.

Changes:

  • Added AddIfAbsent field to HTTPHeaderFilter in api/v1alpha1/shared_types.go
  • Added AddIfAbsent field to IR AddHeader struct in internal/ir/xds.go
  • Updated translateHeaderModifier() in internal/gatewayapi/clienttrafficpolicy.go
  • Updated buildXdsAddedHeaders() in internal/xds/translator/route.go
  • Updated buildHeaderMutationRules() in internal/xds/translator/header_mutation.go
  • Added documentation in site/content/en/latest/tasks/traffic/http-response-headers.md
  • Added testdata files for Gateway API and XDS translator tests

Example usage:

apiVersion: gateway.envoyproxy.io/v1alpha1
kind: ClientTrafficPolicy
metadata:
  name: default-headers
spec:
  targetRef:
    group: gateway.networking.k8s.io
    kind: Gateway
    name: eg
  headers:
    lateResponseHeaders:
      addIfAbsent:
        - name: "content-security-policy"
          value: "default-src 'self'"

Which issue(s) this PR fixes:

Fixes #7656

Release Notes: Yes

**What type of PR is this?**

Feature - adds new API field and functionality

**What this PR does / why we need it**:

This PR adds support for the `addIfAbsent` header action in `HTTPHeaderFilter`, which adds headers only if they don't already exist. This maps to Envoy's
[`ADD_IF_ABSENT`](https://www.envoyproxy.io/docs/envoy/v1.36.2/api-v3/config/core/v3/base.proto#enum-config-core-v3-headervalueoption-headerappendaction) action.

**Use Case:**

Set default headers (e.g., `Content-Security-Policy`, `X-Frame-Options`) at the gateway level that can be overridden by backend responses.
Unlike `add` which appends to existing headers, or `set` which overwrites them, `addIfAbsent` is a no-op if the header is already present.

**Changes:**

- Added `AddIfAbsent` field to `HTTPHeaderFilter` in `api/v1alpha1/shared_types.go`
- Added `AddIfAbsent` field to IR `AddHeader` struct in `internal/ir/xds.go`
- Updated `translateHeaderModifier()` in `internal/gatewayapi/clienttrafficpolicy.go`
- Updated `buildXdsAddedHeaders()` in `internal/xds/translator/route.go`
- Updated `buildHeaderMutationRules()` in `internal/xds/translator/header_mutation.go`
- Added documentation in `site/content/en/latest/tasks/traffic/http-response-headers.md`
- Added testdata files for Gateway API and XDS translator tests

**Example usage:**

```yaml
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: ClientTrafficPolicy
metadata:
  name: default-headers
spec:
  targetRef:
    group: gateway.networking.k8s.io
    kind: Gateway
    name: eg
  headers:
    lateResponseHeaders:
      addIfAbsent:
        - name: "content-security-policy"
          value: "default-src 'self'"
```

Which issue(s) this PR fixes:

Fixes envoyproxy#7656

Release Notes: Yes

Signed-off-by: Michal Skalski <[email protected]>
@michalskalski michalskalski requested a review from a team as a code owner January 16, 2026 15:19
@netlify
Copy link

netlify bot commented Jan 16, 2026

Deploy Preview for cerulean-figolla-1f9435 ready!

Name Link
🔨 Latest commit 8107c3b
🔍 Latest deploy log https://app.netlify.com/projects/cerulean-figolla-1f9435/deploys/696a57004158a50008cac2fd
😎 Deploy Preview https://deploy-preview-7978--cerulean-figolla-1f9435.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

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.

Support adding response header if not present in the ClientTrafficPolicy

1 participant