canonical / postgresql-k8s-operator

A Charmed Operator for running PostgreSQL on Kubernetes
https://charmhub.io/postgresql-k8s
Apache License 2.0
10 stars 20 forks source link

Enhance the error message if charm was deployed without `--trust` flag #431

Closed theoctober19th closed 6 months ago

theoctober19th commented 7 months ago

Steps to reproduce

  1. Deploy the charm without the --trust flag as:
    juju deploy postgresql-k8s --channel 14/stable

Expected behavior

A helpful error message, something similar to the following would be a lot helpful:

Application deployed without --trust flag, which is required for this operator.

It looks like a similar implementation is in use in mysql-router-k8s-operator already: https://github.com/canonical/mysql-router-k8s-operator/blob/105a6f6bbadaa2a5b78f8c29bab173892b5bab87/src/kubernetes_upgrade.py#L35

Actual behavior

The charm is stuck in the following state:

postgresql-k8s/0*  error     idle   10.1.209.10         hook failed: "leader-elected"

which is a bit difficult for a layman user to debug.

Versions

Operating system: Ubuntu 22.04.4 LTS

Juju CLI: 3.4.0-genericlinux-amd64

Juju agent: Model Controller Cloud/Region Version SLA Timestamp new k8s microk8s/localhost 3.4.0 unsupported 08:55:30Z

Charm revision: 193

microk8s: MicroK8s v1.29.2 revision 6529

Log output

Juju debug log:

unit-postgresql-k8s-0: 08:20:39 INFO juju.worker.uniter awaiting error resolution for "leader-elected" hook
unit-postgresql-k8s-0: 08:21:24 INFO juju.worker.uniter awaiting error resolution for "leader-elected" hook
unit-postgresql-k8s-0: 08:25:39 INFO juju.worker.uniter awaiting error resolution for "leader-elected" hook
unit-postgresql-k8s-0: 08:25:40 ERROR unit.postgresql-k8s/0.juju-log Uncaught exception while in charm code:
Traceback (most recent call last):
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/venv/lightkube/core/generic_client.py", line 188, in raise_for_status
    resp.raise_for_status()
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/venv/httpx/_models.py", line 759, in raise_for_status
    raise HTTPStatusError(message, request=request, response=self)
httpx.HTTPStatusError: Client error '403 Forbidden' for url 'https://10.152.183.1/api/v1/namespaces/postgres/services/patroni-postgresql-k8s'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/./src/charm.py", line 1556, in <module>
    main(PostgresqlOperatorCharm, use_juju_for_storage=True)
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/venv/ops/main.py", line 436, in main
    _emit_charm_event(charm, dispatcher.event_name)
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/venv/ops/main.py", line 144, in _emit_charm_event
    event_to_emit.emit(*args, **kwargs)
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/venv/ops/framework.py", line 351, in emit
    framework._emit(event)
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/venv/ops/framework.py", line 853, in _emit
    self._reemit(event_path)
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/venv/ops/framework.py", line 942, in _reemit
    custom_handler(event)
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/./src/charm.py", line 604, in _on_leader_elected
    self._cleanup_old_cluster_resources()
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/./src/charm.py", line 863, in _cleanup_old_cluster_resources
    raise e
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/./src/charm.py", line 854, in _cleanup_old_cluster_resources
    client.delete(
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/venv/lightkube/core/client.py", line 86, in delete
    return self._client.request("delete", res=res, name=name, namespace=namespace, params={
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/venv/lightkube/core/generic_client.py", line 245, in request
    return self.handle_response(method, resp, br)
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/venv/lightkube/core/generic_client.py", line 196, in handle_response
    self.raise_for_status(resp)
  File "/var/lib/juju/agents/unit-postgresql-k8s-0/charm/venv/lightkube/core/generic_client.py", line 190, in raise_for_status
    raise transform_exception(e)
lightkube.core.exceptions.ApiError: services "patroni-postgresql-k8s" is forbidden: User "system:serviceaccount:postgres:postgresql-k8s" cannot delete resource "services" in API group "" in the namespace "postgres"
unit-postgresql-k8s-0: 08:25:41 ERROR juju.worker.uniter.operation hook "leader-elected" (via hook dispatching script: dispatch) failed: exit status 1
unit-postgresql-k8s-0: 08:25:41 INFO juju.worker.uniter awaiting error resolution for "leader-elected" hook

Additional Notes

It would also be awesome if we could update the command in the header section of CharmHub to include the --trust flag.

github-actions[bot] commented 7 months ago

https://warthogs.atlassian.net/browse/DPE-3885

lucasgameiroborges commented 6 months ago

Hi @theoctober19th !

This issue got addressed in #440, please feel free to re-open/open an new ticket if you find any other unexpected behavior. Thank you!