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