initial portal and context combined tests

This commit is contained in:
Stavros kois
2023-12-08 16:20:25 +02:00
parent 67ec3ae9a9
commit 3a0862864f
4 changed files with 483 additions and 174 deletions

View File

@@ -7,6 +7,7 @@ release:
tests:
- it: should pass with service default values
set:
createChartContextConfigmap: true
enabled: true
port: 12345
service:
@@ -33,6 +34,7 @@ tests:
podCIDR: 172.16.0.0/16
svcCIDR: 172.17.0.0/16
appUrl: http://127.0.0.1:12345
appUrlWithPortAndPath: http://127.0.0.1:12345/
appHost: "127.0.0.1"
appPort: "12345"
appPath: /
@@ -40,6 +42,7 @@ tests:
- it: should pass with service loadBalancer values
set:
createChartContextConfigmap: true
ip: 1.2.3.4
service:
my-service:
@@ -67,6 +70,7 @@ tests:
podCIDR: 172.16.0.0/16
svcCIDR: 172.17.0.0/16
appUrl: http://1.2.3.4:12345
appUrlWithPortAndPath: http://1.2.3.4:12345/
appHost: "1.2.3.4"
appPort: "12345"
appPath: /
@@ -74,6 +78,7 @@ tests:
- it: should pass with ingress
set:
createChartContextConfigmap: true
enabled: true
host: example.com
path: /some-path
@@ -115,6 +120,7 @@ tests:
podCIDR: 172.16.0.0/16
svcCIDR: 172.17.0.0/16
appUrl: https://example.com
appUrlWithPortAndPath: https://example.com:443/some-path
appHost: "example.com"
appPort: "443"
appPath: /some-path
@@ -122,6 +128,7 @@ tests:
- it: should pass with ingress and traefik integration
set:
createChartContextConfigmap: true
operator:
verify:
enabled: false
@@ -163,6 +170,7 @@ tests:
podCIDR: 172.16.0.0/16
svcCIDR: 172.17.0.0/16
appUrl: http://example.com
appUrlWithPortAndPath: http://example.com:80/some-path
appHost: "example.com"
appPort: "80"
appPath: /some-path
@@ -170,6 +178,7 @@ tests:
- it: should pass with ingress and tls and traefik integration
set:
createChartContextConfigmap: true
operator:
verify:
enabled: false
@@ -215,6 +224,7 @@ tests:
podCIDR: 172.16.0.0/16
svcCIDR: 172.17.0.0/16
appUrl: https://example.com
appUrlWithPortAndPath: https://example.com:443/some-path
appHost: "example.com"
appPort: "443"
appPath: /some-path
@@ -222,6 +232,7 @@ tests:
- it: should pass with ingress and tls and traefik integration
set:
createChartContextConfigmap: true
operator:
verify:
enabled: false
@@ -267,6 +278,7 @@ tests:
podCIDR: 172.16.0.0/16
svcCIDR: 172.17.0.0/16
appUrl: https://example.com
appUrlWithPortAndPath: https://example.com:443/some-path
appHost: "example.com"
appPort: "443"
appPath: /some-path
@@ -274,6 +286,7 @@ tests:
- it: should pass with ingress and certManager integration and traefik integration
set:
createChartContextConfigmap: true
operator:
verify:
enabled: false
@@ -318,6 +331,7 @@ tests:
podCIDR: 172.16.0.0/16
svcCIDR: 172.17.0.0/16
appUrl: https://example.com
appUrlWithPortAndPath: https://example.com:443/some-path
appHost: "example.com"
appPort: "443"
appPath: /some-path

View File

@@ -0,0 +1,341 @@
suite: portal test
templates:
- common.yaml
release:
name: test-release-name
namespace: test-release-namespace
tests:
- it: should pass with service default values
set:
global: &global
namespace: ix-ns
ixChartContext:
imNotEmpty: true
enabled: true
port: 12345
service:
my-service:
enabled: "{{ .Values.enabled }}"
primary: true
ports:
my-port:
enabled: "{{ .Values.enabled }}"
primary: true
port: "{{ .Values.port }}"
portal:
main:
enabled: "{{ .Values.enabled }}"
asserts:
- documentIndex: &configMapDoc 1
isKind:
of: ConfigMap
- documentIndex: *configMapDoc
equal:
path: metadata.name
value: tcportal-main
- documentIndex: *configMapDoc
equal:
path: data
value:
host: $node_ip
path: /
port: "12345"
protocol: http
url: http://$node_ip:12345/
- it: should pass with service loadBalancer values
set:
global: *global
ip: 1.2.3.4
service:
my-service:
enabled: true
primary: true
type: LoadBalancer
loadBalancerIP: "{{ .Values.ip }}"
ports:
my-port:
enabled: true
primary: true
port: 12345
portal:
main:
enabled: true
asserts:
- documentIndex: &configMapDoc 1
isKind:
of: ConfigMap
- documentIndex: *configMapDoc
equal:
path: metadata.name
value: tcportal-main
- documentIndex: *configMapDoc
equal:
path: data
value:
host: 1.2.3.4
path: /
port: "12345"
protocol: http
url: http://1.2.3.4:12345/
- it: should pass with ingress
set:
global: *global
portal:
main:
enabled: true
enabled: true
host: example.com
path: /some-path
operator:
verify:
enabled: false
ingress:
my-ingress:
enabled: "{{ .Values.enabled }}"
primary: true
integrations:
traefik:
enabled: false
hosts:
- host: "{{ .Values.host }}"
paths:
- path: "{{ .Values.path }}"
service:
my-service:
enabled: true
primary: true
ports:
my-port:
enabled: true
primary: true
port: 12345
asserts:
- documentIndex: &configMapDoc 2
isKind:
of: ConfigMap
- documentIndex: *configMapDoc
equal:
path: metadata.name
value: tcportal-main
- documentIndex: *configMapDoc
equal:
path: data
value:
host: example.com
path: /some-path
port: "443"
protocol: https
url: https://example.com:443/some-path
- it: should pass with ingress and traefik integration
set:
global: *global
portal:
main:
enabled: true
operator:
verify:
enabled: false
ingress:
my-ingress:
enabled: true
primary: true
integrations:
traefik:
enabled: true
entrypoints:
- web
enableFixedMiddlewares: false
hosts:
- host: example.com
paths:
- path: /some-path
service:
my-service:
enabled: true
primary: true
ports:
my-port:
enabled: true
primary: true
port: 12345
asserts:
- documentIndex: &configMapDoc 2
isKind:
of: ConfigMap
- documentIndex: *configMapDoc
equal:
path: metadata.name
value: tcportal-main
- documentIndex: *configMapDoc
equal:
path: data
value:
host: example.com
path: /some-path
port: "80"
protocol: http
url: http://example.com:80/some-path
- it: should pass with ingress and tls and traefik integration
set:
global: *global
portal:
main:
enabled: true
operator:
verify:
enabled: false
ingress:
my-ingress:
enabled: true
primary: true
integrations:
traefik:
enabled: true
entrypoints:
- web
enableFixedMiddlewares: false
hosts:
- host: example.com
paths:
- path: /some-path
tls:
- hosts:
- example.com
secretName: example-tls
service:
my-service:
enabled: true
primary: true
ports:
my-port:
enabled: true
primary: true
port: 12345
asserts:
- documentIndex: &configMapDoc 2
isKind:
of: ConfigMap
- documentIndex: *configMapDoc
equal:
path: metadata.name
value: tcportal-main
- documentIndex: *configMapDoc
equal:
path: data
value:
host: example.com
path: /some-path
port: "443"
protocol: https
url: https://example.com:443/some-path
- it: should pass with ingress and tls and traefik integration
set:
global: *global
portal:
main:
enabled: true
operator:
verify:
enabled: false
ingress:
my-ingress:
enabled: true
primary: true
integrations:
traefik:
enabled: true
entrypoints:
- web
enableFixedMiddlewares: false
hosts:
- host: example.com
paths:
- path: /some-path
tls:
- hosts:
- example.com
secretName: example-tls
service:
my-service:
enabled: true
primary: true
ports:
my-port:
enabled: true
primary: true
port: 12345
asserts:
- documentIndex: &configMapDoc 2
isKind:
of: ConfigMap
- documentIndex: *configMapDoc
equal:
path: metadata.name
value: tcportal-main
- documentIndex: *configMapDoc
equal:
path: data
value:
host: example.com
path: /some-path
port: "443"
protocol: https
url: https://example.com:443/some-path
- it: should pass with ingress and certManager integration and traefik integration
set:
global: *global
portal:
main:
enabled: true
operator:
verify:
enabled: false
ingress:
my-ingress:
enabled: true
primary: true
integrations:
certManager:
enabled: true
certificateIssuer: some-issuer
traefik:
enabled: true
entrypoints:
- web
enableFixedMiddlewares: false
hosts:
- host: example.com
paths:
- path: /some-path
service:
my-service:
enabled: true
primary: true
ports:
my-port:
enabled: true
primary: true
port: 12345
asserts:
- documentIndex: &configMapDoc 2
isKind:
of: ConfigMap
- documentIndex: *configMapDoc
equal:
path: metadata.name
value: tcportal-main
- documentIndex: *configMapDoc
equal:
path: data
value:
host: example.com
path: /some-path
port: "443"
protocol: https
url: https://example.com:443/some-path

View File

@@ -1,5 +1,31 @@
{{/* Returns the primary Workload object */}}
{{- define "tc.v1.common.lib.util.chartcontext" -}}
{{/* Prepare an empty object so it the chartcontext.data util behave properly */}}
{{- $objectData := (dict
"override" dict
"targetSelector" dict
"path" ""
"isPortal" false
) -}}
{{- $context := (include "tc.v1.common.lib.util.chartcontext.data" (dict "rootCtx" $ "objectData" $objectData) | fromYaml) -}}
{{- $_ := set $.Values "chartContext" $context -}}
{{/* This flag is only used in CI/Unit Tests so we can confirm that $context is correctly generated */}}
{{- if $.Values.createChartContextConfigmap -}}
{{- $_ := set $.Values.configmap "chart-context" (dict
"enabled" true
"data" $context
) -}}
{{- end -}}
{{- end -}}
{{- define "tc.v1.common.lib.util.chartcontext.data" -}}
{{- $rootCtx := .rootCtx -}}
{{- $objectData := .objectData -}}
{{/* Create defaults */}}
{{- $protocol := "http" -}}
{{- $host := "127.0.0.1" -}}
@@ -8,23 +34,39 @@
{{- $podCIDR := "172.16.0.0/16" -}}
{{- $svcCIDR := "172.17.0.0/16" -}}
{{- if $objectData.isPortal -}}
{{/* Adjust some defaults */}}
{{- $host = "$node_ip" -}}
{{- $path = $objectData.path | default "/" -}}
{{- end -}}
{{/* TrueNAS SCALE specific code */}}
{{- if $.Values.global.ixChartContext -}}
{{- if $.Values.global.ixChartContext.kubernetes_config -}}
{{- $podCIDR = $.Values.global.ixChartContext.kubernetes_config.cluster_cidr -}}
{{- $svcCIDR = $.Values.global.ixChartContext.kubernetes_config.service_cidr -}}
{{- if $rootCtx.Values.global.ixChartContext -}}
{{- if $rootCtx.Values.global.ixChartContext.kubernetes_config -}}
{{- $podCIDR = $rootCtx.Values.global.ixChartContext.kubernetes_config.cluster_cidr -}}
{{- $svcCIDR = $rootCtx.Values.global.ixChartContext.kubernetes_config.service_cidr -}}
{{- end -}}
{{- else -}}
{{/* TODO: Find ways to implement CIDR detection */}}
{{- end -}}
{{/* If there is ingress, get data from the primary */}}
{{- $primaryIngressName := include "tc.v1.common.lib.util.ingress.primary" (dict "rootCtx" $) -}}
{{- $primaryIngress := (get $.Values.ingress $primaryIngressName) -}}
{{- with $primaryIngress -}}
{{- $primaryIngressName := include "tc.v1.common.lib.util.ingress.primary" (dict "rootCtx" $rootCtx) -}}
{{- $selectedIngress := (get $rootCtx.Values.ingress $primaryIngressName) -}}
{{- with $objectData.targetSelector -}}
{{- if .ingress -}}
{{- $ing := (get $rootCtx.Values.ingress .ingress) -}}
{{- if $ing -}}
{{- $selectedIngress = $ing -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- with $selectedIngress -}}
{{- $firstHost := ((.hosts | default list) | mustFirst) -}}
{{- if $firstHost -}}
{{- $host = tpl $firstHost.host $ -}}
{{- $host = tpl $firstHost.host $rootCtx -}}
{{- $firstPath := (($firstHost.paths | default list) | mustFirst) -}}
{{- if $firstPath -}}
{{- $path = $firstPath.path -}}
@@ -60,59 +102,91 @@
{{- end -}}
{{/* If there is no ingress, we have to use service */}}
{{- if not $primaryIngress -}}
{{- $primaryServiceName := include "tc.v1.common.lib.util.service.primary" (dict "rootCtx" $) -}}
{{- $primaryService := (get $.Values.service $primaryServiceName) -}}
{{- if not $selectedIngress -}}
{{- with $objectData.targetSelector -}}
{{- if .service -}}
{{- $svc := (get $rootCtx.Values.service .service) -}}
{{- if $svc -}}
{{- $selectedService = $svc -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $primaryServiceName := include "tc.v1.common.lib.util.service.primary" (dict "rootCtx" $rootCtx) -}}
{{- $selectedService := (get $rootCtx.Values.service $primaryServiceName) -}}
{{- $primaryPort := dict -}}
{{- if $primaryService -}}
{{- $primaryPortName := include "tc.v1.common.lib.util.service.ports.primary" (dict "rootCtx" $ "svcValues" $primaryService) -}}
{{- $primaryPort = (get $primaryService.ports $primaryPortName) -}}
{{- if $selectedService -}}
{{- $primaryPortName := include "tc.v1.common.lib.util.service.ports.primary" (dict "rootCtx" $rootCtx "svcValues" $selectedService) -}}
{{- $selectedPort := (get $selectedService.ports $primaryPortName) -}}
{{- $port = tpl ($primaryPort.port | toString) $ -}}
{{- with $objectData.targetSelector -}}
{{- if .port -}}
{{- $port := (get $selectedService.ports .port) -}}
{{- if $port -}}
{{- $selectedPort = $port -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if mustHas $primaryPort.type (list "http" "https") -}}
{{- $protocol = $primaryPort.type -}}
{{- $port = tpl ($selectedPort.port | toString) $rootCtx -}}
{{- if mustHas $selectedPort.type (list "http" "https") -}}
{{- $protocol = $selectedPort.type -}}
{{- else -}}
{{- $protocol = "http" -}}
{{- end -}}
{{- if eq $primaryService.type "LoadBalancer" -}}
{{- with $primaryService.loadBalancerIP -}}
{{- $host = tpl . $ | toString -}}
{{- if eq $selectedService.type "LoadBalancer" -}}
{{- with $selectedService.loadBalancerIP -}}
{{- $host = tpl . $rootCtx | toString -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{/* Overrides */}}
{{- with $objectData.override -}}
{{- if .protocol -}}
{{- $protocol = .protocol -}}
{{- end -}}
{{- if .host -}}
{{- $host = .host -}}
{{- end -}}
{{- if .port -}}
{{- $port = .port -}}
{{- end -}}
{{- end -}}
{{/* URL Will not include the path. */}}
{{- $url := printf "%s://%s:%s" $protocol $host $port -}}
{{- $urlWithPortAndPath := printf "%s://%s:%s%s" $protocol $host $port $path -}}
{{/* Clean up the URL */}}
{{- $port = $port | toString -}}
{{- if eq $port "443" -}}
{{- $url = $url | trimSuffix ":443" -}}
{{- $url = $url | replace $protocol "https" -}}
{{- $urlWithPortAndPath = $urlWithPortAndPath | replace $protocol "https" -}}
{{- $protocol = "https" -}}
{{- end -}}
{{- if eq $port "80" -}}
{{- $url = $url | trimSuffix ":80" -}}
{{- $url = $url | replace $protocol "http" -}}
{{- $urlWithPortAndPath = $urlWithPortAndPath | replace $protocol "http" -}}
{{- $protocol = "http" -}}
{{- end -}}
{{- $context := (dict
"podCIDR" $podCIDR
"svcCIDR" $svcCIDR
"appUrl" $url
"appHost" $host
"appPort" $port
"appPath" $path
"appProtocol" $protocol
"podCIDR" $podCIDR "svcCIDR" $svcCIDR
"appUrl" $url "appUrlWithPortAndPath" $urlWithPortAndPath
"appHost" $host "appPort" $port
"appPath" $path "appProtocol" $protocol
) -}}
{{- $_ := set $.Values "chartContext" $context -}}
{{- $_ := set $.Values.configmap "chart-context" (dict
"enabled" true
"data" $context
) -}}
{{- $context | toYaml -}}
{{- end -}}

View File

@@ -4,7 +4,9 @@
*/}}
{{- define "tc.v1.common.spawner.portal" -}}
{{- range $name, $portal := .Values.portal -}}
{{/* Only run this on SCALE */}}
{{- if $.Values.global.ixChartContext -}}
{{- range $name, $portal := $.Values.portal -}}
{{- $enabled := (include "tc.v1.common.lib.util.enabled" (dict
"rootCtx" $ "objectData" $portal
@@ -15,164 +17,42 @@
{{/* Create a copy of the portal */}}
{{- $objectData := (mustDeepCopy $portal) -}}
{{- $override := $objectData.override -}}
{{- $targetSelector := $objectData.targetSelector -}}
{{- $_ := set $objectData "isPortal" true -}}
{{/* Create defaults */}}
{{- $protocol := "http" -}}
{{- $host := "$node_ip" -}}
{{- $port := "443" -}}
{{- $path := $objectData.path | default "/" -}}
{{- $url := "" -}}
{{/* Get service, default to primary */}}
{{- $serviceData := dict "targetSelector" $targetSelector.service -}}
{{- $selectedService := fromYaml ( include "tc.v1.common.lib.helpers.getSelectedServiceValues" (dict "rootCtx" $ "objectData" $serviceData)) }}
{{/* read loadbalancer IPs for metallb */}}
{{- if eq $selectedService.type "LoadBalancer" -}}
{{- with $selectedService.loadBalancerIP -}}
{{- $host = toString . -}}
{{- end -}}
{{/* set temporary storage for port name and port */}}
{{- $targetPort := "" -}}
{{- $selectedPort := "" -}}
{{/* Fetch port values */}}
{{- if $targetSelector.port -}}
{{- $targetPort = $targetSelector.port -}}
{{- else -}}
{{- $targetPort = include "tc.v1.common.lib.util.service.ports.primary" (dict "svcName" $selectedService.shortName "svcValues" $selectedService ) -}}
{{- end -}}
{{- $selectedPort = get $selectedService.ports $targetPort -}}
{{/* store port number */}}
{{- $port = $selectedPort.port -}}
{{- end -}}
{{/* set temporary storage for ingress name and port */}}
{{- $targetIngress := "" -}}
{{- $selectedIngress := "" -}}
{{/* Fetch ingress values */}}
{{- if $targetSelector.ingress -}}
{{- $targetIngress = $targetSelector.ingress -}}
{{- else -}}
{{- $targetIngress = ( include "tc.v1.common.lib.util.ingress.primary" $ ) -}}
{{- end -}}
{{- $selectedIngress = get $.Values.ingress $targetIngress -}}
{{/* store host from ingress number */}}
{{- if $selectedIngress -}}
{{- if $selectedIngress.enabled -}}
{{- with (index $selectedIngress.hosts 0) }}
{{- $host = .host -}}
{{- end }}
{{/* Get the port for the ingress entrypoint */}}
{{- $traefikNamespace := "tc-system" -}}
{{- if $.Values.operator.traefik -}}
{{- if $.Values.operator.traefik.namespace -}}
{{- $traefikNamespace := $.Values.operator.traefik.namespace -}}
{{- end -}}
{{- end -}}
{{- if $selectedIngress.ingressClassName }}
{{- if $.Values.global.ixChartContext -}}
{{- $traefikNamespace = (printf "ix-%s" $selectedIngress.ingressClassName) -}}
{{- else -}}
{{- $traefikNamespace = $selectedIngress.ingressClassName -}}
{{- end -}}
{{- end -}}
{{- $traefikportalhook := lookup "v1" "ConfigMap" $traefikNamespace "portalhook" }}
{{- $entrypoint := "websecure" }}
{{- $protocol = "https" -}}
{{- if $selectedIngress.entrypoint }}
{{- $entrypoint = $selectedIngress.entrypoint }}
{{- end }}
{{- if $traefikportalhook }}
{{- if ( index $traefikportalhook.data $entrypoint ) }}
{{- $port = ( index $traefikportalhook.data $entrypoint ) }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- $port = ( toString $port ) -}}
{{/* Apply overrides */}}
{{- if $override.protocol -}}
{{- $protocol = $override.protocol -}}
{{- end -}}
{{- if $override.host -}}
{{- $host = $override.host -}}
{{- end -}}
{{- if $override.port -}}
{{- $port = $override.port -}}
{{- end -}}
{{/* sanitise */}}
{{- if eq $port "443" -}}
{{- $protocol = "https" -}}
{{- end -}}
{{- if eq $port "80" -}}
{{- $protocol = "http" -}}
{{- end -}}
{{/* TODO: Reenable when iX fixes bugs crashing GUI on empty port */}}
{{/*
{{- if or ( and ( eq $protocol "https" ) ( eq $port "443" ) ) ( and ( eq $protocol "http" ) ( eq $port "80" ) ) -}}
{{- $port = "" -}}
{{- end -}}
*/}}
{{- $port = toString $port -}}
{{/* Construct URL*/}}
{{- if $port -}}
{{- $url = printf "%s://%s:%s%s" $protocol $host $port $path -}}
{{- else -}}
{{- $url = printf "%s://%s%s" $protocol $host $path -}}
{{- end -}}
{{- $context := (include "tc.v1.common.lib.util.chartcontext.data" (dict "rootCtx" $ "objectData" $objectData) | fromYaml) -}}
{{/* create configmap entry*/}}
{{- $portalData := dict "protocol" $protocol "host" $host "port" $port "path" $path "url" $url -}}
{{- $portalData := (dict
"protocol" $context.appProtocol "host" $context.appHost
"port" $context.appPort "path" $context.appPath
"url" $context.appUrlWithPortAndPath
) -}}
{{/* construct configmap */}}
{{- $objectName := ( printf "tcportal-%s" $name ) -}}
{{- $configMap := dict "enabled" true "name" $objectName "shortName" $objectName "data" $portalData -}}
{{- $objectName := (printf "tcportal-%s" $name) -}}
{{- $configMap := dict "name" $objectName "shortName" $objectName "data" $portalData -}}
{{/* Perform validations */}} {{/* Configmaps have a max name length of 253 */}}
{{- include "tc.v1.common.lib.chart.names.validation" (dict "name" $objectName "length" 253) -}}
{{- include "tc.v1.common.lib.configmap.validation" (dict "objectData" $configMap) -}}
{{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $configMap "caller" "ConfigMap") -}}
{{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $configMap "caller" "Portal") -}}
{{- if $.Values.global.ixChartContext -}}
{{/* Call class to create the object */}}
{{- include "tc.v1.common.class.configmap" (dict "rootCtx" $ "objectData" $configMap) -}}
{{/* iXportals */}}
{{- $useNodeIP := false -}}
{{- if eq $host "$node_ip" -}}
{{- $useNodeIP = true -}}
{{- if eq $context.appHost "$node_ip" -}}
{{- $useNodeIP = true -}}
{{- end -}}
{{- $iXPortalData := dict "portalName" $name "useNodeIP" $useNodeIP "protocol" $protocol "host" $host "port" $port "path" $path "url" $url -}}
{{- $iXPortalData := (dict
"portalName" $name "useNodeIP" $useNodeIP
"protocol" $context.appProtocol "host" $context.appHost
"port" $context.appPort "path" $context.appPath
"url" $context.appUrlWithPortAndPath
) -}}
{{- $iXPortals := append $.Values.iXPortals $iXPortalData -}}
{{- $_ := set $.Values "iXPortals" $iXPortals -}}