Skip to content

The Unified and Autonomous Development Platform

feat: Enhance Traefik ingress configuration in helm chart

Robin Shen57d0c72c9a00
diff --git a/server-product/helm/templates/NOTES.txt b/server-product/helm/templates/NOTES.txt
index f058a88349..3f24e5bcbe 100644
--- a/server-product/helm/templates/NOTES.txt
+++ b/server-product/helm/templates/NOTES.txt
@@ -3,13 +3,6 @@ CHART VERSION: {{ .Chart.Version }}
APP VERSION: {{ .Chart.AppVersion }}
{{- if and (not .Values.command) (not .Values.args) (eq .Values.image.name "1dev/server") }}
-###################################################################
-#
-# CAUTION: If you are upgrading from version <= 9.0.0, please make
-# sure to follow https://docs.onedev.io/upgrade-guide/deploy-to-k8s
-# to migrate your data
-#
-###################################################################
** Please be patient while the chart is being deployed **
@@ -22,6 +15,24 @@ You can access OneDev at:
{{- else }}
http://{{ .Values.ingress.host }}
{{- end }}
+{{- if eq (include "ods.traefikEnabled" .) "true" }}
+
+Traffic is routed by Traefik:
+
+ http entry point: {{ default "websecure" .Values.ingress.traefik.entryPoint }}
+ host names: {{ join ", " (.Values.ingress.traefik.hosts | default (list .Values.ingress.host)) }}
+{{- $sshEntryPoint := include "ods.traefikSshEntryPoint" . }}
+{{- if $sshEntryPoint }}
+ ssh entry point: {{ $sshEntryPoint }}
+
+Make sure entry point {{ $sshEntryPoint }} is declared in your Traefik installation, and update
+SSH root url as ssh://{{ .Values.ingress.host }} in menu Administration/System Setting
+{{- else }}
+ ssh: not exposed via Traefik
+
+Set ingress.traefik.sshEntryPoint if you want ssh to be routed by Traefik as well
+{{- end }}
+{{- end }}
{{- else }}
diff --git a/server-product/helm/templates/_helpers.tpl b/server-product/helm/templates/_helpers.tpl
index 106c3ff706..962670d517 100644
--- a/server-product/helm/templates/_helpers.tpl
+++ b/server-product/helm/templates/_helpers.tpl
@@ -66,6 +66,38 @@ Return the ServiceAccount name
{{- end -}}
{{- end -}}
+{{/*
+Return "true" when routing should be done with Traefik CRDs instead of a Kubernetes Ingress
+*/}}
+{{- define "ods.traefikEnabled" -}}
+{{- if (.Values.ingress.traefik | default dict).enabled -}}
+{{- print "true" -}}
+{{- end -}}
+{{- end -}}
+
+{{/*
+Return the Traefik entry point serving ssh traffic, or empty when ssh should not be exposed.
+An absent key means values were carried over from a chart predating this setting, as happens
+with "helm upgrade --reuse-values", in which case the chart default applies so that upgrading
+matches a fresh install. An explicit empty value disables the ssh route.
+*/}}
+{{- define "ods.traefikSshEntryPoint" -}}
+{{- $traefik := .Values.ingress.traefik | default dict -}}
+{{- if hasKey $traefik "sshEntryPoint" -}}
+{{- $traefik.sshEntryPoint | default "" -}}
+{{- else -}}
+{{- print "gitssh" -}}
+{{- end -}}
+{{- end -}}
+
+{{/*
+Return the name of the buffering middleware created along with the Traefik IngressRoute
+*/}}
+{{- define "ods.traefikMiddlewareName" -}}
+{{- $middleware := .Values.ingress.traefik.middleware | default dict -}}
+{{- printf "%s-%s" (include "ods.fullname" .) (default "buffering" $middleware.name) | trunc 63 | trimSuffix "-" -}}
+{{- end -}}
+
{{/*
Return the target Kubernetes version
*/}}
diff --git a/server-product/helm/templates/certificate.yaml b/server-product/helm/templates/certificate.yaml
index 24af17b697..168e6778e3 100644
--- a/server-product/helm/templates/certificate.yaml
+++ b/server-product/helm/templates/certificate.yaml
@@ -1,25 +1,17 @@
-{{- $ingress := get .Values "ingress" | default dict -}}
-{{- $traefik := get $ingress "traefik" | default dict -}}
-{{- $traefikEnabled := get $traefik "enabled" | default false -}}
-
-{{- if $traefikEnabled }}
-{{- if and $.Values.ingress.tls.enabled $.Values.ingress.tls.acme.enabled }}
+{{- $traefikEnabled := eq (include "ods.traefikEnabled" .) "true" -}}
+{{- if and .Values.ingress.enabled $traefikEnabled .Values.ingress.tls.enabled .Values.ingress.tls.acme.enabled }}
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: {{ include "ods.fullname" . }}
+ namespace: {{ .Release.Namespace }}
spec:
secretName: {{ .Values.ingress.tls.secretName }}
issuerRef:
name: {{ include "ods.fullname" . }}
kind: Issuer
dnsNames:
- {{- if .Values.ingress.traefik.hosts }}
- {{- range .Values.ingress.traefik.hosts }}
- - {{ . | quote }}
+ {{- range .Values.ingress.traefik.hosts | default (list .Values.ingress.host) }}
+ - {{ . | quote }}
{{- end }}
- {{- else }}
- - {{ .Values.ingress.host | quote }}
- {{- end }}
-{{- end }}
{{- end }}
diff --git a/server-product/helm/templates/ingress.yaml b/server-product/helm/templates/ingress.yaml
index 6b45575b9d..40c3e66007 100644
--- a/server-product/helm/templates/ingress.yaml
+++ b/server-product/helm/templates/ingress.yaml
@@ -1,18 +1,19 @@
-{{- $ingress := get .Values "ingress" | default dict -}}
-{{- $traefik := get $ingress "traefik" | default dict -}}
-{{- $traefikEnabled := get $traefik "enabled" | default false -}}
-
+{{- $traefikEnabled := eq (include "ods.traefikEnabled" .) "true" -}}
{{- if .Values.ingress.enabled -}}
{{- if not $traefikEnabled }}
-
-{{- /* Default .Values.ingress.annotations to dict if nil AND reassign it */}}
-{{- if not .Values.ingress.annotations -}}
- {{- $_ := set .Values.ingress "annotations" dict -}}
-{{- end -}}
-
+{{- $annotations := dict }}
+{{- if and .Values.ingress.tls.enabled .Values.ingress.tls.acme.enabled }}
+ {{- $_ := set $annotations "cert-manager.io/issuer" (include "ods.fullname" .) }}
+{{- end }}
+{{- with (.Values.ingress.nginx | default dict).annotations }}
+ {{- $annotations = mergeOverwrite $annotations . }}
+{{- end }}
+{{- with .Values.ingress.annotations }}
+ {{- $annotations = mergeOverwrite $annotations . }}
+{{- end }}
{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }}
- {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }}
- {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className -}}
+ {{- if not (hasKey $annotations "kubernetes.io/ingress.class") }}
+ {{- $_ := set $annotations "kubernetes.io/ingress.class" .Values.ingress.className }}
{{- end }}
{{- end }}
{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}}
@@ -28,22 +29,9 @@ metadata:
namespace: {{ .Release.Namespace }}
labels:
{{ include "ods.labels" . | indent 4 }}
+ {{- with $annotations }}
annotations:
- {{- $mergedAnnotations := dict }}
- {{- if and $.Values.ingress.tls.enabled $.Values.ingress.tls.acme.enabled }}
- {{- $_ := set $mergedAnnotations "cert-manager.io/issuer" (include "ods.fullname" .) }}
- {{- end }}
- {{- if .Values.ingress.annotations }}
- {{- $mergedAnnotations = merge $mergedAnnotations (default dict .Values.ingress.annotations) }}
- {{- end }}
- {{- if get .Values.ingress "nginx" | default dict }}
- {{- $nginx := get .Values.ingress "nginx" | default dict }}
- {{- if $nginx.annotations }}
- {{- $mergedAnnotations = merge $mergedAnnotations (default dict $nginx.annotations) }}
- {{- end }}
- {{- end }}
- {{- if $mergedAnnotations }}
- {{- toYaml $mergedAnnotations | nindent 4 }}
+ {{- toYaml . | nindent 4 }}
{{- end }}
spec:
{{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }}
@@ -75,6 +63,10 @@ spec:
{{- end }}
{{- end }}
{{- if $traefikEnabled }}
+{{- $traefik := .Values.ingress.traefik }}
+{{- $middleware := $traefik.middleware | default dict }}
+{{- $sshEntryPoint := include "ods.traefikSshEntryPoint" . }}
+{{- if $sshEntryPoint }}
apiVersion: traefik.io/v1alpha1
kind: IngressRouteTCP
metadata:
@@ -84,17 +76,19 @@ metadata:
{{ include "ods.labels" . | indent 4 }}
spec:
entryPoints:
- - {{ .Values.ingress.traefik.sshEntryPoint }}
+ - {{ $sshEntryPoint }}
routes:
- match: HostSNI(`*`)
services:
- - name: {{ include "ods.fullname" . }}{{- if .Values.service.separateSSH.enabled }}-ssh{{- end }}
{{- if .Values.service.separateSSH.enabled }}
- port: {{ default .Values.service.ports.ssh .Values.service.separateSSH.port | default "22" }}
+ - name: {{ include "ods.fullname" . }}-ssh
+ port: {{ default "22" .Values.service.separateSSH.port }}
{{- else }}
+ - name: {{ include "ods.fullname" . }}
port: {{ default "22" .Values.service.ports.ssh }}
{{- end }}
---
+{{- end }}
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
@@ -104,21 +98,21 @@ metadata:
{{ include "ods.labels" . | indent 4 }}
spec:
entryPoints:
- - {{ .Values.ingress.traefik.entryPoint }}
+ - {{ default "websecure" $traefik.entryPoint }}
routes:
- {{- if .Values.ingress.traefik.hosts }}
- {{- $hosts := list }}
- {{- range .Values.ingress.traefik.hosts }}
- {{- $hosts = append $hosts (regexReplaceAll "\\." . "\\.") }}
- {{- end }}
- - match: HostRegexp(`^({{ join "|" $hosts }})$`)
- {{- else }}
- - match: Host(`{{ .Values.ingress.host }}`)
+ {{- $matchers := list }}
+ {{- range $traefik.hosts | default (list .Values.ingress.host) }}
+ {{- $matchers = append $matchers (printf "`%s`" .) }}
{{- end }}
+ - match: Host({{ join ", " $matchers }})
kind: Rule
middlewares:
- - name: {{ include "ods.fullname" . }}-{{ .Values.ingress.traefik.middleware.name }}
+ - name: {{ include "ods.traefikMiddlewareName" . }}
namespace: {{ .Release.Namespace }}
+ {{- range $traefik.extraMiddlewares }}
+ - name: {{ .name }}
+ namespace: {{ default $.Release.Namespace .namespace }}
+ {{- end }}
services:
- name: {{ include "ods.fullname" . }}
port: {{ default "80" .Values.service.ports.http }}
@@ -133,13 +127,13 @@ spec:
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
- name: {{ include "ods.fullname" . }}-{{ .Values.ingress.traefik.middleware.name }}
+ name: {{ include "ods.traefikMiddlewareName" . }}
namespace: {{ .Release.Namespace }}
labels:
{{ include "ods.labels" . | indent 4 }}
spec:
buffering:
- maxRequestBodyBytes: {{ int .Values.ingress.traefik.middleware.maxRequestBodyBytes }}
- memRequestBodyBytes: {{ int .Values.ingress.traefik.middleware.memRequestBodyBytes }}
+ maxRequestBodyBytes: {{ int64 (default 1073741824 $middleware.maxRequestBodyBytes) }}
+ memRequestBodyBytes: {{ int64 (default 1073741824 $middleware.memRequestBodyBytes) }}
{{- end }}
{{- end }}
diff --git a/server-product/helm/templates/issuer.yaml b/server-product/helm/templates/issuer.yaml
index 5550c5946b..c3760c4cdd 100644
--- a/server-product/helm/templates/issuer.yaml
+++ b/server-product/helm/templates/issuer.yaml
@@ -1,11 +1,12 @@
-{{- if and $.Values.ingress.tls.enabled $.Values.ingress.tls.acme.enabled -}}
+{{- if and .Values.ingress.enabled .Values.ingress.tls.enabled .Values.ingress.tls.acme.enabled -}}
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: {{ include "ods.fullname" . }}
+ namespace: {{ .Release.Namespace }}
spec:
acme:
-{{- if eq $.Values.ingress.tls.acme.type "letsencrypt" }}
+{{- if eq .Values.ingress.tls.acme.type "letsencrypt" }}
{{- if .Values.ingress.tls.acme.production }}
server: https://acme-v02.api.letsencrypt.org/directory
{{- else }}
@@ -18,5 +19,9 @@ spec:
solvers:
- http01:
ingress:
- class: {{ .Values.ingress.className }}
+ {{- with .Values.ingress.className }}
+ class: {{ . }}
+ {{- else }}
+ {}
+ {{- end }}
{{- end }}
diff --git a/server-product/helm/values.yaml b/server-product/helm/values.yaml
index e137f5c984..035f12b644 100644
--- a/server-product/helm/values.yaml
+++ b/server-product/helm/values.yaml
@@ -217,15 +217,20 @@ ingress:
# ingress.className -- Specify ingress class name. Requires Kubernetes >= 1.18.
# Refer to https://kubernetes.io/blog/2020/04/02/improvements-to-the-ingress-api-in-kubernetes-1.18/#specifying-the-class-of-an-ingress for details
className: ""
- # ingress.host -- Set the host name
+ # ingress.host -- Set the host name. This also determines the server url advertised by OneDev,
+ # so it needs to be set even when additional host names are routed via ingress.traefik.hosts
host: onedev.example.com
+ # ingress.annotations -- Specify additional annotations for the Ingress. These are merged on top
+ # of ingress.nginx.annotations and take precedence over them
+ annotations: {}
# ingress.tls -- Configure TLS for the Ingress.
tls:
enabled: false
secretName: "onedev-tls"
acme:
- # ingress.tls.acme.enabled -- If **true**, create certificate issuer resource
+ # ingress.tls.acme.enabled -- If **true**, create certificate issuer resource. Also requires
+ # ingress.tls.enabled to be **true**
enabled: false
# ingress.tls.acme.type -- Specify ACME type. Currently only supports letsencrypt
type: "letsencrypt"
@@ -238,6 +243,8 @@ ingress:
# Nginx configuration
nginx:
+ # ingress.nginx.annotations -- Annotations added to the Ingress when serving traffic with the
+ # nginx ingress controller. Set to an empty map to leave them out
annotations:
nginx.ingress.kubernetes.io/affinity: "cookie"
nginx.ingress.kubernetes.io/affinity-mode: "persistent"
@@ -245,19 +252,38 @@ ingress:
nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
nginx.ingress.kubernetes.io/proxy-body-size: "0"
+
+ # Traefik configuration
traefik:
+ # ingress.traefik.enabled -- If **true**, create Traefik IngressRoute resources instead of a
+ # Kubernetes Ingress. Requires Traefik v3 with its Kubernetes CRD provider enabled. When
+ # ingress.tls.acme.enabled is **true**, the http01 challenge is still solved through a
+ # temporary Kubernetes Ingress, so ingress.className has to point at your Traefik ingress class
+ # and Traefik's Kubernetes Ingress provider has to be enabled as well, otherwise the
+ # certificate will never be issued
enabled: false
+ # ingress.traefik.entryPoint -- Traefik entry point serving http traffic
entryPoint: websecure
+ # ingress.traefik.sshEntryPoint -- Traefik entry point serving ssh traffic. The entry point
+ # has to be declared in your Traefik installation. Set to an empty value to not route ssh
+ # via Traefik
sshEntryPoint: gitssh
- # Used hostPattern if Regex matching is needed for multiple hostnames. If not set host is used.
- #hosts:
+ # ingress.traefik.hosts -- Host names to route to OneDev. Defaults to ingress.host if empty
+ hosts: []
# - code.example.com
# - code.int.example.com
- # ingress.middleware -- Specify middleware for the Ingress.
+ # ingress.traefik.middleware -- Specify the buffering middleware created for the route.
+ # Body size limits need to be large enough to accommodate your biggest git push
middleware:
name: "buffering"
maxRequestBodyBytes: 1073741824
memRequestBodyBytes: 1073741824
+ # ingress.traefik.extraMiddlewares -- Additional middlewares to attach to the route. Each entry
+ # takes a "name" and an optional "namespace" defaulting to the release namespace
+ extraMiddlewares: []
+ # - name: redirect-to-https
+ # namespace: traefik
+
# extraVolumes -- Specify additional Volumes to use.
extraVolumes: []