[DCAEGEN2] Add support for configMaps
[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 For internal use only!
72
73 dcaegen2-services-common._externalVolumes:
74 This template generates a list of volumes associated with the pod,
75 based on information provided in .Values.externalVolumes.  This
76 template works in conjunction with dcaegen2-services-common._externalVolumeMounts
77 to give the microservice access to data in volumes created else.
78 This initial implementation supports ConfigMaps only, as this is the only
79 external volume mounting required by current microservices.
80
81 .Values.externalValues is a list of objects.  Each object has 3 required fields and 1 optional field:
82    - name: the name of the resource (in the current implementation, it must be a ConfigMap)
83      that is to be set up as a volume.  The value is a case sensitive string.  Because the
84      names of resources are sometimes set at deployment time (for instance, to prefix the Helm
85      release to the name), the string can be a Helm template fragment that will be expanded at
86      deployment time.
87    - type: the type of the resource (in the current implementation, only "ConfigMap" is supported).
88      The value is a case-INsensitive string.
89    - mountPoint: the path to the mount point for the volume in the container file system.  The
90      value is a case-sensitive string.
91    - readOnly: (Optional) Boolean flag.  Set to true to mount the volume as read-only.
92      Defaults to false.
93
94 Here is an example fragment from a values.yaml file for a microservice:
95
96 externalVolumes:
97   - name: my-example-configmap
98     type: configmap
99     mountPath: /opt/app/config
100   - name: '{{ include "common.release" . }}-another-example'
101     type: configmap
102     mountPath: /opt/app/otherconfig
103 */}}
104 {{- define "dcaegen2-services-common._externalVolumes" -}}
105   {{- $global := . -}}
106   {{- if .Values.externalVolumes }}
107     {{- range $vol := .Values.externalVolumes }}
108       {{- if eq (lower $vol.type) "configmap" }}
109         {{- $vname := (tpl $vol.name $global) }}
110 - configMap:
111     defaultMode: 420
112     name: {{ $vname }}
113   name: {{ $vname }}
114       {{- end }}
115     {{- end }}
116   {{- end }}
117 {{- end }}
118 {{/*
119 For internal use only!
120
121 dcaegen2-services-common._externalVolumeMounts:
122 This template generates a list of volume mounts for the microservice container,
123 based on information provided in .Values.externalVolumes.  This
124 template works in conjunction with dcaegen2-services-common._externalVolumes
125 to give the microservice access to data in volumes created else.
126 This initial implementation supports ConfigMaps only, as this is the only
127 external volume mounting required by current microservices.
128
129 See the documentation for dcaegen2-services-common._externalVolumes for
130 details on how external volumes are specified in the values.yaml file for
131 the microservice.
132 */}}
133 {{- define "dcaegen2-services-common._externalVolumeMounts" -}}
134   {{- $global := . -}}
135   {{- if .Values.externalVolumes }}
136     {{- range $vol := .Values.externalVolumes }}
137       {{- if eq (lower $vol.type) "configmap" }}
138         {{- $vname := (tpl $vol.name $global) -}}
139         {{- $readOnly := $vol.readOnly | default false }}
140 - mountPath: {{ $vol.mountPath }}
141   name: {{ $vname }}
142   readOnly: {{ $readOnly }}
143       {{- end }}
144     {{- end }}
145   {{- end }}
146 {{- end }}
147 {{/*
148 dcaegen2-services-common.microserviceDeployment:
149 This template produces a Kubernetes Deployment for a DCAE microservice.
150
151 All DCAE microservices currently use very similar Deployments.  Having a
152 common template eliminates a lot of repetition in the individual charts
153 for each microservice.
154
155 The template expects the full chart context as input.  A chart for a
156 DCAE microservice references this template using:
157 {{ include "dcaegen2-services-common.microserviceDeployment" . }}
158 The template directly references data in .Values, and indirectly (through its
159 use of templates from the ONAP "common" collection) references data in
160 .Release.
161
162 The exact content of the Deployment generated from this template
163 depends on the content of .Values.
164
165 The Deployment always includes a single Pod, with a container that uses
166 the DCAE microservice image.
167
168 The Deployment Pod may also include a logging sidecar container.
169 The sidecar is included if .Values.logDirectory is set.  The
170 logging sidecar and the DCAE microservice container share a
171 volume where the microservice logs are written.
172
173 The Deployment includes an initContainer that pushes the
174 microservice's initial configuration (from .Values.applicationConfig)
175 into Consul.  All DCAE microservices retrieve their initial
176 configurations by making an API call to a DCAE platform component called
177 the  config-binding-service.  The config-binding-service currently
178 retrieves configuration information from Consul.
179
180 The Deployment also includes an initContainer that checks for the
181 readiness of other components that the microservice relies on.
182 This container is generated by the "common.readinessCheck.waitfor"
183 template.
184
185 If the microservice acts as a TLS client or server, the Deployment will
186 include an initContainer that retrieves certificate information from
187 the AAF certificate manager.  The information is mounted at the
188 mount point specified in .Values.certDirectory.  If the microservice is
189 a TLS server (indicated by setting .Values.tlsServer to true), the
190 certificate information will include a server cert and key, in various
191 formats.  It will also include the AAF CA cert.   If the microservice is
192 a TLS client only (indicated by setting .Values.tlsServer to false), the
193 certificate information includes only the AAF CA cert.
194
195 Deployed POD may also include a Policy-sync sidecar container.
196 The sidecar is included if .Values.policies is set.  The
197 Policy-sync sidecar polls PolicyEngine (PDP) periodically based
198 on .Values.policies.duration and configuration retrieved is shared with
199 DCAE Microservice container by common volume. Policy can be retrieved based on
200 list of policyID or filter
201 */}}
202
203 {{- define "dcaegen2-services-common.microserviceDeployment" -}}
204 {{- $logDir :=  default "" .Values.logDirectory -}}
205 {{- $certDir := default "" .Values.certDirectory . -}}
206 {{- $tlsServer := default "" .Values.tlsServer -}}
207 {{- $policy := default "" .Values.policies -}}
208
209 apiVersion: apps/v1
210 kind: Deployment
211 metadata: {{- include "common.resourceMetadata" . | nindent 2 }}
212 spec:
213   replicas: 1
214   selector: {{- include "common.selectors" . | nindent 4 }}
215   template:
216     metadata: {{- include "common.templateMetadata" . | nindent 6 }}
217     spec:
218       initContainers:
219       - command:
220         - sh
221         args:
222         - -c
223         - |
224         {{- range $var := .Values.customEnvVars }}
225           export {{ $var.name }}="{{ $var.value }}";
226         {{- end }}
227           cd /config-input && for PFILE in `ls -1`; do envsubst <${PFILE} >/config/${PFILE}; done
228         env:
229         {{- range $cred := .Values.credentials }}
230         - name: {{ $cred.name }}
231           {{- include "common.secret.envFromSecretFast" (dict "global" $ "uid" $cred.uid "key" $cred.key) | indent 10 }}
232         {{- end }}
233         volumeMounts:
234         - mountPath: /config-input
235           name: app-config-input
236         - mountPath: /config
237           name: app-config
238         image: {{ include "repositoryGenerator.image.envsubst" . }}
239         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
240         name: {{ include "common.name" . }}-update-config
241
242       {{ include "common.readinessCheck.waitFor" . | indent 6 | trim }}
243       - name: init-consul
244         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.consulLoaderImage }}
245         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
246         args:
247         - --key-yaml
248         - "{{ include "common.name" . }}|/app-config/application_config.yaml"
249         resources: {{ include "common.resources" . | nindent 2 }}
250         volumeMounts:
251           - mountPath: /app-config
252             name: app-config
253       {{- if $certDir }}
254       - name: init-tls
255         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.tlsImage }}
256         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
257         env:
258         - name: TLS_SERVER
259           value: {{ $tlsServer | quote }}
260         - name: POD_IP
261           valueFrom:
262             fieldRef:
263               apiVersion: v1
264               fieldPath: status.podIP
265         resources: {{ include "common.resources" . | nindent 2 }}
266         volumeMounts:
267         - mountPath: /opt/app/osaaf
268           name: tls-info
269       {{- end }}
270       containers:
271       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.image }}
272         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
273         name: {{ include "common.name" . }}
274         env:
275         {{- if $certDir }}
276         - name: DCAE_CA_CERTPATH
277           value: {{ $certDir}}/cacert.pem
278         {{- end }}
279         - name: CONSUL_HOST
280           value: consul-server.onap
281         - name: CONFIG_BINDING_SERVICE
282           value: config-binding-service
283         - name: CBS_CONFIG_URL
284           value: https://config-binding-service:10443/service_component_all/{{ include "common.name" . }}
285         - name: POD_IP
286           valueFrom:
287             fieldRef:
288               apiVersion: v1
289               fieldPath: status.podIP
290         {{- include "dcaegen2-services-common._ms-specific-env-vars" . | nindent 8 }}
291         {{- if .Values.service }}
292         ports: {{ include "common.containerPorts" . | nindent 10 }}
293         {{- end }}
294         {{- if .Values.readiness }}
295         readinessProbe:
296           initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds | default 5 }}
297           periodSeconds: {{ .Values.readiness.periodSeconds | default 15 }}
298           timeoutSeconds: {{ .Values.readiness.timeoutSeconds | default 1 }}
299           {{- $probeType := .Values.readiness.type | default "httpGet" -}}
300           {{- if eq $probeType "httpGet" }}
301           httpGet:
302             scheme: {{ .Values.readiness.scheme }}
303             path: {{ .Values.readiness.path }}
304             port: {{ .Values.readiness.port }}
305           {{- end }}
306           {{- if eq $probeType "exec" }}
307           exec:
308             command:
309             {{- range $cmd := .Values.readiness.command }}
310             - {{ $cmd }}
311             {{- end }}
312           {{- end }}
313         {{- end }}
314         resources: {{ include "common.resources" . | nindent 2 }}
315         volumeMounts:
316         - mountPath: /app-config
317           name: app-config
318         {{- if $logDir }}
319         - mountPath: {{ $logDir}}
320           name: component-log
321         {{- end }}
322         {{- if $certDir }}
323         - mountPath: {{ $certDir }}
324           name: tls-info
325         {{- end }}
326         {{- if $policy }}
327         - name: policy-shared
328           mountPath: /etc/policies
329         {{- end }}
330         {{- include "dcaegen2-services-common._externalVolumeMounts" . | nindent 8 }}
331       {{- if $logDir }}
332       - image: {{ include "repositoryGenerator.image.logging" . }}
333         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
334         name: filebeat
335         env:
336         - name: POD_IP
337           valueFrom:
338             fieldRef:
339               apiVersion: v1
340               fieldPath: status.podIP
341         resources: {{ include "common.resources" . | nindent 2 }}
342         volumeMounts:
343         - mountPath: /var/log/onap/{{ include "common.name" . }}
344           name: component-log
345         - mountPath: /usr/share/filebeat/data
346           name: filebeat-data
347         - mountPath: /usr/share/filebeat/filebeat.yml
348           name: filebeat-conf
349           subPath: filebeat.yml
350       {{- end }}
351       {{- if $policy }}
352       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.dcaePolicySyncImage }}
353         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
354         name: policy-sync
355         env:
356         - name: POD_IP
357           valueFrom:
358             fieldRef:
359               apiVersion: v1
360               fieldPath: status.podIP
361         - name: POLICY_SYNC_PDP_USER
362           valueFrom:
363             secretKeyRef:
364               name: onap-policy-xacml-pdp-api-creds
365               key: login
366         - name: POLICY_SYNC_PDP_PASS
367           valueFrom:
368             secretKeyRef:
369               name: onap-policy-xacml-pdp-api-creds
370               key: password
371         - name: POLICY_SYNC_PDP_URL
372           value : http{{ if (include "common.needTLS" .) }}s{{ end }}://policy-xacml-pdp:6969
373         - name: POLICY_SYNC_OUTFILE
374           value : "/etc/policies/policies.json"
375         - name: POLICY_SYNC_V1_DECISION_ENDPOINT
376           value : "policy/pdpx/v1/decision"
377         {{- if $policy.filter }}
378         - name: POLICY_SYNC_FILTER
379           value: {{ $policy.filter }}
380         {{- end -}}
381         {{- if $policy.policyID }}
382         - name: POLICY_SYNC_ID
383           value: {{ $policy.policyID }}
384         {{- end -}}
385         {{- if $policy.duration }}
386         - name: POLICY_SYNC_DURATION
387           value: {{ $policy.duration }}
388         {{- end }}
389         resources: {{ include "common.resources" . | nindent 2 }}
390         volumeMounts:
391         - mountPath: /etc/policies
392           name: policy-shared
393         {{- if $certDir }}
394         - mountPath: /opt/ca-certificates/
395           name: tls-info
396         {{- end }}
397       {{- end }}
398       hostname: {{ include "common.name" . }}
399       volumes:
400       - configMap:
401           defaultMode: 420
402           name: {{ include "common.fullname" . }}-application-config-configmap
403         name: app-config-input
404       - emptyDir:
405           medium: Memory
406         name: app-config
407       {{- if $logDir }}
408       - emptyDir: {}
409         name: component-log
410       - emptyDir: {}
411         name: filebeat-data
412       - configMap:
413           defaultMode: 420
414           name: {{ include "common.fullname" . }}-filebeat-configmap
415         name: filebeat-conf
416       {{- end }}
417       {{- if $certDir }}
418       - emptyDir: {}
419         name: tls-info
420       {{- end }}
421       {{- if $policy }}
422       - name: policy-shared
423         emptyDir: {}
424       {{- end }}
425       {{- include "dcaegen2-services-common._externalVolumes" . | nindent 6 }}
426       imagePullSecrets:
427       - name: "{{ include "common.namespace" . }}-docker-registry-key"
428 {{ end -}}