9915a9e4469d782cdc7178c061b3c987bab3ac50
[osm/devops.git] / installers / charm / grafana / src / pod_spec.py
1 #!/usr/bin/env python3
2 # Copyright 2021 Canonical Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # 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, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
18 #
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
21 ##
22
23 import logging
24 from ipaddress import ip_network
25 from typing import Any, Dict, List
26 from urllib.parse import urlparse
27
28 logger = logging.getLogger(__name__)
29
30
31 def _validate_max_file_size(max_file_size: int, site_url: str) -> bool:
32 """Validate max_file_size.
33
34 Args:
35 max_file_size (int): maximum file size allowed.
36 site_url (str): endpoint url.
37
38 Returns:
39 bool: True if valid, false otherwise.
40 """
41 if not site_url:
42 return True
43
44 parsed = urlparse(site_url)
45
46 if not parsed.scheme.startswith("http"):
47 return True
48
49 if max_file_size is None:
50 return False
51
52 return max_file_size >= 0
53
54
55 def _validate_ip_network(network: str) -> bool:
56 """Validate IP network.
57
58 Args:
59 network (str): IP network range.
60
61 Returns:
62 bool: True if valid, false otherwise.
63 """
64 if not network:
65 return True
66
67 try:
68 ip_network(network)
69 except ValueError:
70 return False
71
72 return True
73
74
75 def _validate_data(config_data: Dict[str, Any], relation_data: Dict[str, Any]) -> bool:
76 """Validates passed information.
77
78 Args:
79 config_data (Dict[str, Any]): configuration information.
80 relation_data (Dict[str, Any]): relation information
81
82 Raises:
83 ValueError: when config and/or relation data is not valid.
84 """
85 config_validators = {
86 "site_url": lambda value, _: isinstance(value, str)
87 if value is not None
88 else True,
89 "max_file_size": lambda value, values: _validate_max_file_size(
90 value, values.get("site_url")
91 ),
92 "ingress_whitelist_source_range": lambda value, _: _validate_ip_network(value),
93 "tls_secret_name": lambda value, _: isinstance(value, str)
94 if value is not None
95 else True,
96 }
97 relation_validators = {
98 "prometheus_host": lambda value, _: isinstance(value, str) and len(value) > 0,
99 "prometheus_port": lambda value, _: isinstance(value, str)
100 and len(value) > 0
101 and int(value) > 0,
102 }
103 problems = []
104
105 for key, validator in config_validators.items():
106 valid = validator(config_data.get(key), config_data)
107
108 if not valid:
109 problems.append(key)
110
111 for key, validator in relation_validators.items():
112 valid = validator(relation_data.get(key), relation_data)
113
114 if not valid:
115 problems.append(key)
116
117 if len(problems) > 0:
118 raise ValueError("Errors found in: {}".format(", ".join(problems)))
119
120 return True
121
122
123 def _make_pod_ports(port: int) -> List[Dict[str, Any]]:
124 """Generate pod ports details.
125
126 Args:
127 port (int): port to expose.
128
129 Returns:
130 List[Dict[str, Any]]: pod port details.
131 """
132 return [{"name": "grafana", "containerPort": port, "protocol": "TCP"}]
133
134
135 def _make_pod_envconfig(
136 config: Dict[str, Any], relation_state: Dict[str, Any]
137 ) -> Dict[str, Any]:
138 """Generate pod environment configuration.
139
140 Args:
141 config (Dict[str, Any]): configuration information.
142 relation_state (Dict[str, Any]): relation state information.
143
144 Returns:
145 Dict[str, Any]: pod environment configuration.
146 """
147 envconfig = {}
148
149 return envconfig
150
151
152 def _make_pod_ingress_resources(
153 config: Dict[str, Any], app_name: str, port: int
154 ) -> List[Dict[str, Any]]:
155 """Generate pod ingress resources.
156
157 Args:
158 config (Dict[str, Any]): configuration information.
159 app_name (str): application name.
160 port (int): port to expose.
161
162 Returns:
163 List[Dict[str, Any]]: pod ingress resources.
164 """
165 site_url = config.get("site_url")
166
167 if not site_url:
168 return
169
170 parsed = urlparse(site_url)
171
172 if not parsed.scheme.startswith("http"):
173 return
174
175 max_file_size = config["max_file_size"]
176 ingress_whitelist_source_range = config["ingress_whitelist_source_range"]
177
178 annotations = {
179 "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format(
180 str(max_file_size) + "m" if max_file_size > 0 else max_file_size
181 ),
182 }
183
184 if ingress_whitelist_source_range:
185 annotations[
186 "nginx.ingress.kubernetes.io/whitelist-source-range"
187 ] = ingress_whitelist_source_range
188
189 ingress_spec_tls = None
190
191 if parsed.scheme == "https":
192 ingress_spec_tls = [{"hosts": [parsed.hostname]}]
193 tls_secret_name = config["tls_secret_name"]
194 if tls_secret_name:
195 ingress_spec_tls[0]["secretName"] = tls_secret_name
196 else:
197 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
198
199 ingress = {
200 "name": "{}-ingress".format(app_name),
201 "annotations": annotations,
202 "spec": {
203 "rules": [
204 {
205 "host": parsed.hostname,
206 "http": {
207 "paths": [
208 {
209 "path": "/",
210 "backend": {
211 "serviceName": app_name,
212 "servicePort": port,
213 },
214 }
215 ]
216 },
217 }
218 ]
219 },
220 }
221 if ingress_spec_tls:
222 ingress["spec"]["tls"] = ingress_spec_tls
223
224 return [ingress]
225
226
227 def _make_pod_files(relation: Dict[str, Any]) -> List[Dict[str, Any]]:
228 """Generating ConfigMap information
229
230 Args:
231 relation (Dict[str, Any]): relation information.
232
233 Returns:
234 List[Dict[str, Any]]: ConfigMap information.
235 """
236 files = [
237 {
238 "name": "dashboards",
239 "mountPath": "/etc/grafana/provisioning/dashboards/",
240 "files": [
241 {
242 "path": "dashboard-osm.yml",
243 "content": (
244 "apiVersion: 1\n"
245 "providers:\n"
246 " - name: 'osm'\n"
247 " orgId: 1\n"
248 " folder: ''\n"
249 " type: file\n"
250 " options:\n"
251 " path: /etc/grafana/provisioning/dashboards/\n"
252 ),
253 }
254 ],
255 },
256 {
257 "name": "datasources",
258 "mountPath": "/etc/grafana/provisioning/datasources/",
259 "files": [
260 {
261 "path": "datasource-prometheus.yml",
262 "content": (
263 "datasources:\n"
264 " - access: proxy\n"
265 " editable: true\n"
266 " is_default: true\n"
267 " name: osm_prometheus\n"
268 " orgId: 1\n"
269 " type: prometheus\n"
270 " version: 1\n"
271 " url: http://{}:{}\n".format(
272 relation.get("prometheus_host"),
273 relation.get("prometheus_port"),
274 )
275 ),
276 }
277 ],
278 },
279 ]
280
281 return files
282
283
284 def _make_readiness_probe(port: int) -> Dict[str, Any]:
285 """Generate readiness probe.
286
287 Args:
288 port (int): service port.
289
290 Returns:
291 Dict[str, Any]: readiness probe.
292 """
293 return {
294 "httpGet": {
295 "path": "/api/health",
296 "port": port,
297 },
298 "initialDelaySeconds": 10,
299 "periodSeconds": 10,
300 "timeoutSeconds": 5,
301 "successThreshold": 1,
302 "failureThreshold": 3,
303 }
304
305
306 def _make_liveness_probe(port: int) -> Dict[str, Any]:
307 """Generate liveness probe.
308
309 Args:
310 port (int): service port.
311
312 Returns:
313 Dict[str, Any]: liveness probe.
314 """
315 return {
316 "httpGet": {
317 "path": "/api/health",
318 "port": port,
319 },
320 "initialDelaySeconds": 60,
321 "timeoutSeconds": 30,
322 "failureThreshold": 10,
323 }
324
325
326 def make_pod_spec(
327 image_info: Dict[str, str],
328 config: Dict[str, Any],
329 relation_state: Dict[str, Any],
330 app_name: str = "grafana",
331 port: int = 3000,
332 ) -> Dict[str, Any]:
333 """Generate the pod spec information.
334
335 Args:
336 image_info (Dict[str, str]): Object provided by
337 OCIImageResource("image").fetch().
338 config (Dict[str, Any]): Configuration information.
339 relation_state (Dict[str, Any]): Relation state information.
340 app_name (str, optional): Application name. Defaults to "ro".
341 port (int, optional): Port for the container. Defaults to 9090.
342
343 Returns:
344 Dict[str, Any]: Pod spec dictionary for the charm.
345 """
346 if not image_info:
347 return None
348
349 _validate_data(config, relation_state)
350
351 ports = _make_pod_ports(port)
352 env_config = _make_pod_envconfig(config, relation_state)
353 files = _make_pod_files(relation_state)
354 readiness_probe = _make_readiness_probe(port)
355 liveness_probe = _make_liveness_probe(port)
356 ingress_resources = _make_pod_ingress_resources(config, app_name, port)
357
358 return {
359 "version": 3,
360 "containers": [
361 {
362 "name": app_name,
363 "imageDetails": image_info,
364 "imagePullPolicy": "Always",
365 "ports": ports,
366 "envConfig": env_config,
367 "volumeConfig": files,
368 "kubernetes": {
369 "readinessProbe": readiness_probe,
370 "livenessProbe": liveness_probe,
371 },
372 }
373 ],
374 "kubernetesResources": {
375 "ingressResources": ingress_resources or [],
376 },
377 }