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
, _
: (
40 isinstance(value
, int) and value
> 0
42 "global_request_timeout": lambda value
, _
: isinstance(value
, int) and value
> 0,
43 "log_level": lambda value
, _
: (
44 isinstance(value
, str) and value
in ("INFO", "DEBUG")
46 "collector_interval": lambda value
, _
: isinstance(value
, int) and value
> 0,
47 "evaluator_interval": lambda value
, _
: isinstance(value
, int) and value
> 0,
48 "database_commonkey": lambda value
, _
: (
49 isinstance(value
, str) and len(value
) > 0
51 "vca_host": lambda value
, _
: isinstance(value
, str) and len(value
) > 0,
52 "vca_user": lambda value
, _
: isinstance(value
, str) and len(value
) > 0,
53 "vca_password": lambda value
, _
: isinstance(value
, str) and len(value
) > 0,
54 "vca_cacert": lambda value
, _
: isinstance(value
, str),
56 relation_validators
= {
57 "message_host": lambda value
, _
: isinstance(value
, str) and len(value
) > 0,
58 "message_port": lambda value
, _
: isinstance(value
, int) and value
> 0,
59 "database_uri": lambda value
, _
: (
60 isinstance(value
, str) and value
.startswith("mongodb://")
62 "prometheus_host": lambda value
, _
: isinstance(value
, str) and len(value
) > 0,
63 "prometheus_port": lambda value
, _
: isinstance(value
, int) and value
> 0,
67 for key
, validator
in config_validators
.items():
68 valid
= validator(config_data
.get(key
), config_data
)
73 for key
, validator
in relation_validators
.items():
74 valid
= validator(relation_data
.get(key
), relation_data
)
80 raise ValueError("Errors found in: {}".format(", ".join(problems
)))
83 def _make_pod_ports(port
: int) -> List
[Dict
[str, Any
]]:
84 """Generate pod ports details.
87 port (int): port to expose.
90 List[Dict[str, Any]]: pod port details.
92 return [{"name": "mon", "containerPort": port
, "protocol": "TCP"}]
95 def _make_pod_envconfig(
96 config
: Dict
[str, Any
], relation_state
: Dict
[str, Any
]
98 """Generate pod environment configuration.
101 config (Dict[str, Any]): configuration information.
102 relation_state (Dict[str, Any]): relation state information.
105 Dict[str, Any]: pod environment configuration.
108 # General configuration
109 "ALLOW_ANONYMOUS_LOGIN": "yes",
110 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config
["openstack_default_granularity"],
111 "OSMMON_GLOBAL_REQUEST_TIMEOUT": config
["global_request_timeout"],
112 "OSMMON_GLOBAL_LOGLEVEL": config
["log_level"],
113 "OSMMON_COLLECTOR_INTERVAL": config
["collector_interval"],
114 "OSMMON_EVALUATOR_INTERVAL": config
["evaluator_interval"],
115 # Kafka configuration
116 "OSMMON_MESSAGE_DRIVER": "kafka",
117 "OSMMON_MESSAGE_HOST": relation_state
["message_host"],
118 "OSMMON_MESSAGE_PORT": relation_state
["message_port"],
119 # Database configuration
120 "OSMMON_DATABASE_DRIVER": "mongo",
121 "OSMMON_DATABASE_URI": relation_state
["database_uri"],
122 "OSMMON_DATABASE_COMMONKEY": config
["database_commonkey"],
123 # Prometheus configuration
124 "OSMMON_PROMETHEUS_URL": f
"http://{relation_state['prometheus_host']}:{relation_state['prometheus_port']}",
126 "OSMMON_VCA_HOST": config
["vca_host"],
127 "OSMMON_VCA_USER": config
["vca_user"],
128 "OSMMON_VCA_SECRET": config
["vca_password"],
129 "OSMMON_VCA_CACERT": config
["vca_cacert"],
135 def _make_startup_probe() -> Dict
[str, Any
]:
136 """Generate startup probe.
139 Dict[str, Any]: startup probe.
142 "exec": {"command": ["/usr/bin/pgrep python3"]},
143 "initialDelaySeconds": 60,
148 def _make_readiness_probe(port
: int) -> Dict
[str, Any
]:
149 """Generate readiness probe.
152 port (int): [description]
155 Dict[str, Any]: readiness probe.
163 "successThreshold": 1,
164 "failureThreshold": 3,
168 def _make_liveness_probe(port
: int) -> Dict
[str, Any
]:
169 """Generate liveness probe.
172 port (int): [description]
175 Dict[str, Any]: liveness probe.
181 "initialDelaySeconds": 45,
184 "successThreshold": 1,
185 "failureThreshold": 3,
190 image_info
: Dict
[str, str],
191 config
: Dict
[str, Any
],
192 relation_state
: Dict
[str, Any
],
193 app_name
: str = "mon",
196 """Generate the pod spec information.
199 image_info (Dict[str, str]): Object provided by
200 OCIImageResource("image").fetch().
201 config (Dict[str, Any]): Configuration information.
202 relation_state (Dict[str, Any]): Relation state information.
203 app_name (str, optional): Application name. Defaults to "mon".
204 port (int, optional): Port for the container. Defaults to 8000.
207 Dict[str, Any]: Pod spec dictionary for the charm.
212 _validate_data(config
, relation_state
)
214 ports
= _make_pod_ports(port
)
215 env_config
= _make_pod_envconfig(config
, relation_state
)
222 "imageDetails": image_info
,
223 "imagePullPolicy": "Always",
225 "envConfig": env_config
,
228 "kubernetesResources": {
229 "ingressResources": [],