From 37b92f265f2d644c95c480be6cbc2745697819e8 Mon Sep 17 00:00:00 2001 From: pat-s Date: Fri, 5 Jul 2024 14:48:17 +0200 Subject: [PATCH] overhaul ingress configuration --- templates/_helpers.tpl | 12 +++ templates/gitea/ingress.yaml | 31 ++----- unittests/ingress/basic.yaml | 93 +++++++++++++++++++ .../ingress.tpl.yaml} | 42 ++++----- values.yaml | 5 +- 5 files changed, 134 insertions(+), 49 deletions(-) create mode 100644 unittests/ingress/basic.yaml rename unittests/{deployment/ingress-configuration.yaml => ingress/ingress.tpl.yaml} (93%) diff --git a/templates/_helpers.tpl b/templates/_helpers.tpl index 4c74291..c4eeec1 100644 --- a/templates/_helpers.tpl +++ b/templates/_helpers.tpl @@ -392,3 +392,15 @@ https {{- define "gitea.serviceAccountName" -}} {{ .Values.serviceAccount.name | default (include "gitea.fullname" .) }} {{- end -}} + +{{- define "ingress.annotations" -}} + {{- if .Values.ingress.annotations }} + annotations: + {{- $tp := typeOf .Values.ingress.annotations }} + {{- if eq $tp "string" }} + {{- tpl .Values.ingress.annotations . | nindent 4 }} + {{- else }} + {{- toYaml .Values.ingress.annotations | nindent 4 }} + {{- end }} + {{- end }} +{{- end -}} \ No newline at end of file diff --git a/templates/gitea/ingress.yaml b/templates/gitea/ingress.yaml index cd743fe..412c8cc 100644 --- a/templates/gitea/ingress.yaml +++ b/templates/gitea/ingress.yaml @@ -1,24 +1,14 @@ {{- if .Values.ingress.enabled -}} {{- $fullName := include "gitea.fullname" . -}} {{- $httpPort := .Values.service.http.port -}} -{{- $apiVersion := "extensions/v1beta1" -}} -{{- if .Values.ingress.apiVersion -}} -{{- $apiVersion = .Values.ingress.apiVersion -}} -{{- else if .Capabilities.APIVersions.Has "networking.k8s.io/v1/Ingress" -}} -{{- $apiVersion = "networking.k8s.io/v1" }} -{{- else if .Capabilities.APIVersions.Has "networking.k8s.io/v1beta1/Ingress" -}} -{{- $apiVersion = "networking.k8s.io/v1beta1" }} -{{- end }} -apiVersion: {{ $apiVersion }} +{{- $pathType := .Values.ingress.pathType -}} +apiVersion: networking.k8s.io/v1 kind: Ingress metadata: - name: {{ $fullName }} + name: {{ template "gitea.fullname" . }} labels: {{- include "gitea.labels" . | nindent 4 }} - annotations: - {{- range $key, $value := .Values.ingress.annotations }} - {{ $key }}: {{ $value | quote }} - {{- end }} + {{- template "ingress.annotations" . }} spec: {{- if .Values.ingress.className }} ingressClassName: {{ tpl .Values.ingress.className . }} @@ -38,21 +28,14 @@ spec: - host: {{ tpl .host $ | quote }} http: paths: - {{- range .paths }} - - path: {{ .path }} - {{- if and .pathType (eq $apiVersion "networking.k8s.io/v1") }} - pathType: {{ .pathType }} - {{- end }} + {{- range (.paths | default (list "/")) }} + - path: {{ . }} + pathType: {{ $pathType }} backend: - {{- if eq $apiVersion "networking.k8s.io/v1" }} service: name: {{ $fullName }}-http port: number: {{ $httpPort }} - {{- else }} - serviceName: {{ $fullName }}-http - servicePort: {{ $httpPort }} - {{- end }} {{- end }} {{- end }} {{- end }} diff --git a/unittests/ingress/basic.yaml b/unittests/ingress/basic.yaml new file mode 100644 index 0000000..733e557 --- /dev/null +++ b/unittests/ingress/basic.yaml @@ -0,0 +1,93 @@ +suite: Test ingress.yaml +templates: + - templates/gitea/ingress.yaml +tests: + - it: should enable ingress when ingress.enabled is true + set: + ingress.enabled: true + ingress.apiVersion: networking.k8s.io/v1 + ingress.annotations: + kubernetes.io/ingress.class: nginx + ingress.className: nginx + ingress.tls: + - hosts: + - example.com + secretName: tls-secret + ingress.hosts: + - host: example.com + paths: ["/"] + asserts: + - hasDocuments: + count: 1 + - isKind: + of: Ingress + - equal: + path: metadata.name + value: RELEASE-NAME-gitea + - matchRegex: + path: apiVersion + pattern: networking.k8s.io/v1 + - equal: + path: spec.ingressClassName + value: nginx + - equal: + path: spec.rules[0].host + value: "example.com" + - equal: + path: spec.tls[0].hosts[0] + value: "example.com" + - equal: + path: spec.tls[0].secretName + value: tls-secret + - equal: + path: metadata.annotations["kubernetes.io/ingress.class"] + value: nginx + + - it: should not create ingress when ingress.enabled is false + set: + ingress.enabled: false + asserts: + - hasDocuments: + count: 0 + + - it: Ingress Class using TPL + set: + global.ingress.className: "ingress-class" + ingress.className: "{{ .Values.global.ingress.className }}" + ingress.enabled: true + ingress.hosts[0].host: "some-host" + ingress.tls: + - secretName: gitea-tls + hosts: + - "some-host" + asserts: + - isKind: + of: Ingress + - equal: + path: spec.tls[0].hosts[0] + value: "some-host" + - equal: + path: spec.rules[0].host + value: "some-host" + - equal: + path: spec.ingressClassName + value: "ingress-class" + + - it: hostname using TPL + set: + global.giteaHostName: "gitea.example.com" + ingress.enabled: true + ingress.hosts[0].host: "{{ .Values.global.giteaHostName }}" + ingress.tls: + - secretName: gitea-tls + hosts: + - "{{ .Values.global.giteaHostName }}" + asserts: + - isKind: + of: Ingress + - equal: + path: spec.tls[0].hosts[0] + value: "gitea.example.com" + - equal: + path: spec.rules[0].host + value: "gitea.example.com" diff --git a/unittests/deployment/ingress-configuration.yaml b/unittests/ingress/ingress.tpl.yaml similarity index 93% rename from unittests/deployment/ingress-configuration.yaml rename to unittests/ingress/ingress.tpl.yaml index a6998ee..5136c1c 100644 --- a/unittests/deployment/ingress-configuration.yaml +++ b/unittests/ingress/ingress.tpl.yaml @@ -1,28 +1,7 @@ -suite: ingress template -release: - name: gitea-unittests - namespace: testing +suite: Test ingress tpl use templates: - templates/gitea/ingress.yaml tests: - - it: hostname using TPL - set: - global.giteaHostName: "gitea.example.com" - ingress.enabled: true - ingress.hosts[0].host: "{{ .Values.global.giteaHostName }}" - ingress.tls: - - secretName: gitea-tls - hosts: - - "{{ .Values.global.giteaHostName }}" - asserts: - - isKind: - of: Ingress - - equal: - path: spec.tls[0].hosts[0] - value: "gitea.example.com" - - equal: - path: spec.rules[0].host - value: "gitea.example.com" - it: Ingress Class using TPL set: global.ingress.className: "ingress-class" @@ -45,3 +24,22 @@ tests: - equal: path: spec.ingressClassName value: "ingress-class" + + - it: hostname using TPL + set: + global.giteaHostName: "gitea.example.com" + ingress.enabled: true + ingress.hosts[0].host: "{{ .Values.global.giteaHostName }}" + ingress.tls: + - secretName: gitea-tls + hosts: + - "{{ .Values.global.giteaHostName }}" + asserts: + - isKind: + of: Ingress + - equal: + path: spec.tls[0].hosts[0] + value: "gitea.example.com" + - equal: + path: spec.rules[0].host + value: "gitea.example.com" diff --git a/values.yaml b/values.yaml index 4b6f017..cbe9a37 100644 --- a/values.yaml +++ b/values.yaml @@ -161,15 +161,14 @@ ingress: enabled: false # className: nginx className: + pathType: Prefix annotations: {} # kubernetes.io/ingress.class: nginx # kubernetes.io/tls-acme: "true" hosts: - host: git.example.com - paths: - - path: / - pathType: Prefix + paths: [] tls: [] # - secretName: chart-example-tls # hosts: