From 8e5f88370d3bc941f0fcc491c75ab58e78842dcc Mon Sep 17 00:00:00 2001 From: Jakub Warczarek Date: Thu, 7 Nov 2024 18:38:01 +0100 Subject: [PATCH 1/2] fix: make bug reproducible in tests (ensure not random order of routes) --- internal/dataplane/translator/subtranslator/ingress.go | 6 ++++-- test/integration/ingress_test.go | 7 +------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/internal/dataplane/translator/subtranslator/ingress.go b/internal/dataplane/translator/subtranslator/ingress.go index b8249e6e0d..398d77363d 100644 --- a/internal/dataplane/translator/subtranslator/ingress.go +++ b/internal/dataplane/translator/subtranslator/ingress.go @@ -3,6 +3,7 @@ package subtranslator import ( "fmt" "regexp" + "sort" "strings" "time" "unicode" @@ -235,7 +236,9 @@ func (i *ingressTranslationIndex) Translate() map[string]kongstate.Service { route := meta.translateIntoKongRoute() kongStateService.Routes = append(kongStateService.Routes, *route) } - + sort.SliceStable(kongStateService.Routes, func(i, j int) bool { + return *kongStateService.Routes[i].Name < *kongStateService.Routes[j].Name + }) kongStateServiceCache[kongServiceName] = kongStateService } @@ -651,7 +654,6 @@ func MaybeRewriteURI(service *kongstate.Service, rewriteURIEnable bool) error { }, }, }) - } return nil } diff --git a/test/integration/ingress_test.go b/test/integration/ingress_test.go index 37273bfd7b..f4736abd91 100644 --- a/test/integration/ingress_test.go +++ b/test/integration/ingress_test.go @@ -1092,9 +1092,7 @@ func TestIngressRewriteURI(t *testing.T) { t.Logf("creating an Ingress for service %s without rewrite annotation", service.Name) const serviceDomainDirect = "direct.example" - ingressDirect := generators.NewIngressForService("/", map[string]string{ - annotations.AnnotationPrefix + annotations.StripPathKey: "true", - }, service) + ingressDirect := generators.NewIngressForService("/", nil, service) ingressDirect.Name += "-direct" ingressDirect.Spec.IngressClassName = kong.String(consts.IngressClass) for i := range ingressDirect.Spec.Rules { @@ -1165,7 +1163,6 @@ func TestIngressRewriteURI(t *testing.T) { t.Logf("creating an Ingress for service %s with rewrite annotation", service.Name) const serviceDomainRewrite = "rewrite.example" ingressRewrite := generators.NewIngressForService("/~/foo/(.*)", map[string]string{ - annotations.AnnotationPrefix + annotations.StripPathKey: "true", annotations.AnnotationPrefix + annotations.RewriteURIKey: "/image/$1", }, service) ingressRewrite.Name += "-rewrite" @@ -1178,8 +1175,6 @@ func TestIngressRewriteURI(t *testing.T) { require.NoError(t, err) cleaner.Add(ingressRewrite) - t.Log("rewrite uri feature is enabled") - t.Log("try to access the ingress with valid capture group") helpers.EventuallyGETPath(t, proxyHTTPURL, serviceDomainRewrite, "/foo/jpeg", nil, http.StatusOK, consts.JPEGMagicNumber, nil, ingressWait, waitTick) From 22c892df032984476242bb8baa29258b315e7cdc Mon Sep 17 00:00:00 2001 From: Jakub Warczarek Date: Thu, 7 Nov 2024 18:39:18 +0100 Subject: [PATCH 2/2] fix: annotation konghq.com/rewrite always works --- CHANGELOG.md | 9 +++++++++ internal/dataplane/translator/subtranslator/ingress.go | 3 +-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ff1aaa683..709bf51941 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,9 +100,18 @@ Adding a new version? You'll need three changes: ## Unreleased +### Changed + - Bump version of Gateway API to `1.2.0`. [#6571](https://github.com/Kong/kubernetes-ingress-controller/pull/6571) +### Fixed + +- Fixed annotation `konghq.com/rewrite` that was not being applied sometimes + when `Ingress` without annotation and a different `Ingress` with annotation + pointed to the same `Service`. + [#6569](https://github.com/Kong/kubernetes-ingress-controller/pull/6626) + ## [3.3.1] > Release date: 2024-08-28 diff --git a/internal/dataplane/translator/subtranslator/ingress.go b/internal/dataplane/translator/subtranslator/ingress.go index 398d77363d..0e95f9eff4 100644 --- a/internal/dataplane/translator/subtranslator/ingress.go +++ b/internal/dataplane/translator/subtranslator/ingress.go @@ -632,12 +632,11 @@ func MaybeRewriteURI(service *kongstate.Service, rewriteURIEnable bool) error { rewriteURI, exists := annotations.ExtractRewriteURI(route.Ingress.Annotations) if !exists { - return nil + continue } if !rewriteURIEnable { return fmt.Errorf("konghq.com/rewrite annotation not supported when rewrite uris disabled") } - if rewriteURI == "" { rewriteURI = "/" }