Changes to mariadb charm
[osm/devops.git] / installers / charm / prometheus-kafka-exporter / 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_ip_network(network: str) -> bool:
32 """Validate IP network.
33
34 Args:
35 network (str): IP network range.
36
37 Returns:
38 bool: True if valid, false otherwise.
39 """
40 if not network:
41 return True
42
43 try:
44 ip_network(network)
45 except ValueError:
46 return False
47
48 return True
49
50
51 def _validate_data(config_data: Dict[str, Any], relation_data: Dict[str, Any]) -> bool:
52 """Validates passed information.
53
54 Args:
55 config_data (Dict[str, Any]): configuration information.
56 relation_data (Dict[str, Any]): relation information
57
58 Raises:
59 ValueError: when config and/or relation data is not valid.
60 """
61 config_validators = {
62 "site_url": lambda value, _: isinstance(value, str)
63 if value is not None
64 else True,
65 "ingress_whitelist_source_range": lambda value, _: _validate_ip_network(value),
66 "tls_secret_name": lambda value, _: isinstance(value, str)
67 if value is not None
68 else True,
69 }
70 relation_validators = {
71 "kafka_host": lambda value, _: isinstance(value, str) and len(value) > 0,
72 "kafka_port": lambda value, _: isinstance(value, str)
73 and len(value) > 0
74 and int(value) > 0,
75 }
76 problems = []
77
78 for key, validator in config_validators.items():
79 valid = validator(config_data.get(key), config_data)
80
81 if not valid:
82 problems.append(key)
83
84 for key, validator in relation_validators.items():
85 valid = validator(relation_data.get(key), relation_data)
86
87 if not valid:
88 problems.append(key)
89
90 if len(problems) > 0:
91 raise ValueError("Errors found in: {}".format(", ".join(problems)))
92
93 return True
94
95
96 def _make_pod_ports(port: int) -> List[Dict[str, Any]]:
97 """Generate pod ports details.
98
99 Args:
100 port (int): port to expose.
101
102 Returns:
103 List[Dict[str, Any]]: pod port details.
104 """
105 return [
106 {"name": "prometheus-kafka-exporter", "containerPort": port, "protocol": "TCP"}
107 ]
108
109
110 def _make_pod_envconfig(
111 config: Dict[str, Any], relation_state: Dict[str, Any]
112 ) -> Dict[str, Any]:
113 """Generate pod environment configuration.
114
115 Args:
116 config (Dict[str, Any]): configuration information.
117 relation_state (Dict[str, Any]): relation state information.
118
119 Returns:
120 Dict[str, Any]: pod environment configuration.
121 """
122 envconfig = {}
123
124 return envconfig
125
126
127 def _make_pod_ingress_resources(
128 config: Dict[str, Any], app_name: str, port: int
129 ) -> List[Dict[str, Any]]:
130 """Generate pod ingress resources.
131
132 Args:
133 config (Dict[str, Any]): configuration information.
134 app_name (str): application name.
135 port (int): port to expose.
136
137 Returns:
138 List[Dict[str, Any]]: pod ingress resources.
139 """
140 site_url = config.get("site_url")
141
142 if not site_url:
143 return
144
145 parsed = urlparse(site_url)
146
147 if not parsed.scheme.startswith("http"):
148 return
149
150 ingress_whitelist_source_range = config["ingress_whitelist_source_range"]
151
152 annotations = {}
153
154 if ingress_whitelist_source_range:
155 annotations[
156 "nginx.ingress.kubernetes.io/whitelist-source-range"
157 ] = ingress_whitelist_source_range
158
159 ingress_spec_tls = None
160
161 if parsed.scheme == "https":
162 ingress_spec_tls = [{"hosts": [parsed.hostname]}]
163 tls_secret_name = config["tls_secret_name"]
164 if tls_secret_name:
165 ingress_spec_tls[0]["secretName"] = tls_secret_name
166 else:
167 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
168
169 ingress = {
170 "name": "{}-ingress".format(app_name),
171 "annotations": annotations,
172 "spec": {
173 "rules": [
174 {
175 "host": parsed.hostname,
176 "http": {
177 "paths": [
178 {
179 "path": "/",
180 "backend": {
181 "serviceName": app_name,
182 "servicePort": port,
183 },
184 }
185 ]
186 },
187 }
188 ]
189 },
190 }
191 if ingress_spec_tls:
192 ingress["spec"]["tls"] = ingress_spec_tls
193
194 return [ingress]
195
196
197 def _make_readiness_probe(port: int) -> Dict[str, Any]:
198 """Generate readiness probe.
199
200 Args:
201 port (int): service port.
202
203 Returns:
204 Dict[str, Any]: readiness probe.
205 """
206 return {
207 "httpGet": {
208 "path": "/api/health",
209 "port": port,
210 },
211 "initialDelaySeconds": 10,
212 "periodSeconds": 10,
213 "timeoutSeconds": 5,
214 "successThreshold": 1,
215 "failureThreshold": 3,
216 }
217
218
219 def _make_liveness_probe(port: int) -> Dict[str, Any]:
220 """Generate liveness probe.
221
222 Args:
223 port (int): service port.
224
225 Returns:
226 Dict[str, Any]: liveness probe.
227 """
228 return {
229 "httpGet": {
230 "path": "/api/health",
231 "port": port,
232 },
233 "initialDelaySeconds": 60,
234 "timeoutSeconds": 30,
235 "failureThreshold": 10,
236 }
237
238
239 def _make_pod_command(relation: Dict[str, Any]) -> List[str]:
240 """Generate the startup command.
241
242 Args:
243 relation (Dict[str, Any]): Relation information.
244
245 Returns:
246 List[str]: command to startup the process.
247 """
248 command = [
249 "kafka-exporter",
250 "--kafka.server={}:{}".format(
251 relation.get("kafka_host"), relation.get("kafka_port")
252 ),
253 ]
254
255 return command
256
257
258 def make_pod_spec(
259 image_info: Dict[str, str],
260 config: Dict[str, Any],
261 relation_state: Dict[str, Any],
262 app_name: str = "prometheus-kafka-exporter",
263 port: int = 9308,
264 ) -> Dict[str, Any]:
265 """Generate the pod spec information.
266
267 Args:
268 image_info (Dict[str, str]): Object provided by
269 OCIImageResource("image").fetch().
270 config (Dict[str, Any]): Configuration information.
271 relation_state (Dict[str, Any]): Relation state information.
272 app_name (str, optional): Application name. Defaults to "ro".
273 port (int, optional): Port for the container. Defaults to 9090.
274
275 Returns:
276 Dict[str, Any]: Pod spec dictionary for the charm.
277 """
278 if not image_info:
279 return None
280
281 _validate_data(config, relation_state)
282
283 ports = _make_pod_ports(port)
284 env_config = _make_pod_envconfig(config, relation_state)
285 readiness_probe = _make_readiness_probe(port)
286 liveness_probe = _make_liveness_probe(port)
287 ingress_resources = _make_pod_ingress_resources(config, app_name, port)
288 command = _make_pod_command(relation_state)
289
290 return {
291 "version": 3,
292 "containers": [
293 {
294 "name": app_name,
295 "imageDetails": image_info,
296 "imagePullPolicy": "Always",
297 "ports": ports,
298 "envConfig": env_config,
299 "command": command,
300 "kubernetes": {
301 "readinessProbe": readiness_probe,
302 "livenessProbe": liveness_probe,
303 },
304 }
305 ],
306 "kubernetesResources": {
307 "ingressResources": ingress_resources or [],
308 },
309 }