- def __init__(self, framework, key):
- super().__init__(framework, key)
- self.state.set_default(spec=None)
-
- # Observe Charm related events
- self.framework.observe(self.on.config_changed, self.on_config_changed)
- self.framework.observe(self.on.start, self.on_start)
- self.framework.observe(self.on.upgrade_charm, self.on_upgrade_charm)
- # self.framework.observe(
- # self.on.nbi_relation_joined, self.on_nbi_relation_joined
- # )
-
- def _apply_spec(self):
- # Only apply the spec if this unit is a leader.
- if not self.framework.model.unit.is_leader():
- return
- new_spec = self.make_pod_spec()
- if new_spec == self.state.spec:
- return
- self.framework.model.pod.set_spec(new_spec)
- self.state.spec = new_spec
-
- def make_pod_spec(self):
- config = self.framework.model.config
-
- ports = [
- {"name": "port", "containerPort": config["port"], "protocol": "TCP",},
- ]
-
- kubernetes = {
- "readinessProbe": {
- "tcpSocket": {"port": config["port"]},
- "timeoutSeconds": 5,
- "periodSeconds": 5,
- "initialDelaySeconds": 10,
- },
- "livenessProbe": {
- "tcpSocket": {"port": config["port"]},
- "timeoutSeconds": 5,
- "initialDelaySeconds": 45,
- },
- }
- config_spec = {
- "port": config["port"],
- "server_name": config["server_name"],
- "client_max_body_size": config["client_max_body_size"],
- "nbi_hostname": config["nbi_hostname"],
- "nbi_port": config["nbi_port"],
+class ConfigurePodEvent(EventBase):
+ """Configure Pod event"""
+
+ pass
+
+
+class NgUiEvents(CharmEvents):
+ """NGUI Events"""
+
+ configure_pod = EventSource(ConfigurePodEvent)
+
+
+class NgUiCharm(CharmBase):
+ """NGUI Charm."""
+
+ state = StoredState()
+ on = NgUiEvents()
+
+ def __init__(self, *args) -> NoReturn:
+ """NGUI Charm constructor."""
+ super().__init__(*args)
+
+ # Internal state initialization
+ self.state.set_default(pod_spec=None)
+
+ # North bound interface initialization
+ self.state.set_default(nbi_host=None)
+ self.state.set_default(nbi_port=None)
+
+ self.http_port = NGUI_PORT
+ self.image = OCIImageResource(self, "image")
+
+ # Registering regular events
+ self.framework.observe(self.on.start, self.configure_pod)
+ self.framework.observe(self.on.config_changed, self.configure_pod)
+ # self.framework.observe(self.on.upgrade_charm, self.configure_pod)
+
+ # Registering custom internal events
+ self.framework.observe(self.on.configure_pod, self.configure_pod)
+
+ # Registering required relation changed events
+ self.framework.observe(
+ self.on.nbi_relation_changed, self._on_nbi_relation_changed
+ )
+
+ # Registering required relation departed events
+ self.framework.observe(
+ self.on.nbi_relation_departed, self._on_nbi_relation_departed
+ )
+
+ def _on_nbi_relation_changed(self, event: EventBase) -> NoReturn:
+ """Reads information about the nbi relation.
+
+ Args:
+ event (EventBase): NBI relation event.
+ """
+ data_loc = event.unit if event.unit else event.app
+ logger.error(dict(event.relation.data))
+ nbi_host = event.relation.data[data_loc].get("host")
+ nbi_port = event.relation.data[data_loc].get("port")
+
+ if (
+ nbi_host
+ and nbi_port
+ and (self.state.nbi_host != nbi_host or self.state.nbi_port != nbi_port)
+ ):
+ self.state.nbi_host = nbi_host
+ self.state.nbi_port = nbi_port
+ self.on.configure_pod.emit()
+
+ def _on_nbi_relation_departed(self, event: EventBase) -> NoReturn:
+ """Clears data from nbi relation.
+
+ Args:
+ event (EventBase): NBI relation event.
+ """
+ self.state.nbi_host = None
+ self.state.nbi_port = None
+ self.on.configure_pod.emit()
+
+ def _missing_relations(self) -> str:
+ """Checks if there missing relations.
+
+ Returns:
+ str: string with missing relations
+ """
+ data_status = {
+ "nbi": self.state.nbi_host,