From 157bb1a37b37802090ed68c527672fdd5badd65a Mon Sep 17 00:00:00 2001 From: Gaspard FEREY Date: Tue, 12 Nov 2024 17:30:07 +0100 Subject: [PATCH] [#173] Support for multiple user-defined ports in interactive services --- charts/library-chart/Chart.yaml | 2 +- charts/library-chart/templates/_ingress.tpl | 37 ++++++++++++++++----- charts/library-chart/templates/_route.tpl | 23 +++++++++---- charts/library-chart/templates/_service.tpl | 14 ++++---- 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/charts/library-chart/Chart.yaml b/charts/library-chart/Chart.yaml index 98845a10..4b8b9de4 100644 --- a/charts/library-chart/Chart.yaml +++ b/charts/library-chart/Chart.yaml @@ -1,4 +1,4 @@ apiVersion: v2 name: library-chart -version: 1.5.30 +version: 1.5.31 type: library diff --git a/charts/library-chart/templates/_ingress.tpl b/charts/library-chart/templates/_ingress.tpl index 6aa4b31c..92b57983 100644 --- a/charts/library-chart/templates/_ingress.tpl +++ b/charts/library-chart/templates/_ingress.tpl @@ -70,7 +70,7 @@ spec: backend: service: name: {{ $fullName }} - port: + port: number: {{ $svcPort }} {{- end }} {{- end }} @@ -78,9 +78,13 @@ spec: {{/* Template to generate a custom Ingress */}} {{- define "library-chart.ingressUser" -}} {{- if .Values.ingress.enabled -}} -{{ if .Values.networking.user.enabled }} +{{- if and .Values.networking.user .Values.networking.user.enabled -}} +{{- $userPorts := list -}} +{{- if or .Values.networking.user.ports .Values.networking.user.port -}} +{{- $userPorts = .Values.networking.user.ports | default (list .Values.networking.user.port) -}} +{{- end -}} +{{- if $userPorts -}} {{- $fullName := include "library-chart.fullname" . -}} -{{- $svcPort := .Values.networking.user.port -}} apiVersion: networking.k8s.io/v1 kind: Ingress metadata: @@ -96,13 +100,25 @@ spec: {{- if .Values.ingress.tls }} tls: - hosts: - - {{ .Values.ingress.userHostname | quote }} - {{- if or .Values.ingress.useCertManager .Values.ingress.useTlsSecret}} + {{- range $userPort := $userPorts }} + {{- if eq (len $userPorts) 1 }} + - {{ $.Values.ingress.userHostname | quote }} + {{- else }} + - {{ regexReplaceAll "([^\\.]+)\\.(.*)" $.Values.ingress.userHostname (printf "${1}-%d.${2}" (int $userPort)) | quote }} + {{- end }} + {{- end }} + {{- if or .Values.ingress.useCertManager .Values.ingress.useTlsSecret }} secretName: tls-cert-{{ include "library-chart.fullname" . }} - {{- end }} + {{- end }} {{- end }} rules: + {{- range $userPort := $userPorts }} + {{- with $ }} + {{- if eq (len $userPorts) 1 }} - host: {{ .Values.ingress.userHostname | quote }} + {{- else }} + - host: {{ regexReplaceAll "([^\\.]+)\\.(.*)" .Values.ingress.userHostname (printf "${1}-%d.${2}" (int $userPort)) | quote }} + {{- end }} http: paths: - path: / @@ -110,8 +126,11 @@ spec: backend: service: name: {{ $fullName }} - port: - number: {{ $svcPort }} + port: + number: {{ $userPort }} + {{- end }} + {{- end }} +{{- end }} {{- end }} {{- end }} {{- end }} @@ -151,7 +170,7 @@ spec: backend: service: name: {{ $fullName }} - port: + port: number: {{ $svcPort }} {{- end }} {{- end }} diff --git a/charts/library-chart/templates/_route.tpl b/charts/library-chart/templates/_route.tpl index 456ff5d3..b51595a7 100644 --- a/charts/library-chart/templates/_route.tpl +++ b/charts/library-chart/templates/_route.tpl @@ -38,7 +38,7 @@ spec: to: kind: Service name: {{ $fullName }} - port: + port: targetPort: {{ $svcPort }} tls: termination: {{ .Values.route.tls.termination }} @@ -61,9 +61,11 @@ spec: {{/* Template to generate a custom Route */}} {{- define "library-chart.routeUser" -}} {{- if .Values.route.enabled -}} -{{ if .Values.networking.user.enabled }} +{{- if and .Values.networking.user .Values.networking.user.enabled (or .Values.networking.user.ports .Values.networking.user.port) -}} {{- $fullName := include "library-chart.fullname" . -}} -{{- $svcPort := .Values.networking.user.port -}} +{{- $userPorts := .Values.networking.user.ports | default (list .Values.networking.user.port) -}} +{{- range $userPort := $userPorts -}} +{{- with $ -}} apiVersion: route.openshift.io/v1 kind: Route metadata: @@ -73,13 +75,17 @@ metadata: annotations: {{- include "library-chart.route.annotations" . | nindent 4 }} spec: +{{- if eq (len $userPorts) 1 }} host: {{ .Values.route.userHostname | quote }} +{{- else }} + host: {{ regexReplaceAll "([^\\.]+)\\.(.*)" .Values.route.userHostname (printf "${1}-d.${2}" $userPort) | quote }} +{{- end }} path: / to: kind: Service name: {{ $fullName }} - port: - targetPort: {{ $svcPort }} + port: + targetPort: {{ $userPort }} tls: termination: {{ .Values.route.tls.termination }} {{- if .Values.route.tls.key }} @@ -92,9 +98,12 @@ spec: caCertificate: {{- .Values.route.tls.caCertificate }} {{- end }} {{- if .Values.route.tls.destinationCACertificate }} - destinationCACertificate: {{- .Values.route.tls.destinationCACertificate }} + destinationCACertificate: {{ .Values.route.tls.destinationCACertificate }} {{- end }} wildcardPolicy: {{ .Values.route.wildcardPolicy }} +--- +{{- end }} +{{- end }} {{- end }} {{- end }} {{- end }} @@ -119,7 +128,7 @@ spec: to: kind: Service name: {{ $fullName }} - port: + port: targetPort: {{ $svcPort }} tls: termination: {{ .Values.route.tls.termination }} diff --git a/charts/library-chart/templates/_service.tpl b/charts/library-chart/templates/_service.tpl index c9c540cf..389a44e7 100644 --- a/charts/library-chart/templates/_service.tpl +++ b/charts/library-chart/templates/_service.tpl @@ -2,6 +2,10 @@ {{/* Template to generate a Service */}} {{- define "library-chart.service" -}} +{{- $userPorts := list -}} +{{- if and .Values.networking.user .Values.networking.user.enabled (or .Values.networking.user.ports .Values.networking.user.port) -}} +{{- $userPorts = .Values.networking.user.ports | default (list .Values.networking.user.port) -}} +{{- end -}} apiVersion: v1 kind: Service metadata: @@ -18,13 +22,11 @@ spec: targetPort: {{ default .Values.networking.service.port .Values.networking.service.targetPort }} protocol: TCP name: main - {{ if .Values.networking.user }} - {{ if .Values.networking.user.enabled }} - - port: {{ .Values.networking.user.port }} - targetPort: {{ .Values.networking.user.port }} + {{- range $userPort := $userPorts }} + - port: {{ $userPort }} + targetPort: {{ $userPort }} protocol: TCP - name: user - {{- end }} + name: {{ printf "user-%d" (int $userPort) | quote }} {{- end }} {{ if .Values.spark }} {{ if .Values.spark.sparkui }}