aboutsummaryrefslogtreecommitdiffstats
path: root/kubernetes/common/common/templates
diff options
context:
space:
mode:
authorSylvain Desbureaux <sylvain.desbureaux@orange.com>2019-12-12 14:35:01 +0100
committerSylvain Desbureaux <sylvain.desbureaux@orange.com>2020-02-04 11:51:11 +0100
commit60c7480788eee721c42e824ae22115073705c9c5 (patch)
tree445ad7d9e6a13aae49c996aa9fd422282b0e7b7b /kubernetes/common/common/templates
parent0f3e13456f6a311786d29e7a2a560070e3d96e81 (diff)
[COMMON] Create templates for services and PV
Proposition of common templates to make service declaration and PV declaration consistent accross OOM. Propositions of templates for sub parties of resource definitions such as metadatas, selector and containerPorts. I've also made an example with cassandra. Change-Id: I8b8aa8eb61dafba75e89add1979114a0eefce243 Issue-ID: OOM-1971 Signed-off-by: Sylvain Desbureaux <sylvain.desbureaux@orange.com>
Diffstat (limited to 'kubernetes/common/common/templates')
-rw-r--r--kubernetes/common/common/templates/_labels.tpl62
-rw-r--r--kubernetes/common/common/templates/_name.tpl4
-rw-r--r--kubernetes/common/common/templates/_pod.tpl38
-rw-r--r--kubernetes/common/common/templates/_service.tpl107
-rw-r--r--kubernetes/common/common/templates/_storage.tpl (renamed from kubernetes/common/common/templates/_storageClass.tpl)33
-rw-r--r--kubernetes/common/common/templates/_tplValue.tpl28
6 files changed, 267 insertions, 5 deletions
diff --git a/kubernetes/common/common/templates/_labels.tpl b/kubernetes/common/common/templates/_labels.tpl
new file mode 100644
index 0000000000..95d51e17b7
--- /dev/null
+++ b/kubernetes/common/common/templates/_labels.tpl
@@ -0,0 +1,62 @@
+{{/*
+# Copyright © 2019 Orange
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+*/}}
+
+
+
+{{/*
+Common labels
+*/}}
+{{- define "common.labels" -}}
+app.kubernetes.io/name: {{ include "common.name" . }}
+helm.sh/chart: {{ include "common.chart" . }}
+app.kubernetes.io/instance: {{ include "common.release" . }}
+app.kubernetes.io/managed-by: {{ .Release.Service }}
+{{- end -}}
+
+{{/*
+Labels to use on deploy.spec.selector.matchLabels and svc.spec.selector
+*/}}
+{{- define "common.matchLabels" -}}
+app.kubernetes.io/name: {{ include "common.name" . }}
+app.kubernetes.io/instance: {{ include "common.release" . }}
+{{- end -}}
+
+{{/*
+ Generate "top" metadata for Deployment / StatefulSet / ...
+*/}}
+{{- define "common.resourceMetadata" -}}
+name: {{ include "common.fullname" . }}
+namespace: {{ include "common.namespace" . }}
+labels: {{- include "common.labels" . | nindent 2 }}
+{{- end -}}
+
+{{/*
+ Generate selectors for Deployment / StatefulSet / ...
+*/}}
+{{- define "common.selectors" -}}
+matchLabels: {{- include "common.matchLabels" . | nindent 2 }}
+{{- end -}}
+
+{{/*
+ Generate "template" metadata for Deployment / StatefulSet / ...
+*/}}
+{{- define "common.templateMetadata" -}}
+{{- if .Values.podAnnotations }}
+annotations: {{- include "common.tplValue" (dict "value" .Values.podAnnotations "context" $) | nindent 2 }}
+{{- end }}
+labels: {{- include "common.labels" . | nindent 2 }}
+name: {{ include "common.name" . }}
+{{- end -}}
diff --git a/kubernetes/common/common/templates/_name.tpl b/kubernetes/common/common/templates/_name.tpl
index 7719cdbb9f..943078ff2f 100644
--- a/kubernetes/common/common/templates/_name.tpl
+++ b/kubernetes/common/common/templates/_name.tpl
@@ -49,3 +49,7 @@
{{- define "common.release" -}}
{{- first (regexSplit "-" .Release.Name -1) }}
{{- end -}}
+
+{{- define "common.chart" -}}
+{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
+{{- end -}}
diff --git a/kubernetes/common/common/templates/_pod.tpl b/kubernetes/common/common/templates/_pod.tpl
new file mode 100644
index 0000000000..9329572a92
--- /dev/null
+++ b/kubernetes/common/common/templates/_pod.tpl
@@ -0,0 +1,38 @@
+{{/*
+# Copyright © 2019 Orange
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+*/}}
+
+{{/*
+ Generate the container port list.
+ Will use first ".Values.service.ports" list.
+ Will append ports from ".Values.service.headlessPorts" only if port number is
+ not already in port list.
+*/}}
+{{- define "common.containerPorts" -}}
+{{- $ports := default (list) .Values.service.ports }}
+{{- $portsNumber := list }}
+{{- range $index, $port := $ports }}
+{{- $portsNumber = append $portsNumber $port.port }}
+{{- end }}
+{{- range $index, $port := .Values.service.headlessPorts }}
+{{- if not (has $port.port $portsNumber) }}
+{{- $ports = append $ports $port }}
+{{- end }}
+{{- end }}
+{{- range $index, $port := $ports }}
+- containerPort: {{ $port.port }}
+ name: {{ $port.name }}
+{{- end }}
+{{- end -}}
diff --git a/kubernetes/common/common/templates/_service.tpl b/kubernetes/common/common/templates/_service.tpl
index 77b77d059a..075f7965b9 100644
--- a/kubernetes/common/common/templates/_service.tpl
+++ b/kubernetes/common/common/templates/_service.tpl
@@ -20,7 +20,7 @@
The default will be the chart name (or .Values.nameOverride if set).
And the use of .Values.service.name overrides all.
- - .Values.service.name : override default service (ie. chart) name
+ - .Values.service.name: override default service (ie. chart) name
*/}}
{{/*
Expand the service name for a chart.
@@ -28,4 +28,107 @@
{{- define "common.servicename" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- default $name .Values.service.name | trunc 63 | trimSuffix "-" -}}
-{{- end -}} \ No newline at end of file
+{{- end -}}
+
+{{/* Define the metadata of Service
+ The function takes from one to three arguments (inside a dictionary):
+ - .dot : environment (.)
+ - .suffix : a string which will be added at the end of the name (with a '-').
+ - .annotations: the annotations to add
+ Usage example:
+ {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
+ {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
+*/}}
+{{- define "common.serviceMetadata" -}}
+ {{- $dot := default . .dot -}}
+ {{- $suffix := default "" .suffix -}}
+ {{- $annotations := default "" .annotations -}}
+{{- if $annotations -}}
+annotations: {{- include "common.tplValue" (dict "value" $annotations "context" $dot) | nindent 2 }}
+{{- end }}
+name: {{ include "common.servicename" $dot }}{{ if $suffix }}{{ print "-" $suffix }}{{ end }}
+namespace: {{ include "common.namespace" $dot }}
+labels: {{- include "common.labels" $dot | nindent 2 -}}
+{{- end -}}
+
+{{/* Define the ports of Service
+ The function takes three arguments (inside a dictionary):
+ - .dot : environment (.)
+ - .ports : an array of ports
+ - .portType: the type of the service
+*/}}
+{{- define "common.servicePorts" -}}
+{{- $portType := .portType -}}
+{{- $dot := .dot -}}
+{{- range $index, $port := .ports }}
+- port: {{ $port.port }}
+ targetPort: {{ $port.name }}
+ {{- if (eq $portType "NodePort") }}
+ nodePort: {{ $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}{{ $port.nodePort }}
+ {{- end }}
+ name: {{ $port.name }}
+{{- end -}}
+{{- end -}}
+
+{{/* Create generic service template
+ The function takes several arguments (inside a dictionary):
+ - .dot : environment (.)
+ - .ports : an array of ports
+ - .portType: the type of the service
+ - .suffix : a string which will be added at the end of the name (with a '-')
+ - .annotations: the annotations to add
+ - .publishNotReadyAddresses: if we publish not ready address
+ - .headless: if the service is headless
+*/}}
+{{- define "common.genericService" -}}
+{{- $dot := default . .dot -}}
+{{- $suffix := default "" .suffix -}}
+{{- $annotations := default "" .annotations -}}
+{{- $publishNotReadyAddresses := default false .publishNotReadyAddresses -}}
+{{- $portType := .portType -}}
+{{- $ports := .ports -}}
+{{- $headless := default false .headless -}}
+apiVersion: v1
+kind: Service
+metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "dot" $dot ) | nindent 2 }}
+spec:
+ {{- if $headless }}
+ clusterIP: None
+ {{- end }}
+ ports: {{- include "common.servicePorts" (dict "portType" $portType "ports" $ports "dot" $dot) | nindent 4 }}
+ {{- if $publishNotReadyAddresses }}
+ publishNotReadyAddresses: true
+ {{- end }}
+ type: {{ $portType }}
+ selector: {{- include "common.matchLabels" $dot | nindent 4 }}
+{{- end -}}
+
+{{/* Create service template */}}
+{{- define "common.service" -}}
+{{- $suffix := default "" .Values.service.suffix -}}
+{{- $annotations := default "" .Values.service.annotations -}}
+{{- $publishNotReadyAddresses := default false .Values.service.publishNotReadyAddresses -}}
+{{- $portType := .Values.service.type -}}
+{{- $ports := .Values.service.ports -}}
+{{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "portType" $portType) }}
+{{- end -}}
+
+{{/* Create headless service template */}}
+{{- define "common.headlessService" -}}
+{{- $suffix := include "common._makeHeadlessSuffix" . -}}
+{{- $annotations := default "" .Values.service.headless.annotations -}}
+{{- $publishNotReadyAddresses := default false .Values.service.headless.publishNotReadyAddresses -}}
+{{- $ports := .Values.service.headlessPorts -}}
+{{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "portType" "ClusterIP" "headless" true ) }}
+{{- end -}}
+
+{{/*
+ Generate the right suffix for headless service
+*/}}
+{{- define "common._makeHeadlessSuffix" -}}
+{{- if hasKey .Values.service.headless "suffix" }}
+{{- .Values.service.headless.suffix }}
+{{- else }}
+{{- print "headless" }}
+{{- end }}
+{{- end -}}
diff --git a/kubernetes/common/common/templates/_storageClass.tpl b/kubernetes/common/common/templates/_storage.tpl
index 8fd1f9772b..ae9335909d 100644
--- a/kubernetes/common/common/templates/_storageClass.tpl
+++ b/kubernetes/common/common/templates/_storage.tpl
@@ -49,9 +49,36 @@
Calculate if we need a PV. If a storageClass is provided, then we don't need.
*/}}
{{- define "common.needPV" -}}
-{{- if or (or .Values.persistence.storageClassOverride .Values.persistence.storageClass) .Values.global.persistence.storageClass -}}
- False
-{{- else -}}
+{{- if not (or (or .Values.persistence.storageClassOverride .Values.persistence.storageClass) .Values.global.persistence.storageClass) -}}
True
{{- end -}}
{{- end -}}
+
+{{/*
+ Generate N PV for a statefulset
+*/}}
+{{- define "common.replicaPV" -}}
+{{- $global := . }}
+{{- if and $global.Values.persistence.enabled (not $global.Values.persistence.existingClaim) }}
+{{- if (include "common.needPV" .) -}}
+{{- range $i := until (int $global.Values.replicaCount)}}
+---
+kind: PersistentVolume
+apiVersion: v1
+metadata:
+ name: {{ include "common.fullname" $global }}-data-{{$i}}
+ namespace: {{ include "common.namespace" $global }}
+ labels: {{- include "common.labels" $global | nindent 4 }}
+spec:
+ capacity:
+ storage: {{ $global.Values.persistence.size}}
+ accessModes:
+ - {{ $global.Values.persistence.accessMode }}
+ persistentVolumeReclaimPolicy: {{ $global.Values.persistence.volumeReclaimPolicy }}
+ storageClassName: "{{ include "common.fullname" $global }}-data"
+ hostPath:
+ path: {{ $global.Values.global.persistence.mountPath | default $global.Values.persistence.mountPath }}/{{ include "common.release" $global }}/{{ $global.Values.persistence.mountSubPath }}-{{$i}}
+{{- end -}}
+{{- end -}}
+{{- end -}}
+{{- end -}}
diff --git a/kubernetes/common/common/templates/_tplValue.tpl b/kubernetes/common/common/templates/_tplValue.tpl
new file mode 100644
index 0000000000..b74ecbda19
--- /dev/null
+++ b/kubernetes/common/common/templates/_tplValue.tpl
@@ -0,0 +1,28 @@
+{{/*
+# Copyright © 2019 Orange
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+*/}}
+
+{{/*
+Renders a value that contains template.
+Usage:
+{{ include "common.tplValue" ( dict "value" .Values.path.to.the.Value "context" $) }}
+*/}}
+{{- define "common.tplValue" -}}
+ {{- if typeIs "string" .value }}
+ {{- tpl .value .context }}
+ {{- else }}
+ {{- tpl (.value | toYaml) .context }}
+ {{- end }}
+{{- end -}}