canonical / canonical/postgresql-operator

postgresql fails to install with IPv6

Open
#1,928 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
20
Forks
36
Avg merge
4d 15h
Merged PRs (30d)
30

Description

## Steps to reproduce

1. Install postgresql (`--channel 16/stable`) on a cloud with ipv6 support

## Expected behavior
PostgreSQL becomes active/idle

## Actual behavior

The charm fails on leader-elect:
```
postgresql/1* error idle 14 fd42:a615:ea50:2a68:216:3eff:fef1:6b2 hook failed: "leader-elected"
```
This appears to be due to the charms inability to grab a port when an IPv6 address is preset:
```
unit-postgresql-1: 23:59:06 ERROR unit.postgresql/1.juju-log root:Uncaught exception while in charm code:
Traceback (most recent call last):
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/httpx/_urlparse.py", line 409, in normalize_port
port_as_int = int(port)
^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'a615:ea50:2a68:216:3eff:fef1:6b2:8008'
......
^^^^^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/httpx/_urlparse.py", line 321, in urlparse
parsed_port: int | None = normalize_port(port, scheme)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/httpx/_urlparse.py", line 411, in normalize_port
raise InvalidURL(f"Invalid port: {port!r}")
httpx.InvalidURL: Invalid port: 'a615:ea50:2a68:216:3eff:fef1:6b2:8008'
```

## Versions

Operating system: ubuntu@24.04

Juju CLI: `3.6.27-genericlinux-amd64`

Juju agent: `3.6.27`

Charm revision: `1158`

LXD: `5.21.7 LTS`

## Log output

Juju debug log:
```
unit-postgresql-1: 23:59:06 ERROR unit.postgresql/1.juju-log root:Uncaught exception while in charm code:
Traceback (most recent call last):
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/httpx/_urlparse.py", line 409, in normalize_port
port_as_int = int(port)
^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'a615:ea50:2a68:216:3eff:fef1:6b2:8008'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/lib/juju/agents/unit-postgresql-1/charm/src/charm.py", line 3294, in
main(PostgresqlOperatorCharm)
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/ops/__init__.py", line 360, in __call__
return _main.main(charm_class=charm_class, use_juju_for_storage=use_juju_for_storage)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/ops/_main.py", line 507, in main
manager.run()
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/ops/_main.py", line 491, in run
self._emit()
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/ops/_main.py", line 426, in _emit
self._emit_charm_event(self.dispatcher.event_name)
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/ops/_main.py", line 470, in _emit_charm_event
event_to_emit.emit(*args, **kwargs)
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/ops/framework.py", line 337, in emit
framework._emit(event)
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/ops/framework.py", line 910, in _emit
self._reemit(event_path)
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/ops/framework.py", line 1028, in _reemit
custom_handler(event)
File "/var/lib/juju/agents/unit-postgresql-1/charm/src/charm.py", line 1694, in _on_leader_elected
self.update_config()
File "/var/lib/juju/agents/unit-postgresql-1/charm/src/charm.py", line 2857, in update_config
pg_parameters = self._build_postgresql_parameters()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/src/charm.py", line 2821, in _build_postgresql_parameters
pg_parameters = self.postgresql.build_postgresql_parameters(
^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/functools.py", line 995, in __get__
val = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/src/charm.py", line 869, in postgresql
primary_host=self.primary_endpoint,
^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/functools.py", line 995, in __get__
val = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/src/charm.py", line 885, in primary_endpoint
primary = self._patroni.get_primary() or self._patroni.get_standby_leader()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/src/cluster.py", line 340, in get_primary
cluster_status = self.cluster_status(alternative_endpoints)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/src/cluster.py", line 277, in cluster_status
if response := parallel_patroni_get_request(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/single_kernel_postgresql/utils/__init__.py", line 197, in parallel_patroni_get_request
return run(_async_get_request(uri, endpoints, cafile, auth, verify))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/single_kernel_postgresql/utils/__init__.py", line 182, in _async_get_request
if result := await task:
^^^^^^^^^^
File "/usr/lib/python3.12/asyncio/tasks.py", line 631, in _wait_for_one
return f.result() # May raise f.exception().
^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/single_kernel_postgresql/utils/__init__.py", line 169, in _httpx_get_request
return (await client.get(url)).raise_for_status().json()
^^^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/httpx/_client.py", line 1768, in get
return await self.request(
^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/httpx/_client.py", line 1527, in request
request = self.build_request(
^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/httpx/_client.py", line 366, in build_request
url = self._merge_url(url)
^^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/httpx/_client.py", line 396, in _merge_url
merge_url = URL(url)
^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/httpx/_urls.py", line 117, in __init__
self._uri_reference = urlparse(url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/httpx/_urlparse.py", line 321, in urlparse
parsed_port: int | None = normalize_port(port, scheme)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/lib/juju/agents/unit-postgresql-1/charm/venv/lib/python3.12/site-packages/httpx/_urlparse.py", line 411, in normalize_port
raise InvalidURL(f"Invalid port: {port!r}")
httpx.InvalidURL: Invalid port: 'a615:ea50:2a68:216:3eff:fef1:6b2:8008'
unit-postgresql-1: 23:59:07 ERROR juju.worker.uniter.operation hook "leader-elected" (via hook dispatching script: dispatch) failed: exit status 1
```

## Additional context

Contributor guide

Open the contributing guide

Research direction

Start in src/charm.py at _on_leader_elected, update_config, and _build_postgresql_parameters, then follow primary_endpoint into src/cluster.py and cluster_status. Reproduce on an IPv6-enabled cloud and inspect the endpoint passed to parallel_patroni_get_request. Done means the leader-elected hook completes and PostgreSQL reaches active/idle without the httpx.InvalidURL error.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.