From 88b97a2e629de73f36093e939085681fcff7279e Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Sun, 24 Dec 2023 01:13:31 +0200 Subject: [PATCH] chore(homepage): optimize generation and add test (#649) --- .../tests/ingress/homepage_test.yaml | 66 +++++++++++++++++-- library/common/Chart.yaml | 2 +- library/common/templates/class/_ingress.tpl | 1 + .../templates/lib/ingress/_targetSelector.tpl | 7 +- .../lib/ingress/integrations/_homepage.tpl | 16 +++-- 5 files changed, 76 insertions(+), 16 deletions(-) diff --git a/library/common-test/tests/ingress/homepage_test.yaml b/library/common-test/tests/ingress/homepage_test.yaml index a86314d09..0e6fb5854 100644 --- a/library/common-test/tests/ingress/homepage_test.yaml +++ b/library/common-test/tests/ingress/homepage_test.yaml @@ -18,12 +18,20 @@ tests: operator: &operator verify: enabled: false - service: &service + service: my-service: enabled: true primary: true ports: main: + enabled: true + primary: true + protocol: https + port: 9443 + my-service2: + enabled: true + ports: + port-2: enabled: true primary: true port: 80 @@ -71,8 +79,20 @@ tests: - host: test-host paths: - path: /test-path + my-ingress3: + enabled: true + targetSelector: + my-service2: port-2 + integrations: + traefik: *traefik + homepage: + enabled: true + hosts: + - host: "{{ .Values.someHost }}" + paths: + - path: "{{ .Values.somePath }}" asserts: - - documentIndex: &ingressDoc 1 + - documentIndex: &ingressDoc 2 isKind: of: Ingress - documentIndex: *ingressDoc @@ -82,9 +102,10 @@ tests: gethomepage.dev/enabled: "true" gethomepage.dev/name: CommonTest gethomepage.dev/description: Helper chart to test different use cases of the common library - gethomepage.dev/href: https://test-host/test-path/ + gethomepage.dev/href: https://test-host/test-path + gethomepage.dev/widget.url: https://test-release-name-common-test.test-release-namespace.svc:9443 gethomepage.dev/icon: https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png - gethomepage.dev/widget.type: common-test + gethomepage.dev/widget.type: commontest - documentIndex: *ingressDoc equal: path: metadata.name @@ -93,7 +114,7 @@ tests: equal: path: metadata.namespace value: test-release-namespace - - documentIndex: &otherIngressDoc 2 + - documentIndex: &otherIngressDoc 3 isKind: of: Ingress - documentIndex: *otherIngressDoc @@ -104,7 +125,7 @@ tests: gethomepage.dev/name: some-name gethomepage.dev/description: some-desc gethomepage.dev/group: some-group - gethomepage.dev/href: https://test-host/test-path/ + gethomepage.dev/href: https://test-host/test-path gethomepage.dev/icon: some-icon gethomepage.dev/widget.url: some-url gethomepage.dev/weight: "1" @@ -122,12 +143,43 @@ tests: equal: path: metadata.namespace value: test-release-namespace + - documentIndex: &thirdIngressDoc 4 + isKind: + of: Ingress + - documentIndex: *thirdIngressDoc + equal: + path: metadata.annotations + value: + gethomepage.dev/enabled: "true" + gethomepage.dev/name: CommonTest + gethomepage.dev/description: Helper chart to test different use cases of the common library + gethomepage.dev/href: https://test-host/test-path + gethomepage.dev/widget.url: http://test-release-name-common-test-my-service2.test-release-namespace.svc:80 + gethomepage.dev/icon: https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png + gethomepage.dev/widget.type: commontest + - documentIndex: *thirdIngressDoc + equal: + path: metadata.name + value: test-release-name-common-test-my-ingress3 + - documentIndex: *thirdIngressDoc + equal: + path: metadata.namespace + value: test-release-namespace # Failures - it: should fail with podSelector not a slice set: operator: *operator - service: *service + service: &service + my-service: + enabled: true + primary: true + ports: + main: + enabled: true + primary: true + protocol: https + port: 9443 ingress: my-ingress1: enabled: true diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 7fd4ce978..0241f05d7 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: common sources: null type: library -version: 16.2.19 +version: 16.2.20 diff --git a/library/common/templates/class/_ingress.tpl b/library/common/templates/class/_ingress.tpl index 3b0049cdf..4c465f00f 100644 --- a/library/common/templates/class/_ingress.tpl +++ b/library/common/templates/class/_ingress.tpl @@ -12,6 +12,7 @@ objectData: The object data to be used to render the Ingress. {{- $objectData := .objectData -}} {{- $svcData := (include "tc.v1.common.lib.ingress.targetSelector" (dict "rootCtx" $rootCtx "objectData" $objectData) | fromYaml) -}} + {{- $_ := set $objectData "selectedService" $svcData -}} {{- if not (hasKey $objectData "integrations") -}} {{- $_ := set $objectData "integrations" dict -}} diff --git a/library/common/templates/lib/ingress/_targetSelector.tpl b/library/common/templates/lib/ingress/_targetSelector.tpl index 171156310..91a1e02b5 100644 --- a/library/common/templates/lib/ingress/_targetSelector.tpl +++ b/library/common/templates/lib/ingress/_targetSelector.tpl @@ -79,7 +79,12 @@ {{- $expandedSvcName = printf "%s-%s" $expandedSvcName $svcName -}} {{- end -}} - {{- $selectedService = (dict "name" $expandedSvcName "port" (tpl ($portData.port | toString) $rootCtx)) -}} + {{- $protocol := default "http" -}} + {{- if eq $portData.protocol "https" -}} + {{- $protocol = "https" -}} + {{- end -}} + + {{- $selectedService = (dict "name" $expandedSvcName "port" (tpl ($portData.port | toString) $rootCtx) "protocol" $protocol) -}} {{- $selectedService | toYaml -}} {{- end -}} diff --git a/library/common/templates/lib/ingress/integrations/_homepage.tpl b/library/common/templates/lib/ingress/integrations/_homepage.tpl index 0b5a25a36..4c82a38b7 100644 --- a/library/common/templates/lib/ingress/integrations/_homepage.tpl +++ b/library/common/templates/lib/ingress/integrations/_homepage.tpl @@ -9,13 +9,14 @@ {{- end -}} {{- include "tc.v1.common.lib.ingress.integration.homepage.validation" (dict "objectData" $objectData) -}} - {{- $svcData := (include "tc.v1.common.lib.ingress.targetSelector" (dict "rootCtx" $rootCtx "objectData" $objectData) | fromYaml) -}} {{- $name := $homepage.name | default ($rootCtx.Chart.Name | camelcase) -}} {{- $desc := $homepage.description | default $rootCtx.Chart.Description -}} {{- $icon := $homepage.icon | default $rootCtx.Chart.Icon -}} - {{- $type := ( $homepage.widget.type | default $rootCtx.Chart.Name ) | lower -}} - {{- $type = regexReplaceAll "\\W+" $type "" -}} + {{- $defaultType := $rootCtx.Chart.Name | lower -}} + {{/* Remove any non-characters from the default type */}} + {{- $defaultType = regexReplaceAll "\\W+" $defaultType "" -}} + {{- $type := $homepage.widget.type | default $defaultType -}} {{- $url := $homepage.widget.url -}} {{- $href := $homepage.href -}} @@ -29,11 +30,12 @@ {{- end -}} {{- if not $url -}} - {{- $svc := $svcData.name -}} - {{- $port := $svcData.port -}} - {{- $ns := printf "%s" (include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "Ingress")) -}} + {{- $svc := $objectData.selectedService.name -}} + {{- $port := $objectData.selectedService.port -}} + {{- $prot := $objectData.selectedService.protocol -}} + {{- $ns := include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "Ingress") -}} - {{- $url = printf "http://%s.$ns.svc:%s" $svc $ns $port -}} + {{- $url = printf "%s://%s.%s.svc:%s" $prot $svc $ns $port -}} {{- end -}} {{- $_ := set $objectData.annotations "gethomepage.dev/enabled" "true" -}}