Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
324 changes: 324 additions & 0 deletions e2e/ingress_nginx_redirect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,324 @@
/*
Copyright 2026 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 e2e

import (
"fmt"
"net/http"
"regexp"
"testing"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/ingressnginx"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/istio"
"github.com/stretchr/testify/require"
networkingv1 "k8s.io/api/networking/v1"
)

func TestIngressNGINXRedirect(t *testing.T) {
t.Parallel()
t.Run("to Istio", func(t *testing.T) {
t.Parallel()
t.Run("permanent redirect", func(t *testing.T) {
suffix, err := randString()
require.NoError(t, err)
redirectURL := fmt.Sprintf("https://new-site-%s.example.com/new-path/", suffix)

runTestCase(t, &testCase{
gatewayImplementation: istio.ProviderName,
providers: []string{ingressnginx.Name},
providerFlags: map[string]map[string]string{
ingressnginx.Name: {
ingressnginx.NginxIngressClassFlag: ingressnginx.NginxIngressClass,
},
},
ingresses: []*networkingv1.Ingress{
basicIngress().
withName("permanent-redirect").
withIngressClass(ingressnginx.NginxIngressClass).
withAnnotation("nginx.ingress.kubernetes.io/permanent-redirect", redirectURL).
build(),
},
verifiers: map[string][]verifier{
"permanent-redirect": {
&httpRequestVerifier{
path: "/",
allowedCodes: []int{
http.StatusMovedPermanently, // 301
},
headerMatches: []headerMatch{
{
name: "Location",
patterns: []*maybeNegativePattern{
{pattern: regexp.MustCompile("^" + regexp.QuoteMeta(redirectURL) + "$")},
},
},
},
},
},
},
})
})

t.Run("temporal redirect", func(t *testing.T) {
suffix, err := randString()
require.NoError(t, err)
redirectURL := fmt.Sprintf("https://temp-site-%s.example.com/temp-path/", suffix)

runTestCase(t, &testCase{
gatewayImplementation: istio.ProviderName,
providers: []string{ingressnginx.Name},
providerFlags: map[string]map[string]string{
ingressnginx.Name: {
ingressnginx.NginxIngressClassFlag: ingressnginx.NginxIngressClass,
},
},
ingresses: []*networkingv1.Ingress{
basicIngress().
withName("temporal-redirect").
withIngressClass(ingressnginx.NginxIngressClass).
withAnnotation("nginx.ingress.kubernetes.io/temporal-redirect", redirectURL).
build(),
},
verifiers: map[string][]verifier{
"temporal-redirect": {
&httpRequestVerifier{
path: "/",
allowedCodes: []int{
http.StatusFound, // 302
},
headerMatches: []headerMatch{
{
name: "Location",
patterns: []*maybeNegativePattern{
{pattern: regexp.MustCompile("^" + regexp.QuoteMeta(redirectURL) + "$")},
},
},
},
},
},
},
})
})

t.Run("permanent redirect with supported custom code", func(t *testing.T) {
suffix, err := randString()
require.NoError(t, err)
redirectURL := fmt.Sprintf("https://custom-code-%s.example.com/path/", suffix)

runTestCase(t, &testCase{
gatewayImplementation: istio.ProviderName,
providers: []string{ingressnginx.Name},
providerFlags: map[string]map[string]string{
ingressnginx.Name: {
ingressnginx.NginxIngressClassFlag: ingressnginx.NginxIngressClass,
},
},
ingresses: []*networkingv1.Ingress{
basicIngress().
withName("permanent-redirect-301").
withIngressClass(ingressnginx.NginxIngressClass).
withAnnotation("nginx.ingress.kubernetes.io/permanent-redirect", redirectURL).
withAnnotation("nginx.ingress.kubernetes.io/permanent-redirect-code", "301").
build(),
},
verifiers: map[string][]verifier{
"permanent-redirect-301": {
&httpRequestVerifier{
path: "/",
allowedCodes: []int{
http.StatusMovedPermanently, // 301
},
headerMatches: []headerMatch{
{
name: "Location",
patterns: []*maybeNegativePattern{
{pattern: regexp.MustCompile("^" + regexp.QuoteMeta(redirectURL) + "$")},
},
},
},
},
},
},
})
})

t.Run("temporal redirect with supported custom code", func(t *testing.T) {
suffix, err := randString()
require.NoError(t, err)
redirectURL := fmt.Sprintf("https://custom-temp-%s.example.com/path/", suffix)

runTestCase(t, &testCase{
gatewayImplementation: istio.ProviderName,
providers: []string{ingressnginx.Name},
providerFlags: map[string]map[string]string{
ingressnginx.Name: {
ingressnginx.NginxIngressClassFlag: ingressnginx.NginxIngressClass,
},
},
ingresses: []*networkingv1.Ingress{
basicIngress().
withName("temporal-redirect-302").
withIngressClass(ingressnginx.NginxIngressClass).
withAnnotation("nginx.ingress.kubernetes.io/temporal-redirect", redirectURL).
withAnnotation("nginx.ingress.kubernetes.io/temporal-redirect-code", "302").
build(),
},
verifiers: map[string][]verifier{
"temporal-redirect-302": {
&httpRequestVerifier{
path: "/",
allowedCodes: []int{
http.StatusFound, // 302
},
headerMatches: []headerMatch{
{
name: "Location",
patterns: []*maybeNegativePattern{
{pattern: regexp.MustCompile("^" + regexp.QuoteMeta(redirectURL) + "$")},
},
},
},
},
},
},
})
})

t.Run("redirect with scheme and hostname only", func(t *testing.T) {
redirectURL := "https://another-domain.example.com/"

runTestCase(t, &testCase{
gatewayImplementation: istio.ProviderName,
providers: []string{ingressnginx.Name},
providerFlags: map[string]map[string]string{
ingressnginx.Name: {
ingressnginx.NginxIngressClassFlag: ingressnginx.NginxIngressClass,
},
},
ingresses: []*networkingv1.Ingress{
basicIngress().
withName("redirect-hostname-only").
withIngressClass(ingressnginx.NginxIngressClass).
withAnnotation("nginx.ingress.kubernetes.io/permanent-redirect", redirectURL).
build(),
},
verifiers: map[string][]verifier{
"redirect-hostname-only": {
&httpRequestVerifier{
path: "/some/path",
allowedCodes: []int{
http.StatusMovedPermanently, // 301
},
headerMatches: []headerMatch{
{
name: "Location",
patterns: []*maybeNegativePattern{
{pattern: regexp.MustCompile("^" + regexp.QuoteMeta(redirectURL) + "$")},
},
},
},
},
},
},
})
})

t.Run("redirect with port", func(t *testing.T) {
suffix, err := randString()
require.NoError(t, err)
redirectURL := fmt.Sprintf("https://custom-port-%s.example.com:8443/secure/", suffix)

runTestCase(t, &testCase{
gatewayImplementation: istio.ProviderName,
providers: []string{ingressnginx.Name},
providerFlags: map[string]map[string]string{
ingressnginx.Name: {
ingressnginx.NginxIngressClassFlag: ingressnginx.NginxIngressClass,
},
},
ingresses: []*networkingv1.Ingress{
basicIngress().
withName("redirect-with-port").
withIngressClass(ingressnginx.NginxIngressClass).
withAnnotation("nginx.ingress.kubernetes.io/temporal-redirect", redirectURL).
build(),
},
verifiers: map[string][]verifier{
"redirect-with-port": {
&httpRequestVerifier{
path: "/",
allowedCodes: []int{
http.StatusFound, // 302
},
headerMatches: []headerMatch{
{
name: "Location",
patterns: []*maybeNegativePattern{
{pattern: regexp.MustCompile("^" + regexp.QuoteMeta(redirectURL) + "$")},
},
},
},
},
},
},
})
})

t.Run("both redirect annotations - temporal takes priority", func(t *testing.T) {
suffix, err := randString()
require.NoError(t, err)
permanentURL := fmt.Sprintf("https://permanent-%s.example.com/path/", suffix)
temporalURL := fmt.Sprintf("https://temporal-%s.example.com/path/", suffix)

runTestCase(t, &testCase{
gatewayImplementation: istio.ProviderName,
providers: []string{ingressnginx.Name},
providerFlags: map[string]map[string]string{
ingressnginx.Name: {
ingressnginx.NginxIngressClassFlag: ingressnginx.NginxIngressClass,
},
},
ingresses: []*networkingv1.Ingress{
basicIngress().
withName("both-redirects").
withIngressClass(ingressnginx.NginxIngressClass).
withAnnotation("nginx.ingress.kubernetes.io/permanent-redirect", permanentURL).
withAnnotation("nginx.ingress.kubernetes.io/temporal-redirect", temporalURL).
build(),
},
verifiers: map[string][]verifier{
"both-redirects": {
&httpRequestVerifier{
path: "/",
allowedCodes: []int{
http.StatusFound, // 302 - temporal takes priority
},
headerMatches: []headerMatch{
{
name: "Location",
patterns: []*maybeNegativePattern{
{pattern: regexp.MustCompile("^" + regexp.QuoteMeta(temporalURL) + "$")},
},
},
},
},
},
},
})
})
})
}
9 changes: 9 additions & 0 deletions pkg/i2gw/providers/ingressnginx/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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
Loading