[common] Read and the service ports correctly (#170)

This commit is contained in:
Bernd Schörgers 2020-11-16 16:02:36 +01:00 committed by GitHub
parent e67b0cc17b
commit e46d898ee4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 23 deletions

View File

@ -2,7 +2,7 @@ apiVersion: v2
name: common name: common
description: Function library for k8s-at-home charts description: Function library for k8s-at-home charts
type: library type: library
version: 1.5.0 version: 1.5.1
keywords: keywords:
- k8s-at-home - k8s-at-home
- common - common

View File

@ -1,3 +1,6 @@
{{/*
service class: all services should adhere to this
*/}}
{{- define "common.classes.service" -}} {{- define "common.classes.service" -}}
{{- $values := .Values.service -}} {{- $values := .Values.service -}}
{{- if hasKey . "ObjectValues" -}} {{- if hasKey . "ObjectValues" -}}
@ -9,7 +12,7 @@
{{- if hasKey $values "nameSuffix" -}} {{- if hasKey $values "nameSuffix" -}}
{{- $serviceName = printf "%v-%v" $serviceName $values.nameSuffix -}} {{- $serviceName = printf "%v-%v" $serviceName $values.nameSuffix -}}
{{ end -}} {{ end -}}
{{- $svcType := $values.type -}} {{- $svcType := $values.type | default "" -}}
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
metadata: metadata:
@ -58,17 +61,7 @@ spec:
{{- if $values.publishNotReadyAddresses }} {{- if $values.publishNotReadyAddresses }}
publishNotReadyAddresses: {{ $values.publishNotReadyAddresses }} publishNotReadyAddresses: {{ $values.publishNotReadyAddresses }}
{{- end }} {{- end }}
ports: {{- include "common.classes.service.ports" (dict "svcType" $svcType "values" $values ) | trim | nindent 2 }}
- 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 }}
selector: selector:
{{- include "common.labels.selectorLabels" . | nindent 4 }} {{- include "common.labels.selectorLabels" . | nindent 4 }}
{{- end }} {{- end }}

View File

@ -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 }}

View File

@ -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" -}} {{- define "common.controller.mainContainer" -}}
- name: {{ template "common.names.fullname" . }} - name: {{ template "common.names.fullname" . }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
@ -14,15 +16,7 @@
value: {{ $value | quote }} value: {{ $value | quote }}
{{- end }} {{- end }}
{{- end }} {{- end }}
ports: {{- include "common.controller.ports" . | trim | nindent 2 }}
- 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 }}
volumeMounts: volumeMounts:
{{- range $index, $PVC := .Values.persistence }} {{- range $index, $PVC := .Values.persistence }}
{{- if $PVC.enabled }} {{- if $PVC.enabled }}

View File

@ -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 -}}