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 "database_commonkey": lambda value
, _
: isinstance(value
, str)
41 "log_level": lambda value
, _
: isinstance(value
, str)
42 and value
in ("INFO", "DEBUG"),
43 "vca_host": lambda value
, _
: isinstance(value
, str) and len(value
) > 1,
44 "vca_port": lambda value
, _
: isinstance(value
, int) and value
> 0,
45 "vca_user": lambda value
, _
: isinstance(value
, str) and len(value
) > 1,
46 "vca_pubkey": lambda value
, _
: isinstance(value
, str) and len(value
) > 1,
47 "vca_password": lambda value
, _
: isinstance(value
, str) and len(value
) > 1,
48 "vca_cacert": lambda value
, _
: isinstance(value
, str),
49 "vca_cloud": lambda value
, _
: isinstance(value
, str) and len(value
) > 1,
50 "vca_k8s_cloud": lambda value
, _
: isinstance(value
, str) and len(value
) > 1,
51 "vca_apiproxy": lambda value
, _
: (isinstance(value
, str) and len(value
) > 1)
55 relation_validators
= {
56 "ro_host": lambda value
, _
: isinstance(value
, str) and len(value
) > 1,
57 "ro_port": lambda value
, _
: isinstance(value
, int) and value
> 0,
58 "message_host": lambda value
, _
: isinstance(value
, str) and len(value
) > 1,
59 "message_port": lambda value
, _
: isinstance(value
, int) and value
> 0,
60 "database_uri": lambda value
, _
: isinstance(value
, str) and len(value
) > 1,
64 for key
, validator
in config_validators
.items():
65 valid
= validator(config_data
.get(key
), config_data
)
70 for key
, validator
in relation_validators
.items():
71 valid
= validator(relation_data
.get(key
), relation_data
)
77 raise ValueError("Errors found in: {}".format(", ".join(problems
)))
80 def _make_pod_ports(port
: int) -> List
[Dict
[str, Any
]]:
81 """Generate pod ports details.
84 port (int): port to expose.
87 List[Dict[str, Any]]: pod port details.
89 return [{"name": "lcm", "containerPort": port
, "protocol": "TCP"}]
92 def _make_pod_envconfig(
93 config
: Dict
[str, Any
], relation_state
: Dict
[str, Any
]
95 """Generate pod environment configuration.
98 config (Dict[str, Any]): configuration information.
99 relation_state (Dict[str, Any]): relation state information.
102 Dict[str, Any]: pod environment configuration.
105 # General configuration
106 "ALLOW_ANONYMOUS_LOGIN": "yes",
107 "OSMLCM_GLOBAL_LOGLEVEL": config
["log_level"],
109 "OSMLCM_RO_HOST": relation_state
["ro_host"],
110 "OSMLCM_RO_PORT": relation_state
["ro_port"],
111 "OSMLCM_RO_TENANT": "osm",
112 # Kafka configuration
113 "OSMLCM_MESSAGE_DRIVER": "kafka",
114 "OSMLCM_MESSAGE_HOST": relation_state
["message_host"],
115 "OSMLCM_MESSAGE_PORT": relation_state
["message_port"],
116 # Database configuration
117 "OSMLCM_DATABASE_DRIVER": "mongo",
118 "OSMLCM_DATABASE_URI": relation_state
["database_uri"],
119 "OSMLCM_DATABASE_COMMONKEY": config
["database_commonkey"],
120 # Storage configuration
121 "OSMLCM_STORAGE_DRIVER": "mongo",
122 "OSMLCM_STORAGE_PATH": "/app/storage",
123 "OSMLCM_STORAGE_COLLECTION": "files",
124 "OSMLCM_STORAGE_URI": relation_state
["database_uri"],
126 "OSMLCM_VCA_HOST": config
["vca_host"],
127 "OSMLCM_VCA_PORT": config
["vca_port"],
128 "OSMLCM_VCA_USER": config
["vca_user"],
129 "OSMLCM_VCA_PUBKEY": config
["vca_pubkey"],
130 "OSMLCM_VCA_SECRET": config
["vca_password"],
131 "OSMLCM_VCA_CACERT": config
["vca_cacert"],
132 "OSMLCM_VCA_CLOUD": config
["vca_cloud"],
133 "OSMLCM_VCA_K8S_CLOUD": config
["vca_k8s_cloud"],
136 if "vca_apiproxy" in config
and config
["vca_apiproxy"]:
137 envconfig
["OSMLCM_VCA_APIPROXY"] = config
["vca_apiproxy"]
142 def _make_startup_probe() -> Dict
[str, Any
]:
143 """Generate startup probe.
146 Dict[str, Any]: startup probe.
149 "exec": {"command": ["/usr/bin/pgrep python3"]},
150 "initialDelaySeconds": 60,
155 def _make_readiness_probe(port
: int) -> Dict
[str, Any
]:
156 """Generate readiness probe.
159 port (int): [description]
162 Dict[str, Any]: readiness probe.
169 "initialDelaySeconds": 45,
174 def _make_liveness_probe(port
: int) -> Dict
[str, Any
]:
175 """Generate liveness probe.
178 port (int): [description]
181 Dict[str, Any]: liveness probe.
188 "initialDelaySeconds": 45,
194 image_info
: Dict
[str, str],
195 config
: Dict
[str, Any
],
196 relation_state
: Dict
[str, Any
],
197 app_name
: str = "lcm",
200 """Generate the pod spec information.
203 image_info (Dict[str, str]): Object provided by
204 OCIImageResource("image").fetch().
205 config (Dict[str, Any]): Configuration information.
206 relation_state (Dict[str, Any]): Relation state information.
207 app_name (str, optional): Application name. Defaults to "lcm".
208 port (int, optional): Port for the container. Defaults to 9999.
211 Dict[str, Any]: Pod spec dictionary for the charm.
216 _validate_data(config
, relation_state
)
218 ports
= _make_pod_ports(port
)
219 env_config
= _make_pod_envconfig(config
, relation_state
)
226 "imageDetails": image_info
,
227 "imagePullPolicy": "Always",
229 "envConfig": env_config
,
232 "kubernetesResources": {
233 "ingressResources": [],