From 7ea9dcf42b3e543ae9a7f078ebf6ee411094f1af Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Tue, 10 Dec 2024 14:29:11 +0200 Subject: [PATCH] chore(common): normalize ingress paths earlier (#29817) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes # **⚙️ Type of change** - [ ] ⚙️ Feature/App addition - [ ] 🪛 Bugfix - [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [x] 🔃 Refactor of current code **🧪 How Has This Been Tested?** **📃 Notes:** **✔️ Checklist:** - [x] ⚖️ My code follows the style guidelines of this project - [x] 👀 I have performed a self-review of my own code - [ ] #️⃣ I have commented my code, particularly in hard-to-understand areas - [ ] 📄 I have made corresponding changes to the documentation - [x] ⚠️ My changes generate no new warnings - [x] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [x] ⬆️ I increased versions for any altered app according to semantic versioning - [x] I made sure the title starts with `feat(chart-name):`, `fix(chart-name):` or `chore(chart-name):` **➕ App addition** If this PR is an app addition please make sure you have done the following. - [ ] 🖼️ I have added an icon in the Chart's root directory called `icon.png` --- _Please don't blindly check all the boxes. Read them and only check those that apply. Those checkboxes are there for the reviewer to see what is this all about and the status of this PR with a quick glance._ --- .../tests/ingress/homepage_test.yaml | 21 ++++++++++++++++++ .../common-test/tests/ingress/rules_test.yaml | 12 ++++++++++ .../common/templates/class/_ingress.tpl | 22 ++++++++++++++----- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/charts/library/common-test/tests/ingress/homepage_test.yaml b/charts/library/common-test/tests/ingress/homepage_test.yaml index d7a587f40ba9..a6918ec7a942 100644 --- a/charts/library/common-test/tests/ingress/homepage_test.yaml +++ b/charts/library/common-test/tests/ingress/homepage_test.yaml @@ -110,6 +110,14 @@ tests: value: some-other-value-slice - key: some-empty-key-slice value: "" + my-ingress5: + enabled: true + integrations: + traefik: *traefik + homepage: + enabled: true + hosts: + - host: no-paths-host asserts: - documentIndex: &ingressDoc 2 isKind: @@ -207,6 +215,19 @@ tests: gethomepage.dev/href: https://test-host/test-path gethomepage.dev/icon: https://truecharts.org/img/hotlink-ok/chart-icons/common-test.webp gethomepage.dev/pod-selector: app.kubernetes.io/instance=test-release-name,pod.lifecycle in (permanent) + - documentIndex: &fifthIngressDoc 6 + isKind: + of: Ingress + - documentIndex: *fifthIngressDoc + isSubset: + path: metadata.annotations + content: + gethomepage.dev/enabled: "true" + gethomepage.dev/name: TestReleaseName + gethomepage.dev/description: Helper chart to test different use cases of the common library + gethomepage.dev/href: https://no-paths-host/ + gethomepage.dev/icon: https://truecharts.org/img/hotlink-ok/chart-icons/common-test.webp + gethomepage.dev/pod-selector: app.kubernetes.io/instance=test-release-name,pod.lifecycle in (permanent) # Failures - it: should fail with podSelector not a slice diff --git a/charts/library/common-test/tests/ingress/rules_test.yaml b/charts/library/common-test/tests/ingress/rules_test.yaml index 2b36ef01bdb4..6fc4ee3e5cc8 100644 --- a/charts/library/common-test/tests/ingress/rules_test.yaml +++ b/charts/library/common-test/tests/ingress/rules_test.yaml @@ -208,6 +208,7 @@ tests: overrideService: name: other-service port: 8080 + - host: host-no-paths integrations: *integrations asserts: - documentIndex: &ingressDoc 2 @@ -248,3 +249,14 @@ tests: name: test-release-name-common-test-other-service port: number: 8080 + + - host: host-no-paths + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: test-release-name-common-test-my-service2 + port: + number: 9000 diff --git a/charts/library/common/templates/class/_ingress.tpl b/charts/library/common/templates/class/_ingress.tpl index 1880739dab42..2125528b26ed 100644 --- a/charts/library/common/templates/class/_ingress.tpl +++ b/charts/library/common/templates/class/_ingress.tpl @@ -26,6 +26,19 @@ objectData: The object data to be used to render the Ingress. {{- $ingressClassName = (tpl $objectData.ingressClassName $rootCtx) -}} {{- end -}} + {{- range $h := $objectData.hosts -}} + {{- $_ := set $h "host" (tpl $h.host $rootCtx) -}} + + {{- if not $h.paths -}} {{/* If no paths given, default to "/" */}} + {{- $_ := set $h "paths" (list (dict "path" "/")) -}} + {{- end -}} + + {{- range $p := $h.paths -}} + {{- $_ := set $p "path" (tpl ($p.path | default "/") $rootCtx) -}} + {{- $_ := set $p "pathType" (tpl ($p.pathType | default "Prefix") $rootCtx) -}} + {{- end -}} + {{- end -}} + {{/* When Stop All is set, force ingressClass "stopped" to yeet ingress from the ingresscontroller */}} {{- if (include "tc.v1.common.lib.util.stopAll" $rootCtx) -}} @@ -59,18 +72,15 @@ spec: ingressClassName: {{ $ingressClassName | default nil }} rules: {{- range $h := $objectData.hosts }} - - host: {{ (tpl $h.host $rootCtx) | quote }} + - host: {{ $h.host | quote }} http: paths: - {{- if not $h.paths -}} {{/* If no paths given, default to "/" */}} - {{- $_ := set $h "paths" (list (dict "path" "/")) -}} - {{- end -}} {{- range $p := $h.paths -}} {{- $newSvcData := (include "tc.v1.common.lib.ingress.backend.data" (dict "rootCtx" $rootCtx "svcData" $svcData "override" $p.overrideService)) | fromYaml }} - - path: {{ tpl ($p.path | default "/") $rootCtx }} - pathType: {{ tpl ($p.pathType | default "Prefix") $rootCtx }} + - path: {{ $p.path }} + pathType: {{ $p.pathType }} backend: service: name: {{ $newSvcData.name }}