Skip to content

Commit c9c6029

Browse files
authored
fix: react on ingress changed (#125)
* fix: react on ingress changed * override restart * use restart() in all events * fix: flip order - setup planner relations after restart call
1 parent d8c12bc commit c9c6029

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

charms/planner-operator/src/charm.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import ops
1414
import paas_charm.go
15+
1516
from planner import Flavor, PlannerClient, PlannerError
1617

1718
logger = logging.getLogger(__name__)
@@ -138,17 +139,32 @@ def _on_disable_flavor_action(self, event: ops.ActionEvent) -> None:
138139
event.fail(error_msg)
139140
logger.error("Failed to disable flavor %s: %s", flavor, error_msg)
140141

142+
def restart(self, rerun_migrations: bool = False) -> None:
143+
"""Restart the workload and reconcile planner relation data.
144+
145+
Overrides the parent to sync relation endpoints holistically
146+
on every restart (ingress changes, config changes, etc.).
147+
148+
Args:
149+
rerun_migrations: whether it is necessary to run the migrations again.
150+
"""
151+
if not self.is_ready():
152+
return
153+
super().restart(rerun_migrations=rerun_migrations)
154+
self._setup_planner_relations()
155+
141156
def _on_manager_relation_changed(self, _: ops.RelationChangedEvent) -> None:
142157
"""Handle changes to the github-runner-manager relation."""
143-
self._setup()
158+
self.restart()
144159

145160
def _on_planner_relation_broken(self, _: ops.RelationBrokenEvent) -> None:
146161
"""Handle planner relation broken events."""
147-
self._setup()
162+
self.restart()
148163

149-
def _setup(self) -> None:
150-
"""Set up the planner application."""
151-
self.unit.status = ops.MaintenanceStatus("Setting up planner application")
164+
def _setup_planner_relations(self) -> None:
165+
"""Reconcile planner auth tokens, relation endpoints, and flavors."""
166+
if not self.unit.is_leader():
167+
return
152168
try:
153169
client = self._create_planner_client()
154170
except ConfigError:
@@ -158,15 +174,6 @@ def _setup(self) -> None:
158174
)
159175
return
160176

161-
self.unit.status = ops.MaintenanceStatus("Setup planner integrations")
162-
self._setup_planner_relation(client=client)
163-
self.unit.status = ops.ActiveStatus()
164-
165-
def _setup_planner_relation(self, client: PlannerClient) -> None:
166-
"""Setup the planner relations if this unit is the leader."""
167-
if not self.unit.is_leader():
168-
return
169-
170177
all_token_names = client.list_auth_token_names()
171178
auth_token_names = [
172179
token for token in all_token_names if self._check_name_fit_auth_token(token)
@@ -190,10 +197,11 @@ def _setup_planner_relation(self, client: PlannerClient) -> None:
190197
auth_token_name=auth_token_name,
191198
)
192199
except (PlannerError, JujuError):
193-
logger.exception(
194-
"Failed to reconcile relation %s, skipping", relation.id
195-
)
200+
logger.exception("Failed to reconcile relation %s, skipping", relation.id)
196201
reconciled.add(auth_token_name)
202+
# Always sync the endpoint so it reflects the current _base_url
203+
# (e.g. after ingress is added, removed, or changed).
204+
relation.data[self.app]["endpoint"] = self._base_url
197205
flavor_config = RelationFlavorConfig.from_relation_data(relation.data[relation.app])
198206
if flavor_config is not None:
199207
wanted_flavors[flavor_config.name] = flavor_config
@@ -226,9 +234,6 @@ def _create_relation_credentials(
226234
logger.exception("Failed to grant secret for relation %d", relation.id)
227235
raise JujuError(f"Failed to grant secret for relation {relation.id}") from err
228236

229-
# The _base_url is set up by the parent class paas_charm.go.Charm.
230-
# It points to ingress URL if there is one, otherwise to the K8S service.
231-
relation.data[self.app]["endpoint"] = self._base_url
232237
relation.data[self.app]["token"] = secret.id
233238

234239
def _delete_orphaned_credentials(self, client: PlannerClient, token_name: str) -> None:

0 commit comments

Comments
 (0)