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 pydantic
import ValidationError
25 from typing
import Any
, Dict
, NoReturn
27 from ops
.charm
import CharmBase
, CharmEvents
28 from ops
.framework
import EventBase
, EventSource
, StoredState
29 from ops
.main
import main
30 from ops
.model
import ActiveStatus
, BlockedStatus
, MaintenanceStatus
31 from oci_image
import OCIImageResource
, OCIImageResourceError
33 from pod_spec
import make_pod_spec
35 logger
= logging
.getLogger(__name__
)
38 class ConfigurePodEvent(EventBase
):
39 """Configure Pod event"""
44 class PolEvents(CharmEvents
):
47 configure_pod
= EventSource(ConfigurePodEvent
)
50 class PolCharm(CharmBase
):
56 def __init__(self
, *args
) -> NoReturn
:
57 """POL Charm constructor."""
58 super().__init
__(*args
)
60 # Internal state initialization
61 self
.state
.set_default(pod_spec
=None)
63 # Message bus data initialization
64 self
.state
.set_default(message_host
=None)
65 self
.state
.set_default(message_port
=None)
67 # Database data initialization
68 self
.state
.set_default(database_uri
=None)
70 self
.image
= OCIImageResource(self
, "image")
72 # Registering regular events
73 self
.framework
.observe(self
.on
.start
, self
.configure_pod
)
74 self
.framework
.observe(self
.on
.config_changed
, self
.configure_pod
)
75 self
.framework
.observe(self
.on
.upgrade_charm
, self
.configure_pod
)
77 # Registering custom internal events
78 self
.framework
.observe(self
.on
.configure_pod
, self
.configure_pod
)
80 # Registering required relation events
81 self
.framework
.observe(
82 self
.on
.kafka_relation_changed
, self
._on
_kafka
_relation
_changed
84 self
.framework
.observe(
85 self
.on
.mongodb_relation_changed
, self
._on
_mongodb
_relation
_changed
88 # Registering required relation departed events
89 self
.framework
.observe(
90 self
.on
.kafka_relation_departed
, self
._on
_kafka
_relation
_departed
92 self
.framework
.observe(
93 self
.on
.mongodb_relation_departed
, self
._on
_mongodb
_relation
_departed
96 def _on_kafka_relation_changed(self
, event
: EventBase
) -> NoReturn
:
97 """Reads information about the kafka relation.
100 event (EventBase): Kafka relation event.
102 data_loc
= event
.unit
if event
.unit
else event
.app
104 message_host
= event
.relation
.data
[data_loc
].get("host")
105 message_port
= event
.relation
.data
[data_loc
].get("port")
111 self
.state
.message_host
!= message_host
112 or self
.state
.message_port
!= message_port
115 self
.state
.message_host
= message_host
116 self
.state
.message_port
= message_port
117 self
.on
.configure_pod
.emit()
119 def _on_kafka_relation_departed(self
, event
: EventBase
) -> NoReturn
:
120 """Clear kafka relation data.
123 event (EventBase): Kafka relation event.
125 self
.state
.message_host
= None
126 self
.state
.message_port
= None
127 self
.on
.configure_pod
.emit()
129 def _on_mongodb_relation_changed(self
, event
: EventBase
) -> NoReturn
:
130 """Reads information about the DB relation.
133 event (EventBase): DB relation event.
135 data_loc
= event
.unit
if event
.unit
else event
.app
137 database_uri
= event
.relation
.data
[data_loc
].get("connection_string")
139 if database_uri
and self
.state
.database_uri
!= database_uri
:
140 self
.state
.database_uri
= database_uri
141 self
.on
.configure_pod
.emit()
143 def _on_mongodb_relation_departed(self
, event
: EventBase
) -> NoReturn
:
144 """Clear mongodb relation data.
147 event (EventBase): DB relation event.
149 self
.state
.database_uri
= None
150 self
.on
.configure_pod
.emit()
152 def _missing_relations(self
) -> str:
153 """Checks if there missing relations.
156 str: string with missing relations
159 "kafka": self
.state
.message_host
,
160 "mongodb": self
.state
.database_uri
,
163 missing_relations
= [k
for k
, v
in data_status
.items() if not v
]
165 return ", ".join(missing_relations
)
168 def relation_state(self
) -> Dict
[str, Any
]:
169 """Collects relation state configuration for pod spec assembly.
172 Dict[str, Any]: relation state information.
175 "message_host": self
.state
.message_host
,
176 "message_port": self
.state
.message_port
,
177 "database_uri": self
.state
.database_uri
,
180 return relation_state
182 def configure_pod(self
, event
: EventBase
) -> NoReturn
:
183 """Assemble the pod spec and apply it, if possible.
186 event (EventBase): Hook or Relation event that started the
189 if missing
:= self
._missing
_relations
():
190 self
.unit
.status
= BlockedStatus(
191 "Waiting for {0} relation{1}".format(
192 missing
, "s" if "," in missing
else ""
197 if not self
.unit
.is_leader():
198 self
.unit
.status
= ActiveStatus("ready")
201 self
.unit
.status
= MaintenanceStatus("Assembling pod spec")
203 # Fetch image information
205 self
.unit
.status
= MaintenanceStatus("Fetching image information")
206 image_info
= self
.image
.fetch()
207 except OCIImageResourceError
:
208 self
.unit
.status
= BlockedStatus("Error fetching image information")
212 pod_spec
= make_pod_spec(
218 except ValidationError
as exc
:
219 logger
.exception("Config/Relation data validation error")
220 self
.unit
.status
= BlockedStatus(str(exc
))
223 if self
.state
.pod_spec
!= pod_spec
:
224 self
.model
.pod
.set_spec(pod_spec
)
225 self
.state
.pod_spec
= pod_spec
227 self
.unit
.status
= ActiveStatus("ready")
230 if __name__
== "__main__":