Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
363d20e
added temoporal and permanent redirect
jgreeer Jan 9, 2026
a969c7b
moved annotations to annotations.go
jgreeer Jan 9, 2026
d67662b
start of adding status codes
jgreeer Jan 12, 2026
584206e
Merge branch 'main' into ingress-nginx/support-redirect
jgreeer Jan 13, 2026
222d591
add more redirect annotations to file
jgreeer Jan 14, 2026
08cc6f6
remove status codes and warn about those annotations
jgreeer Jan 14, 2026
6edbfa0
parse URL and set for redirect
jgreeer Jan 16, 2026
3e3c4d4
add warnings for proxy-redirect annotations
jgreeer Jan 16, 2026
01fb5bf
Merge branch 'main' into ingress-nginx/support-redirect
jgreeer Jan 23, 2026
712e81c
fix imports
jgreeer Jan 23, 2026
26f8b91
Update pkg/i2gw/providers/ingressnginx/redirect.go
jgreeer Jan 28, 2026
0fd3ffc
Update pkg/i2gw/providers/ingressnginx/annotations.go
jgreeer Jan 28, 2026
f9a7f93
give priority to temporal if both annotations are present
jgreeer Jan 29, 2026
e82e154
update copyright
jgreeer Jan 29, 2026
6722897
change test for both annotations case
jgreeer Jan 29, 2026
dead24e
Merge branch 'main' into ingress-nginx/support-redirect
jgreeer Feb 5, 2026
406cba4
added check for port number
jgreeer Feb 5, 2026
c75b765
Merge branch 'main' into ingress-nginx/support-redirect
jgreeer Feb 12, 2026
00711fa
fixed backendrefs for redirect rule
jgreeer Feb 12, 2026
f385e0b
modify existing rule instead of creating a new one
jgreeer Feb 13, 2026
3346624
replace full path as default
jgreeer Feb 13, 2026
a431c4e
fix tests
jgreeer Feb 13, 2026
23bf2c9
lint
jgreeer Feb 13, 2026
5e76879
iterate on all rules
jgreeer Feb 13, 2026
3524eb4
accept default codes in permanent-redirect-code annotation
jgreeer Feb 18, 2026
993ca43
Merge branch 'main' into ingress-nginx/support-redirect
jgreeer Feb 18, 2026
77174ab
added intergation tests
jgreeer Feb 18, 2026
b55125f
get non canary ingress
jgreeer Feb 24, 2026
1aad786
check if ingress is nil
jgreeer Feb 26, 2026
5353829
add other status codes
jgreeer Feb 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/i2gw/providers/ingressnginx/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ const (
// Rewrite annotations
RewriteTargetAnnotation = "nginx.ingress.kubernetes.io/rewrite-target"

// Redirect annotations
PermanentRedirectAnnotation = "nginx.ingress.kubernetes.io/permanent-redirect"
PermanentRedirectCodeAnnotation = "nginx.ingress.kubernetes.io/permanent-redirect-code"
TemporalRedirectAnnotation = "nginx.ingress.kubernetes.io/temporal-redirect"
TemporalRedirectCodeAnnotation = "nginx.ingress.kubernetes.io/temporal-redirect-code"
FromToWWWRedirectAnnotation = "nginx.ingress.kubernetes.io/from-to-www-redirect"
ProxyRedirectFromAnnotation = "nginx.ingress.kubernetes.io/proxy-redirect-from"
ProxyRedirectToAnnotation = "nginx.ingress.kubernetes.io/proxy-redirect-to"

// Header annotations
XForwardedPrefixAnnotation = "nginx.ingress.kubernetes.io/x-forwarded-prefix"
UpstreamVhostAnnotation = "nginx.ingress.kubernetes.io/upstream-vhost"
Expand Down
1 change: 1 addition & 0 deletions pkg/i2gw/providers/ingressnginx/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func newResourcesToIRConverter() *resourcesToIRConverter {
return &resourcesToIRConverter{
featureParsers: []i2gw.FeatureParser{
canaryFeature,
redirectFeature,
headerModifierFeature,
regexFeature,
},
Expand Down
168 changes: 168 additions & 0 deletions pkg/i2gw/providers/ingressnginx/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ package ingressnginx

import (
"fmt"
"net/url"
"strconv"

emitterir "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/emitter_intermediate"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
providerir "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/provider_intermediate"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/common"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand All @@ -30,6 +34,170 @@ import (
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)

// redirectFeature converts permanent and temporal redirect annotations to Gateway API RequestRedirect filters.
// - permanent-redirect uses a 301 status code
// - temporal-redirect uses a 302 status code
func redirectFeature(ingresses []networkingv1.Ingress, _ map[types.NamespacedName]map[string]int32, ir *providerir.ProviderIR) field.ErrorList {
var errs field.ErrorList

ruleGroups := common.GetRuleGroups(ingresses)
for _, rg := range ruleGroups {
for _, rule := range rg.Rules {

// Warn about unsupported proxy-redirect annotations
if rule.Ingress.Annotations[ProxyRedirectFromAnnotation] != "" {
notify(notifications.WarningNotification, fmt.Sprintf("ingress %s/%s uses unsupported annotation %s",
rule.Ingress.Namespace, rule.Ingress.Name, ProxyRedirectFromAnnotation), &rule.Ingress)
}
if rule.Ingress.Annotations[ProxyRedirectToAnnotation] != "" {
notify(notifications.WarningNotification, fmt.Sprintf("ingress %s/%s uses unsupported annotation %s",
rule.Ingress.Namespace, rule.Ingress.Name, ProxyRedirectToAnnotation), &rule.Ingress)
}

permanentRedirectURL, hasPermanent := rule.Ingress.Annotations[PermanentRedirectAnnotation]
temporalRedirectURL, hasTemporal := rule.Ingress.Annotations[TemporalRedirectAnnotation]

// Skip if neither annotation is present
if !hasPermanent && !hasTemporal {
continue
}

// Determine redirect URL and status code
// If both are present, temporal takes priority
var redirectURL string
var statusCode int
var annotationUsed string

if hasTemporal {
redirectURL = temporalRedirectURL
statusCode = 302
annotationUsed = TemporalRedirectAnnotation

// Warn if both annotations are present
if hasPermanent {
notify(notifications.WarningNotification, fmt.Sprintf("ingress %s/%s has both %s and %s annotations, using %s",
rule.Ingress.Namespace, rule.Ingress.Name, PermanentRedirectAnnotation, TemporalRedirectAnnotation, TemporalRedirectAnnotation), &rule.Ingress)
}

// Warn about unsupported custom status code annotation
if rule.Ingress.Annotations[TemporalRedirectCodeAnnotation] != "" {
notify(notifications.WarningNotification, fmt.Sprintf("ingress %s/%s uses unsupported annotation %s",
rule.Ingress.Namespace, rule.Ingress.Name, TemporalRedirectCodeAnnotation), &rule.Ingress)
}
} else {
redirectURL = permanentRedirectURL
statusCode = 301
annotationUsed = PermanentRedirectAnnotation

// Warn about unsupported custom status code annotation
if rule.Ingress.Annotations[PermanentRedirectCodeAnnotation] != "" {
notify(notifications.WarningNotification, fmt.Sprintf("ingress %s/%s uses unsupported annotation %s",
rule.Ingress.Namespace, rule.Ingress.Name, PermanentRedirectCodeAnnotation), &rule.Ingress)
}
}

// Validate redirect URL is not empty
if redirectURL == "" {
errs = append(errs, field.Invalid(
field.NewPath("ingress", rule.Ingress.Namespace, rule.Ingress.Name, "metadata", "annotations", annotationUsed),
redirectURL,
"redirect URL cannot be empty",
))
continue
}

// Parse the redirect URL
parsedURL, err := url.Parse(redirectURL)
if err != nil {
errs = append(errs, field.Invalid(
field.NewPath("ingress", rule.Ingress.Namespace, rule.Ingress.Name, "metadata", "annotations", annotationUsed),
redirectURL,
fmt.Sprintf("invalid redirect URL: %v", err),
))
continue
}

// Apply redirect to all rules in the ingress
for _, ingressRule := range rule.Ingress.Spec.Rules {
routeName := common.RouteName(rule.Ingress.Name, ingressRule.Host)
routeKey := types.NamespacedName{Namespace: rule.Ingress.Namespace, Name: routeName}
httpRouteContext, routeExists := ir.HTTPRoutes[routeKey]
if !routeExists {
continue
}

// Create the redirect filter
redirectFilterConfig := &gatewayv1.HTTPRequestRedirectFilter{
StatusCode: ptr.To(statusCode),
}

// Set scheme if present
if parsedURL.Scheme != "" {
redirectFilterConfig.Scheme = ptr.To(parsedURL.Scheme)
}

// Set hostname if present
if parsedURL.Hostname() != "" {
hostname := gatewayv1.PreciseHostname(parsedURL.Hostname())
redirectFilterConfig.Hostname = &hostname
}

// Set port if present
if parsedURL.Port() != "" {
port, err := strconv.Atoi(parsedURL.Port())
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why are we ignoring this error? Let's append to errs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This did get me thinking... we could have named ports here right?

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.

this does gets caught by url.Parse() before we reach this, but i added a check to be safe. also not sure what you mean steven?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@jgreeer i just reread this and what I said makes no sense

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@jgreeer Atoi can only really fail if the port overflows int, which shouldn't be possible unless you are actively trying to mess up. This seems fine.

if err == nil {
portNumber := gatewayv1.PortNumber(port)
redirectFilterConfig.Port = &portNumber
} else {
errs = append(errs, field.Invalid(
field.NewPath("ingress", rule.Ingress.Namespace, rule.Ingress.Name, "metadata", "annotations", annotationUsed),
redirectURL,
fmt.Sprintf("invalid port in redirect URL: %v", err),
))
continue
}
}

// Set path if present
if parsedURL.Path != "" {
pathType := gatewayv1.FullPathHTTPPathModifier
redirectFilterConfig.Path = &gatewayv1.HTTPPathModifier{
Type: pathType,
ReplaceFullPath: ptr.To(parsedURL.Path),
}
}

redirectFilter := gatewayv1.HTTPRouteFilter{
Type: gatewayv1.HTTPRouteFilterRequestRedirect,
RequestRedirect: redirectFilterConfig,
}

// Add redirect rule at the beginning of all rules
redirectRule := gatewayv1.HTTPRouteRule{
Filters: []gatewayv1.HTTPRouteFilter{redirectFilter},
// Clear backend refs as redirects don't route to backends
BackendRefs: nil,
}

// Prepend the redirect rule to existing rules
httpRouteContext.HTTPRoute.Spec.Rules = append(
[]gatewayv1.HTTPRouteRule{redirectRule},
httpRouteContext.HTTPRoute.Spec.Rules...,
)

Comment thread
jgreeer marked this conversation as resolved.
ir.HTTPRoutes[routeKey] = httpRouteContext

notify(notifications.InfoNotification,
fmt.Sprintf("parsed %q annotation of ingress %s/%s with redirect to %q (status code: %d). ",
annotationUsed, rule.Ingress.Namespace, rule.Ingress.Name, redirectURL, statusCode),
&httpRouteContext.HTTPRoute)
}
}
}

return errs
}

// Ingress NGINX has some quirky behaviors around SSL redirect.
// The formula we follow is that if an ingress has certs configured, and it does not have the
// "nginx.ingress.kubernetes.io/ssl-redirect" annotation set to "false" (or "0", etc), then we
Expand Down
Loading