From 19c94fef21ce4119d930012d03e17a64008f26a3 Mon Sep 17 00:00:00 2001 From: Sergio Ostapowicz Date: Tue, 9 May 2023 16:27:11 -0300 Subject: [PATCH] feat: Added support for Consul wan address Added required variables to enable a secondary IP Address for Standby-Leader replication purposes on Multi-Ha cluster setup, resolving a different IP address when querying the Consul DNS service rather than resolving the same IP address from the Main DC cluster. We are achieving the target using the available Consul tagged_addresses feature for WAN and LAN setup. --- patroni/dcs/consul.py | 9 ++++++++- patroni/ha.py | 1 + patroni/postgresql/config.py | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/patroni/dcs/consul.py b/patroni/dcs/consul.py index 5a2e5700a..9cbdfc8e1 100644 --- a/patroni/dcs/consul.py +++ b/patroni/dcs/consul.py @@ -491,6 +491,7 @@ def _update_service(self, data: Dict[str, Any]) -> Optional[bool]: api_parts = api_parts._replace(path='/{0}'.format(role)) conn_url: str = data['conn_url'] conn_parts = urlparse(conn_url) + conn_wan = data['addresses'] check = base.Check.http(api_parts.geturl(), self._service_check_interval, deregister='{0}s'.format(self._client.http.ttl * 10)) if self._service_check_tls_server_name is not None: @@ -507,6 +508,12 @@ def _update_service(self, data: Dict[str, Any]) -> Optional[bool]: params = { 'service_id': '{0}/{1}'.format(self._scope, self._name), 'address': conn_parts.hostname, + 'tagged_addresses': { + 'wan_ipv4' : { + 'address' : conn_wan, + 'port' : conn_parts.port + } + }, 'port': conn_parts.port, 'check': check, 'tags': tags, @@ -529,7 +536,7 @@ def _update_service(self, data: Dict[str, Any]) -> Optional[bool]: def update_service(self, old_data: Dict[str, Any], new_data: Dict[str, Any], force: bool = False) -> Optional[bool]: update = False - for key in ['role', 'api_url', 'conn_url', 'state']: + for key in ['role', 'api_url', 'conn_url', 'addresses', 'state']: if key not in new_data: logger.warning('Could not register service: not enough params in member data') return diff --git a/patroni/ha.py b/patroni/ha.py index b994ad9ab..f773d4199 100644 --- a/patroni/ha.py +++ b/patroni/ha.py @@ -285,6 +285,7 @@ def touch_member(self) -> bool: with self._member_state_lock: data: Dict[str, Any] = { 'conn_url': self.state_handler.connection_string, + 'addresses': self.state_handler.multi_connection, 'api_url': self.patroni.api.connection_string, 'state': self.state_handler.state, 'role': self.state_handler.role, diff --git a/patroni/postgresql/config.py b/patroni/postgresql/config.py index 4d6bacfba..8fc17a9ae 100644 --- a/patroni/postgresql/config.py +++ b/patroni/postgresql/config.py @@ -942,6 +942,7 @@ def resolve_connection_addresses(self) -> None: if self._config.get('use_unix_socket_repl') else tcp_local_address self._postgresql.connection_string = uri('postgres', netloc, self._postgresql.database) + self._postgresql.multi_connection = self._config.get('connect_address_wan') self._postgresql.set_connection_kwargs(self.local_connect_kwargs) def _get_pg_settings(