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