浏览代码

Add common implementation of services

Waqar Ahmed 4 年之前
父节点
当前提交
4f2ef3691a
共有 2 个文件被更改,包括 60 次插入0 次删除
  1. 41 0
      library/common/templates/classes/_service.tpl
  2. 19 0
      library/common/templates/classes/_service_ports.tpl

+ 41 - 0
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 }}

+ 19 - 0
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 }}