Merge "[DCEAGEN] Add app-config volume mount to common dceagen2 template"
[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 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 */}}
19 {{/*
20 For internal use only!
21
22 dcaegen2-services-common._ms-specific-env-vars:
23 This template generates a list of microservice-specific environment variables
24 as specified in .Values.applicationEnv.  The
25 dcaegen2-services-common.microServiceDeployment uses this template
26 to add the microservice-specific environment variables to the microservice's container.
27 These environment variables are in addition to a standard set of environment variables
28 provided to all microservices.
29
30 The template expects a single argument, pointing to the caller's global context.
31
32 Microservice-specific environment variables can be specified in two ways:
33   1. As literal string values.
34   2. As values that are sourced from a secret, identified by the secret's
35      uid and the key within the secret that provides the value.
36
37 The following example shows an example of each type.  The example assumes
38 that a secret has been created using the OOM common secret mechanism, with
39 a secret uid "example-secret" and a key called "password".
40
41 applicationEnv:
42   APPLICATION_PASSWORD:
43     secretUid: example-secret
44     key: password
45   APPLICATION_EXAMPLE: "An example value"
46
47 The example would set two environment variables on the microservice's container,
48 one called "APPLICATION_PASSWORD" with the value set from the "password" key in
49 the secret with uid "example-secret", and one called "APPLICATION_EXAMPLE" set to
50 the the literal string "An example value".
51 */}}
52 {{- define "dcaegen2-services-common._ms-specific-env-vars" -}}
53   {{- $global := . }}
54   {{- if .Values.applicationEnv }}
55     {{- range $envName, $envValue := .Values.applicationEnv }}
56       {{- if kindIs "string" $envValue }}
57 - name: {{ $envName }}
58   value: {{ $envValue | quote }}
59       {{- else }}
60         {{ if or (not $envValue.secretUid) (not $envValue.key) }}
61           {{ fail (printf "Env %s definition is not a string and does not contain secretUid or key fields" $envName) }}
62         {{- end }}
63 - name: {{ $envName }}
64   {{- include "common.secret.envFromSecretFast" (dict "global" $global "uid" $envValue.secretUid "key" $envValue.key) | indent 2 }}
65       {{- end -}}
66     {{- end }}
67   {{- end }}
68 {{- end -}}
69 {{/*
70 dcaegen2-services-common.microserviceDeployment:
71 This template produces a Kubernetes Deployment for a DCAE microservice.
72
73 All DCAE microservices currently use very similar Deployments.  Having a
74 common template eliminates a lot of repetition in the individual charts
75 for each microservice.
76
77 The template expects the full chart context as input.  A chart for a
78 DCAE microservice references this template using:
79 {{ include "dcaegen2-services-common.microserviceDeployment" . }}
80 The template directly references data in .Values, and indirectly (through its
81 use of templates from the ONAP "common" collection) references data in
82 .Release.
83
84 The exact content of the Deployment generated from this template
85 depends on the content of .Values.
86
87 The Deployment always includes a single Pod, with a container that uses
88 the DCAE microservice image.
89
90 The Deployment Pod may also include a logging sidecar container.
91 The sidecar is included if .Values.logDirectory is set.  The
92 logging sidecar and the DCAE microservice container share a
93 volume where the microservice logs are written.
94
95 The Deployment includes an initContainer that pushes the
96 microservice's initial configuration (from .Values.applicationConfig)
97 into Consul.  All DCAE microservices retrieve their initial
98 configurations by making an API call to a DCAE platform component called
99 the  config-binding-service.  The config-binding-service currently
100 retrieves configuration information from Consul.
101
102 The Deployment also includes an initContainer that checks for the
103 readiness of other components that the microservice relies on.
104 This container is generated by the "common.readinessCheck.waitfor"
105 template.
106
107 If the microservice acts as a TLS client or server, the Deployment will
108 include an initContainer that retrieves certificate information from
109 the AAF certificate manager.  The information is mounted at the
110 mount point specified in .Values.certDirectory.  If the microservice is
111 a TLS server (indicated by setting .Values.tlsServer to true), the
112 certificate information will include a server cert and key, in various
113 formats.  It will also include the AAF CA cert.   If the microservice is
114 a TLS client only (indicated by setting .Values.tlsServer to false), the
115 certificate information includes only the AAF CA cert.
116 */}}
117
118 {{- define "dcaegen2-services-common.microserviceDeployment" -}}
119 {{- $logDir :=  default "" .Values.logDirectory -}}
120 {{- $certDir := default "" .Values.certDirectory . -}}
121 {{- $tlsServer := default "" .Values.tlsServer -}}
122 apiVersion: apps/v1
123 kind: Deployment
124 metadata: {{- include "common.resourceMetadata" . | nindent 2 }}
125 spec:
126   replicas: 1
127   selector: {{- include "common.selectors" . | nindent 4 }}
128   template:
129     metadata: {{- include "common.templateMetadata" . | nindent 6 }}
130     spec:
131       initContainers:
132       - command:
133         - sh
134         args:
135         - -c
136         - |
137         {{- range $var := .Values.customEnvVars }}
138           export {{ $var.name }}="{{ $var.value }}";
139         {{- end }}
140           cd /config-input && for PFILE in `ls -1`; do envsubst <${PFILE} >/config/${PFILE}; done
141         env:
142         {{- range $cred := .Values.credentials }}
143         - name: {{ $cred.name }}
144           {{- include "common.secret.envFromSecretFast" (dict "global" $ "uid" $cred.uid "key" $cred.key) | indent 10 }}
145         {{- end }}
146         volumeMounts:
147         - mountPath: /config-input
148           name: app-config-input
149         - mountPath: /config
150           name: app-config
151         image: {{ include "repositoryGenerator.image.envsubst" . }}
152         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
153         name: {{ include "common.name" . }}-update-config
154
155       {{ include "common.readinessCheck.waitFor" . | indent 6 | trim }}
156       - name: init-consul
157         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.consulLoaderImage }}
158         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
159         args:
160         - --key-yaml
161         - "{{ include "common.name" . }}|/app-config/application_config.yaml"
162         resources: {{ include "common.resources" . | nindent 2 }}
163         volumeMounts:
164           - mountPath: /app-config
165             name: app-config
166       {{- if $certDir }}
167       - name: init-tls
168         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.tlsImage }}
169         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
170         env:
171         - name: TLS_SERVER
172           value: {{ $tlsServer | quote }}
173         - name: POD_IP
174           valueFrom:
175             fieldRef:
176               apiVersion: v1
177               fieldPath: status.podIP
178         resources: {{ include "common.resources" . | nindent 2 }}
179         volumeMounts:
180         - mountPath: /opt/app/osaaf
181           name: tls-info
182       {{- end }}
183       containers:
184       - image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.image }}
185         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
186         name: {{ include "common.name" . }}
187         env:
188         {{- if $certDir }}
189         - name: DCAE_CA_CERTPATH
190           value: {{ $certDir}}/cacert.pem
191         {{- end }}
192         - name: CONSUL_HOST
193           value: consul-server.onap
194         - name: CONFIG_BINDING_SERVICE
195           value: config-binding-service
196         - name: CBS_CONFIG_URL
197           value: https://config-binding-service:10443/service_component_all/{{ include "common.name" . }}
198         - name: POD_IP
199           valueFrom:
200             fieldRef:
201               apiVersion: v1
202               fieldPath: status.podIP
203         {{- include "dcaegen2-services-common._ms-specific-env-vars" . | nindent 8 }}
204         {{- if .Values.service }}
205         ports: {{ include "common.containerPorts" . | nindent 10 }}
206         {{- end }}
207         {{- if .Values.readiness }}
208         readinessProbe:
209           initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds | default 5 }}
210           periodSeconds: {{ .Values.readiness.periodSeconds | default 15 }}
211           timeoutSeconds: {{ .Values.readiness.timeoutSeconds | default 1 }}
212           {{- $probeType := .Values.readiness.type | default "httpGet" -}}
213           {{- if eq $probeType "httpGet" }}
214           httpGet:
215             scheme: {{ .Values.readiness.scheme }}
216             path: {{ .Values.readiness.path }}
217             port: {{ .Values.readiness.port }}
218           {{- end }}
219           {{- if eq $probeType "exec" }}
220           exec:
221             command:
222             {{- range $cmd := .Values.readiness.command }}
223             - {{ $cmd }}
224             {{- end }}
225           {{- end }}
226         {{- end }}
227         resources: {{ include "common.resources" . | nindent 2 }}
228         volumeMounts:
229         - mountPath: /app-config
230           name: app-config
231         {{- if $logDir }}
232         - mountPath: {{ $logDir}}
233           name: component-log
234         {{- end }}
235         {{- if $certDir }}
236         - mountPath: {{ $certDir }}
237           name: tls-info
238         {{- end }}
239       {{- if $logDir }}
240       - image: {{ include "repositoryGenerator.image.logging" . }}
241         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
242         name: filebeat
243         env:
244         - name: POD_IP
245           valueFrom:
246             fieldRef:
247               apiVersion: v1
248               fieldPath: status.podIP
249         resources: {{ include "common.resources" . | nindent 2 }}
250         volumeMounts:
251         - mountPath: /var/log/onap/{{ include "common.name" . }}
252           name: component-log
253         - mountPath: /usr/share/filebeat/data
254           name: filebeat-data
255         - mountPath: /usr/share/filebeat/filebeat.yml
256           name: filebeat-conf
257           subPath: filebeat.yml
258       {{- end }}
259       hostname: {{ include "common.name" . }}
260       volumes:
261       - configMap:
262           defaultMode: 420
263           name: {{ include "common.fullname" . }}-application-config-configmap
264         name: app-config-input
265       - emptyDir:
266           medium: Memory
267         name: app-config
268       {{- if $logDir }}
269       - emptyDir: {}
270         name: component-log
271       - emptyDir: {}
272         name: filebeat-data
273       - configMap:
274           defaultMode: 420
275           name: {{ include "common.fullname" . }}-filebeat-configmap
276         name: filebeat-conf
277       {{- end }}
278       {{- if $certDir }}
279       - emptyDir: {}
280         name: tls-info
281       {{- end }}
282       imagePullSecrets:
283       - name: "{{ include "common.namespace" . }}-docker-registry-key"
284 {{ end -}}