5de526288e1e49ebeef4586b33f1e4b328cec227
[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 # ================================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #     http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END=========================================================
20 */}}
21 {{/*
22 For internal use only!
23
24 dcaegen2-services-common._ms-specific-env-vars:
25 This template generates a list of microservice-specific environment variables
26 as specified in .Values.applicationEnv.  The
27 dcaegen2-services-common.microServiceDeployment uses this template
28 to add the microservice-specific environment variables to the microservice's container.
29 These environment variables are in addition to a standard set of environment variables
30 provided to all microservices.
31
32 The template expects a single argument, pointing to the caller's global context.
33
34 Microservice-specific environment variables can be specified in two ways:
35   1. As literal string values.
36   2. As values that are sourced from a secret, identified by the secret's
37      uid and the key within the secret that provides the value.
38
39 The following example shows an example of each type.  The example assumes
40 that a secret has been created using the OOM common secret mechanism, with
41 a secret uid "example-secret" and a key called "password".
42
43 applicationEnv:
44   APPLICATION_PASSWORD:
45     secretUid: example-secret
46     key: password
47   APPLICATION_EXAMPLE: "An example value"
48
49 The example would set two environment variables on the microservice's container,
50 one called "APPLICATION_PASSWORD" with the value set from the "password" key in
51 the secret with uid "example-secret", and one called "APPLICATION_EXAMPLE" set to
52 the the literal string "An example value".
53 */}}
54 {{- define "dcaegen2-services-common._ms-specific-env-vars" -}}
55   {{- $global := . }}
56   {{- if .Values.applicationEnv }}
57     {{- range $envName, $envValue := .Values.applicationEnv }}
58       {{- if kindIs "string" $envValue }}
59 - name: {{ $envName }}
60   value: {{ $envValue | quote }}
61       {{- else }}
62         {{ if or (not $envValue.secretUid) (not $envValue.key) }}
63           {{ fail (printf "Env %s definition is not a string and does not contain secretUid or key fields" $envName) }}
64         {{- end }}
65 - name: {{ $envName }}
66   {{- include "common.secret.envFromSecretFast" (dict "global" $global "uid" $envValue.secretUid "key" $envValue.key) | indent 2 }}
67       {{- end -}}
68     {{- end }}
69   {{- end }}
70 {{- end -}}
71 {{/*
72 For internal use only!
73
74 dcaegen2-services-common._externalVolumes:
75 This template generates a list of volumes associated with the pod,
76 based on information provided in .Values.externalVolumes.  This
77 template works in conjunction with dcaegen2-services-common._externalVolumeMounts
78 to give the microservice access to data in volumes created else.
79 This initial implementation supports ConfigMaps only, as this is the only
80 external volume mounting required by current microservices.
81
82 .Values.externalValues is a list of objects.  Each object has 3 required fields and 1 optional field:
83    - name: the name of the resource (in the current implementation, it must be a ConfigMap)
84      that is to be set up as a volume.  The value is a case sensitive string.  Because the
85      names of resources are sometimes set at deployment time (for instance, to prefix the Helm
86      release to the name), the string can be a Helm template fragment that will be expanded at
87      deployment time.
88    - type: the type of the resource (in the current implementation, only "ConfigMap" is supported).
89      The value is a case-INsensitive string.
90    - mountPoint: the path to the mount point for the volume in the container file system.  The
91      value is a case-sensitive string.
92    - readOnly: (Optional) Boolean flag.  Set to true to mount the volume as read-only.
93      Defaults to false.
94
95 Here is an example fragment from a values.yaml file for a microservice:
96
97 externalVolumes:
98   - name: my-example-configmap
99     type: configmap
100     mountPath: /opt/app/config
101   - name: '{{ include "common.release" . }}-another-example'
102     type: configmap
103     mountPath: /opt/app/otherconfig
104 */}}
105 {{- define "dcaegen2-services-common._externalVolumes" -}}
106   {{- $global := . -}}
107   {{- if .Values.externalVolumes }}
108     {{- range $vol := .Values.externalVolumes }}
109       {{- if eq (lower $vol.type) "configmap" }}
110         {{- $vname := (tpl $vol.name $global) }}
111 - configMap:
112     defaultMode: 420
113     name: {{ $vname }}
114   name: {{ $vname }}
115       {{- end }}
116     {{- end }}
117   {{- end }}
118 {{- end }}
119 {{/*
120 For internal use only!
121
122 dcaegen2-services-common._externalVolumeMounts:
123 This template generates a list of volume mounts for the microservice container,
124 based on information provided in .Values.externalVolumes.  This
125 template works in conjunction with dcaegen2-services-common._externalVolumes
126 to give the microservice access to data in volumes created else.
127 This initial implementation supports ConfigMaps only, as this is the only
128 external volume mounting required by current microservices.
129
130 See the documentation for dcaegen2-services-common._externalVolumes for
131 details on how external volumes are specified in the values.yaml file for
132 the microservice.
133 */}}
134 {{- define "dcaegen2-services-common._externalVolumeMounts" -}}
135   {{- $global := . -}}
136   {{- if .Values.externalVolumes }}
137     {{- range $vol := .Values.externalVolumes }}
138       {{- if eq (lower $vol.type) "configmap" }}
139         {{- $vname := (tpl $vol.name $global) -}}
140         {{- $readOnly := $vol.readOnly | default false }}
141 - mountPath: {{ $vol.mountPath }}
142   name: {{ $vname }}
143   readOnly: {{ $readOnly }}
144       {{- end }}
145     {{- end }}
146   {{- end }}
147 {{- end }}
148 {{/*
149 dcaegen2-services-common.microserviceDeployment:
150 This template produces a Kubernetes Deployment for a DCAE microservice.
151
152 All DCAE microservices currently use very similar Deployments.  Having a
153 common template eliminates a lot of repetition in the individual charts
154 for each microservice.
155
156 The template expects the full chart context as input.  A chart for a
157 DCAE microservice references this template using:
158 {{ include "dcaegen2-services-common.microserviceDeployment" . }}
159 The template directly references data in .Values, and indirectly (through its
160 use of templates from the ONAP "common" collection) references data in
161 .Release.
162
163 The exact content of the Deployment generated from this template
164 depends on the content of .Values.
165
166 The Deployment always includes a single Pod, with a container that uses
167 the DCAE microservice image.
168
169 The Deployment Pod may also include a logging sidecar container.
170 The sidecar is included if .Values.logDirectory is set.  The
171 logging sidecar and the DCAE microservice container share a
172 volume where the microservice logs are written.
173
174 The Deployment includes an initContainer that pushes the
175 microservice's initial configuration (from .Values.applicationConfig)
176 into Consul.  All DCAE microservices retrieve their initial
177 configurations by making an API call to a DCAE platform component called
178 the  config-binding-service.  The config-binding-service currently
179 retrieves configuration information from Consul.
180
181 The Deployment also includes an initContainer that checks for the
182 readiness of other components that the microservice relies on.
183 This container is generated by the "common.readinessCheck.waitfor"
184 template.
185
186 If the microservice acts as a TLS client or server, the Deployment will
187 include an initContainer that retrieves certificate information from
188 the AAF certificate manager.  The information is mounted at the
189 mount point specified in .Values.certDirectory.  If the microservice is
190 a TLS server (indicated by setting .Values.tlsServer to true), the
191 certificate information will include a server cert and key, in various
192 formats.  It will also include the AAF CA cert.   If the microservice is
193 a TLS client only (indicated by setting .Values.tlsServer to false), the
194 certificate information includes only the AAF CA cert.
195
196 Deployed POD may also include a Policy-sync sidecar container.
197 The sidecar is included if .Values.policies is set.  The
198 Policy-sync sidecar polls PolicyEngine (PDP) periodically based
199 on .Values.policies.duration and configuration retrieved is shared with
200 DCAE Microservice container by common volume. Policy can be retrieved based on
201 list of policyID or filter
202 */}}
203
204 {{- define "dcaegen2-services-common.microserviceDeployment" -}}
205 {{- $logDir :=  default "" .Values.logDirectory -}}
206 {{- $certDir := default "" .Values.certDirectory . -}}
207 {{- $tlsServer := default "" .Values.tlsServer -}}
208 {{- $policy := default "" .Values.policies -}}
209
210 apiVersion: apps/v1
211 kind: Deployment
212 metadata: {{- include "common.resourceMetadata" . | nindent 2 }}
213 spec:
214   replicas: 1
215   selector: {{- include "common.selectors" . | nindent 4 }}
216   template:
217     metadata: {{- include "common.templateMetadata" . | nindent 6 }}
218     spec:
219       initContainers:
220       - command:
221         - sh
222         args:
223         - -c
224         - |
225         {{- range $var := .Values.customEnvVars }}
226           export {{ $var.name }}="{{ $var.value }}";
227         {{- end }}
228           cd /config-input && for PFILE in `ls -1`; do envsubst <${PFILE} >/config/${PFILE}; done
229         env:
230         {{- range $cred := .Values.credentials }}
231         - name: {{ $cred.name }}
232           {{- include "common.secret.envFromSecretFast" (dict "global" $ "uid" $cred.uid "key" $cred.key) | indent 10 }}
233         {{- end }}
234         volumeMounts:
235         - mountPath: /config-input
236           name: app-config-input
237         - mountPath: /config
238           name: app-config
239         image: {{ include "repositoryGenerator.image.envsubst" . }}
240         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
241         name: {{ include "common.name" . }}-update-config
242
243       {{ include "common.readinessCheck.waitFor" . | indent 6 | trim }}
244       - name: init-consul
245         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.consulLoaderImage }}
246         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
247         args:
248         - --key-yaml
249         - "{{ include "common.name" . }}|/app-config/application_config.yaml"
250         resources: {{ include "common.resources" . | nindent 2 }}
251         volumeMounts:
252           - mountPath: /app-config
253             name: app-config
254       {{- if $certDir }}
255       - name: init-tls
256         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.tlsImage }}
257         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
258         env:
259         - name: TLS_SERVER
260           value: {{ $tlsServer | quote }}
261         - name: POD_IP
262           valueFrom:
263             fieldRef:
264               apiVersion: v1
265               fieldPath: status.podIP
266         resources: {{ include "common.resources" . | nindent 2 }}
267         volumeMounts:
268         - mountPath: /opt/app/osaaf
269           name: tls-info
270       {{- end }}
271       {{ include "dcaegen2-services-common._certPostProcessor" .  | nindent 4 }}
272       containers:
273       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.image }}
274         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
275         name: {{ include "common.name" . }}
276         env:
277         {{- if $certDir }}
278         - name: DCAE_CA_CERTPATH
279           value: {{ $certDir }}/cacert.pem
280         {{- end }}
281         - name: CONSUL_HOST
282           value: consul-server.onap
283         - name: CONFIG_BINDING_SERVICE
284           value: config-binding-service
285         - name: CBS_CONFIG_URL
286           value: https://config-binding-service:10443/service_component_all/{{ include "common.name" . }}
287         - name: POD_IP
288           valueFrom:
289             fieldRef:
290               apiVersion: v1
291               fieldPath: status.podIP
292         {{- include "dcaegen2-services-common._ms-specific-env-vars" . | nindent 8 }}
293         {{- if .Values.service }}
294         ports: {{ include "common.containerPorts" . | nindent 10 }}
295         {{- end }}
296         {{- if .Values.readiness }}
297         readinessProbe:
298           initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds | default 5 }}
299           periodSeconds: {{ .Values.readiness.periodSeconds | default 15 }}
300           timeoutSeconds: {{ .Values.readiness.timeoutSeconds | default 1 }}
301           {{- $probeType := .Values.readiness.type | default "httpGet" -}}
302           {{- if eq $probeType "httpGet" }}
303           httpGet:
304             scheme: {{ .Values.readiness.scheme }}
305             path: {{ .Values.readiness.path }}
306             port: {{ .Values.readiness.port }}
307           {{- end }}
308           {{- if eq $probeType "exec" }}
309           exec:
310             command:
311             {{- range $cmd := .Values.readiness.command }}
312             - {{ $cmd }}
313             {{- end }}
314           {{- end }}
315         {{- end }}
316         resources: {{ include "common.resources" . | nindent 2 }}
317         volumeMounts:
318         - mountPath: /app-config
319           name: app-config
320         {{- if $logDir }}
321         - mountPath: {{ $logDir}}
322           name: component-log
323         {{- end }}
324         {{- if $certDir }}
325         - mountPath: {{ $certDir }}
326           name: tls-info
327           {{- if and .Values.certificates .Values.global.cmpv2Enabled .Values.global.CMPv2CertManagerIntegration -}}
328           {{- include "common.certManager.volumeMountsReadOnly" . | nindent 8 -}}
329           {{- end -}}
330         {{- end }}
331         {{- if $policy }}
332         - name: policy-shared
333           mountPath: /etc/policies
334         {{- end }}
335         {{- include "dcaegen2-services-common._externalVolumeMounts" . | nindent 8 }}
336       {{- if $logDir }}
337       - image: {{ include "repositoryGenerator.image.logging" . }}
338         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
339         name: filebeat
340         env:
341         - name: POD_IP
342           valueFrom:
343             fieldRef:
344               apiVersion: v1
345               fieldPath: status.podIP
346         resources: {{ include "common.resources" . | nindent 2 }}
347         volumeMounts:
348         - mountPath: /var/log/onap/{{ include "common.name" . }}
349           name: component-log
350         - mountPath: /usr/share/filebeat/data
351           name: filebeat-data
352         - mountPath: /usr/share/filebeat/filebeat.yml
353           name: filebeat-conf
354           subPath: filebeat.yml
355       {{- end }}
356       {{- if $policy }}
357       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.dcaePolicySyncImage }}
358         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
359         name: policy-sync
360         env:
361         - name: POD_IP
362           valueFrom:
363             fieldRef:
364               apiVersion: v1
365               fieldPath: status.podIP
366         - name: POLICY_SYNC_PDP_USER
367           valueFrom:
368             secretKeyRef:
369               name: onap-policy-xacml-pdp-api-creds
370               key: login
371         - name: POLICY_SYNC_PDP_PASS
372           valueFrom:
373             secretKeyRef:
374               name: onap-policy-xacml-pdp-api-creds
375               key: password
376         - name: POLICY_SYNC_PDP_URL
377           value : http{{ if (include "common.needTLS" .) }}s{{ end }}://policy-xacml-pdp:6969
378         - name: POLICY_SYNC_OUTFILE
379           value : "/etc/policies/policies.json"
380         - name: POLICY_SYNC_V1_DECISION_ENDPOINT
381           value : "policy/pdpx/v1/decision"
382         {{- if $policy.filter }}
383         - name: POLICY_SYNC_FILTER
384           value: {{ $policy.filter }}
385         {{- end -}}
386         {{- if $policy.policyID }}
387         - name: POLICY_SYNC_ID
388           value: {{ $policy.policyID }}
389         {{- end -}}
390         {{- if $policy.duration }}
391         - name: POLICY_SYNC_DURATION
392           value: {{ $policy.duration }}
393         {{- end }}
394         resources: {{ include "common.resources" . | nindent 2 }}
395         volumeMounts:
396         - mountPath: /etc/policies
397           name: policy-shared
398         {{- if $certDir }}
399         - mountPath: /opt/ca-certificates/
400           name: tls-info
401         {{- end }}
402       {{- end }}
403       hostname: {{ include "common.name" . }}
404       volumes:
405       - configMap:
406           defaultMode: 420
407           name: {{ include "common.fullname" . }}-application-config-configmap
408         name: app-config-input
409       - emptyDir:
410           medium: Memory
411         name: app-config
412       {{- if $logDir }}
413       - emptyDir: {}
414         name: component-log
415       - emptyDir: {}
416         name: filebeat-data
417       - configMap:
418           defaultMode: 420
419           name: {{ include "common.fullname" . }}-filebeat-configmap
420         name: filebeat-conf
421       {{- end }}
422       {{- if $certDir }}
423       - emptyDir: {}
424         name: tls-info
425         {{ if and .Values.certificates .Values.global.cmpv2Enabled .Values.global.CMPv2CertManagerIntegration -}}
426         {{ include "common.certManager.volumesReadOnly" . | nindent 6 }}
427         {{- end }}
428       {{- end }}
429       {{- if $policy }}
430       - name: policy-shared
431         emptyDir: {}
432       {{- end }}
433       {{- include "dcaegen2-services-common._externalVolumes" . | nindent 6 }}
434       imagePullSecrets:
435       - name: "{{ include "common.namespace" . }}-docker-registry-key"
436 {{ end -}}
437
438 {{/*
439   For internal use
440
441   Template to attach CertPostProcessor which merges CMPv2 truststore with AAF truststore
442   and swaps keystore files.
443 */}}
444 {{- define "dcaegen2-services-common._certPostProcessor" -}}
445   {{- $certDir := default "" .Values.certDirectory . -}}
446   {{- if and $certDir .Values.certificates .Values.global.cmpv2Enabled .Values.global.CMPv2CertManagerIntegration -}}
447     {{- $cmpv2Certificate := (index .Values.certificates 0) -}}
448     {{- $cmpv2CertificateDir := $cmpv2Certificate.mountPath -}}
449     {{- $certType := "pem" -}}
450     {{- if $cmpv2Certificate.keystore -}}
451       {{- $certType = (index $cmpv2Certificate.keystore.outputType 0) -}}
452     {{- end -}}
453     {{- $truststoresPaths := printf "%s/%s:%s/%s" $certDir "cacert.pem" $cmpv2CertificateDir "cacert.pem" -}}
454     {{- $truststoresPasswordPaths := ":" -}}
455     {{- $keystoreSourcePaths := printf "%s/%s:%s/%s" $cmpv2CertificateDir "cert.pem" $cmpv2CertificateDir "key.pem" -}}
456     {{- $keystoreDestinationPaths := printf "%s/%s:%s/%s" $certDir "cert.pem" $certDir "key.pem" -}}
457     {{- if not (eq $certType "pem") -}}
458       {{- $truststoresPaths = printf "%s/%s:%s/%s.%s" $certDir "trust.jks" $cmpv2CertificateDir "truststore" $certType -}}
459       {{- $truststoresPasswordPaths = printf "%s/%s:%s/%s" $certDir "trust.pass" $cmpv2CertificateDir "truststore.pass" -}}
460       {{- $keystoreSourcePaths = printf "%s/%s.%s:%s/%s" $cmpv2CertificateDir "keystore" $certType $cmpv2CertificateDir "keystore.pass" -}}
461       {{- $keystoreDestinationPaths = printf "%s/%s.%s:%s/%s.pass" $certDir "cert" $certType $certDir $certType -}}
462     {{- end }}
463   - name: cert-post-processor
464     image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.certPostProcessorImage }}
465     imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
466     resources:
467       {{- include "common.resources" . | nindent 4 }}
468     volumeMounts:
469     - mountPath: {{ $certDir }}
470       name: tls-info
471       {{- include "common.certManager.volumeMountsReadOnly" . | nindent 4 }}
472     env:
473     - name: TRUSTSTORES_PATHS
474       value: {{ $truststoresPaths | quote}}
475     - name: TRUSTSTORES_PASSWORDS_PATHS
476       value: {{ $truststoresPasswordPaths | quote }}
477     - name: KEYSTORE_SOURCE_PATHS
478       value: {{ $keystoreSourcePaths | quote }}
479     - name: KEYSTORE_DESTINATION_PATHS
480       value: {{ $keystoreDestinationPaths | quote }}
481   {{- end }}
482 {{- end -}}