From df2319b392f0e405073eae092cf1fe9658f4aa96 Mon Sep 17 00:00:00 2001 From: Matthis Holleville Date: Tue, 27 Feb 2024 09:24:49 +0100 Subject: [PATCH] chore(haproxy): streamline service port configuration for flexibility Refactored the service port configuration in the HAProxy Helm chart to allow for more flexible and deterministic exposure of ports. This change introduces a new `exposedPorts` option in `values.yaml`, which, when specified, takes precedence over the default container ports for service exposure. This enhancement facilitates clearer and more customizable service definitions, catering to diverse deployment scenarios. Signed-off-by: Matthis Holleville --- haproxy/templates/_helpers.tpl | 23 +++++++++++++++++++++++ haproxy/templates/service.yaml | 9 +-------- haproxy/values.yaml | 6 ++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/haproxy/templates/_helpers.tpl b/haproxy/templates/_helpers.tpl index 8c23e8f..71537a7 100644 --- a/haproxy/templates/_helpers.tpl +++ b/haproxy/templates/_helpers.tpl @@ -103,3 +103,26 @@ Encode an imagePullSecret string. {{- end }} {{/* vim: set filetype=mustache: */}} + +{{/* +Exposed ports to service +*/}} +{{- define "haproxy.exposedPorts" }} +{{- $exposedPorts := .Values.service.exposedPorts }} +{{- if empty $exposedPorts }} +{{- $containerPorts := .Values.containerPorts }} +{{- range $key, $port := $containerPorts }} +- name: {{ $key }} + protocol: TCP + port: {{ $port }} + targetPort: {{ $key }} +{{- end }} +{{- else }} +{{- range $key, $port := $exposedPorts }} +- name: {{ $key }} + protocol: TCP + port: {{ $port }} + targetPort: {{ $key }} +{{- end }} +{{- end }} +{{- end }} \ No newline at end of file diff --git a/haproxy/templates/service.yaml b/haproxy/templates/service.yaml index 776d2b9..1f54def 100644 --- a/haproxy/templates/service.yaml +++ b/haproxy/templates/service.yaml @@ -51,14 +51,7 @@ spec: {{- end }} {{- if or .Values.containerPorts .Values.service.additionalPorts }} ports: - {{- with .Values.containerPorts }} - {{- range $key, $port := . }} - - name: {{ $key }} - protocol: TCP - port: {{ $port }} - targetPort: {{ $key }} - {{- end }} - {{- end }} + {{- include "haproxy.exposedPorts" . | nindent 3 -}} {{- with .Values.service.additionalPorts }} {{- range $key, $port := . }} - name: {{ $key }} diff --git a/haproxy/values.yaml b/haproxy/values.yaml index 13adc4a..85644ea 100644 --- a/haproxy/values.yaml +++ b/haproxy/values.yaml @@ -484,6 +484,12 @@ service: additionalPorts: {} # prometheus: 9101 + ## Deterministic exposure of ports to the service + ## .Values.containerPorts will be exposed if exposedPorts is empty + ## ref: https://kubernetes.io/docs/concepts/services-networking/service/ + exposedPorts: {} + # http: 80 + serviceMonitor: ## Toggle the ServiceMonitor true if you have Prometheus Operator installed and configured enabled: false