-
Notifications
You must be signed in to change notification settings - Fork 143
implement header manipulation for nginxingress #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
k8s-ci-robot
merged 16 commits into
kubernetes-sigs:main
from
eladmotola:feature/nginxingress-implement-header-manipulation
Jan 13, 2026
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e4bc299
implement header manipulation for nginxingress
eladmotola aa0232e
pr comments
eladmotola e507ae5
fmt
eladmotola 7855e52
fmt
eladmotola a981915
fmt
eladmotola 2607469
fmt
eladmotola 925d85f
add TODOs
eladmotola 0e7ad52
Merge branch 'main' into feature/nginxingress-implement-header-manipu…
eladmotola 409a726
fix mege from master
eladmotola 2679f65
fmt
eladmotola 6b089ba
Update pkg/i2gw/providers/ingressnginx/headers.go
eladmotola 2177c00
continue instead of panic
eladmotola 0e266fb
Update pkg/i2gw/providers/ingressnginx/headers_test.go
eladmotola f0caa45
Update pkg/i2gw/providers/ingressnginx/annotations.go
eladmotola 1ac3ca8
Update pkg/i2gw/providers/ingressnginx/headers.go
eladmotola 4ab5f61
Update pkg/i2gw/providers/ingressnginx/utils.go
eladmotola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| Copyright 2023 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package ingressnginx | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/intermediate" | ||
| "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications" | ||
| networkingv1 "k8s.io/api/networking/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
| gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| xForwardedPrefixAnnotation = "nginx.ingress.kubernetes.io/x-forwarded-prefix" | ||
| upstreamVhostAnnotation = "nginx.ingress.kubernetes.io/upstream-vhost" | ||
| connectionProxyHeaderAnnotation = "nginx.ingress.kubernetes.io/connection-proxy-header" | ||
| ) | ||
|
|
||
| func headerModifierFeature(_ []networkingv1.Ingress, _ map[types.NamespacedName]map[string]int32, ir *intermediate.IR) field.ErrorList { | ||
| for _, httpRouteContext := range ir.HTTPRoutes { | ||
| for i := range httpRouteContext.HTTPRoute.Spec.Rules { | ||
| if i >= len(httpRouteContext.RuleBackendSources) { | ||
| continue | ||
| } | ||
| sources := httpRouteContext.RuleBackendSources[i] | ||
| if len(sources) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| // Check the first source for the annotation | ||
| ingress := sources[0].Ingress | ||
|
eladmotola marked this conversation as resolved.
Outdated
|
||
| if ingress == nil { | ||
|
eladmotola marked this conversation as resolved.
|
||
| continue | ||
| } | ||
|
|
||
| headersToSet := make(map[string]string) | ||
|
|
||
| // 1. x-forwarded-prefix | ||
| if val, ok := ingress.Annotations[xForwardedPrefixAnnotation]; ok && val != "" { | ||
|
eladmotola marked this conversation as resolved.
Outdated
|
||
| headersToSet["X-Forwarded-Prefix"] = val | ||
| } | ||
|
|
||
| // 2. upstream-vhost -> Host header | ||
| if val, ok := ingress.Annotations[upstreamVhostAnnotation]; ok && val != "" { | ||
| headersToSet["Host"] = val | ||
| } | ||
|
|
||
| // 3. connection-proxy-header -> Connection header | ||
| if val, ok := ingress.Annotations[connectionProxyHeaderAnnotation]; ok && val != "" { | ||
| headersToSet["Connection"] = val | ||
| } | ||
|
|
||
| // 4. custom-headers -> Warn unsupported | ||
| if _, ok := ingress.Annotations["nginx.ingress.kubernetes.io/custom-headers"]; ok { | ||
| notify(notifications.WarningNotification, fmt.Sprintf("Ingress %s/%s uses 'nginx.ingress.kubernetes.io/custom-headers' which is not supported as it requires cluster access to read ConfigMaps.", ingress.Namespace, ingress.Name), &httpRouteContext.HTTPRoute) | ||
|
eladmotola marked this conversation as resolved.
Outdated
eladmotola marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| if len(headersToSet) > 0 { | ||
| // Create or append to RequestHeaderModifier filter | ||
| applyHeaderModifiers(ingress, &httpRouteContext.HTTPRoute, i, headersToSet) | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func applyHeaderModifiers(ingress *networkingv1.Ingress, httpRoute *gatewayv1.HTTPRoute, ruleIndex int, headersToSet map[string]string) { | ||
| // Check if a RequestHeaderModifier filter already exists | ||
| var filter *gatewayv1.HTTPRouteFilter | ||
| for j, f := range httpRoute.Spec.Rules[ruleIndex].Filters { | ||
| if f.Type == gatewayv1.HTTPRouteFilterRequestHeaderModifier && f.RequestHeaderModifier != nil { | ||
| filter = &httpRoute.Spec.Rules[ruleIndex].Filters[j] | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if filter == nil { | ||
| f := gatewayv1.HTTPRouteFilter{ | ||
| Type: gatewayv1.HTTPRouteFilterRequestHeaderModifier, | ||
| RequestHeaderModifier: &gatewayv1.HTTPHeaderFilter{ | ||
| Set: []gatewayv1.HTTPHeader{}, | ||
| }, | ||
| } | ||
| httpRoute.Spec.Rules[ruleIndex].Filters = append(httpRoute.Spec.Rules[ruleIndex].Filters, f) | ||
|
eladmotola marked this conversation as resolved.
|
||
| // Get pointer to the newly added filter | ||
| filter = &httpRoute.Spec.Rules[ruleIndex].Filters[len(httpRoute.Spec.Rules[ruleIndex].Filters)-1] | ||
| } | ||
|
|
||
| for name, value := range headersToSet { | ||
| // Check if header is already being set to avoid duplicates? | ||
| // Simply append for now, or check existence. | ||
| exists := false | ||
| for _, h := range filter.RequestHeaderModifier.Set { | ||
| if string(h.Name) == name { | ||
| exists = true | ||
| break | ||
| } | ||
| } | ||
| if !exists { | ||
| filter.RequestHeaderModifier.Set = append(filter.RequestHeaderModifier.Set, gatewayv1.HTTPHeader{ | ||
| Name: gatewayv1.HTTPHeaderName(name), | ||
| Value: value, | ||
| }) | ||
| notify(notifications.InfoNotification, fmt.Sprintf("Applied header modifier %s: %s to rule %d of route %s/%s", name, value, ruleIndex, httpRoute.Namespace, httpRoute.Name), httpRoute) | ||
|
eladmotola marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.