diff options
author | Sylvain Desbureaux <sylvain.desbureaux@orange.com> | 2019-12-12 14:35:01 +0100 |
---|---|---|
committer | Sylvain Desbureaux <sylvain.desbureaux@orange.com> | 2020-02-04 11:51:11 +0100 |
commit | 60c7480788eee721c42e824ae22115073705c9c5 (patch) | |
tree | 445ad7d9e6a13aae49c996aa9fd422282b0e7b7b /kubernetes | |
parent | 0f3e13456f6a311786d29e7a2a560070e3d96e81 (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')
-rw-r--r-- | kubernetes/common/cassandra/templates/backup/cronjob.yaml | 10 | ||||
-rw-r--r-- | kubernetes/common/cassandra/templates/pv.yaml | 33 | ||||
-rw-r--r-- | kubernetes/common/cassandra/templates/service.yaml | 36 | ||||
-rw-r--r-- | kubernetes/common/cassandra/templates/statefulset.yaml | 27 | ||||
-rw-r--r-- | kubernetes/common/cassandra/values.yaml | 21 | ||||
-rw-r--r-- | kubernetes/common/common/templates/_labels.tpl | 62 | ||||
-rw-r--r-- | kubernetes/common/common/templates/_name.tpl | 4 | ||||
-rw-r--r-- | kubernetes/common/common/templates/_pod.tpl | 38 | ||||
-rw-r--r-- | kubernetes/common/common/templates/_service.tpl | 107 | ||||
-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.tpl | 28 | ||||
-rwxr-xr-x | kubernetes/onap/values.yaml | 2 | ||||
-rw-r--r-- | kubernetes/sdc/values.yaml | 2 |
13 files changed, 294 insertions, 109 deletions
diff --git a/kubernetes/common/cassandra/templates/backup/cronjob.yaml b/kubernetes/common/cassandra/templates/backup/cronjob.yaml index 2edc8071f9..e4f2aabfa0 100644 --- a/kubernetes/common/cassandra/templates/backup/cronjob.yaml +++ b/kubernetes/common/cassandra/templates/backup/cronjob.yaml @@ -19,11 +19,7 @@ kind: CronJob metadata: name: {{ include "common.fullname" . }}-backup namespace: {{ include "common.namespace" . }} - labels: - app: {{ include "common.fullname" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} - release: {{ include "common.release" . }} - heritage: {{ .Release.Service }} + labels: {{- include "common.labels" . | nindent 4 }} spec: schedule: {{ .Values.backup.cron | quote }} concurrencyPolicy: Forbid @@ -31,6 +27,10 @@ spec: jobTemplate: spec: template: + metadata: + labels: {{- include "common.labels" . | nindent 12 }} + annotations: {{- include "common.tplValue" (dict "value" .Values.podAnnotations "context" .) | nindent 12 }} + name: {{ include "common.name" . }} spec: restartPolicy: Never initContainers: diff --git a/kubernetes/common/cassandra/templates/pv.yaml b/kubernetes/common/cassandra/templates/pv.yaml index fd0a758e63..76a224ab5f 100644 --- a/kubernetes/common/cassandra/templates/pv.yaml +++ b/kubernetes/common/cassandra/templates/pv.yaml @@ -12,35 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -{{- $global := . }} -{{- if and $global.Values.persistence.enabled (not $global.Values.persistence.existingClaim) }} -{{- if eq "True" (include "common.needPV" .) -}} -{{- range $i := until (int $global.Values.replicaCount)}} ---- -apiVersion: v1 -kind: PersistentVolume -metadata: - name: {{ include "common.release" $global }}-{{ $global.Values.service.name }}-{{ $i }} - namespace: {{ $global.Release.Namespace }} - labels: - type: {{ $global.Values.persistence.storageType }} - app: {{ $global.Values.service.name }} - chart: {{ $global.Chart.Name }}-{{ $global.Chart.Version | replace "+" "_" }} - release: {{ include "common.release" $global }} - heritage: {{ $global.Release.Service }} -spec: - capacity: - storage: {{ $global.Values.persistence.size }} - accessModes: - {{- if $global.Values.backup.enabled }} - - ReadWriteMany - {{- else }} - - ReadWriteOnce - {{- end }} - persistentVolumeReclaimPolicy: {{ $global.Values.persistence.volumeReclaimPolicy }} - storageClassName: "{{ include "common.fullname" $global }}-data" - hostPath: - path: {{ $global.Values.persistence.mountPath }}/{{ include "common.release" $global }}/{{ $global.Values.persistence.mountSubPath }}-{{ $i }} -{{- end -}} -{{- end -}} -{{- end -}} +{{ include "common.replicaPV" . }} diff --git a/kubernetes/common/cassandra/templates/service.yaml b/kubernetes/common/cassandra/templates/service.yaml index 6cbddbea40..0b91076f82 100644 --- a/kubernetes/common/cassandra/templates/service.yaml +++ b/kubernetes/common/cassandra/templates/service.yaml @@ -12,38 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -apiVersion: v1 -kind: Service -metadata: - name: {{ include "common.servicename" . }} - namespace: {{ include "common.namespace" . }} - labels: - app: {{ include "common.name" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} - release: {{ include "common.release" . }} - heritage: {{ .Release.Service }} - annotations: - service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" -spec: - type: {{ .Values.service.type }} - publishNotReadyAddresses: true - ports: - {{if eq .Values.service.type "NodePort" -}} - {{- $global := . }} - {{- range $index, $ports := .Values.service.ports }} - - port: {{ $ports.port }} - targetPort: {{ $ports.port }} - nodePort: {{ $global.Values.global.nodePortPrefix | default $global.Values.nodePortPrefix }}{{ $ports.nodePort }} - name: {{ $ports.name }} - {{- end }} -{{- else -}} - {{- range $index, $ports := .Values.service.ports }} - - port: {{ $ports.port }} - targetPort: {{ $ports.port }} - name: {{ $ports.name }} - {{- end }} -{{- end}} - selector: - app: {{ include "common.name" . }} - release: {{ include "common.release" . }} - clusterIP: None +{{ include "common.headlessService" . }} diff --git a/kubernetes/common/cassandra/templates/statefulset.yaml b/kubernetes/common/cassandra/templates/statefulset.yaml index b737a8f960..16aa27f68a 100644 --- a/kubernetes/common/cassandra/templates/statefulset.yaml +++ b/kubernetes/common/cassandra/templates/statefulset.yaml @@ -12,42 +12,25 @@ # See the License for the specific language governing permissions and # limitations under the License. -apiVersion: apps/v1beta1 +apiVersion: apps/v1 kind: StatefulSet -metadata: - name: {{ include "common.fullname" . }} - namespace: {{ include "common.namespace" . }} - labels: - app: {{ include "common.name" . }} - chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} - release: {{ include "common.release" . }} - heritage: {{ .Release.Service }} +metadata: {{- include "common.resourceMetadata" . | nindent 2 }} spec: - selector: - matchLabels: - app: {{ include "common.name" . }} - release: {{ include "common.release" . }} + selector: {{- include "common.selectors" . | nindent 4 }} serviceName: {{ include "common.servicename" . }} replicas: {{ .Values.replicaCount }} podManagementPolicy: {{ .Values.podManagementPolicy }} updateStrategy: type: {{ .Values.updateStrategy.type }} template: - metadata: - labels: - app: {{ include "common.name" . }} - release: {{ include "common.release" . }} - name: {{ include "common.name" . }} + metadata: {{- include "common.templateMetadata" . | nindent 6 }} spec: hostNetwork: {{ .Values.hostNetwork }} containers: - name: {{ include "common.name" . }} image: {{ .Values.image }} imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }} - ports: - {{- range $index, $ports := .Values.service.ports }} - - containerPort: {{ $ports.port }} - {{- end }} + ports: {{ include "common.containerPorts" . | nindent 8 }} volumeMounts: - name: {{ include "common.fullname" . }}-data mountPath: /var/lib/cassandra diff --git a/kubernetes/common/cassandra/values.yaml b/kubernetes/common/cassandra/values.yaml index c3af7e59b2..dfa0a3e250 100644 --- a/kubernetes/common/cassandra/values.yaml +++ b/kubernetes/common/cassandra/values.yaml @@ -75,22 +75,27 @@ readiness: failureThreshold: 3 service: - type: ClusterIP name: cassandra - ports: - - name: intra + headless: + suffix: "" + annotations: + service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" + publishNotReadyAddresses: true + headlessPorts: + - name: tcp-intra port: 7000 - name: tls port: 7001 - - name: jmx + - name: tcp-jmx port: 7199 - - name: cql + - name: tcp-cql port: 9042 - - name: thrift + - name: tcp-thrift port: 9160 - - name: agent + - name: tcp-agent port: 61621 +podAnnotations: {} podManagementPolicy: OrderedReady updateStrategy: type: RollingUpdate @@ -116,7 +121,7 @@ persistence: ## ## storageClass: "-" ## Not set as it depends of the backup enabledment or not. - #accessMode: ReadWriteOnce + accessMode: ReadWriteOnce size: 2Gi mountPath: /dockerdata-nfs mountSubPath: cassandra 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 -}} diff --git a/kubernetes/onap/values.yaml b/kubernetes/onap/values.yaml index 5344a423f3..2395495df3 100755 --- a/kubernetes/onap/values.yaml +++ b/kubernetes/onap/values.yaml @@ -173,4 +173,4 @@ vid: vnfsdk: enabled: false modeling: - enabled: false
\ No newline at end of file + enabled: false diff --git a/kubernetes/sdc/values.yaml b/kubernetes/sdc/values.yaml index a7006bc56c..6abf911a89 100644 --- a/kubernetes/sdc/values.yaml +++ b/kubernetes/sdc/values.yaml @@ -29,7 +29,7 @@ global: ubuntuInitRepository: oomk8s ubuntuInitImage: ubuntu-init:1.0.0 cassandra: - #This flag allows SDC to instantiate its own cluster, serviceName + #This flag allows SDC to instantiate its own cluster, serviceName #should be sdc-cs if this flag is enabled localCluster: false #The cassandra service name to connect to (default: shared cassandra service) |