1c50565b429f0024ec9e2fc5a0d7566574d43b23
[osm/devops.git] / installers / charm / mon / src / pod_spec.py
1 #!/usr/bin/env python3
2 # Copyright 2020 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 typing import Any, Dict, List, NoReturn
25
26 logger = logging.getLogger(__name__)
27
28
29 def _validate_data(
30 config_data: Dict[str, Any], relation_data: Dict[str, Any]
31 ) -> NoReturn:
32 """Validate input data.
33
34 Args:
35 config_data (Dict[str, Any]): configuration data.
36 relation_data (Dict[str, Any]): relation data.
37 """
38 config_validators = {
39 "openstack_default_granularity": lambda value, _: isinstance(value, int)
40 and value > 0,
41 "global_request_timeout": lambda value, _: isinstance(value, int) and value > 0,
42 "log_level": lambda value, _: isinstance(value, str)
43 and value in ("INFO", "DEBUG"),
44 "collector_interval": lambda value, _: isinstance(value, int) and value > 0,
45 "evaluator_interval": lambda value, _: isinstance(value, int) and value > 0,
46 "database_commonkey": lambda value, _: isinstance(value, str)
47 and len(value) > 0,
48 "vca_host": lambda value, _: isinstance(value, str) and len(value) > 0,
49 "vca_user": lambda value, _: isinstance(value, str) and len(value) > 0,
50 "vca_password": lambda value, _: isinstance(value, str) and len(value) > 0,
51 "vca_cacert": lambda value, _: isinstance(value, str),
52 }
53 relation_validators = {
54 "message_host": lambda value, _: isinstance(value, str) and len(value) > 0,
55 "message_port": lambda value, _: isinstance(value, int) and value > 0,
56 "database_uri": lambda value, _: isinstance(value, str)
57 and value.startswith("mongodb://"),
58 "prometheus_host": lambda value, _: isinstance(value, str) and len(value) > 0,
59 "prometheus_port": lambda value, _: isinstance(value, int) and value > 0,
60 }
61 problems = []
62
63 for key, validator in config_validators.items():
64 valid = validator(config_data.get(key), config_data)
65
66 if not valid:
67 problems.append(key)
68
69 for key, validator in relation_validators.items():
70 valid = validator(relation_data.get(key), relation_data)
71
72 if not valid:
73 problems.append(key)
74
75 if len(problems) > 0:
76 raise ValueError("Errors found in: {}".format(", ".join(problems)))
77
78
79 def _make_pod_ports(port: int) -> List[Dict[str, Any]]:
80 """Generate pod ports details.
81
82 Args:
83 port (int): port to expose.
84
85 Returns:
86 List[Dict[str, Any]]: pod port details.
87 """
88 return [{"name": "mon", "containerPort": port, "protocol": "TCP"}]
89
90
91 def _make_pod_envconfig(
92 config: Dict[str, Any], relation_state: Dict[str, Any]
93 ) -> Dict[str, Any]:
94 """Generate pod environment configuration.
95
96 Args:
97 config (Dict[str, Any]): configuration information.
98 relation_state (Dict[str, Any]): relation state information.
99
100 Returns:
101 Dict[str, Any]: pod environment configuration.
102 """
103 envconfig = {
104 # General configuration
105 "ALLOW_ANONYMOUS_LOGIN": "yes",
106 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config["openstack_default_granularity"],
107 "OSMMON_GLOBAL_REQUEST_TIMEOUT": config["global_request_timeout"],
108 "OSMMON_GLOBAL_LOGLEVEL": config["log_level"],
109 "OSMMON_COLLECTOR_INTERVAL": config["collector_interval"],
110 "OSMMON_EVALUATOR_INTERVAL": config["evaluator_interval"],
111 # Kafka configuration
112 "OSMMON_MESSAGE_DRIVER": "kafka",
113 "OSMMON_MESSAGE_HOST": relation_state["message_host"],
114 "OSMMON_MESSAGE_PORT": relation_state["message_port"],
115 # Database configuration
116 "OSMMON_DATABASE_DRIVER": "mongo",
117 "OSMMON_DATABASE_URI": relation_state["database_uri"],
118 "OSMMON_DATABASE_COMMONKEY": config["database_commonkey"],
119 # Prometheus configuration
120 "OSMMON_PROMETHEUS_URL": f"http://{relation_state['prometheus_host']}:{relation_state['prometheus_port']}",
121 # VCA configuration
122 "OSMMON_VCA_HOST": config["vca_host"],
123 "OSMMON_VCA_USER": config["vca_user"],
124 "OSMMON_VCA_SECRET": config["vca_password"],
125 "OSMMON_VCA_CACERT": config["vca_cacert"],
126 }
127
128 return envconfig
129
130
131 def _make_startup_probe() -> Dict[str, Any]:
132 """Generate startup probe.
133
134 Returns:
135 Dict[str, Any]: startup probe.
136 """
137 return {
138 "exec": {"command": ["/usr/bin/pgrep python3"]},
139 "initialDelaySeconds": 60,
140 "timeoutSeconds": 5,
141 }
142
143
144 def _make_readiness_probe(port: int) -> Dict[str, Any]:
145 """Generate readiness probe.
146
147 Args:
148 port (int): [description]
149
150 Returns:
151 Dict[str, Any]: readiness probe.
152 """
153 return {
154 "tcpSocket": {
155 "port": port,
156 },
157 "periodSeconds": 10,
158 "timeoutSeconds": 5,
159 "successThreshold": 1,
160 "failureThreshold": 3,
161 }
162
163
164 def _make_liveness_probe(port: int) -> Dict[str, Any]:
165 """Generate liveness probe.
166
167 Args:
168 port (int): [description]
169
170 Returns:
171 Dict[str, Any]: liveness probe.
172 """
173 return {
174 "tcpSocket": {
175 "port": port,
176 },
177 "initialDelaySeconds": 45,
178 "periodSeconds": 10,
179 "timeoutSeconds": 5,
180 "successThreshold": 1,
181 "failureThreshold": 3,
182 }
183
184
185 def make_pod_spec(
186 image_info: Dict[str, str],
187 config: Dict[str, Any],
188 relation_state: Dict[str, Any],
189 app_name: str = "mon",
190 port: int = 8000,
191 ) -> Dict[str, Any]:
192 """Generate the pod spec information.
193
194 Args:
195 image_info (Dict[str, str]): Object provided by
196 OCIImageResource("image").fetch().
197 config (Dict[str, Any]): Configuration information.
198 relation_state (Dict[str, Any]): Relation state information.
199 app_name (str, optional): Application name. Defaults to "mon".
200 port (int, optional): Port for the container. Defaults to 8000.
201
202 Returns:
203 Dict[str, Any]: Pod spec dictionary for the charm.
204 """
205 if not image_info:
206 return None
207
208 _validate_data(config, relation_state)
209
210 ports = _make_pod_ports(port)
211 env_config = _make_pod_envconfig(config, relation_state)
212
213 return {
214 "version": 3,
215 "containers": [
216 {
217 "name": app_name,
218 "imageDetails": image_info,
219 "imagePullPolicy": "Always",
220 "ports": ports,
221 "envConfig": env_config,
222 }
223 ],
224 "kubernetesResources": {
225 "ingressResources": [],
226 },
227 }