[SO] update cnf-adapter
[oom.git] / kubernetes / common / readinessCheck / templates / _readinessCheck.tpl
1 {{/*
2 # Copyright © 2020 Orange
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 */}}
16
17 {{/*
18   Generate readiness part for a pod
19   Will look by default to .Values.wait_for
20   There are two formats available.
21
22   The simple one (where wait_for is just list of containers):
23
24   wait_for:
25     - aaf-locate
26     - aaf-cm
27     - aaf-service
28
29   The powerful one (where wait_for is a map):
30
31   wait_for:
32     name: myname
33     containers:
34       - aaf-locate
35       - aaf-cm
36       - aaf-service
37
38   the powerful one allows also to wait for pod names with this
39   (has to start with the given pod name):
40   wait_for:
41     name: myname
42     pods:
43       - test-pod
44
45   the powerful one allows also to wait for a service to be
46   available, which means all pods are deployed, which are
47   selected by the service definition:
48   wait_for:
49     name: myservice
50     services:
51       - mariadb-galera-service
52
53   the powerful one allows also to wait for pods with the
54   given "app" label:
55   wait_for:
56     name: myname
57     apps:
58       - mariadb-galera
59
60   the powerful one allows also to wait for jobs with this:
61   wait_for:
62     name: myname
63     jobs:
64       - '{{ include "common.release" . }}-the-job'
65
66   Be careful, as on the example above, the job name may have a "non fixed" name
67   and thus don't forget to use templates if needed
68
69   The function can takes below arguments (inside a dictionary):
70      - .dot : environment (.)
71      - .initRoot : the root dictionary of readinessCheck submodule
72                    (default to .Values.readinessCheck)
73      - .wait_for : list of service / containers / pods /apps / jobs to wait for
74                    (default to .Values.wait_for)
75
76   Example calls:
77     {{ include "common.readinessCheck.waitFor" . }}
78     {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.where.my.wait_for.is ) }}
79 */}}
80 {{- define "common.readinessCheck.waitFor" -}}
81 {{-   $dot := default . .dot -}}
82 {{-   $initRoot := default $dot.Values.readinessCheck .initRoot -}}
83 {{/*  Our version of helm doesn't support deepCopy so we need this nasty trick */}}
84 {{-   $subchartDot := fromJson (include "common.subChartDot" (dict "dot" $dot "initRoot" $initRoot)) }}
85 {{-   $wait_for := default $initRoot.wait_for .wait_for -}}
86 {{-   $containers := index (ternary (dict "containers" $wait_for) $wait_for (kindIs "slice" $wait_for)) "containers" -}}
87 {{-   $services := index (ternary (dict) $wait_for (kindIs "slice" $wait_for)) "services" -}}
88 {{-   $serviceMeshes := index (ternary (dict) $wait_for (kindIs "slice" $wait_for)) "serviceMeshes" -}}
89 {{-   $pods := index (ternary (dict) $wait_for (kindIs "slice" $wait_for)) "pods" -}}
90 {{-   $apps := index (ternary (dict) $wait_for (kindIs "slice" $wait_for)) "apps" -}}
91 {{-   $namePart := index (ternary (dict) $wait_for (kindIs "slice" $wait_for)) "name" -}}
92 {{-   $jobs := index (ternary (dict) $wait_for (kindIs "slice" $wait_for)) "jobs" -}}
93 {{-   $timeout := index (ternary (dict) $wait_for (kindIs "slice" $wait_for)) "timeout" -}}
94 - name: {{ include "common.name" $dot }}{{ ternary "" (printf "-%s" $namePart) (empty $namePart) }}-readiness
95   image: {{ include "repositoryGenerator.image.readiness" $subchartDot }}
96   imagePullPolicy: {{ $subchartDot.Values.global.pullPolicy | default $subchartDot.Values.pullPolicy }}
97   securityContext:
98     runAsUser: {{ $subchartDot.Values.user }}
99     runAsGroup: {{ $subchartDot.Values.group }}
100     readOnlyRootFilesystem: true
101     privileged: false
102     allowPrivilegeEscalation: false
103     capabilities:
104       drop:
105         - ALL
106         - CAP_NET_RAW
107   command:
108   - /app/ready.py
109   args:
110   {{- range $container := default (list) $containers }}
111   - --container-name
112   - {{ tpl $container $dot }}
113   {{- end }}
114   {{- range $pod := default (list) $pods }}
115   - --pod-name
116   - {{ tpl $pod $dot }}
117   {{- end }}
118   {{- range $service := default (list) $services }}
119   - --service-name
120   - {{ tpl $service $dot }}
121   {{- end }}
122   {{- range $serviceMesh := default (list) $serviceMeshes }}
123   - --service-mesh-check
124   - {{ tpl $serviceMesh $dot }}
125   {{- end }}
126   {{- range $app := default (list) $apps }}
127   - --app-name
128   - {{ tpl $app $dot }}
129   {{- end }}
130   {{- range $job := $jobs }}
131   - --job-name
132   - {{ tpl $job $dot }}
133   {{- end }}
134   {{- if hasKey $wait_for "timeout" }}
135   - -t
136   - {{ $timeout | quote }}
137   {{- end }}
138   env:
139   - name: NAMESPACE
140   {{- if $subchartDot.Values.namespace }}
141     value: {{ $subchartDot.Values.namespace }}
142   {{- else }}
143     valueFrom:
144       fieldRef:
145         apiVersion: v1
146         fieldPath: metadata.namespace
147   {{- end }}
148   resources:
149     limits:
150       cpu: {{ $subchartDot.Values.limits.cpu }}
151       memory: {{ $subchartDot.Values.limits.memory }}
152     requests:
153       cpu: {{ $subchartDot.Values.requests.cpu }}
154       memory: {{ $subchartDot.Values.requests.memory }}
155 {{- end -}}