From e46d898ee480d6cad51812ebbc50ef6b5aee9a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernd=20Sch=C3=B6rgers?= Date: Mon, 16 Nov 2020 16:02:36 +0100 Subject: [PATCH] [common] Read and the service ports correctly (#170) --- charts/common/Chart.yaml | 2 +- charts/common/templates/classes/_service.tpl | 17 +++------ .../templates/classes/_service_ports.tpl | 23 ++++++++++++ .../templates/lib/controller/_container.tpl | 14 +++----- .../templates/lib/controller/_ports.tpl | 36 +++++++++++++++++++ 5 files changed, 69 insertions(+), 23 deletions(-) create mode 100644 charts/common/templates/classes/_service_ports.tpl create mode 100644 charts/common/templates/lib/controller/_ports.tpl diff --git a/charts/common/Chart.yaml b/charts/common/Chart.yaml index 95866652..c790adf4 100644 --- a/charts/common/Chart.yaml +++ b/charts/common/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v2 name: common description: Function library for k8s-at-home charts type: library -version: 1.5.0 +version: 1.5.1 keywords: - k8s-at-home - common diff --git a/charts/common/templates/classes/_service.tpl b/charts/common/templates/classes/_service.tpl index 19b8fd5e..395dff15 100644 --- a/charts/common/templates/classes/_service.tpl +++ b/charts/common/templates/classes/_service.tpl @@ -1,3 +1,6 @@ +{{/* +service class: all services should adhere to this +*/}} {{- define "common.classes.service" -}} {{- $values := .Values.service -}} {{- if hasKey . "ObjectValues" -}} @@ -9,7 +12,7 @@ {{- if hasKey $values "nameSuffix" -}} {{- $serviceName = printf "%v-%v" $serviceName $values.nameSuffix -}} {{ end -}} -{{- $svcType := $values.type -}} +{{- $svcType := $values.type | default "" -}} apiVersion: v1 kind: Service metadata: @@ -58,17 +61,7 @@ spec: {{- if $values.publishNotReadyAddresses }} publishNotReadyAddresses: {{ $values.publishNotReadyAddresses }} {{- end }} - ports: - - port: {{ $values.port.port }} - targetPort: {{ $values.port.targetPort }} - protocol: {{ $values.port.protocol }} - name: {{ $values.port.name }} - {{- if (and (eq $svcType "NodePort") (not (empty $values.port.nodePort))) }} - nodePort: {{ $values.port.nodePort }} - {{ end }} - {{- with $values.additionalPorts }} - {{ toYaml . | nindent 4 }} - {{- end }} + {{- include "common.classes.service.ports" (dict "svcType" $svcType "values" $values ) | trim | nindent 2 }} selector: {{- include "common.labels.selectorLabels" . | nindent 4 }} {{- end }} diff --git a/charts/common/templates/classes/_service_ports.tpl b/charts/common/templates/classes/_service_ports.tpl new file mode 100644 index 00000000..3508e1de --- /dev/null +++ b/charts/common/templates/classes/_service_ports.tpl @@ -0,0 +1,23 @@ +{{/* +logic that lists the ports and additionalPorts for a service +*/}} +{{- define "common.classes.service.ports" -}} + {{- $ports := list -}} + {{- $values := .values -}} + {{- $ports = mustAppend $ports $values.port -}} + {{- range $_ := $values.additionalPorts -}} + {{- $ports = mustAppend $ports . -}} + {{- end }} + {{- if $ports -}} + ports: + {{- range $_ := $ports }} + - port: {{ .port }} + targetPort: {{ .targetPort | default .name }} + protocol: {{ .protocol | default "TCP" }} + name: {{ .name }} + {{- if (and (eq $.svcType "NodePort") (not (empty .nodePort))) }} + nodePort: {{ .nodePort }} + {{ end }} + {{- end -}} + {{- end -}} +{{- end }} diff --git a/charts/common/templates/lib/controller/_container.tpl b/charts/common/templates/lib/controller/_container.tpl index 6d58d5bd..671471e6 100644 --- a/charts/common/templates/lib/controller/_container.tpl +++ b/charts/common/templates/lib/controller/_container.tpl @@ -1,4 +1,6 @@ -{{- /* The main containter that will be included in the controller */ -}} +{{- /* +The main containter that will be included in the controller +*/ -}} {{- define "common.controller.mainContainer" -}} - name: {{ template "common.names.fullname" . }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" @@ -14,15 +16,7 @@ value: {{ $value | quote }} {{- end }} {{- end }} - ports: - - name: {{ .Values.service.port.name }} - containerPort: {{ .Values.service.port.port }} - protocol: {{ .Values.service.port.protocol }} - {{- range $port := .Values.service.additionalPorts }} - - name: {{ $port.name }} - containerPort: {{ $port.port }} - protocol: {{ $port.protocol }} - {{- end }} + {{- include "common.controller.ports" . | trim | nindent 2 }} volumeMounts: {{- range $index, $PVC := .Values.persistence }} {{- if $PVC.enabled }} diff --git a/charts/common/templates/lib/controller/_ports.tpl b/charts/common/templates/lib/controller/_ports.tpl new file mode 100644 index 00000000..ccf4b222 --- /dev/null +++ b/charts/common/templates/lib/controller/_ports.tpl @@ -0,0 +1,36 @@ +{{/* +ports included by the controller +*/}} +{{- define "common.controller.ports" -}} + {{- $ports := list -}} + {{- with .Values.service -}} + {{- $serviceValues := deepCopy . -}} + {{/* append the ports for the main service */}} + {{- if .enabled -}} + {{- $ports = mustAppend $ports .port -}} + {{- range $_ := .additionalPorts -}} + {{/* append the additonalPorts for the main service */}} + {{- $ports = mustAppend $ports . -}} + {{- end }} + {{- end }} + {{/* append the ports for each additional service */}} + {{- range $_ := .additionalServices }} + {{- if .enabled -}} + {{- $ports = mustAppend $ports .port -}} + {{- range $_ := .additionalPorts -}} + {{/* append the additonalPorts for each additional service */}} + {{- $ports = mustAppend $ports . -}} + {{- end }} + {{- end }} + {{- end }} + {{- end }} + {{/* export/render the list of ports */}} + {{- if $ports -}} + ports: + {{- range $_ := $ports }} + - name: {{ required "Missing port.name" .name }} + containerPort: {{ required "Missing port.port" .port }} + protocol: {{ .protocol | default "TCP" }} + {{- end -}} + {{- end -}} +{{- end -}}