[DCAEGEN2] Updates for R10
[oom.git] / kubernetes / dcaegen2-services / common / dcaegen2-services-common / templates / _deployment.tpl
1 {{/*
2 #============LICENSE_START========================================================
3 # ================================================================================
4 # Copyright (c) 2021 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.
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: {{ $envValue | quote }}
62       {{- else }}
63         {{ if or (not $envValue.secretUid) (not $envValue.key) }}
64           {{ fail (printf "Env %s definition is not a string and does not contain secretUid or key fields" $envName) }}
65         {{- end }}
66 - name: {{ $envName }}
67   {{- include "common.secret.envFromSecretFast" (dict "global" $global "uid" $envValue.secretUid "key" $envValue.key) | indent 2 }}
68       {{- end -}}
69     {{- end }}
70   {{- end }}
71 {{- end -}}
72 {{/*
73 For internal use only!
74
75 dcaegen2-services-common._externalVolumes:
76 This template generates a list of volumes associated with the pod,
77 based on information provided in .Values.externalVolumes.  This
78 template works in conjunction with dcaegen2-services-common._externalVolumeMounts
79 to give the microservice access to data in volumes created else.
80 This initial implementation supports ConfigMaps only, as this is the only
81 external volume mounting required by current microservices.
82
83 .Values.externalVolumes is a list of objects.  Each object has 3 required fields and 2 optional fields:
84    - name: the name of the resource (in the current implementation, it must be a ConfigMap)
85      that is to be set up as a volume.  The value is a case sensitive string.  Because the
86      names of resources are sometimes set at deployment time (for instance, to prefix the Helm
87      release to the name), the string can be a Helm template fragment that will be expanded at
88      deployment time.
89    - type: the type of the resource (in the current implementation, only "ConfigMap" is supported).
90      The value is a case-INsensitive string.
91    - mountPoint: the path to the mount point for the volume in the container file system.  The
92      value is a case-sensitive string.
93    - readOnly: (Optional) Boolean flag.  Set to true to mount the volume as read-only.
94      Defaults to false.
95    - optional: (Optional) Boolean flag.  Set to true to make the configMap optional (i.e., to allow the
96      microservice's pod to start even if the configMap doesn't exist).  If set to false, the configMap must
97      be present in order for the microservice's pod to start. Defaults to true.  (Note that this
98      default is the opposite of the Kubernetes default.  We've done this to be consistent with the behavior
99      of the DCAE Cloudify plugin for Kubernetes [k8splugin], which always set "optional" to true.)
100
101 Here is an example fragment from a values.yaml file for a microservice:
102
103 externalVolumes:
104   - name: my-example-configmap
105     type: configmap
106     mountPath: /opt/app/config
107   - name: '{{ include "common.release" . }}-another-example'
108     type: configmap
109     mountPath: /opt/app/otherconfig
110     optional: false
111 */}}
112 {{- define "dcaegen2-services-common._externalVolumes" -}}
113   {{- $global := . -}}
114   {{- if .Values.externalVolumes }}
115     {{- range $vol := .Values.externalVolumes }}
116       {{- if eq (lower $vol.type) "configmap" }}
117         {{- $vname := (tpl $vol.name $global) -}}
118         {{- $opt := hasKey $vol "optional" | ternary $vol.optional true }}
119 - configMap:
120     defaultMode: 420
121     name: {{ $vname }}
122     optional: {{ $opt }}
123   name: {{ $vname }}
124       {{- end }}
125     {{- end }}
126   {{- end }}
127 {{- end }}
128 {{/*
129 For internal use only!
130
131 dcaegen2-services-common._externalVolumeMounts:
132 This template generates a list of volume mounts for the microservice container,
133 based on information provided in .Values.externalVolumes.  This
134 template works in conjunction with dcaegen2-services-common._externalVolumes
135 to give the microservice access to data in volumes created else.
136 This initial implementation supports ConfigMaps only, as this is the only
137 external volume mounting required by current microservices.
138
139 See the documentation for dcaegen2-services-common._externalVolumes for
140 details on how external volumes are specified in the values.yaml file for
141 the microservice.
142 */}}
143 {{- define "dcaegen2-services-common._externalVolumeMounts" -}}
144   {{- $global := . -}}
145   {{- if .Values.externalVolumes }}
146     {{- range $vol := .Values.externalVolumes }}
147       {{- if eq (lower $vol.type) "configmap" }}
148         {{- $vname := (tpl $vol.name $global) -}}
149         {{- $readOnly := $vol.readOnly | default false }}
150 - mountPath: {{ $vol.mountPath }}
151   name: {{ $vname }}
152   readOnly: {{ $readOnly }}
153       {{- end }}
154     {{- end }}
155   {{- end }}
156 {{- end }}
157 {{/*
158 dcaegen2-services-common.microserviceDeployment:
159 This template produces a Kubernetes Deployment for a DCAE microservice.
160
161 All DCAE microservices currently use very similar Deployments.  Having a
162 common template eliminates a lot of repetition in the individual charts
163 for each microservice.
164
165 The template expects the full chart context as input.  A chart for a
166 DCAE microservice references this template using:
167 {{ include "dcaegen2-services-common.microserviceDeployment" . }}
168 The template directly references data in .Values, and indirectly (through its
169 use of templates from the ONAP "common" collection) references data in
170 .Release.
171
172 The exact content of the Deployment generated from this template
173 depends on the content of .Values.
174
175 The Deployment always includes a single Pod, with a container that uses
176 the DCAE microservice image.
177
178 The Deployment Pod may also include a logging sidecar container.
179 The sidecar is included if .Values.logDirectory is set.  The
180 logging sidecar and the DCAE microservice container share a
181 volume where the microservice logs are written.
182
183 The Deployment includes an initContainer that pushes the
184 microservice's initial configuration (from .Values.applicationConfig)
185 into Consul.  All DCAE microservices retrieve their initial
186 configurations by making an API call to a DCAE platform component called
187 the  config-binding-service.  The config-binding-service currently
188 retrieves configuration information from Consul.
189
190 The Deployment also includes an initContainer that checks for the
191 readiness of other components that the microservice relies on.
192 This container is generated by the "common.readinessCheck.waitfor"
193 template.
194
195 If the microservice acts as a TLS client or server, the Deployment will
196 include an initContainer that retrieves certificate information from
197 the AAF certificate manager.  The information is mounted at the
198 mount point specified in .Values.certDirectory.  If the microservice is
199 a TLS server (indicated by setting .Values.tlsServer to true), the
200 certificate information will include a server cert and key, in various
201 formats.  It will also include the AAF CA cert.   If the microservice is
202 a TLS client only (indicated by setting .Values.tlsServer to false), the
203 certificate information includes only the AAF CA cert.
204
205 Deployed POD may also include a Policy-sync sidecar container.
206 The sidecar is included if .Values.policies is set.  The
207 Policy-sync sidecar polls PolicyEngine (PDP) periodically based
208 on .Values.policies.duration and configuration retrieved is shared with
209 DCAE Microservice container by common volume. Policy can be retrieved based on
210 list of policyID or filter. An optional policyRelease parameter can be specified
211 to override the default policy helm release (used for retreiving the secret containing
212 pdp username and password)
213
214 Following is example policy config override
215
216 dcaePolicySyncImage: onap/org.onap.dcaegen2.deployments.dcae-services-policy-sync:1.0.1
217 policies:
218   duration: 300
219   policyRelease: "onap"
220   policyID: |
221     '["onap.vfirewall.tca","onap.vdns.tca"]'
222 */}}
223
224 {{- define "dcaegen2-services-common.microserviceDeployment" -}}
225 {{- $logDir :=  default "" .Values.logDirectory -}}
226 {{- $certDir := default "" .Values.certDirectory . -}}
227 {{- $tlsServer := default "" .Values.tlsServer -}}
228 {{- $commonRelease :=  print (include "common.release" .) -}}
229 {{- $policy := default dict .Values.policies -}}
230 {{- $policyRls := default $commonRelease $policy.policyRelease -}}
231 {{- $drFeedConfig := default "" .Values.drFeedConfig -}}
232
233 apiVersion: apps/v1
234 kind: Deployment
235 metadata: {{- include "common.resourceMetadata" . | nindent 2 }}
236 spec:
237   replicas: 1
238   selector: {{- include "common.selectors" . | nindent 4 }}
239   template:
240     metadata: {{- include "common.templateMetadata" . | nindent 6 }}
241     spec:
242       initContainers:
243       {{- if not $drFeedConfig }}
244       - command:
245         - sh
246         args:
247         - -c
248         - |
249         {{- range $var := .Values.customEnvVars }}
250           export {{ $var.name }}="{{ $var.value }}";
251         {{- end }}
252           cd /config-input && for PFILE in `ls -1`; do envsubst <${PFILE} >/config/${PFILE}; done
253         env:
254         {{- range $cred := .Values.credentials }}
255         - name: {{ $cred.name }}
256           {{- include "common.secret.envFromSecretFast" (dict "global" $ "uid" $cred.uid "key" $cred.key) | indent 10 }}
257         {{- end }}
258         volumeMounts:
259         - mountPath: /config-input
260           name: app-config-input
261         - mountPath: /config
262           name: app-config
263         image: {{ include "repositoryGenerator.image.envsubst" . }}
264         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
265         name: {{ include "common.name" . }}-update-config
266       {{- end }}
267       {{ include "common.readinessCheck.waitFor" . | indent 6 | trim }}
268       {{- include "common.dmaap.provisioning.initContainer" . | nindent 6 }}
269       - name: init-consul
270         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.consulLoaderImage }}
271         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
272         args:
273         - --key-yaml
274         - "{{ include "common.name" . }}|/app-config/application_config.yaml"
275         env:
276         - name: CONSUL_HOST
277           value: {{ .Values.consulHost | default "consul-server-ui" }}.{{ include "common.namespace" . }}
278         resources: {{ include "common.resources" . | nindent 2 }}
279         volumeMounts:
280           - mountPath: /app-config
281             name: app-config
282       {{- if $certDir }}
283       - name: init-tls
284         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.tlsImage }}
285         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
286         env:
287         - name: TLS_SERVER
288           value: {{ $tlsServer | quote }}
289         - name: POD_IP
290           valueFrom:
291             fieldRef:
292               apiVersion: v1
293               fieldPath: status.podIP
294         resources: {{ include "common.resources" . | nindent 2 }}
295         volumeMounts:
296         - mountPath: /opt/app/osaaf
297           name: tls-info
298       {{- end }}
299       {{ include "dcaegen2-services-common._certPostProcessor" .  | nindent 4 }}
300       containers:
301       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.image }}
302         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
303         name: {{ include "common.name" . }}
304         env:
305         {{- range $cred := .Values.credentials }}
306         - name: {{ $cred.name }}
307           {{- include "common.secret.envFromSecretFast" (dict "global" $ "uid" $cred.uid "key" $cred.key) | indent 10 }}
308         {{- end }}
309         {{- if $certDir }}
310         - name: DCAE_CA_CERTPATH
311           value: {{ $certDir }}/cacert.pem
312         {{- end }}
313         - name: CONSUL_HOST
314           value: consul-server.onap
315         - name: CONFIG_BINDING_SERVICE
316           value: config-binding-service
317         - name: CBS_CONFIG_URL
318           value: https://config-binding-service:10443/service_component_all/{{ include "common.name" . }}
319         - name: POD_IP
320           valueFrom:
321             fieldRef:
322               apiVersion: v1
323               fieldPath: status.podIP
324         {{- include "dcaegen2-services-common._ms-specific-env-vars" . | nindent 8 }}
325         {{- if .Values.service }}
326         ports: {{ include "common.containerPorts" . | nindent 10 }}
327         {{- end }}
328         {{- if .Values.readiness }}
329         readinessProbe:
330           initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds | default 5 }}
331           periodSeconds: {{ .Values.readiness.periodSeconds | default 15 }}
332           timeoutSeconds: {{ .Values.readiness.timeoutSeconds | default 1 }}
333           {{- $probeType := .Values.readiness.type | default "httpGet" -}}
334           {{- if eq $probeType "httpGet" }}
335           httpGet:
336             scheme: {{ .Values.readiness.scheme }}
337             path: {{ .Values.readiness.path }}
338             port: {{ .Values.readiness.port }}
339           {{- end }}
340           {{- if eq $probeType "exec" }}
341           exec:
342             command:
343             {{- range $cmd := .Values.readiness.command }}
344             - {{ $cmd }}
345             {{- end }}
346           {{- end }}
347         {{- end }}
348         resources: {{ include "common.resources" . | nindent 2 }}
349         volumeMounts:
350         - mountPath: /app-config
351           name: app-config
352         - mountPath: /app-config-input
353           name: app-config-input
354         {{- if $logDir }}
355         - mountPath: {{ $logDir}}
356           name: component-log
357         {{- end }}
358         {{- if $certDir }}
359         - mountPath: {{ $certDir }}
360           name: tls-info
361           {{- if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
362           {{- include "common.certManager.volumeMountsReadOnly" . | nindent 8 -}}
363           {{- end -}}
364         {{- end }}
365         {{- if $policy }}
366         - name: policy-shared
367           mountPath: /etc/policies
368         {{- end }}
369         {{- include "dcaegen2-services-common._externalVolumeMounts" . | nindent 8 }}
370       {{- if $logDir }}
371       - image: {{ include "repositoryGenerator.image.logging" . }}
372         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
373         name: filebeat
374         env:
375         - name: POD_IP
376           valueFrom:
377             fieldRef:
378               apiVersion: v1
379               fieldPath: status.podIP
380         resources: {{ include "common.resources" . | nindent 2 }}
381         volumeMounts:
382         - mountPath: /var/log/onap/{{ include "common.name" . }}
383           name: component-log
384         - mountPath: /usr/share/filebeat/data
385           name: filebeat-data
386         - mountPath: /usr/share/filebeat/filebeat.yml
387           name: filebeat-conf
388           subPath: filebeat.yml
389       {{- end }}
390       {{- if $policy }}
391       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.dcaePolicySyncImage }}
392         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
393         name: policy-sync
394         env:
395         - name: POD_IP
396           valueFrom:
397             fieldRef:
398               apiVersion: v1
399               fieldPath: status.podIP
400         - name: POLICY_SYNC_PDP_USER
401           valueFrom:
402             secretKeyRef:
403               name: {{ $policyRls }}-policy-xacml-pdp-api-creds
404               key: login
405         - name: POLICY_SYNC_PDP_PASS
406           valueFrom:
407             secretKeyRef:
408               name: {{ $policyRls }}-policy-xacml-pdp-api-creds
409               key: password
410         - name: POLICY_SYNC_PDP_URL
411           value : http{{ if (include "common.needTLS" .) }}s{{ end }}://policy-xacml-pdp:6969
412         - name: POLICY_SYNC_OUTFILE
413           value : "/etc/policies/policies.json"
414         - name: POLICY_SYNC_V1_DECISION_ENDPOINT
415           value : "policy/pdpx/v1/decision"
416         {{- if $policy.filter }}
417         - name: POLICY_SYNC_FILTER
418           value: {{ $policy.filter }}
419         {{- end -}}
420         {{- if $policy.policyID }}
421         - name: POLICY_SYNC_ID
422           value: {{ $policy.policyID }}
423         {{- end -}}
424         {{- if $policy.duration }}
425         - name: POLICY_SYNC_DURATION
426           value: "{{ $policy.duration }}"
427         {{- end }}
428         resources: {{ include "common.resources" . | nindent 2 }}
429         volumeMounts:
430         - mountPath: /etc/policies
431           name: policy-shared
432         {{- if $certDir }}
433         - mountPath: /opt/ca-certificates/
434           name: tls-info
435         {{- end }}
436       {{- end }}
437       hostname: {{ include "common.name" . }}
438       serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}}
439       volumes:
440       - configMap:
441           defaultMode: 420
442           name: {{ include "common.fullname" . }}-application-config-configmap
443         name: app-config-input
444       - emptyDir:
445           medium: Memory
446         name: app-config
447       {{- if $logDir }}
448       - emptyDir: {}
449         name: component-log
450       - emptyDir: {}
451         name: filebeat-data
452       - configMap:
453           defaultMode: 420
454           name: {{ include "common.fullname" . }}-filebeat-configmap
455         name: filebeat-conf
456       {{- end }}
457       {{- if $certDir }}
458       - emptyDir: {}
459         name: tls-info
460         {{ if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
461         {{ include "common.certManager.volumesReadOnly" . | nindent 6 }}
462         {{- end }}
463       {{- end }}
464       {{- if $policy }}
465       - name: policy-shared
466         emptyDir: {}
467       {{- end }}
468       {{- include "common.dmaap.provisioning._volumes" . | nindent 6 -}}
469       {{- include "dcaegen2-services-common._externalVolumes" . | nindent 6 }}
470       imagePullSecrets:
471       - name: "{{ include "common.namespace" . }}-docker-registry-key"
472 {{ end -}}
473
474 {{/*
475   For internal use
476
477   Template to attach CertPostProcessor which merges CMPv2 truststore with AAF truststore
478   and swaps keystore files.
479 */}}
480 {{- define "dcaegen2-services-common._certPostProcessor" -}}
481   {{- $certDir := default "" .Values.certDirectory . -}}
482   {{- if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
483     {{- $cmpv2Certificate := (index .Values.certificates 0) -}}
484     {{- $cmpv2CertificateDir := $cmpv2Certificate.mountPath -}}
485     {{- $certType := "pem" -}}
486     {{- if $cmpv2Certificate.keystore -}}
487       {{- $certType = (index $cmpv2Certificate.keystore.outputType 0) -}}
488     {{- end -}}
489     {{- $truststoresPaths := printf "%s/%s:%s/%s" $certDir "cacert.pem" $cmpv2CertificateDir "cacert.pem" -}}
490     {{- $truststoresPasswordPaths := ":" -}}
491     {{- $keystoreSourcePaths := printf "%s/%s:%s/%s" $cmpv2CertificateDir "cert.pem" $cmpv2CertificateDir "key.pem" -}}
492     {{- $keystoreDestinationPaths := printf "%s/%s:%s/%s" $certDir "cert.pem" $certDir "key.pem" -}}
493     {{- if not (eq $certType "pem") -}}
494       {{- $truststoresPaths = printf "%s/%s:%s/%s.%s" $certDir "trust.jks" $cmpv2CertificateDir "truststore" $certType -}}
495       {{- $truststoresPasswordPaths = printf "%s/%s:%s/%s" $certDir "trust.pass" $cmpv2CertificateDir "truststore.pass" -}}
496       {{- $keystoreSourcePaths = printf "%s/%s.%s:%s/%s" $cmpv2CertificateDir "keystore" $certType $cmpv2CertificateDir "keystore.pass" -}}
497       {{- $keystoreDestinationPaths = printf "%s/%s.%s:%s/%s.pass" $certDir "cert" $certType $certDir $certType -}}
498     {{- end }}
499   - name: cert-post-processor
500     image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.certPostProcessorImage }}
501     imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
502     resources:
503       {{- include "common.resources" . | nindent 4 }}
504     volumeMounts:
505     - mountPath: {{ $certDir }}
506       name: tls-info
507       {{- include "common.certManager.volumeMountsReadOnly" . | nindent 4 }}
508     env:
509     - name: TRUSTSTORES_PATHS
510       value: {{ $truststoresPaths | quote}}
511     - name: TRUSTSTORES_PASSWORDS_PATHS
512       value: {{ $truststoresPasswordPaths | quote }}
513     - name: KEYSTORE_SOURCE_PATHS
514       value: {{ $keystoreSourcePaths | quote }}
515     - name: KEYSTORE_DESTINATION_PATHS
516       value: {{ $keystoreDestinationPaths | quote }}
517   {{- end }}
518 {{- end -}}
519
520 {{/*
521   Template returns string "true" if CMPv2 certificates should be used and nothing (so it can be used in with statements)
522   when they shouldn't. Example use:
523     {{- if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
524
525 */}}
526 {{- define "dcaegen2-services-common.shouldUseCmpv2Certificates" -}}
527   {{- $certDir := default "" .Values.certDirectory . -}}
528   {{- if (and $certDir .Values.certificates .Values.global.cmpv2Enabled .Values.useCmpv2Certificates) -}}
529   true
530   {{- end -}}
531 {{- end -}}