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 "log_level": lambda value
, _
: (
40 isinstance(value
, str) and value
in ("INFO", "DEBUG")
43 relation_validators
= {
44 "message_host": lambda value
, _
: isinstance(value
, str) and len(value
) > 0,
45 "message_port": lambda value
, _
: isinstance(value
, int) and value
> 0,
46 "database_uri": lambda value
, _
: (
47 isinstance(value
, str) and value
.startswith("mongodb://")
52 for key
, validator
in config_validators
.items():
53 valid
= validator(config_data
.get(key
), config_data
)
58 for key
, validator
in relation_validators
.items():
59 valid
= validator(relation_data
.get(key
), relation_data
)
65 raise ValueError("Errors found in: {}".format(", ".join(problems
)))
68 def _make_pod_ports(port
: int) -> List
[Dict
[str, Any
]]:
69 """Generate pod ports details.
72 port (int): port to expose.
75 List[Dict[str, Any]]: pod port details.
77 return [{"name": "pol", "containerPort": port
, "protocol": "TCP"}]
80 def _make_pod_envconfig(
81 config
: Dict
[str, Any
], relation_state
: Dict
[str, Any
]
83 """Generate pod environment configuration.
86 config (Dict[str, Any]): configuration information.
87 relation_state (Dict[str, Any]): relation state information.
90 Dict[str, Any]: pod environment configuration.
93 # General configuration
94 "ALLOW_ANONYMOUS_LOGIN": "yes",
95 "OSMPOL_GLOBAL_LOGLEVEL": config
["log_level"],
97 "OSMPOL_MESSAGE_HOST": relation_state
["message_host"],
98 "OSMPOL_MESSAGE_DRIVER": "kafka",
99 "OSMPOL_MESSAGE_PORT": relation_state
["message_port"],
100 # Database configuration
101 "OSMPOL_DATABASE_DRIVER": "mongo",
102 "OSMPOL_DATABASE_URI": relation_state
["database_uri"],
108 def _make_startup_probe() -> Dict
[str, Any
]:
109 """Generate startup probe.
112 Dict[str, Any]: startup probe.
115 "exec": {"command": ["/usr/bin/pgrep", "python3"]},
116 "initialDelaySeconds": 60,
121 def _make_readiness_probe() -> Dict
[str, Any
]:
122 """Generate readiness probe.
125 Dict[str, Any]: readiness probe.
129 "command": ["sh", "-c", "osm-pol-healthcheck || exit 1"],
133 "successThreshold": 1,
134 "failureThreshold": 3,
138 def _make_liveness_probe() -> Dict
[str, Any
]:
139 """Generate liveness probe.
142 Dict[str, Any]: liveness probe.
146 "command": ["sh", "-c", "osm-pol-healthcheck || exit 1"],
148 "initialDelaySeconds": 45,
151 "successThreshold": 1,
152 "failureThreshold": 3,
157 image_info
: Dict
[str, str],
158 config
: Dict
[str, Any
],
159 relation_state
: Dict
[str, Any
],
160 app_name
: str = "pol",
163 """Generate the pod spec information.
166 image_info (Dict[str, str]): Object provided by
167 OCIImageResource("image").fetch().
168 config (Dict[str, Any]): Configuration information.
169 relation_state (Dict[str, Any]): Relation state information.
170 app_name (str, optional): Application name. Defaults to "pol".
171 port (int, optional): Port for the container. Defaults to 80.
174 Dict[str, Any]: Pod spec dictionary for the charm.
179 _validate_data(config
, relation_state
)
181 ports
= _make_pod_ports(port
)
182 env_config
= _make_pod_envconfig(config
, relation_state
)
189 "imageDetails": image_info
,
190 "imagePullPolicy": "Always",
192 "envConfig": env_config
,
195 "kubernetesResources": {
196 "ingressResources": [],