[DCAEGEN2-SVCS] Support config update via configMap
[oom.git] / kubernetes / dcaegen2-services / common / dcaegen2-services-common / templates / _deployment.tpl
1 {{/*
2 #============LICENSE_START========================================================
3 # ================================================================================
4 # Copyright (c) 2021-2022 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 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.log.path is set.  The
180 logging sidecar and the DCAE microservice container share a
181 volume where the microservice logs are written.
182
183 Deployed POD may also include a Policy-sync sidecar container.
184 The sidecar is included if .Values.policies is set.  The
185 Policy-sync sidecar polls PolicyEngine (PDP) periodically based
186 on .Values.policies.duration and configuration retrieved is shared with
187 DCAE Microservice container by common volume. Policy can be retrieved based on
188 list of policyID or filter. An optional policyRelease parameter can be specified
189 to override the default policy helm release (used for retreiving the secret containing
190 pdp username and password)
191
192 Following is example policy config override
193
194 dcaePolicySyncImage: onap/org.onap.dcaegen2.deployments.dcae-services-policy-sync:1.0.1
195 policies:
196   duration: 300
197   policyRelease: "onap"
198   policyID: |
199     '["onap.vfirewall.tca","onap.vdns.tca"]'
200
201 The Deployment includes an initContainer that checks for the
202 readiness of other components that the microservice relies on.
203 This container is generated by the "common.readinessCheck.waitfor"
204 template. See the documentation for this template
205 (oom/kubernetes/common/readinessCheck/templates/_readinessCheck.tpl).
206
207 If the microservice uses a DMaaP Data Router (DR) feed, the Deployment
208 includes an initContainer that makes provisioning requests to the DMaaP
209 bus controller (dmaap-bc) to create the feed and to set up a publisher
210 and/or subscriber to the feed.  The Deployment also includes a second
211 initContainer that merges the information returned by the provisioning
212 process into the microservice's configuration.  See the documentation for
213 the common DMaaP provisioning template
214 (oom/kubernetes/common/common/templates/_dmaapProvisioning.tpl).
215
216 If the microservice acts as a TLS client or server, the Deployment will
217 include an initContainer that retrieves certificate information from
218 the AAF certificate manager.  The information is mounted at the
219 mount point specified in .Values.certDirectory.  If the microservice is
220 a TLS server (indicated by setting .Values.tlsServer to true), the
221 certificate information will include a server cert and key, in various
222 formats.  It will also include the AAF CA cert.   If the microservice is
223 a TLS client only (indicated by setting .Values.tlsServer to false), the
224 certificate information includes only the AAF CA cert.
225
226 If the microservice uses certificates from an external CMPv2 provider,
227 the Deployment will include an initContainer that performs certificate
228 post-processing.
229 */}}
230
231 {{- define "dcaegen2-services-common.microserviceDeployment" -}}
232 {{- $log := default dict .Values.log -}}
233 {{- $logDir :=  default "" $log.path -}}
234 {{- $certDir := default "" .Values.certDirectory . -}}
235 {{- $tlsServer := default "" .Values.tlsServer -}}
236 {{- $commonRelease :=  print (include "common.release" .) -}}
237 {{- $policy := default dict .Values.policies -}}
238 {{- $policyRls := default $commonRelease $policy.policyRelease -}}
239 {{- $drFeedConfig := default "" .Values.drFeedConfig -}}
240 {{- $dcaeName := print (include "common.fullname" .) }}
241 {{- $dcaeLabel := (dict "dcaeMicroserviceName" $dcaeName) -}}
242 {{- $dot := . -}}
243 apiVersion: apps/v1
244 kind: Deployment
245 metadata: {{- include "common.resourceMetadata" (dict "dot" $dot "labels" $dcaeLabel) | nindent 2 }}
246 spec:
247   replicas: 1
248   selector: {{- include "common.selectors" . | nindent 4 }}
249   template:
250     metadata: {{- include "common.templateMetadata" . | nindent 6 }}
251     spec:
252       initContainers:
253       {{ include "common.readinessCheck.waitFor" . | indent 6 | trim }}
254       {{- include "common.dmaap.provisioning.initContainer" . | nindent 6 }}
255       {{- if $certDir }}
256       - name: init-tls
257         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.tlsImage }}
258         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
259         env:
260         - name: TLS_SERVER
261           value: {{ $tlsServer | quote }}
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: /opt/app/osaaf
270           name: tls-info
271       {{- end }}
272       {{ include "dcaegen2-services-common._certPostProcessor" .  | nindent 4 }}
273       containers:
274       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.image }}
275         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
276         name: {{ include "common.name" . }}
277         env:
278         {{- range $cred := .Values.credentials }}
279         - name: {{ $cred.name }}
280           {{- include "common.secret.envFromSecretFast" (dict "global" $ "uid" $cred.uid "key" $cred.key) | indent 10 }}
281         {{- end }}
282         {{- if $certDir }}
283         - name: DCAE_CA_CERTPATH
284           value: {{ $certDir }}/cacert.pem
285         {{- end }}
286         - name: CONSUL_HOST
287           value: consul-server.onap
288         - name: CONFIG_BINDING_SERVICE
289           value: config-binding-service
290         - name: CBS_CONFIG_URL
291           value: https://config-binding-service:10443/service_component_all/{{ include "common.name" . }}
292         - name: POD_IP
293           valueFrom:
294             fieldRef:
295               apiVersion: v1
296               fieldPath: status.podIP
297         {{- include "dcaegen2-services-common._ms-specific-env-vars" . | nindent 8 }}
298         {{- if .Values.service }}
299         ports: {{ include "common.containerPorts" . | nindent 10 }}
300         {{- end }}
301         {{- if .Values.readiness }}
302         readinessProbe:
303           initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds | default 5 }}
304           periodSeconds: {{ .Values.readiness.periodSeconds | default 15 }}
305           timeoutSeconds: {{ .Values.readiness.timeoutSeconds | default 1 }}
306           {{- $probeType := .Values.readiness.type | default "httpGet" -}}
307           {{- if eq $probeType "httpGet" }}
308           httpGet:
309             scheme: {{ .Values.readiness.scheme }}
310             path: {{ .Values.readiness.path }}
311             port: {{ .Values.readiness.port }}
312           {{- end }}
313           {{- if eq $probeType "exec" }}
314           exec:
315             command:
316             {{- range $cmd := .Values.readiness.command }}
317             - {{ $cmd }}
318             {{- end }}
319           {{- end }}
320         {{- end }}
321         resources: {{ include "common.resources" . | nindent 2 }}
322         volumeMounts:
323         - mountPath: /app-config
324           name: {{ ternary "app-config-input" "app-config" (not $drFeedConfig) }}
325         - mountPath: /app-config-input
326           name: app-config-input
327         {{- if $logDir }}
328         - mountPath: {{ $logDir}}
329           name: logs
330         {{- end }}
331         {{- if $certDir }}
332         - mountPath: {{ $certDir }}
333           name: tls-info
334           {{- if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
335           {{- include "common.certManager.volumeMountsReadOnly" . | nindent 8 -}}
336           {{- end -}}
337         {{- end }}
338         {{- if $policy }}
339         - name: policy-shared
340           mountPath: /etc/policies
341         {{- end }}
342         {{- include "dcaegen2-services-common._externalVolumeMounts" . | nindent 8 }}
343       {{- if $logDir }}
344       {{ include "common.log.sidecar" . | nindent 6 }}
345       {{- end }}
346       {{- if $policy }}
347       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.dcaePolicySyncImage }}
348         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
349         name: policy-sync
350         env:
351         - name: POD_IP
352           valueFrom:
353             fieldRef:
354               apiVersion: v1
355               fieldPath: status.podIP
356         - name: POLICY_SYNC_PDP_USER
357           valueFrom:
358             secretKeyRef:
359               name: {{ $policyRls }}-policy-xacml-pdp-restserver-creds
360               key: login
361         - name: POLICY_SYNC_PDP_PASS
362           valueFrom:
363             secretKeyRef:
364               name: {{ $policyRls }}-policy-xacml-pdp-restserver-creds
365               key: password
366         - name: POLICY_SYNC_PDP_URL
367           value : http{{ if (include "common.needTLS" .) }}s{{ end }}://policy-xacml-pdp:6969
368         - name: POLICY_SYNC_OUTFILE
369           value : "/etc/policies/policies.json"
370         - name: POLICY_SYNC_V1_DECISION_ENDPOINT
371           value : "policy/pdpx/v1/decision"
372         {{- if $policy.filter }}
373         - name: POLICY_SYNC_FILTER
374           value: {{ $policy.filter }}
375         {{- end -}}
376         {{- if $policy.policyID }}
377         - name: POLICY_SYNC_ID
378           value: {{ $policy.policyID }}
379         {{- end -}}
380         {{- if $policy.duration }}
381         - name: POLICY_SYNC_DURATION
382           value: "{{ $policy.duration }}"
383         {{- end }}
384         resources: {{ include "common.resources" . | nindent 2 }}
385         volumeMounts:
386         - mountPath: /etc/policies
387           name: policy-shared
388         {{- if $certDir }}
389         - mountPath: /opt/ca-certificates/
390           name: tls-info
391         {{- end }}
392       {{- end }}
393       hostname: {{ include "common.name" . }}
394       serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}}
395       volumes:
396       - configMap:
397           defaultMode: 420
398           name: {{ include "common.fullname" . }}-application-config-configmap
399         name: app-config-input
400       - emptyDir:
401           medium: Memory
402         name: app-config
403       {{- if $logDir }}
404       - emptyDir: {}
405         name: logs
406       {{ include "common.log.volumes" (dict "dot" . "configMapNamePrefix" (tpl .Values.logConfigMapNamePrefix . )) | nindent 6 }}
407       {{- end }}
408       {{- if $certDir }}
409       - emptyDir: {}
410         name: tls-info
411         {{ if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
412         {{ include "common.certManager.volumesReadOnly" . | nindent 6 }}
413         {{- end }}
414       {{- end }}
415       {{- if $policy }}
416       - name: policy-shared
417         emptyDir: {}
418       {{- end }}
419       {{- include "common.dmaap.provisioning._volumes" . | nindent 6 -}}
420       {{- include "dcaegen2-services-common._externalVolumes" . | nindent 6 }}
421       imagePullSecrets:
422       - name: "{{ include "common.namespace" . }}-docker-registry-key"
423 {{ end -}}
424
425 {{/*
426   For internal use
427
428   Template to attach CertPostProcessor which merges CMPv2 truststore with AAF truststore
429   and swaps keystore files.
430 */}}
431 {{- define "dcaegen2-services-common._certPostProcessor" -}}
432   {{- $certDir := default "" .Values.certDirectory . -}}
433   {{- if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
434     {{- $cmpv2Certificate := (index .Values.certificates 0) -}}
435     {{- $cmpv2CertificateDir := $cmpv2Certificate.mountPath -}}
436     {{- $certType := "pem" -}}
437     {{- if $cmpv2Certificate.keystore -}}
438       {{- $certType = (index $cmpv2Certificate.keystore.outputType 0) -}}
439     {{- end -}}
440     {{- $truststoresPaths := printf "%s/%s:%s/%s" $certDir "cacert.pem" $cmpv2CertificateDir "cacert.pem" -}}
441     {{- $truststoresPasswordPaths := ":" -}}
442     {{- $keystoreSourcePaths := printf "%s/%s:%s/%s" $cmpv2CertificateDir "cert.pem" $cmpv2CertificateDir "key.pem" -}}
443     {{- $keystoreDestinationPaths := printf "%s/%s:%s/%s" $certDir "cert.pem" $certDir "key.pem" -}}
444     {{- if not (eq $certType "pem") -}}
445       {{- $truststoresPaths = printf "%s/%s:%s/%s.%s" $certDir "trust.jks" $cmpv2CertificateDir "truststore" $certType -}}
446       {{- $truststoresPasswordPaths = printf "%s/%s:%s/%s" $certDir "trust.pass" $cmpv2CertificateDir "truststore.pass" -}}
447       {{- $keystoreSourcePaths = printf "%s/%s.%s:%s/%s" $cmpv2CertificateDir "keystore" $certType $cmpv2CertificateDir "keystore.pass" -}}
448       {{- $keystoreDestinationPaths = printf "%s/%s.%s:%s/%s.pass" $certDir "cert" $certType $certDir $certType -}}
449     {{- end }}
450   - name: cert-post-processor
451     image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.certPostProcessorImage }}
452     imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
453     resources:
454       {{- include "common.resources" . | nindent 4 }}
455     volumeMounts:
456     - mountPath: {{ $certDir }}
457       name: tls-info
458       {{- include "common.certManager.volumeMountsReadOnly" . | nindent 4 }}
459     env:
460     - name: TRUSTSTORES_PATHS
461       value: {{ $truststoresPaths | quote}}
462     - name: TRUSTSTORES_PASSWORDS_PATHS
463       value: {{ $truststoresPasswordPaths | quote }}
464     - name: KEYSTORE_SOURCE_PATHS
465       value: {{ $keystoreSourcePaths | quote }}
466     - name: KEYSTORE_DESTINATION_PATHS
467       value: {{ $keystoreDestinationPaths | quote }}
468   {{- end }}
469 {{- end -}}
470
471 {{/*
472   Template returns string "true" if CMPv2 certificates should be used and nothing (so it can be used in with statements)
473   when they shouldn't. Example use:
474     {{- if (include "dcaegen2-services-common.shouldUseCmpv2Certificates" .) -}}
475
476 */}}
477 {{- define "dcaegen2-services-common.shouldUseCmpv2Certificates" -}}
478   {{- $certDir := default "" .Values.certDirectory . -}}
479   {{- if (and $certDir .Values.certificates .Values.global.cmpv2Enabled .Values.useCmpv2Certificates) -}}
480   true
481   {{- end -}}
482 {{- end -}}