Skip to content

Commit

Permalink
chore(homepage): optimize generation and add test (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros-k committed Dec 23, 2023
1 parent 0595e8f commit 88b97a2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 16 deletions.
66 changes: 59 additions & 7 deletions library/common-test/tests/ingress/homepage_test.yaml
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -93,7 +114,7 @@ tests:
equal:
path: metadata.namespace
value: test-release-namespace
- documentIndex: &otherIngressDoc 2
- documentIndex: &otherIngressDoc 3
isKind:
of: Ingress
- documentIndex: *otherIngressDoc
Expand All @@ -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"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/common/Chart.yaml
Expand Up @@ -15,4 +15,4 @@ maintainers:
name: common
sources: null
type: library
version: 16.2.19
version: 16.2.20
1 change: 1 addition & 0 deletions library/common/templates/class/_ingress.tpl
Expand Up @@ -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 -}}
Expand Down
7 changes: 6 additions & 1 deletion library/common/templates/lib/ingress/_targetSelector.tpl
Expand Up @@ -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 -}}
16 changes: 9 additions & 7 deletions library/common/templates/lib/ingress/integrations/_homepage.tpl
Expand Up @@ -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 -}}

Expand All @@ -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" -}}
Expand Down

0 comments on commit 88b97a2

Please sign in to comment.