From b502f05917499cf6ca3e76775f5a4c85edcf6562 Mon Sep 17 00:00:00 2001 From: Pando85 Date: Wed, 14 Jul 2021 10:29:37 +0200 Subject: [PATCH 1/2] Add ingress support --- charts/maildev/templates/NOTES.txt | 7 +++ charts/maildev/templates/_helpers.tpl | 38 +++++++++++++ charts/maildev/templates/ingress.yaml | 78 +++++++++++++++++++++++++++ charts/maildev/values.yaml | 38 +++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 charts/maildev/templates/ingress.yaml diff --git a/charts/maildev/templates/NOTES.txt b/charts/maildev/templates/NOTES.txt index 34a2b1e..550f16b 100644 --- a/charts/maildev/templates/NOTES.txt +++ b/charts/maildev/templates/NOTES.txt @@ -9,3 +9,10 @@ Watch the status with: 'kubectl get svc --namespace {{ .Release.Namespace }} -w {{ include "maildev.fullname" . }}' {{- end }} +{{- if .Values.ingress.enabled }} + + From outside the cluster, the server URL(s) are: +{{- range .Values.ingress.hosts }} + http://{{ . }} +{{- end }} +{{- end }} diff --git a/charts/maildev/templates/_helpers.tpl b/charts/maildev/templates/_helpers.tpl index c3039c6..f889671 100644 --- a/charts/maildev/templates/_helpers.tpl +++ b/charts/maildev/templates/_helpers.tpl @@ -61,3 +61,41 @@ Create the name of the service account to use {{- default "default" .Values.serviceAccount.name }} {{- end }} {{- end }} + +{{/* +Get KubeVersion removing pre-release information. +*/}} +{{- define "maildev.kubeVersion" -}} + {{- default .Capabilities.KubeVersion.Version (regexFind "v[0-9]+\\.[0-9]+\\.[0-9]+" .Capabilities.KubeVersion.Version) -}} +{{- end -}} + +{{/* +Return the appropriate apiVersion for ingress. +*/}} +{{- define "maildev.ingress.apiVersion" -}} + {{- if and (.Capabilities.APIVersions.Has "networking.k8s.io/v1") (semverCompare ">= 1.19.x" (include "maildev.kubeVersion" .)) -}} + {{- print "networking.k8s.io/v1" -}} + {{- else if .Capabilities.APIVersions.Has "networking.k8s.io/v1beta1" -}} + {{- print "networking.k8s.io/v1beta1" -}} + {{- else -}} + {{- print "extensions/v1beta1" -}} + {{- end -}} +{{- end -}} +{{/* +Return if ingress is stable. +*/}} +{{- define "maildev.ingress.isStable" -}} + {{- eq (include "maildev.ingress.apiVersion" .) "networking.k8s.io/v1" -}} +{{- end -}} +{{/* +Return if ingress supports ingressClassName. +*/}} +{{- define "maildev.ingress.supportsIngressClassName" -}} + {{- or (eq (include "maildev.ingress.isStable" .) "true") (and (eq (include "maildev.ingress.apiVersion" .) "networking.k8s.io/v1beta1") (semverCompare ">= 1.18.x" (include "maildev.kubeVersion" .))) -}} +{{- end -}} +{{/* +Return if ingress supports pathType. +*/}} +{{- define "maildev.ingress.supportsPathType" -}} + {{- or (eq (include "maildev.ingress.isStable" .) "true") (and (eq (include "maildev.ingress.apiVersion" .) "networking.k8s.io/v1beta1") (semverCompare ">= 1.18.x" (include "maildev.kubeVersion" .))) -}} +{{- end -}} diff --git a/charts/maildev/templates/ingress.yaml b/charts/maildev/templates/ingress.yaml new file mode 100644 index 0000000..6eefa14 --- /dev/null +++ b/charts/maildev/templates/ingress.yaml @@ -0,0 +1,78 @@ +{{- if .Values.ingress.enabled -}} +{{- $ingressApiIsStable := eq (include "maildev.ingress.isStable" .) "true" -}} +{{- $ingressSupportsIngressClassName := eq (include "maildev.ingress.supportsIngressClassName" .) "true" -}} +{{- $ingressSupportsPathType := eq (include "maildev.ingress.supportsPathType" .) "true" -}} +{{- $fullName := include "maildev.fullname" . -}} +{{- $serviceName := printf "%s-web" $fullName -}} +{{- $servicePort := .Values.ports.web -}} +{{- $ingressPath := .Values.ingress.path -}} +{{- $ingressPathType := .Values.ingress.pathType -}} +{{- $extraPaths := .Values.ingress.extraPaths -}} +apiVersion: {{ include "maildev.ingress.apiVersion" . }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "maildev.labels" . | nindent 4 }} +{{- if .Values.ingress.labels }} +{{ toYaml .Values.ingress.labels | indent 4 }} +{{- end }} + {{- if .Values.ingress.annotations }} + annotations: + {{- range $key, $value := .Values.ingress.annotations }} + {{ $key }}: {{ tpl $value $ | quote }} + {{- end }} + {{- end }} +spec: + {{- if and $ingressSupportsIngressClassName .Values.ingress.ingressClassName }} + ingressClassName: {{ .Values.ingress.ingressClassName }} + {{- end -}} +{{- if .Values.ingress.tls }} + tls: +{{ tpl (toYaml .Values.ingress.tls) $ | indent 4 }} +{{- end }} + rules: + {{- if .Values.ingress.hosts }} + {{- range .Values.ingress.hosts }} + - host: {{ tpl . $}} + http: + paths: +{{- if $extraPaths }} +{{ toYaml $extraPaths | indent 10 }} +{{- end }} + - path: {{ $ingressPath }} + {{- if $ingressSupportsPathType }} + pathType: {{ $ingressPathType }} + {{- end }} + backend: + {{- if $ingressApiIsStable }} + service: + name: {{ $serviceName }} + port: + number: {{ $servicePort }} + {{- else }} + serviceName: {{ $serviceName }} + servicePort: {{ $servicePort }} + {{- end }} + {{- end }} + {{- else }} + - http: + paths: + - backend: + {{- if $ingressApiIsStable }} + service: + name: {{ $serviceName }} + port: + number: {{ $servicePort }} + {{- else }} + serviceName: {{ $serviceName }} + servicePort: {{ $servicePort }} + {{- end }} + {{- if $ingressPath }} + path: {{ $ingressPath }} + {{- end }} + {{- if $ingressSupportsPathType }} + pathType: {{ $ingressPathType }} + {{- end }} + {{- end -}} +{{- end }} diff --git a/charts/maildev/values.yaml b/charts/maildev/values.yaml index 9212fa3..879b52b 100644 --- a/charts/maildev/values.yaml +++ b/charts/maildev/values.yaml @@ -76,6 +76,44 @@ securityContext: {} service: type: ClusterIP +ingress: + enabled: false + # For Kubernetes >= 1.18 you should specify the ingress-controller via the field ingressClassName + # See https://kubernetes.io/blog/2020/04/02/improvements-to-the-ingress-api-in-kubernetes-1.18/#specifying-the-class-of-an-ingress + # ingressClassName: nginx + # Values can be templated + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + labels: {} + path: / + + # pathType is only for k8s >= 1.18 + pathType: Prefix + + hosts: + - chart-example.local + ## Extra paths to prepend to every host configuration. This is useful when working with annotation based services. + extraPaths: [] + # - path: /* + # backend: + # serviceName: ssl-redirect + # servicePort: use-annotation + ## Or for k8s > 1.19 + # - path: /* + # pathType: Prefix + # backend: + # service: + # name: ssl-redirect + # port: + # name: service + + + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + resources: {} # We usually recommend not to specify default resources and to leave this as a conscious # choice for the user. This also increases chances charts run on environments with little From 1f4caa5ebc30e050ad30cd2eebcc22cac2ddfddb Mon Sep 17 00:00:00 2001 From: Pando85 Date: Thu, 15 Jul 2021 16:03:14 +0200 Subject: [PATCH 2/2] Merge smtp and web service in one unique service --- charts/maildev/templates/ingress.yaml | 9 ++++----- charts/maildev/templates/service-web.yaml | 17 ----------------- .../{service-smtp.yaml => service.yaml} | 8 ++++++-- 3 files changed, 10 insertions(+), 24 deletions(-) delete mode 100644 charts/maildev/templates/service-web.yaml rename charts/maildev/templates/{service-smtp.yaml => service.yaml} (67%) diff --git a/charts/maildev/templates/ingress.yaml b/charts/maildev/templates/ingress.yaml index 6eefa14..24b2c4c 100644 --- a/charts/maildev/templates/ingress.yaml +++ b/charts/maildev/templates/ingress.yaml @@ -3,7 +3,6 @@ {{- $ingressSupportsIngressClassName := eq (include "maildev.ingress.supportsIngressClassName" .) "true" -}} {{- $ingressSupportsPathType := eq (include "maildev.ingress.supportsPathType" .) "true" -}} {{- $fullName := include "maildev.fullname" . -}} -{{- $serviceName := printf "%s-web" $fullName -}} {{- $servicePort := .Values.ports.web -}} {{- $ingressPath := .Values.ingress.path -}} {{- $ingressPathType := .Values.ingress.pathType -}} @@ -47,11 +46,11 @@ spec: backend: {{- if $ingressApiIsStable }} service: - name: {{ $serviceName }} + name: {{ $fullName }} port: number: {{ $servicePort }} {{- else }} - serviceName: {{ $serviceName }} + serviceName: {{ $fullName }} servicePort: {{ $servicePort }} {{- end }} {{- end }} @@ -61,11 +60,11 @@ spec: - backend: {{- if $ingressApiIsStable }} service: - name: {{ $serviceName }} + name: {{ $fullName }} port: number: {{ $servicePort }} {{- else }} - serviceName: {{ $serviceName }} + serviceName: {{ $fullName }} servicePort: {{ $servicePort }} {{- end }} {{- if $ingressPath }} diff --git a/charts/maildev/templates/service-web.yaml b/charts/maildev/templates/service-web.yaml deleted file mode 100644 index e488a1f..0000000 --- a/charts/maildev/templates/service-web.yaml +++ /dev/null @@ -1,17 +0,0 @@ -{{- if .Values.ports.web }} -apiVersion: v1 -kind: Service -metadata: - name: {{ include "maildev.fullname" . }}-web - labels: - {{- include "maildev.labels" . | nindent 4 }} -spec: - type: {{ .Values.service.type }} - ports: - - name: web-{{ .Values.ports.web }}-tcp - port: {{ .Values.ports.web }} - protocol: TCP - targetPort: {{ .Values.ports.web }} - selector: - {{- include "maildev.selectorLabels" . | nindent 4 }} -{{- end }} diff --git a/charts/maildev/templates/service-smtp.yaml b/charts/maildev/templates/service.yaml similarity index 67% rename from charts/maildev/templates/service-smtp.yaml rename to charts/maildev/templates/service.yaml index 8fe3631..cb3acb4 100644 --- a/charts/maildev/templates/service-smtp.yaml +++ b/charts/maildev/templates/service.yaml @@ -2,16 +2,20 @@ apiVersion: v1 kind: Service metadata: - name: {{ include "maildev.fullname" . }}-smtp + name: {{ include "maildev.fullname" . }} labels: {{- include "maildev.labels" . | nindent 4 }} spec: type: {{ .Values.service.type }} ports: - - name: smtp-{{ .Values.ports.smtp }}-tcp + - name: smtp port: {{ .Values.ports.smtp }} protocol: TCP targetPort: {{ .Values.ports.smtp }} + - name: web + port: {{ .Values.ports.web }} + protocol: TCP + targetPort: {{ .Values.ports.web }} selector: {{- include "maildev.selectorLabels" . | nindent 4 }} {{- end }}