2114d677e62df2c6820475caf93cfe37d5e1834a
[oom.git] / kubernetes / common / common / templates / _storage.tpl
1 {{/*
2 # Copyright © 2019 Amdocs, Bell Canada, Orange
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 */}}
16
17 {{/*
18   Give the root folder for ONAP when using host pathes
19
20   The function takes up to two arguments (inside a dictionary):
21     - .dot : environment (.)
22     - .subPath: the sub path to use, default to
23                 ".Values.persistence.mountSubPath"
24
25   Example calls:
26     {{ include "common.storageClass" . }}
27     {{ include "common.storageClass" (dict "dot" . "subPath" "my-awesome-subPath") }}
28 */}}
29 {{- define "common.persistencePath" -}}
30 {{-   $dot := default . .dot -}}
31 {{-   $subPath := default $dot.Values.persistence.mountSubPath .subPath -}}
32 {{ $dot.Values.global.persistence.mountPath | default $dot.Values.persistence.mountPath }}/{{ include "common.release" $dot }}/{{ $subPath }}
33 {{- end -}}
34
35 {{/*
36   Expand the name of the storage class.
37   The value "common.fullname"-data is used by default,
38   unless either override mechanism is used.
39
40   - .Values.global.persistence.storageClass  : override default storageClass for
41                                                all charts
42   - .Values.persistence.storageClassOverride : override global and default
43                                                storage class on a per chart
44                                                basis
45   - .Values.persistence.storageClass         : override default storage class on
46                                                per chart basis
47
48   The function takes up to two arguments (inside a dictionary):
49      - .dot : environment (.)
50      - .suffix: suffix to name. if not set, default to "data" when no override
51         mechanism is used.
52      - .persistenceInfos: the persitence values to use, default to
53                           `.Values.persistence`.
54                           Need to be the dict key from `.Values` in string
55                           format.
56                           let's say you have:
57
58                               persistence:
59                                 logs:
60                                   enabled: true
61                                   size: 100Mi
62                                   accessMode: ReadWriteOnce
63                                   ...
64
65                           then you have to put `.Values.persitence.logs` in
66                           order to use it.
67
68
69   Example calls:
70     {{ include "common.storageClass" . }}
71     {{ include "common.storageClass" (dict "dot" . "suffix" "my-awesome-suffix") }}
72     {{ include "common.storageClass" (dict "dot" . "suffix" "my-awesome-suffix" "persistenceInfos" .Values.persistenceLog) }}
73 */}}
74 {{- define "common.storageClass" -}}
75 {{-   $dot := default . .dot -}}
76 {{-   $suffix := default "data" .suffix -}}
77 {{- $persistenceInfos := default $dot.Values.persistence .persistenceInfos -}}
78   {{- if $persistenceInfos.storageClassOverride -}}
79     {{- if ne "-" $persistenceInfos.storageClassOverride -}}
80       {{- $persistenceInfos.storageClassOverride -}}
81     {{- else -}}
82       {{- $storage_class := "" -}}
83       {{- printf "%q" $storage_class -}}
84     {{- end -}}
85   {{- else -}}
86     {{- if or $persistenceInfos.storageClass $dot.Values.global.persistence.storageClass }}
87       {{- if ne "-" (default $persistenceInfos.storageClass $dot.Values.global.persistence.storageClass) -}}
88         {{- default $persistenceInfos.storageClass $dot.Values.global.persistence.storageClass -}}
89       {{- else -}}
90         {{- $storage_class := "" -}}
91         {{- printf "%q" $storage_class -}}
92       {{- end -}}
93     {{- else -}}
94       {{- printf "%s-%s" (include "common.fullname" $dot) $suffix -}}
95     {{- end -}}
96   {{- end -}}
97 {{- end -}}
98
99 {{/*
100   Calculate if we need a PV. If a storageClass is provided, then we don't need.
101 */}}
102 {{- define "common.needPV" -}}
103 {{- if not (or (or .Values.persistence.storageClassOverride .Values.persistence.storageClass) .Values.global.persistence.storageClass) -}}
104   True
105 {{- end -}}
106 {{- end -}}
107
108 {{/*
109   Generate a PV
110
111   The function takes up to three arguments (inside a dictionary):
112      - .dot : environment (.)
113      - .suffix: suffix to name. if not set, default to "data".
114      - .persistenceInfos: the persitence values to use, default to
115                           `.Values.persistence`.
116                           Need to be the dict key from `.Values` in string
117                           format.
118                           let's say you have:
119
120                               persistence:
121                                 logs:
122                                   enabled: true
123                                   size: 100Mi
124                                   accessMode: ReadWriteOnce
125                                   ...
126
127                           then you have to put `.Values.persitence.logs` in
128                           order to use it.
129
130   Example calls:
131     {{ include "common.PV" . }}
132     {{ include "common.PV" (dict "dot" . "suffix" "my-awesome-suffix" "persistenceInfos".Values.persistenceLog ) }}
133     {{ include "common.PV" (dict "dot" . "subPath" "persistenceInfos" .Values.persistence.log) }}
134 */}}
135 {{- define "common.PV" -}}
136 {{- $dot := default . .dot -}}
137 {{- $suffix := default "data" .suffix -}}
138 {{- $metadata_suffix := ternary "" $suffix (eq $suffix "data") -}}
139 {{- $persistenceInfos := default $dot.Values.persistence .persistenceInfos -}}
140 {{- if and $persistenceInfos.enabled (not $persistenceInfos.existingClaim) -}}
141 {{- if (include "common.needPV" $dot) -}}
142 kind: PersistentVolume
143 apiVersion: v1
144 metadata: {{- include "common.resourceMetadata" (dict "dot" $dot "suffix" $suffix "labels" $persistenceInfos.labels) | nindent 2 }}
145 spec:
146   capacity:
147     storage: {{ $persistenceInfos.size }}
148   accessModes:
149     - {{ $persistenceInfos.accessMode }}
150   persistentVolumeReclaimPolicy: {{ $persistenceInfos.volumeReclaimPolicy }}
151   storageClassName: "{{ include "common.fullname" $dot }}-{{ $suffix }}"
152   hostPath:
153     path: {{ include "common.persistencePath" (dict "dot" $dot "subPath" $persistenceInfos.mountSubPath) }}
154 {{- end -}}
155 {{- end -}}
156 {{- end -}}
157
158 {{/*
159   Generate N PV for a statefulset
160
161   The function takes up to two arguments (inside a dictionary):
162      - .dot : environment (.)
163      - .suffix: suffix to name. if not set, default to "data".
164      - .persistenceInfos: the persitence values to use, default to
165                           `.Values.persistence`.
166                           Need to be the dict key from `.Values` in string
167                           format.
168                           let's say you have:
169
170                               persistence:
171                                 logs:
172                                   enabled: true
173                                   size: 100Mi
174                                   accessMode: ReadWriteOnce
175                                   ...
176
177                           then you have to put `.Values.persitence.logs` in
178                           order to use it.
179
180   Example calls:
181     {{ include "common.replicaPV" . }}
182     {{ include "common.replicaPV" (dict "dot" . "suffix" "my-awesome-suffix" "persistenceInfos" .Values.persistenceLog) }}
183     {{ include "common.replicaPV" (dict dot" . "subPath" "persistenceInfos" .Values.persistence.log) }}
184 */}}
185 {{- define "common.replicaPV" -}}
186 {{- $dot := default . .dot -}}
187 {{- $suffix := default "data" .suffix -}}
188 {{- $metadata_suffix := ternary "" $suffix (eq $suffix "data") -}}
189 {{- $persistenceInfos := default $dot.Values.persistence .persistenceInfos -}}
190 {{- if and $persistenceInfos.enabled (not $persistenceInfos.existingClaim) -}}
191 {{- if (include "common.needPV" $dot) -}}
192 {{/* TODO: see if we can use "common.PV" after branching F release */}}
193 {{- range $i := until (int $dot.Values.replicaCount) }}
194 {{- $range_suffix := printf "%s-%d" $metadata_suffix $i }}
195 ---
196 kind: PersistentVolume
197 apiVersion: v1
198 metadata: {{- include "common.resourceMetadata" (dict "dot" $dot "suffix" $range_suffix "labels" $persistenceInfos.labels) | nindent 2 }}
199 spec:
200   capacity:
201     storage: {{ $persistenceInfos.size }}
202   accessModes:
203     - {{ $persistenceInfos.accessMode }}
204   persistentVolumeReclaimPolicy: {{ $persistenceInfos.volumeReclaimPolicy }}
205   storageClassName: "{{ include "common.fullname" $dot }}-{{ $suffix }}"
206   hostPath:
207     path: {{ include "common.persistencePath"  (dict "dot" $dot "subPath" $persistenceInfos.mountSubPath) }}-{{ $i }}
208 {{- end -}}
209 {{- end -}}
210 {{- end -}}
211 {{- end -}}
212
213 {{/*
214   Generate a PVC
215
216   The function takes up to two arguments (inside a dictionary):
217      - .dot : environment (.)
218      - .suffix: suffix to name. if not set, default to "data".
219      - .persistenceInfos: the persitence values to use, default to
220                           `.Values.persistence`.
221                           Need to be the dict key from `.Values` in string
222                           format.
223                           let's say you have:
224
225                               persistence:
226                                 logs:
227                                   enabled: true
228                                   size: 100Mi
229                                   accessMode: ReadWriteOnce
230                                   ...
231
232                           then you have to put `.Values.persitence.logs` in
233                           order to use it.
234
235   Example calls:
236     {{ include "common.PVC" . }}
237     {{ include "common.PVC" (dict "dot" . "suffix" "my-awesome-suffix" "persistenceInfos" .Values.persistenceLog) }}
238     {{ include "common.PVC" (dict dot" . "subPath" "persistenceInfos" .Values.persistence.log) }}
239 */}}
240 {{- define "common.PVC" -}}
241 {{- $dot := default . .dot -}}
242 {{- $persistenceInfos := default $dot.Values.persistence .persistenceInfos -}}
243 {{- $suffix := default "data" .suffix -}}
244 {{- $metadata_suffix := ternary "" $suffix (eq $suffix "data") -}}
245 {{- if and $persistenceInfos.enabled (not $persistenceInfos.existingClaim) -}}
246 kind: PersistentVolumeClaim
247 apiVersion: v1
248 {{ include "common.PVCTemplate" (dict "dot" $dot "suffix" $suffix "persistenceInfos" $persistenceInfos) }}
249 {{- end -}}
250 {{- end -}}
251
252 {{/*
253   Generate a PVC template for a statefulset
254
255   The function takes up to two arguments (inside a dictionary):
256      - .dot : environment (.)
257      - .suffix: suffix to name. if not set, default to "data".
258      - .persistenceInfos: the persitence values to use, default to
259                           `.Values.persistence`.
260                           Need to be the dict key from `.Values` in string
261                           format.
262                           let's say you have:
263
264                               persistence:
265                                 logs:
266                                   enabled: true
267                                   size: 100Mi
268                                   accessMode: ReadWriteOnce
269                                   ...
270
271                           then you have to put `.Values.persitence.logs` in
272                           order to use it.
273
274   Example calls:
275     {{ include "common.PVCTemplate" . }}
276     {{ include "common.PVCTemplate" (dict "dot" . "suffix" "my-awesome-suffix" "persistenceInfos" .Values.persistenceLog) }}
277     {{ include "common.PVCTemplate" (dict dot" . "subPath" "persistenceInfos" .Values.persistence.log) }}
278 */}}
279 {{- define "common.PVCTemplate" -}}
280 {{- $dot := default . .dot -}}
281 {{- $persistenceInfos := default $dot.Values.persistence .persistenceInfos -}}
282 {{- $suffix := default "data" .suffix -}}
283 {{- $metadata_suffix := ternary "" $suffix (eq $suffix "data") -}}
284 metadata: {{- include "common.resourceMetadata" (dict "dot" $dot "suffix" $metadata_suffix "annotations" $persistenceInfos.annotations) | nindent 2 }}
285 spec:
286   accessModes:
287   - {{ $persistenceInfos.accessMode }}
288   storageClassName: {{ include "common.storageClass" (dict "dot" $dot "suffix" $suffix "persistenceInfos" $persistenceInfos ) }}
289   resources:
290     requests:
291       storage: {{ $persistenceInfos.size }}
292 {{- end -}}