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
, NoReturn
26 from ops
.charm
import CharmBase
, CharmEvents
27 from ops
.framework
import EventBase
, EventSource
, StoredState
28 from ops
.main
import main
29 from ops
.model
import ActiveStatus
, BlockedStatus
, MaintenanceStatus
30 from oci_image
import OCIImageResource
, OCIImageResourceError
32 from pod_spec
import make_pod_spec
34 logger
= logging
.getLogger(__name__
)
37 class ConfigurePodEvent(EventBase
):
38 """Configure Pod event"""
43 class PolEvents(CharmEvents
):
46 configure_pod
= EventSource(ConfigurePodEvent
)
49 class PolCharm(CharmBase
):
55 def __init__(self
, *args
) -> NoReturn
:
56 """POL Charm constructor."""
57 super().__init
__(*args
)
59 # Internal state initialization
60 self
.state
.set_default(pod_spec
=None)
62 # Message bus data initialization
63 self
.state
.set_default(message_host
=None)
64 self
.state
.set_default(message_port
=None)
66 # Database data initialization
67 self
.state
.set_default(database_uri
=None)
69 self
.image
= OCIImageResource(self
, "image")
71 # Registering regular events
72 self
.framework
.observe(self
.on
.start
, self
.configure_pod
)
73 self
.framework
.observe(self
.on
.config_changed
, self
.configure_pod
)
74 self
.framework
.observe(self
.on
.upgrade_charm
, self
.configure_pod
)
76 # Registering custom internal events
77 self
.framework
.observe(self
.on
.configure_pod
, self
.configure_pod
)
79 # Registering required relation events
80 self
.framework
.observe(
81 self
.on
.kafka_relation_changed
, self
._on
_kafka
_relation
_changed
83 self
.framework
.observe(
84 self
.on
.mongodb_relation_changed
, self
._on
_mongodb
_relation
_changed
87 # Registering required relation departed events
88 self
.framework
.observe(
89 self
.on
.kafka_relation_departed
, self
._on
_kafka
_relation
_departed
91 self
.framework
.observe(
92 self
.on
.mongodb_relation_departed
, self
._on
_mongodb
_relation
_departed
95 def _on_kafka_relation_changed(self
, event
: EventBase
) -> NoReturn
:
96 """Reads information about the kafka relation.
99 event (EventBase): Kafka relation event.
101 message_host
= event
.relation
.data
[event
.unit
].get("host")
102 message_port
= event
.relation
.data
[event
.unit
].get("port")
108 self
.state
.message_host
!= message_host
109 or self
.state
.message_port
!= message_port
112 self
.state
.message_host
= message_host
113 self
.state
.message_port
= message_port
114 self
.on
.configure_pod
.emit()
116 def _on_kafka_relation_departed(self
, event
: EventBase
) -> NoReturn
:
117 """Clear kafka relation data.
120 event (EventBase): Kafka relation event.
122 self
.state
.message_host
= None
123 self
.state
.message_port
= None
124 self
.on
.configure_pod
.emit()
126 def _on_mongodb_relation_changed(self
, event
: EventBase
) -> NoReturn
:
127 """Reads information about the DB relation.
130 event (EventBase): DB relation event.
132 database_uri
= event
.relation
.data
[event
.unit
].get("connection_string")
134 if database_uri
and self
.state
.database_uri
!= database_uri
:
135 self
.state
.database_uri
= database_uri
136 self
.on
.configure_pod
.emit()
138 def _on_mongodb_relation_departed(self
, event
: EventBase
) -> NoReturn
:
139 """Clear mongodb relation data.
142 event (EventBase): DB relation event.
144 self
.state
.database_uri
= None
145 self
.on
.configure_pod
.emit()
147 def _missing_relations(self
) -> str:
148 """Checks if there missing relations.
151 str: string with missing relations
154 "kafka": self
.state
.message_host
,
155 "mongodb": self
.state
.database_uri
,
158 missing_relations
= [k
for k
, v
in data_status
.items() if not v
]
160 return ", ".join(missing_relations
)
163 def relation_state(self
) -> Dict
[str, Any
]:
164 """Collects relation state configuration for pod spec assembly.
167 Dict[str, Any]: relation state information.
170 "message_host": self
.state
.message_host
,
171 "message_port": self
.state
.message_port
,
172 "database_uri": self
.state
.database_uri
,
175 return relation_state
177 def configure_pod(self
, event
: EventBase
) -> NoReturn
:
178 """Assemble the pod spec and apply it, if possible.
181 event (EventBase): Hook or Relation event that started the
184 if missing
:= self
._missing
_relations
():
185 self
.unit
.status
= BlockedStatus(
186 "Waiting for {0} relation{1}".format(
187 missing
, "s" if "," in missing
else ""
192 if not self
.unit
.is_leader():
193 self
.unit
.status
= ActiveStatus("ready")
196 self
.unit
.status
= MaintenanceStatus("Assembling pod spec")
198 # Fetch image information
200 self
.unit
.status
= MaintenanceStatus("Fetching image information")
201 image_info
= self
.image
.fetch()
202 except OCIImageResourceError
:
203 self
.unit
.status
= BlockedStatus("Error fetching image information")
207 pod_spec
= make_pod_spec(
213 except ValueError as exc
:
214 logger
.exception("Config/Relation data validation error")
215 self
.unit
.status
= BlockedStatus(str(exc
))
218 if self
.state
.pod_spec
!= pod_spec
:
219 self
.model
.pod
.set_spec(pod_spec
)
220 self
.state
.pod_spec
= pod_spec
222 self
.unit
.status
= ActiveStatus("ready")
225 if __name__
== "__main__":