Skip to content

Commit f24155f

Browse files
ziyang-lin-404MonPote
authored andcommitted
Enhance ingress path handling and annotation generation
1 parent 95c68f7 commit f24155f

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

internal/controller/scalityuicomponentexposer/controller_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,53 @@ var _ = Describe("ScalityUIComponentExposer Controller", func() {
396396
_ = k8sClient.Delete(ctx, uiWithNetworks)
397397
})
398398
})
399+
400+
Context("When testing path handling", func() {
401+
It("should preserve original path format without forcing trailing slashes", func() {
402+
networks := &uiv1alpha1.UINetworks{
403+
Host: "test.example.com",
404+
}
405+
406+
By("Testing path without trailing slash - should remain as-is")
407+
rules := getIngressRules(networks, "/test-component")
408+
Expect(rules).To(HaveLen(1))
409+
Expect(rules[0].Host).To(Equal("test.example.com"))
410+
Expect(rules[0].Path).To(Equal("/test-component"))
411+
412+
By("Testing path with trailing slash - should remain as-is")
413+
rules = getIngressRules(networks, "/test-component/")
414+
Expect(rules).To(HaveLen(1))
415+
Expect(rules[0].Path).To(Equal("/test-component/"))
416+
417+
By("Testing empty path - should default to root")
418+
rules = getIngressRules(networks, "")
419+
Expect(rules).To(HaveLen(1))
420+
Expect(rules[0].Path).To(Equal("/"))
421+
422+
By("Testing root path - should remain as-is")
423+
rules = getIngressRules(networks, "/")
424+
Expect(rules).To(HaveLen(1))
425+
Expect(rules[0].Path).To(Equal("/"))
426+
})
427+
428+
It("should generate flexible nginx rewrite rules for runtime configuration", func() {
429+
networks := &uiv1alpha1.UINetworks{
430+
Host: "test.example.com",
431+
}
432+
433+
By("Testing annotation generation for path without trailing slash")
434+
annotations := getIngressAnnotations(networks, "/test-app", "test-exposer")
435+
Expect(annotations).To(HaveKey("nginx.ingress.kubernetes.io/configuration-snippet"))
436+
437+
snippet := annotations["nginx.ingress.kubernetes.io/configuration-snippet"]
438+
// Should match both /test-app/.well-known/... and /test-app//.well-known/...
439+
Expect(snippet).To(ContainSubstring("^/test-app/?/?"))
440+
441+
By("Testing annotation generation for path with trailing slash")
442+
annotations = getIngressAnnotations(networks, "/test-app/", "test-exposer")
443+
snippet = annotations["nginx.ingress.kubernetes.io/configuration-snippet"]
444+
// Should normalize to /test-app and still be flexible
445+
Expect(snippet).To(ContainSubstring("^/test-app/?/?"))
446+
})
447+
})
399448
})

internal/controller/scalityuicomponentexposer/ingress.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scalityuicomponentexposer
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/scality/reconciler-framework/reconciler"
78
"github.com/scality/reconciler-framework/resources"
@@ -98,10 +99,17 @@ func getIngressClassName(networks *uiv1alpha1.UINetworks) string {
9899

99100
// getIngressRules returns the ingress rules based on networks config
100101
func getIngressRules(networks *uiv1alpha1.UINetworks, path string) []resources.IngressHostPath {
102+
// Use the path as-is, without forcing trailing slash
103+
// Default to "/" if path is empty
104+
ingressPath := path
105+
if ingressPath == "" {
106+
ingressPath = "/"
107+
}
108+
101109
rules := []resources.IngressHostPath{
102110
{
103111
Host: networks.Host,
104-
Path: path + "/",
112+
Path: ingressPath,
105113
},
106114
}
107115
return rules
@@ -123,11 +131,13 @@ func getIngressAnnotations(networks *uiv1alpha1.UINetworks, path string, exposer
123131

124132
// Add rewrite annotation for exposer runtime configuration path
125133
// Use configuration-snippet for conditional rewriting
134+
// Handle both paths with and without trailing slashes
135+
normalizedPath := strings.TrimSuffix(path, "/")
126136
configSnippet := fmt.Sprintf(`
127-
if ($request_uri ~ "^%s/\\.well-known/runtime-app-configuration(\\?.*)?$") {
137+
if ($request_uri ~ "^%s/?/?\\.well-known/runtime-app-configuration(\\?.*)?$") {
128138
rewrite ^.*$ /.well-known/%s/%s break;
129139
}
130-
`, path, configsSubdirectory, exposerName)
140+
`, normalizedPath, configsSubdirectory, exposerName)
131141
annotations["nginx.ingress.kubernetes.io/configuration-snippet"] = configSnippet
132142

133143
return annotations

0 commit comments

Comments
 (0)