[SO] 16.0.4 release
[oom.git] / kubernetes / dcaegen2-services / common / dcaegen2-services-common / templates / _deployment.tpl
1 {{/*
2 #============LICENSE_START========================================================
3 # ================================================================================
4 # Copyright (c) 2021-2023 J. F. Lucas. All rights reserved.
5 # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
6 # Copyright (c) 2021 Nokia. All rights reserved.
7 # Copyright (c) 2021 Nordix Foundation.
8 # ================================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #     http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 # ============LICENSE_END=========================================================
21 */}}
22 {{/*
23 For internal use only!
24
25 dcaegen2-services-common._ms-specific-env-vars:
26 This template generates a list of microservice-specific environment variables
27 as specified in .Values.applicationEnv.  The
28 dcaegen2-services-common.microServiceDeployment uses this template
29 to add the microservice-specific environment variables to the microservice's container.
30 These environment variables are in addition to a standard set of environment variables
31 provided to all microservices.
32
33 The template expects a single argument, pointing to the caller's global context.
34
35 Microservice-specific environment variables can be specified in two ways:
36   1. As literal string values. (The values can also be Helm template fragments.)
37   2. As values that are sourced from a secret, identified by the secret's
38      uid and the key within the secret that provides the value.
39
40 The following example shows an example of each type.  The example assumes
41 that a secret has been created using the OOM common secret mechanism, with
42 a secret uid "example-secret" and a key called "password".
43
44 applicationEnv:
45   APPLICATION_PASSWORD:
46     secretUid: example-secret
47     key: password
48   APPLICATION_EXAMPLE: "An example value"
49
50 The example would set two environment variables on the microservice's container,
51 one called "APPLICATION_PASSWORD" with the value set from the "password" key in
52 the secret with uid "example-secret", and one called "APPLICATION_EXAMPLE" set to
53 the the literal string "An example value".
54 */}}
55 {{- define "dcaegen2-services-common._ms-specific-env-vars" -}}
56   {{- $global := . }}
57   {{- if .Values.applicationEnv }}
58     {{- range $envName, $envValue := .Values.applicationEnv }}
59       {{- if kindIs "string" $envValue }}
60 - name: {{ $envName }}
61   value: {{ tpl $envValue $global | quote }}
62       {{- else }}
63         {{- if and (hasKey $envValue "externalSecret") ($envValue.externalSecret) }}
64 - name: {{ $envName }}
65   valueFrom:
66     secretKeyRef:
67       name: {{ tpl $envValue.externalSecretUid $global | quote }}
68       key: {{ tpl $envValue.key $global | quote }}
69         {{- else }}
70           {{ if or (not $envValue.secretUid) (not $envValue.key) }}
71             {{ fail (printf "Env %s definition is not a string and does not contain secretUid or key fields" $envName) }}
72           {{- end }}
73 - name: {{ $envName }}
74   {{- include "common.secret.envFromSecretFast" (dict "global" $global "uid" $envValue.secretUid "key" $envValue.key) | indent 2 }}
75         {{- end }}
76       {{- end -}}
77     {{- end }}
78   {{- end }}
79 {{- end -}}
80 {{/*
81 For internal use only!
82
83 dcaegen2-services-common._externalVolumes:
84 This template generates a list of volumes associated with the pod,
85 based on information provided in .Values.externalVolumes.  This
86 template works in conjunction with dcaegen2-services-common._externalVolumeMounts
87 to give the microservice access to data in volumes created else.
88 This implementation supports ConfigMaps & EmptyDirs only, as this is the only
89 external volume mounting required by current microservices.
90
91 .Values.externalVolumes is a list of objects.  Each object has 3 required fields and 2 optional fields:
92    - name: the name of the resource (in the current implementation, it must be a ConfigMap)
93      that is to be set up as a volume.  The value is a case sensitive string.  Because the
94      names of resources are sometimes set at deployment time (for instance, to prefix the Helm
95      release to the name), the string can be a Helm template fragment that will be expanded at
96      deployment time.
97    - type: the type of the resource (in the current implementation, only "ConfigMap" & "emptyDir" is supported).
98      The value is a case-INsensitive string.
99    - mountPoint: the path to the mount point for the volume in the container file system.  The
100      value is a case-sensitive string.
101    - readOnly: (Optional) Boolean flag.  Set to true to mount the volume as read-only.
102      Defaults to false.
103    - optional: (Optional) Boolean flag.  Set to true to make the configMap optional (i.e., to allow the
104      microservice's pod to start even if the configMap doesn't exist).  If set to false, the configMap must
105      be present in order for the microservice's pod to start. Defaults to true.  (Note that this
106      default is the opposite of the Kubernetes default.  We've done this to be consistent with the behavior
107      of the DCAE Cloudify plugin for Kubernetes [k8splugin], which always set "optional" to true.)
108
109 Here is an example fragment from a values.yaml file for a microservice:
110
111 externalVolumes:
112   - name: my-example-configmap
113     type: configmap
114     mountPath: /opt/app/config
115   - name: '{{ include "common.release" . }}-another-example'
116     type: emptyDir
117     mountPath: /opt/app/otherconfig
118     optional: false
119 */}}
120 {{- define "dcaegen2-services-common._externalVolumes" -}}
121   {{- $global := . -}}
122   {{- if .Values.externalVolumes }}
123     {{- range $vol := .Values.externalVolumes }}
124       {{- $vname := (tpl $vol.name $global) -}}
125       {{- if eq (lower $vol.type) "configmap" }}
126         {{- $opt := hasKey $vol "optional" | ternary $vol.optional true }}
127 - configMap:
128     defaultMode: 420
129     name: {{ $vname }}
130     optional: {{ $opt }}
131   name: {{ $vname }}
132       {{- else if eq (lower $vol.type) "emptydir" }}
133 - name: {{ $vname }}
134   emptyDir:
135     sizeLimit: {{ $vol.sizeLimit }}
136       {{- end }}
137     {{- end }}
138   {{- end }}
139 {{- end }}
140 {{/*
141 For internal use only!
142
143 dcaegen2-services-common._externalVolumeMounts:
144 This template generates a list of volume mounts for the microservice container,
145 based on information provided in .Values.externalVolumes.  This
146 template works in conjunction with dcaegen2-services-common._externalVolumes
147 to give the microservice access to data in volumes created else.
148 This initial implementation supports ConfigMaps & EmptyDirs, as this is the only
149 external volume mounting required by current microservices.
150
151 See the documentation for dcaegen2-services-common._externalVolumes for
152 details on how external volumes are specified in the values.yaml file for
153 the microservice.
154 */}}
155 {{- define "dcaegen2-services-common._externalVolumeMounts" -}}
156   {{- $global := . -}}
157   {{- if .Values.externalVolumes }}
158     {{- range $vol := .Values.externalVolumes }}
159       {{- $vname := (tpl $vol.name $global) -}}
160       {{- if eq (lower $vol.type) "configmap" }}
161         {{- $readOnly := $vol.readOnly | default false }}
162 - mountPath: {{ $vol.mountPath }}
163   name: {{ $vname }}
164   readOnly: {{ $readOnly }}
165       {{- else if eq (lower $vol.type) "emptydir" }}
166 - mountPath: {{ $vol.mountPath }}
167   name: {{ $vname }}
168       {{- end }}
169     {{- end }}
170   {{- end }}
171 {{- end }}
172
173 {{/*
174 dcaegen2-services-common.microserviceDeployment:
175 This template produces a Kubernetes Deployment for a DCAE microservice.
176
177 All DCAE microservices currently use very similar Deployments.  Having a
178 common template eliminates a lot of repetition in the individual charts
179 for each microservice.
180
181 The template expects the full chart context as input.  A chart for a
182 DCAE microservice references this template using:
183 {{ include "dcaegen2-services-common.microserviceDeployment" . }}
184 The template directly references data in .Values, and indirectly (through its
185 use of templates from the ONAP "common" collection) references data in
186 .Release.
187
188 The exact content of the Deployment generated from this template
189 depends on the content of .Values.
190
191 The Deployment always includes a single Pod, with a container that uses
192 the DCAE microservice image.  The image name and tag are specified by
193 .Values.image.  By default, the image comes from the ONAP repository
194 (registry) set up by the common repositoryGenerator template.  A different
195 repository for the microservice image can be set using
196 .Values.imageRepositoryOverride.   Note that this repository must not
197 require authentication, because there is no way to specify credentials for
198 the override repository.  imageRepositoryOverride is intended primarily
199 for testing purposes.
200
201 The Deployment Pod may also include a logging sidecar container.
202 The sidecar is included if .Values.log.path is set.  The
203 logging sidecar and the DCAE microservice container share a
204 volume where the microservice logs are written.
205
206 Deployed POD may also include a Policy-sync sidecar container.
207 The sidecar is included if .Values.policies is set.  The
208 Policy-sync sidecar polls PolicyEngine (PDP) periodically based
209 on .Values.policies.duration and configuration retrieved is shared with
210 DCAE Microservice container by common volume. Policy can be retrieved based on
211 list of policyID or filter. An optional policyRelease parameter can be specified
212 to override the default policy helm release (used for retreiving the secret containing
213 pdp username and password)
214
215 Following is example policy config override
216
217 dcaePolicySyncImage: onap/org.onap.dcaegen2.deployments.dcae-services-policy-sync:1.0.1
218 policies:
219   duration: 300
220   policyRelease: "onap"
221   policyID: |
222     '["onap.vfirewall.tca","onap.vdns.tca"]'
223
224 The Deployment includes an initContainer that checks for the
225 readiness of other components that the microservice relies on.
226 This container is generated by the "common.readinessCheck.waitfor"
227 template. See the documentation for this template
228 (oom/kubernetes/common/readinessCheck/templates/_readinessCheck.tpl).
229
230 If the microservice uses a DMaaP Data Router (DR) feed, the Deployment
231 includes an initContainer that makes provisioning requests to the DMaaP
232 bus controller (dmaap-bc) to create the feed and to set up a publisher
233 and/or subscriber to the feed.  The Deployment also includes a second
234 initContainer that merges the information returned by the provisioning
235 process into the microservice's configuration.  See the documentation for
236 the common DMaaP provisioning template
237 (oom/kubernetes/common/common/templates/_dmaapProvisioning.tpl).
238
239 If the microservice uses certificates from an external CMPv2 provider,
240 the Deployment will include an initContainer that performs certificate
241 post-processing.
242 */}}
243
244 {{- define "dcaegen2-services-common.microserviceDeployment" -}}
245 {{- $log := default dict .Values.log -}}
246 {{- $logDir :=  default "" $log.path -}}
247 {{- $tmp := default dict .Values.tmpDir -}}
248 {{- $tmpDir :=  default false $tmp.enabled -}}
249 {{- $ves := default false .Values.ves -}}
250 {{- $certDir := (eq "true" (include "common.needTLS" .)) | ternary (default "" .Values.certDirectory . ) "" -}}
251 {{- $commonRelease :=  print (include "common.release" .) -}}
252 {{- $policy := default dict .Values.policies -}}
253 {{- $policyRls := default $commonRelease $policy.policyRelease -}}
254 {{- $drNeedProvisioning := or .Values.drFeedConfig .Values.drSubConfig -}}
255 {{- $dcaeName := print (include "common.fullname" .) }}
256 {{- $dcaeLabel := (dict "dcaeMicroserviceName" $dcaeName) -}}
257 {{- $podLabels := default .Values.podLabels .labels -}}
258 {{- $dot := . -}}
259 apiVersion: apps/v1
260 kind: Deployment
261 metadata: {{- include "common.resourceMetadata" (dict "dot" $dot "labels" $dcaeLabel) | nindent 2 }}
262 spec:
263   replicas: 1
264   selector: {{- include "common.selectors" . | nindent 4 }}
265   template:
266     {{- if $podLabels}}
267     metadata: {{- include "common.templateMetadata" (dict "dot" . "labels" $podLabels) | nindent 6 }}
268     {{- else }}
269     metadata: {{- include "common.templateMetadata" . | nindent 6 }}
270     {{- end }}
271     spec:
272       securityContext:
273         {{- toYaml .Values.podSecurityContext | nindent 8 }}
274       initContainers:
275
276       {{- if $ves }}
277         {{- include "dcaegen2-ves-collector.vesCollectorCopyEtc" . | nindent 6 }}
278       {{- end }}
279       {{- if .Values.readinessCheck }}
280         {{ include "common.readinessCheck.waitFor" . | nindent 6 }}
281       {{- end }}
282       {{- include "common.dmaap.provisioning.initContainer" . | nindent 6 }}
283       {{ include "dcaegen2-services-common._certPostProcessor" .  | nindent 4 }}
284       containers:
285       - image: {{ default ( include "repositoryGenerator.repository" . ) .Values.imageRepositoryOverride }}/{{ .Values.image }}
286         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
287         name: {{ include "common.name" . }}
288         securityContext:
289           {{- toYaml .Values.containerSecurityContext | nindent 10 }}
290         env:
291         {{- range $cred := .Values.credentials }}
292         - name: {{ $cred.name }}
293           {{- include "common.secret.envFromSecretFast" (dict "global" $ "uid" $cred.uid "key" $cred.key) | indent 10 }}
294         {{- end }}
295         {{- if $certDir }}
296         - name: DCAE_CA_CERTPATH
297           value: {{ $certDir }}/cacert.pem
298         {{- end }}
299         - name: CONSUL_HOST
300           value: consul-server.onap
301         - name: CONFIG_BINDING_SERVICE
302           value: config-binding-service
303         - name: CBS_CONFIG_URL
304           value: https://config-binding-service:10443/service_component_all/{{ include "common.name" . }}
305         - name: POD_IP
306           valueFrom:
307             fieldRef:
308               apiVersion: v1
309               fieldPath: status.podIP
310         {{- include "dcaegen2-services-common._ms-specific-env-vars" . | nindent 8 }}
311         {{- if .Values.service }}
312         ports: {{ include "common.containerPorts" . | nindent 10 }}
313         {{- end }}
314         {{- if .Values.readiness }}
315         readinessProbe:
316           initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds | default 5 }}
317           periodSeconds: {{ .Values.readiness.periodSeconds | default 15 }}
318           timeoutSeconds: {{ .Values.readiness.timeoutSeconds | default 1 }}
319           {{- $probeType := .Values.readiness.type | default "httpGet" -}}
320           {{- if eq $probeType "httpGet" }}
321           httpGet:
322             scheme: {{ .Values.readiness.scheme }}
323             path: {{ .Values.readiness.path }}
324             port: {{ .Values.readiness.port }}
325           {{- end }}
326           {{- if eq $probeType "exec" }}
327           exec:
328             command:
329             {{- range $cmd := .Values.readiness.command }}
330             - {{ $cmd }}
331             {{- end }}
332           {{- end }}
333         {{- end }}
334         {{- if .Values.liveness }}
335         livenessProbe:
336             initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds | default 5 }}
337             periodSeconds: {{ .Values.liveness.periodSeconds | default 15 }}
338             timeoutSeconds: {{ .Values.liveness.timeoutSeconds | default 1 }}
339             {{- $probeType := .Values.liveness.type | default "httpGet" -}}
340             {{- if eq $probeType "httpGet" }}
341             httpGet:
342              scheme: {{ .Values.liveness.scheme }}
343              path: {{ .Values.liveness.path }}
344              port: {{ .Values.liveness.port }}
345             {{- end }}
346             {{- if eq $probeType "exec" }}
347             exec:
348                 command:
349                 {{- range $cmd := .Values.liveness.command }}
350                 - {{ $cmd }}
351                 {{- end }}
352             {{- end }}
353         {{- end }}
354         resources: {{ include "common.resources" . | nindent 10 }}
355         volumeMounts:
356         - mountPath: /app-config
357           name: {{ ternary "app-config-input" "app-config" (not $drNeedProvisioning) }}
358         - mountPath: /app-config-input
359           name: app-config-input
360         {{- if $tmpDir }}
361         - mountPath: /tmp
362           name: tmp
363         {{- end }}
364         {{- if $logDir }}
365         - mountPath: {{ $logDir}}
366           name: logs
367         {{- end }}
368         {{- if $certDir }}
369         - mountPath: {{ $certDir }}
370           name: tls-info
371           {{- if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
372           {{- include "common.certManager.volumeMountsReadOnly" . | nindent 8 -}}
373           {{- end -}}
374         {{- end }}
375         {{- if $policy }}
376         - name: policy-shared
377           mountPath: /etc/policies
378         {{- end }}
379         {{- include "dcaegen2-services-common._externalVolumeMounts" . | nindent 8 }}
380       {{- if $logDir }}
381       {{ include "common.log.sidecar" . | nindent 6 }}
382       {{- end }}
383       {{- if $policy }}
384       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.dcaePolicySyncImage }}
385         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
386         name: policy-sync
387         env:
388         - name: POD_IP
389           valueFrom:
390             fieldRef:
391               apiVersion: v1
392               fieldPath: status.podIP
393         - name: POLICY_SYNC_PDP_USER
394           valueFrom:
395             secretKeyRef:
396               name: {{ $policyRls }}-policy-xacml-pdp-restserver-creds
397               key: login
398         - name: POLICY_SYNC_PDP_PASS
399           valueFrom:
400             secretKeyRef:
401               name: {{ $policyRls }}-policy-xacml-pdp-restserver-creds
402               key: password
403         - name: POLICY_SYNC_PDP_URL
404           value : http{{ if (include "common.needTLS" .) }}s{{ end }}://policy-xacml-pdp:6969
405         - name: POLICY_SYNC_OUTFILE
406           value : "/etc/policies/policies.json"
407         - name: POLICY_SYNC_V1_DECISION_ENDPOINT
408           value : "policy/pdpx/v1/decision"
409         {{- if $policy.filter }}
410         - name: POLICY_SYNC_FILTER
411           value: {{ $policy.filter }}
412         {{- end -}}
413         {{- if $policy.policyID }}
414         - name: POLICY_SYNC_ID
415           value: {{ $policy.policyID }}
416         {{- end -}}
417         {{- if $policy.duration }}
418         - name: POLICY_SYNC_DURATION
419           value: "{{ $policy.duration }}"
420         {{- end }}
421         resources: {{ include "common.resources" . | nindent 10 }}
422         volumeMounts:
423         - mountPath: /etc/policies
424           name: policy-shared
425       {{- end }}
426       hostname: {{ include "common.name" . }}
427       serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}}
428       volumes:
429       - configMap:
430           defaultMode: 420
431           name: {{ include "common.fullname" . }}-application-config-configmap
432         name: app-config-input
433       - emptyDir:
434           medium: Memory
435         name: app-config
436       {{- if $tmpDir }}
437       - name: tmp
438         emptyDir:
439           sizeLimit: 128Mi
440       {{- end }}
441       {{- if $logDir }}
442       - name: logs
443         emptyDir:
444           sizeLimit: 128Mi
445       {{ include "common.log.volumes" (dict "dot" . "configMapNamePrefix" (tpl .Values.logConfigMapNamePrefix . )) | nindent 6 }}
446       {{- end }}
447       {{- if $certDir }}
448       - emptyDir: {}
449         name: tls-info
450         {{ if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
451         {{ include "common.certManager.volumesReadOnly" . | nindent 6 }}
452         {{- end }}
453       {{- end }}
454       {{- if $policy }}
455       - name: policy-shared
456         emptyDir: {}
457       {{- end }}
458       {{- include "common.dmaap.provisioning._volumes" . | nindent 6 -}}
459       {{- include "dcaegen2-services-common._externalVolumes" . | nindent 6 }}
460       {{- include "common.imagePullSecrets" . | nindent 6 }}
461 {{ end -}}
462
463 {{/*
464   For internal use
465
466   Template to attach CertPostProcessor which merges CMPv2 truststore with AAF truststore
467   and swaps keystore files.
468 */}}
469 {{- define "dcaegen2-services-common._certPostProcessor" -}}
470   {{- $certDir := default "" .Values.certDirectory . -}}
471   {{- if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
472     {{- $cmpv2Certificate := (index .Values.certificates 0) -}}
473     {{- $cmpv2CertificateDir := $cmpv2Certificate.mountPath -}}
474     {{- $certType := "pem" -}}
475     {{- if $cmpv2Certificate.keystore -}}
476       {{- $certType = (index $cmpv2Certificate.keystore.outputType 0) -}}
477     {{- end -}}
478     {{- $truststoresPaths := printf "%s/%s:%s/%s" $certDir "cacert.pem" $cmpv2CertificateDir "cacert.pem" -}}
479     {{- $truststoresPasswordPaths := ":" -}}
480     {{- $keystoreSourcePaths := printf "%s/%s:%s/%s" $cmpv2CertificateDir "cert.pem" $cmpv2CertificateDir "key.pem" -}}
481     {{- $keystoreDestinationPaths := printf "%s/%s:%s/%s" $certDir "cert.pem" $certDir "key.pem" -}}
482     {{- if not (eq $certType "pem") -}}
483       {{- $truststoresPaths = printf "%s/%s:%s/%s.%s" $certDir "trust.jks" $cmpv2CertificateDir "truststore" $certType -}}
484       {{- $truststoresPasswordPaths = printf "%s/%s:%s/%s" $certDir "trust.pass" $cmpv2CertificateDir "truststore.pass" -}}
485       {{- $keystoreSourcePaths = printf "%s/%s.%s:%s/%s" $cmpv2CertificateDir "keystore" $certType $cmpv2CertificateDir "keystore.pass" -}}
486       {{- $keystoreDestinationPaths = printf "%s/%s.%s:%s/%s.pass" $certDir "cert" $certType $certDir $certType -}}
487     {{- end }}
488   - name: cert-post-processor
489     image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.certPostProcessorImage }}
490     imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
491     resources:
492       {{- include "common.resources" . | nindent 4 }}
493     volumeMounts:
494     - mountPath: {{ $certDir }}
495       name: tls-info
496       {{- include "common.certManager.volumeMountsReadOnly" . | nindent 4 }}
497     env:
498     - name: TRUSTSTORES_PATHS
499       value: {{ $truststoresPaths | quote}}
500     - name: TRUSTSTORES_PASSWORDS_PATHS
501       value: {{ $truststoresPasswordPaths | quote }}
502     - name: KEYSTORE_SOURCE_PATHS
503       value: {{ $keystoreSourcePaths | quote }}
504     - name: KEYSTORE_DESTINATION_PATHS
505       value: {{ $keystoreDestinationPaths | quote }}
506   {{- end }}
507 {{- end -}}
508
509 {{/*
510   Template returns string "true" if CMPv2 certificates should be used and nothing (so it can be used in with statements)
511   when they shouldn't. Example use:
512     {{- if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
513
514 */}}
515 {{- define "dcaegen2-services-common.shouldUseCmpv2Certificates" -}}
516   {{- $certDir := default "" .Values.certDirectory . -}}
517   {{- if (and $certDir .Values.certificates .Values.global.cmpv2Enabled .Values.useCmpv2Certificates) -}}
518   true
519   {{- end -}}
520 {{- end -}}