2 # Copyright 2021 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
23 # pylint: disable=E0213
27 from typing
import NoReturn
, Optional
30 from ops
.main
import main
31 from opslib
.osm
.charm
import CharmedOsmBase
, RelationsMissing
32 from opslib
.osm
.interfaces
.http
import HttpClient
33 from opslib
.osm
.interfaces
.kafka
import KafkaClient
34 from opslib
.osm
.interfaces
.mongo
import MongoClient
35 from opslib
.osm
.pod
import ContainerV3Builder
, PodSpecV3Builder
36 from opslib
.osm
.validator
import ModelValidator
, validator
39 logger
= logging
.getLogger(__name__
)
44 class ConfigModel(ModelValidator
):
53 database_commonkey
: str
54 mongodb_uri
: Optional
[str]
56 vca_apiproxy
: Optional
[str]
57 # Model-config options
58 vca_model_config_agent_metadata_url
: Optional
[str]
59 vca_model_config_agent_stream
: Optional
[str]
60 vca_model_config_apt_ftp_proxy
: Optional
[str]
61 vca_model_config_apt_http_proxy
: Optional
[str]
62 vca_model_config_apt_https_proxy
: Optional
[str]
63 vca_model_config_apt_mirror
: Optional
[str]
64 vca_model_config_apt_no_proxy
: Optional
[str]
65 vca_model_config_automatically_retry_hooks
: Optional
[bool]
66 vca_model_config_backup_dir
: Optional
[str]
67 vca_model_config_cloudinit_userdata
: Optional
[str]
68 vca_model_config_container_image_metadata_url
: Optional
[str]
69 vca_model_config_container_image_stream
: Optional
[str]
70 vca_model_config_container_inherit_properties
: Optional
[str]
71 vca_model_config_container_networking_method
: Optional
[str]
72 vca_model_config_default_series
: Optional
[str]
73 vca_model_config_default_space
: Optional
[str]
74 vca_model_config_development
: Optional
[bool]
75 vca_model_config_disable_network_management
: Optional
[bool]
76 vca_model_config_egress_subnets
: Optional
[str]
77 vca_model_config_enable_os_refresh_update
: Optional
[bool]
78 vca_model_config_enable_os_upgrade
: Optional
[bool]
79 vca_model_config_fan_config
: Optional
[str]
80 vca_model_config_firewall_mode
: Optional
[str]
81 vca_model_config_ftp_proxy
: Optional
[str]
82 vca_model_config_http_proxy
: Optional
[str]
83 vca_model_config_https_proxy
: Optional
[str]
84 vca_model_config_ignore_machine_addresses
: Optional
[bool]
85 vca_model_config_image_metadata_url
: Optional
[str]
86 vca_model_config_image_stream
: Optional
[str]
87 vca_model_config_juju_ftp_proxy
: Optional
[str]
88 vca_model_config_juju_http_proxy
: Optional
[str]
89 vca_model_config_juju_https_proxy
: Optional
[str]
90 vca_model_config_juju_no_proxy
: Optional
[str]
91 vca_model_config_logforward_enabled
: Optional
[bool]
92 vca_model_config_logging_config
: Optional
[str]
93 vca_model_config_lxd_snap_channel
: Optional
[str]
94 vca_model_config_max_action_results_age
: Optional
[str]
95 vca_model_config_max_action_results_size
: Optional
[str]
96 vca_model_config_max_status_history_age
: Optional
[str]
97 vca_model_config_max_status_history_size
: Optional
[str]
98 vca_model_config_net_bond_reconfigure_delay
: Optional
[str]
99 vca_model_config_no_proxy
: Optional
[str]
100 vca_model_config_provisioner_harvest_mode
: Optional
[str]
101 vca_model_config_proxy_ssh
: Optional
[bool]
102 vca_model_config_snap_http_proxy
: Optional
[str]
103 vca_model_config_snap_https_proxy
: Optional
[str]
104 vca_model_config_snap_store_assertions
: Optional
[str]
105 vca_model_config_snap_store_proxy
: Optional
[str]
106 vca_model_config_snap_store_proxy_url
: Optional
[str]
107 vca_model_config_ssl_hostname_verification
: Optional
[bool]
108 vca_model_config_test_mode
: Optional
[bool]
109 vca_model_config_transmit_vendor_metrics
: Optional
[bool]
110 vca_model_config_update_status_hook_interval
: Optional
[str]
112 @validator("log_level")
113 def validate_log_level(cls
, v
):
114 if v
not in {"INFO", "DEBUG"}:
115 raise ValueError("value must be INFO or DEBUG")
118 @validator("mongodb_uri")
119 def validate_mongodb_uri(cls
, v
):
120 if v
and not v
.startswith("mongodb://"):
121 raise ValueError("mongodb_uri is not properly formed")
125 class LcmCharm(CharmedOsmBase
):
126 def __init__(self
, *args
) -> NoReturn
:
127 super().__init
__(*args
, oci_image
="image")
129 self
.kafka_client
= KafkaClient(self
, "kafka")
130 self
.framework
.observe(self
.on
["kafka"].relation_changed
, self
.configure_pod
)
131 self
.framework
.observe(self
.on
["kafka"].relation_broken
, self
.configure_pod
)
133 self
.mongodb_client
= MongoClient(self
, "mongodb")
134 self
.framework
.observe(self
.on
["mongodb"].relation_changed
, self
.configure_pod
)
135 self
.framework
.observe(self
.on
["mongodb"].relation_broken
, self
.configure_pod
)
137 self
.ro_client
= HttpClient(self
, "ro")
138 self
.framework
.observe(self
.on
["ro"].relation_changed
, self
.configure_pod
)
139 self
.framework
.observe(self
.on
["ro"].relation_broken
, self
.configure_pod
)
141 def _check_missing_dependencies(self
, config
: ConfigModel
):
142 missing_relations
= []
144 if self
.kafka_client
.is_missing_data_in_unit():
145 missing_relations
.append("kafka")
146 if not config
.mongodb_uri
and self
.mongodb_client
.is_missing_data_in_unit():
147 missing_relations
.append("mongodb")
148 if self
.ro_client
.is_missing_data_in_app():
149 missing_relations
.append("ro")
151 if missing_relations
:
152 raise RelationsMissing(missing_relations
)
154 def build_pod_spec(self
, image_info
):
156 config
= ConfigModel(**dict(self
.config
))
158 if config
.mongodb_uri
and not self
.mongodb_client
.is_missing_data_in_unit():
159 raise Exception("Mongodb data cannot be provided via config and relation")
162 self
._check
_missing
_dependencies
(config
)
164 # Create Builder for the PodSpec
165 pod_spec_builder
= PodSpecV3Builder()
168 container_builder
= ContainerV3Builder(self
.app
.name
, image_info
)
169 container_builder
.add_port(name
=self
.app
.name
, port
=PORT
)
170 container_builder
.add_envs(
172 # General configuration
173 "ALLOW_ANONYMOUS_LOGIN": "yes",
174 "OSMLCM_GLOBAL_LOGLEVEL": config
.log_level
,
176 "OSMLCM_RO_HOST": self
.ro_client
.host
,
177 "OSMLCM_RO_PORT": self
.ro_client
.port
,
178 "OSMLCM_RO_TENANT": "osm",
179 # Kafka configuration
180 "OSMLCM_MESSAGE_DRIVER": "kafka",
181 "OSMLCM_MESSAGE_HOST": self
.kafka_client
.host
,
182 "OSMLCM_MESSAGE_PORT": self
.kafka_client
.port
,
183 # Database configuration
184 "OSMLCM_DATABASE_DRIVER": "mongo",
185 "OSMLCM_DATABASE_URI": config
.mongodb_uri
186 or self
.mongodb_client
.connection_string
,
187 "OSMLCM_DATABASE_COMMONKEY": config
.database_commonkey
,
188 # Storage configuration
189 "OSMLCM_STORAGE_DRIVER": "mongo",
190 "OSMLCM_STORAGE_PATH": "/app/storage",
191 "OSMLCM_STORAGE_COLLECTION": "files",
192 "OSMLCM_STORAGE_URI": self
.mongodb_client
.connection_string
,
194 "OSMLCM_VCA_HOST": config
.vca_host
,
195 "OSMLCM_VCA_PORT": config
.vca_port
,
196 "OSMLCM_VCA_USER": config
.vca_user
,
197 "OSMLCM_VCA_PUBKEY": config
.vca_pubkey
,
198 "OSMLCM_VCA_SECRET": config
.vca_secret
,
199 "OSMLCM_VCA_CACERT": config
.vca_cacert
,
200 "OSMLCM_VCA_CLOUD": config
.vca_cloud
,
201 "OSMLCM_VCA_K8S_CLOUD": config
.vca_k8s_cloud
,
204 if config
.vca_apiproxy
:
205 container_builder
.add_env("OSMLCM_VCA_APIPROXY", config
.vca_apiproxy
)
207 model_config_envs
= {
208 f
"OSMLCM_{k.upper()}": v
209 for k
, v
in self
.config
.items()
210 if k
.startswith("vca_model_config")
213 if model_config_envs
:
214 container_builder
.add_envs(model_config_envs
)
216 container
= container_builder
.build()
218 # Add container to pod spec
219 pod_spec_builder
.add_container(container
)
221 return pod_spec_builder
.build()
224 if __name__
== "__main__":
228 # class ConfigurePodEvent(EventBase):
229 # """Configure Pod event"""
234 # class LcmEvents(CharmEvents):
237 # configure_pod = EventSource(ConfigurePodEvent)
240 # class LcmCharm(CharmBase):
243 # state = StoredState()
246 # def __init__(self, *args) -> NoReturn:
247 # """LCM Charm constructor."""
248 # super().__init__(*args)
250 # # Internal state initialization
251 # self.state.set_default(pod_spec=None)
253 # # Message bus data initialization
254 # self.state.set_default(message_host=None)
255 # self.state.set_default(message_port=None)
257 # # Database data initialization
258 # self.state.set_default(database_uri=None)
260 # # RO data initialization
261 # self.state.set_default(ro_host=None)
262 # self.state.set_default(ro_port=None)
264 # self.port = LCM_PORT
265 # self.image = OCIImageResource(self, "image")
267 # # Registering regular events
268 # self.framework.observe(self.on.start, self.configure_pod)
269 # self.framework.observe(self.on.config_changed, self.configure_pod)
270 # self.framework.observe(self.on.upgrade_charm, self.configure_pod)
272 # # Registering custom internal events
273 # self.framework.observe(self.on.configure_pod, self.configure_pod)
275 # # Registering required relation events
276 # self.framework.observe(
277 # self.on.kafka_relation_changed, self._on_kafka_relation_changed
279 # self.framework.observe(
280 # self.on.mongodb_relation_changed, self._on_mongodb_relation_changed
282 # self.framework.observe(
283 # self.on.ro_relation_changed, self._on_ro_relation_changed
286 # # Registering required relation broken events
287 # self.framework.observe(
288 # self.on.kafka_relation_broken, self._on_kafka_relation_broken
290 # self.framework.observe(
291 # self.on.mongodb_relation_broken, self._on_mongodb_relation_broken
293 # self.framework.observe(
294 # self.on.ro_relation_broken, self._on_ro_relation_broken
297 # def _on_kafka_relation_changed(self, event: EventBase) -> NoReturn:
298 # """Reads information about the kafka relation.
301 # event (EventBase): Kafka relation event.
303 # message_host = event.relation.data[event.unit].get("host")
304 # message_port = event.relation.data[event.unit].get("port")
310 # self.state.message_host != message_host
311 # or self.state.message_port != message_port
314 # self.state.message_host = message_host
315 # self.state.message_port = message_port
316 # self.on.configure_pod.emit()
318 # def _on_kafka_relation_broken(self, event: EventBase) -> NoReturn:
319 # """Clears data from kafka relation.
322 # event (EventBase): Kafka relation event.
324 # self.state.message_host = None
325 # self.state.message_port = None
326 # self.on.configure_pod.emit()
328 # def _on_mongodb_relation_changed(self, event: EventBase) -> NoReturn:
329 # """Reads information about the DB relation.
332 # event (EventBase): DB relation event.
334 # database_uri = event.relation.data[event.unit].get("connection_string")
336 # if database_uri and self.state.database_uri != database_uri:
337 # self.state.database_uri = database_uri
338 # self.on.configure_pod.emit()
340 # def _on_mongodb_relation_broken(self, event: EventBase) -> NoReturn:
341 # """Clears data from mongodb relation.
344 # event (EventBase): DB relation event.
346 # self.state.database_uri = None
347 # self.on.configure_pod.emit()
349 # def _on_ro_relation_changed(self, event: EventBase) -> NoReturn:
350 # """Reads information about the RO relation.
353 # event (EventBase): Keystone relation event.
355 # ro_host = event.relation.data[event.unit].get("host")
356 # ro_port = event.relation.data[event.unit].get("port")
361 # and (self.state.ro_host != ro_host or self.state.ro_port != ro_port)
363 # self.state.ro_host = ro_host
364 # self.state.ro_port = ro_port
365 # self.on.configure_pod.emit()
367 # def _on_ro_relation_broken(self, event: EventBase) -> NoReturn:
368 # """Clears data from ro relation.
371 # event (EventBase): Keystone relation event.
373 # self.state.ro_host = None
374 # self.state.ro_port = None
375 # self.on.configure_pod.emit()
377 # def _missing_relations(self) -> str:
378 # """Checks if there missing relations.
381 # str: string with missing relations
384 # "kafka": self.state.message_host,
385 # "mongodb": self.state.database_uri,
386 # "ro": self.state.ro_host,
389 # missing_relations = [k for k, v in data_status.items() if not v]
391 # return ", ".join(missing_relations)
394 # def relation_state(self) -> Dict[str, Any]:
395 # """Collects relation state configuration for pod spec assembly.
398 # Dict[str, Any]: relation state information.
401 # "message_host": self.state.message_host,
402 # "message_port": self.state.message_port,
403 # "database_uri": self.state.database_uri,
404 # "ro_host": self.state.ro_host,
405 # "ro_port": self.state.ro_port,
408 # return relation_state
410 # def configure_pod(self, event: EventBase) -> NoReturn:
411 # """Assemble the pod spec and apply it, if possible.
414 # event (EventBase): Hook or Relation event that started the
417 # if missing := self._missing_relations():
418 # self.unit.status = BlockedStatus(
419 # "Waiting for {0} relation{1}".format(
420 # missing, "s" if "," in missing else ""
425 # if not self.unit.is_leader():
426 # self.unit.status = ActiveStatus("ready")
429 # self.unit.status = MaintenanceStatus("Assembling pod spec")
431 # # Fetch image information
433 # self.unit.status = MaintenanceStatus("Fetching image information")
434 # image_info = self.image.fetch()
435 # except OCIImageResourceError:
436 # self.unit.status = BlockedStatus("Error fetching image information")
440 # pod_spec = make_pod_spec(
443 # self.relation_state,
444 # self.model.app.name,
447 # except ValueError as exc:
448 # logger.exception("Config/Relation data validation error")
449 # self.unit.status = BlockedStatus(str(exc))
452 # if self.state.pod_spec != pod_spec:
453 # self.model.pod.set_spec(pod_spec)
454 # self.state.pod_spec = pod_spec
456 # self.unit.status = ActiveStatus("ready")
459 # if __name__ == "__main__":