Skip to content

Commit

Permalink
catch 403 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgameiroborges committed Jun 24, 2024
1 parent 7617d3b commit b76beb5
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,16 @@ def get_all_k8s_node_hostnames_and_ips(
self,
) -> Tuple[list[str], list[str]]:
"""Return all node hostnames and IPs registered in k8s."""
node = lightkube.Client().get(
lightkube.resources.core_v1.Node,
name=self._node_name,
namespace=self._namespace,
)
try:
node = lightkube.Client().get(
lightkube.resources.core_v1.Node,
name=self._node_name,
namespace=self._namespace,
)
except lightkube.ApiError as e:
if e.status.code == 403:
self.on_deployed_without_trust()
return
hostnames = []
ips = []
for a in node.status.addresses:
Expand Down Expand Up @@ -282,7 +287,10 @@ def patch_port(self, use_node_port: bool = False) -> None:
patch_type=lightkube.types.PatchType.MERGE,
)
logger.debug("Patched k8s service")
except lightkube.ApiError:
except lightkube.ApiError as e:
if e.status.code == 403:
self.on_deployed_without_trust()
return
logger.exception("Failed to patch k8s service")
raise

Expand Down Expand Up @@ -924,6 +932,12 @@ def client_relations(self) -> List[Relation]:
relations.append(relation)
return relations

def on_deployed_without_trust(self) -> None:
"""Blocks the application and returns a specific error message for deployments made without --trust."""
self.unit.status = BlockedStatus(
f"Insufficient permissions, try: `juju trust {self.app.name} --scope=cluster`"
)


if __name__ == "__main__":
main(PgBouncerK8sCharm)

0 comments on commit b76beb5

Please sign in to comment.