Skip to content

Commit

Permalink
trigger _reconcile_lb() on every hook
Browse files Browse the repository at this point in the history
  • Loading branch information
IbraAoad committed Dec 11, 2024
1 parent 3422910 commit d654944
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,14 @@ def _basic_auth_user(self) -> Optional[str]:

@property
def _loadbalancer_annotations(self) -> Optional[Dict[str, str]]:
"""A comma-separated list of annotations to apply to the LoadBalancer service.
"""Parses and returns annotations to apply to the LoadBalancer service.
The input string is expected to be in the format: "key1=value1,key2=value2,key3=value3".
The annotations are expected as a string in the configuration,
formatted as: "key1=value1,key2=value2,key3=value3". This string is
parsed into a dictionary where each key-value pair corresponds to an annotation.
Returns:
Optional[Dict[str, str]]: A dictionary of annotations if provided in the Juju config and valid, otherwise None.
"""
lb_annotations = cast(Optional[str], self.config.get("loadbalancer_annotations", None))
return parse_annotations(lb_annotations)
Expand Down Expand Up @@ -404,13 +409,9 @@ def _reconcile_lb(self):
"""Reconcile the LoadBalancer's state."""
klm = self._get_lb_resource_manager()

if not self._annotations_valid:
klm.delete()
return

resources_list = []
resource_to_append = self._construct_lb()
resources_list.append(resource_to_append)
if self._annotations_valid:
resources_list.append(self._construct_lb())
klm.reconcile(resources_list)

@property
Expand Down Expand Up @@ -462,6 +463,7 @@ def _on_recv_ca_cert_available(self, event: CertificateTransferAvailableEvent):
if not self.container.can_connect():
return
self._update_received_ca_certs(event)
self._reconcile_lb()

def _update_received_ca_certs(self, event: Optional[CertificateTransferAvailableEvent] = None):
"""Push the cert attached to the event, if it is given; otherwise push all certs.
Expand All @@ -488,6 +490,7 @@ def _update_received_ca_certs(self, event: Optional[CertificateTransferAvailable
def _on_recv_ca_cert_removed(self, event: CertificateTransferRemovedEvent):
# Assuming only one cert per relation (this is in line with the original lib design).
self.traefik.remove_cas([event.relation_id])
self._reconcile_lb()

def _is_tls_enabled(self) -> bool:
"""Return True if TLS is enabled."""
Expand Down Expand Up @@ -652,7 +655,6 @@ def _config_hash(self) -> int:
(
self._external_host,
self.config["routing_mode"],
self.config.get("loadbalancer_annotations", None),
self._is_forward_auth_enabled,
self._basic_auth_user,
self._is_tls_enabled(),
Expand All @@ -667,6 +669,7 @@ def _update_config_if_changed(self):
# that we're processing a config-changed event, doesn't necessarily mean that our config has changed (duh!)
# If the config hash has changed since we last calculated it, we need to
# recompute our state from scratch, based on all data sent over the relations and all configs
self._reconcile_lb()
new_config_hash = self._config_hash
if self._stored.config_hash != new_config_hash:
self._stored.config_hash = new_config_hash
Expand Down Expand Up @@ -828,6 +831,7 @@ def _handle_ingress_data_removed(self, event: RelationEvent):
# self._process_status_and_configurations(). For this reason, the static config in
# traefik.STATIC_CONFIG_PATH will be updated only on update-status.
# https://github.com/canonical/operator/issues/888
self._reconcile_lb()

def _handle_traefik_route_ready(self, event: TraefikRouteRequirerReadyEvent):
"""A traefik_route charm has published some ingress data."""
Expand Down Expand Up @@ -859,7 +863,7 @@ def _handle_traefik_route_ready(self, event: TraefikRouteRequirerReadyEvent):
"Failed to merge traefik-route static configs. " "Check logs for details."
)
return

self._reconcile_lb()
self.unit.status = ActiveStatus(f"Serving at {self._external_host}")

def _process_ingress_relation(self, relation: Relation):
Expand Down

0 comments on commit d654944

Please sign in to comment.