a50d96f635eb005b7e4420a128907d84b519a1eb
[osm/devops.git] / installers / charm / 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 [{"name": "kafka-exporter", "containerPort": port, "protocol": "TCP"}]
106
107
108 def _make_pod_envconfig(
109 config: Dict[str, Any], relation_state: Dict[str, Any]
110 ) -> Dict[str, Any]:
111 """Generate pod environment configuration.
112
113 Args:
114 config (Dict[str, Any]): configuration information.
115 relation_state (Dict[str, Any]): relation state information.
116
117 Returns:
118 Dict[str, Any]: pod environment configuration.
119 """
120 envconfig = {}
121
122 return envconfig
123
124
125 def _make_pod_ingress_resources(
126 config: Dict[str, Any], app_name: str, port: int
127 ) -> List[Dict[str, Any]]:
128 """Generate pod ingress resources.
129
130 Args:
131 config (Dict[str, Any]): configuration information.
132 app_name (str): application name.
133 port (int): port to expose.
134
135 Returns:
136 List[Dict[str, Any]]: pod ingress resources.
137 """
138 site_url = config.get("site_url")
139
140 if not site_url:
141 return
142
143 parsed = urlparse(site_url)
144
145 if not parsed.scheme.startswith("http"):
146 return
147
148 ingress_whitelist_source_range = config["ingress_whitelist_source_range"]
149
150 annotations = {}
151
152 if ingress_whitelist_source_range:
153 annotations[
154 "nginx.ingress.kubernetes.io/whitelist-source-range"
155 ] = ingress_whitelist_source_range
156
157 ingress_spec_tls = None
158
159 if parsed.scheme == "https":
160 ingress_spec_tls = [{"hosts": [parsed.hostname]}]
161 tls_secret_name = config["tls_secret_name"]
162 if tls_secret_name:
163 ingress_spec_tls[0]["secretName"] = tls_secret_name
164 else:
165 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
166
167 ingress = {
168 "name": "{}-ingress".format(app_name),
169 "annotations": annotations,
170 "spec": {
171 "rules": [
172 {
173 "host": parsed.hostname,
174 "http": {
175 "paths": [
176 {
177 "path": "/",
178 "backend": {
179 "serviceName": app_name,
180 "servicePort": port,
181 },
182 }
183 ]
184 },
185 }
186 ]
187 },
188 }
189 if ingress_spec_tls:
190 ingress["spec"]["tls"] = ingress_spec_tls
191
192 return [ingress]
193
194
195 def _make_readiness_probe(port: int) -> Dict[str, Any]:
196 """Generate readiness probe.
197
198 Args:
199 port (int): service port.
200
201 Returns:
202 Dict[str, Any]: readiness probe.
203 """
204 return {
205 "httpGet": {
206 "path": "/api/health",
207 "port": port,
208 },
209 "initialDelaySeconds": 10,
210 "periodSeconds": 10,
211 "timeoutSeconds": 5,
212 "successThreshold": 1,
213 "failureThreshold": 3,
214 }
215
216
217 def _make_liveness_probe(port: int) -> Dict[str, Any]:
218 """Generate liveness probe.
219
220 Args:
221 port (int): service port.
222
223 Returns:
224 Dict[str, Any]: liveness probe.
225 """
226 return {
227 "httpGet": {
228 "path": "/api/health",
229 "port": port,
230 },
231 "initialDelaySeconds": 60,
232 "timeoutSeconds": 30,
233 "failureThreshold": 10,
234 }
235
236
237 def _make_pod_command(relation: Dict[str, Any]) -> List[str]:
238 """Generate the startup command.
239
240 Args:
241 relation (Dict[str, Any]): Relation information.
242
243 Returns:
244 List[str]: command to startup the process.
245 """
246 command = [
247 "kafka_exporter",
248 "--kafka.server={}:{}".format(
249 relation.get("kafka_host"), relation.get("kafka_port")
250 ),
251 ]
252
253 return command
254
255
256 def make_pod_spec(
257 image_info: Dict[str, str],
258 config: Dict[str, Any],
259 relation_state: Dict[str, Any],
260 app_name: str = "kafka-exporter",
261 port: int = 9308,
262 ) -> Dict[str, Any]:
263 """Generate the pod spec information.
264
265 Args:
266 image_info (Dict[str, str]): Object provided by
267 OCIImageResource("image").fetch().
268 config (Dict[str, Any]): Configuration information.
269 relation_state (Dict[str, Any]): Relation state information.
270 app_name (str, optional): Application name. Defaults to "ro".
271 port (int, optional): Port for the container. Defaults to 9090.
272
273 Returns:
274 Dict[str, Any]: Pod spec dictionary for the charm.
275 """
276 if not image_info:
277 return None
278
279 _validate_data(config, relation_state)
280
281 ports = _make_pod_ports(port)
282 env_config = _make_pod_envconfig(config, relation_state)
283 readiness_probe = _make_readiness_probe(port)
284 liveness_probe = _make_liveness_probe(port)
285 ingress_resources = _make_pod_ingress_resources(config, app_name, port)
286 command = _make_pod_command(relation_state)
287
288 return {
289 "version": 3,
290 "containers": [
291 {
292 "name": app_name,
293 "imageDetails": image_info,
294 "imagePullPolicy": "Always",
295 "ports": ports,
296 "envConfig": env_config,
297 "command": command,
298 "kubernetes": {
299 "readinessProbe": readiness_probe,
300 "livenessProbe": liveness_probe,
301 },
302 }
303 ],
304 "kubernetesResources": {
305 "ingressResources": ingress_resources or [],
306 },
307 }