9c9cc7074d6330a469281669ea259f541d15924e
[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       containers:
194       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.image }}
195         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
196         name: {{ include "common.name" . }}
197         env:
198         {{- if $certDir }}
199         - name: DCAE_CA_CERTPATH
200           value: {{ $certDir}}/cacert.pem
201         {{- end }}
202         - name: CONSUL_HOST
203           value: consul-server.onap
204         - name: CONFIG_BINDING_SERVICE
205           value: config-binding-service
206         - name: CBS_CONFIG_URL
207           value: https://config-binding-service:10443/service_component_all/{{ include "common.name" . }}
208         - name: POD_IP
209           valueFrom:
210             fieldRef:
211               apiVersion: v1
212               fieldPath: status.podIP
213         {{- include "dcaegen2-services-common._ms-specific-env-vars" . | nindent 8 }}
214         {{- if .Values.service }}
215         ports: {{ include "common.containerPorts" . | nindent 10 }}
216         {{- end }}
217         {{- if .Values.readiness }}
218         readinessProbe:
219           initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds | default 5 }}
220           periodSeconds: {{ .Values.readiness.periodSeconds | default 15 }}
221           timeoutSeconds: {{ .Values.readiness.timeoutSeconds | default 1 }}
222           {{- $probeType := .Values.readiness.type | default "httpGet" -}}
223           {{- if eq $probeType "httpGet" }}
224           httpGet:
225             scheme: {{ .Values.readiness.scheme }}
226             path: {{ .Values.readiness.path }}
227             port: {{ .Values.readiness.port }}
228           {{- end }}
229           {{- if eq $probeType "exec" }}
230           exec:
231             command:
232             {{- range $cmd := .Values.readiness.command }}
233             - {{ $cmd }}
234             {{- end }}
235           {{- end }}
236         {{- end }}
237         resources: {{ include "common.resources" . | nindent 2 }}
238         volumeMounts:
239         - mountPath: /app-config
240           name: app-config
241         {{- if $logDir }}
242         - mountPath: {{ $logDir}}
243           name: component-log
244         {{- end }}
245         {{- if $certDir }}
246         - mountPath: {{ $certDir }}
247           name: tls-info
248         {{- end }}
249         {{- if $policy }}
250         - name: policy-shared
251           mountPath: /etc/policies
252         {{- end }}
253       {{- if $logDir }}
254       - image: {{ include "repositoryGenerator.image.logging" . }}
255         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
256         name: filebeat
257         env:
258         - name: POD_IP
259           valueFrom:
260             fieldRef:
261               apiVersion: v1
262               fieldPath: status.podIP
263         resources: {{ include "common.resources" . | nindent 2 }}
264         volumeMounts:
265         - mountPath: /var/log/onap/{{ include "common.name" . }}
266           name: component-log
267         - mountPath: /usr/share/filebeat/data
268           name: filebeat-data
269         - mountPath: /usr/share/filebeat/filebeat.yml
270           name: filebeat-conf
271           subPath: filebeat.yml
272       {{- end }}
273       {{- if $policy }}
274       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.dcaePolicySyncImage }}
275         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
276         name: policy-sync
277         env:
278         - name: POD_IP
279           valueFrom:
280             fieldRef:
281               apiVersion: v1
282               fieldPath: status.podIP
283         - name: POLICY_SYNC_PDP_USER
284           valueFrom:
285             secretKeyRef:
286               name: onap-policy-xacml-pdp-api-creds
287               key: login
288         - name: POLICY_SYNC_PDP_PASS
289           valueFrom:
290             secretKeyRef:
291               name: onap-policy-xacml-pdp-api-creds
292               key: password
293         - name: POLICY_SYNC_PDP_URL
294           value : http{{ if (include "common.needTLS" .) }}s{{ end }}://policy-xacml-pdp:6969 
295         - name: POLICY_SYNC_OUTFILE
296           value : "/etc/policies/policies.json"
297         - name: POLICY_SYNC_V1_DECISION_ENDPOINT
298           value : "policy/pdpx/v1/decision"
299         {{- if $policy.filter }}
300         - name: POLICY_SYNC_FILTER
301           value: {{ $policy.filter }}
302         {{- end -}}
303         {{- if $policy.policyID }}
304         - name: POLICY_SYNC_ID
305           value: {{ $policy.policyID }}
306         {{- end -}}
307         {{- if $policy.duration }}
308         - name: POLICY_SYNC_DURATION
309           value: {{ $policy.duration }}
310         {{- end }}
311         resources: {{ include "common.resources" . | nindent 2 }}
312         volumeMounts:
313         - mountPath: /etc/policies
314           name: policy-shared
315         {{- if $certDir }}
316         - mountPath: /opt/ca-certificates/
317           name: tls-info
318         {{- end }}
319       {{- end }}
320       hostname: {{ include "common.name" . }}
321       volumes:
322       - configMap:
323           defaultMode: 420
324           name: {{ include "common.fullname" . }}-application-config-configmap
325         name: app-config-input
326       - emptyDir:
327           medium: Memory
328         name: app-config
329       {{- if $logDir }}
330       - emptyDir: {}
331         name: component-log
332       - emptyDir: {}
333         name: filebeat-data
334       - configMap:
335           defaultMode: 420
336           name: {{ include "common.fullname" . }}-filebeat-configmap
337         name: filebeat-conf
338       {{- end }}
339       {{- if $certDir }}
340       - emptyDir: {}
341         name: tls-info
342       {{- end }}
343       {{- if $policy }}
344       - name: policy-shared
345         emptyDir: {}
346       {{- end }}
347       imagePullSecrets:
348       - name: "{{ include "common.namespace" . }}-docker-registry-key"
349 {{ end -}}