From 4f2ef3691a0d6d3b007a068c0b0f581be9485620 Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Thu, 28 Jan 2021 21:18:40 +0500 Subject: [PATCH] Add common implementation of services --- library/common/templates/classes/_service.tpl | 41 +++++++++++++++++++ .../templates/classes/_service_ports.tpl | 19 +++++++++ 2 files changed, 60 insertions(+) create mode 100644 library/common/templates/classes/_service.tpl create mode 100644 library/common/templates/classes/_service_ports.tpl diff --git a/library/common/templates/classes/_service.tpl b/library/common/templates/classes/_service.tpl new file mode 100644 index 0000000000..08a0b1b31b --- /dev/null +++ b/library/common/templates/classes/_service.tpl @@ -0,0 +1,41 @@ +{{/* +This template serves as a blueprint for all Service objects that are created +within the common library. +*/}} +{{- define "common.classes.service" -}} +{{- $values := . -}} +{{- $serviceName := include "common.names.fullname" . -}} +{{- if hasKey $values "nameSuffix" -}} + {{- $serviceName = printf "%v-%v" $serviceName $values.nameSuffix -}} +{{ end -}} +{{- $svcType := $values.type | default "" -}} +apiVersion: v1 +kind: Service +metadata: + name: {{ $serviceName }} + labels: + {{- include "common.labels" . | nindent 4 }} + {{- if $values.labels }} + {{ toYaml $values.labels | nindent 4 }} + {{- end }} + {{- if $values.annotations }} + {{- with $values.annotations }} + annotations: + {{ toYaml . | nindent 4 }} + {{- end }} + {{- end }} +spec: + {{- if (or (eq $svcType "ClusterIP") (empty $svcType)) }} + type: ClusterIP + {{- if $values.clusterIP }} + clusterIP: {{ $values.clusterIP }} + {{end}} + {{- else if eq $svcType "NodePort" }} + type: {{ $svcType }} + {{- else }} + {{- fail "Only ClusterIP and NodePort services are supported in common chart" }} + {{- 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/library/common/templates/classes/_service_ports.tpl b/library/common/templates/classes/_service_ports.tpl new file mode 100644 index 0000000000..a61900b42a --- /dev/null +++ b/library/common/templates/classes/_service_ports.tpl @@ -0,0 +1,19 @@ +{{/* +Render all the ports and additionalPorts for a Service object. +*/}} +{{- define "common.classes.service.ports" -}} + {{- $values := .values -}} + {{- $ports := $values.ports -}} + {{- if $ports -}} + ports: + {{- range $_ := $ports }} + - port: {{ .port }} + targetPort: {{ .targetPort | default "http" }} + protocol: {{ .protocol | default "TCP" }} + name: {{ .name | default "http" }} + {{- if (and (eq $.svcType "NodePort") (not (empty .nodePort))) }} + nodePort: {{ .nodePort }} + {{ end }} + {{- end -}} + {{- end -}} +{{- end }}