2 # Copyright 2020 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
24 from typing
import Any
, Dict
, List
, NoReturn
26 logger
= logging
.getLogger(__name__
)
30 config_data
: Dict
[str, Any
], relation_data
: Dict
[str, Any
]
32 """Validate input data.
35 config_data (Dict[str, Any]): configuration data.
36 relation_data (Dict[str, Any]): relation data.
39 "openstack_default_granularity": lambda value
, _
: isinstance(value
, int)
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)
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),
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,
63 for key
, validator
in config_validators
.items():
64 valid
= validator(config_data
.get(key
), config_data
)
69 for key
, validator
in relation_validators
.items():
70 valid
= validator(relation_data
.get(key
), relation_data
)
76 raise ValueError("Errors found in: {}".format(", ".join(problems
)))
79 def _make_pod_ports(port
: int) -> List
[Dict
[str, Any
]]:
80 """Generate pod ports details.
83 port (int): port to expose.
86 List[Dict[str, Any]]: pod port details.
88 return [{"name": "mon", "containerPort": port
, "protocol": "TCP"}]
91 def _make_pod_envconfig(
92 config
: Dict
[str, Any
], relation_state
: Dict
[str, Any
]
94 """Generate pod environment configuration.
97 config (Dict[str, Any]): configuration information.
98 relation_state (Dict[str, Any]): relation state information.
101 Dict[str, Any]: pod environment configuration.
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']}",
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"],
131 def _make_startup_probe() -> Dict
[str, Any
]:
132 """Generate startup probe.
135 Dict[str, Any]: startup probe.
138 "exec": {"command": ["/usr/bin/pgrep python3"]},
139 "initialDelaySeconds": 60,
144 def _make_readiness_probe(port
: int) -> Dict
[str, Any
]:
145 """Generate readiness probe.
148 port (int): [description]
151 Dict[str, Any]: readiness probe.
159 "successThreshold": 1,
160 "failureThreshold": 3,
164 def _make_liveness_probe(port
: int) -> Dict
[str, Any
]:
165 """Generate liveness probe.
168 port (int): [description]
171 Dict[str, Any]: liveness probe.
177 "initialDelaySeconds": 45,
180 "successThreshold": 1,
181 "failureThreshold": 3,
186 image_info
: Dict
[str, str],
187 config
: Dict
[str, Any
],
188 relation_state
: Dict
[str, Any
],
189 app_name
: str = "mon",
192 """Generate the pod spec information.
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.
203 Dict[str, Any]: Pod spec dictionary for the charm.
208 _validate_data(config
, relation_state
)
210 ports
= _make_pod_ports(port
)
211 env_config
= _make_pod_envconfig(config
, relation_state
)
218 "imageDetails": image_info
,
219 "imagePullPolicy": "Always",
221 "envConfig": env_config
,
224 "kubernetesResources": {
225 "ingressResources": [],