-
Notifications
You must be signed in to change notification settings - Fork 26
[DPE-7594] Sync up pg_hba changes and remove trigger #1007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
81f055d
9456b49
09ca1e5
1fa040e
daff032
8b0e099
efde10d
f6de4ae
aaada84
8f42b60
5fb6e65
2b421e0
a2baa62
2d3d97f
71e392d
d3edfba
5978879
e04b552
41da715
12ba662
a264193
d8c0a00
2226c0a
2efe251
cb5b320
85cdbe7
49574a8
5d5837b
f94f6f3
d9add35
fad3ed7
4f23dab
af03cd0
62834af
80ee8f8
78aef57
3a39640
f80167e
6844c7d
44615bc
cec1ec0
636c16f
d59b9ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
"""Charmed Kubernetes Operator for the PostgreSQL database.""" | ||
|
||
import datetime | ||
import itertools | ||
import json | ||
import logging | ||
|
@@ -13,6 +12,8 @@ | |
import shutil | ||
import sys | ||
import time | ||
from datetime import datetime | ||
from hashlib import shake_128 | ||
from pathlib import Path | ||
from typing import Literal, get_args | ||
from urllib.parse import urlparse | ||
|
@@ -221,9 +222,7 @@ def __init__(self, *args): | |
"/usr/bin/juju-exec" if self.model.juju_version.major > 2 else "/usr/bin/juju-run" | ||
) | ||
self._observer = AuthorisationRulesObserver(self, run_cmd) | ||
self.framework.observe( | ||
self.on.authorisation_rules_change, self._on_authorisation_rules_change | ||
) | ||
self.framework.observe(self.on.databases_change, self._on_databases_change) | ||
self.framework.observe(self.on.config_changed, self._on_config_changed) | ||
self.framework.observe(self.on.leader_elected, self._on_leader_elected) | ||
self.framework.observe(self.on[PEER].relation_changed, self._on_peer_relation_changed) | ||
|
@@ -281,9 +280,11 @@ def __init__(self, *args): | |
self, relation_name=TRACING_RELATION_NAME, protocols=[TRACING_PROTOCOL] | ||
) | ||
|
||
def _on_authorisation_rules_change(self, _): | ||
"""Handle authorisation rules change event.""" | ||
timestamp = datetime.datetime.now() | ||
def _on_databases_change(self, _): | ||
"""Handle databases change event.""" | ||
self.update_config() | ||
logger.debug("databases changed") | ||
timestamp = datetime.now() | ||
self._peers.data[self.unit].update({"pg_hba_needs_update_timestamp": str(timestamp)}) | ||
logger.debug(f"authorisation rules changed at {timestamp}") | ||
|
||
|
@@ -580,14 +581,14 @@ def _on_peer_relation_changed(self, event: HookEvent) -> None: # noqa: C901 | |
if self.unit.is_leader(): | ||
if self._initialize_cluster(event): | ||
logger.debug("Deferring on_peer_relation_changed: Leader initialized cluster") | ||
event.defer() | ||
else: | ||
logger.debug("_initialized_cluster failed on _peer_relation_changed") | ||
return | ||
else: | ||
logger.debug( | ||
"Deferring on_peer_relation_changed: Cluster must be initialized before members can join" | ||
"Early exit on_peer_relation_changed: Cluster must be initialized before members can join" | ||
) | ||
event.defer() | ||
return | ||
|
||
# If the leader is the one receiving the event, it adds the new members, | ||
|
@@ -2119,6 +2120,9 @@ def update_config(self, is_creating_backup: bool = False) -> bool: | |
self._restart_metrics_service() | ||
self._restart_ldap_sync_service() | ||
|
||
self.unit_peer_data.update({"user_hash": self.generate_user_hash}) | ||
if self.unit.is_leader(): | ||
self.app_peer_data.update({"user_hash": self.generate_user_hash}) | ||
return True | ||
|
||
def _validate_config_options(self) -> None: | ||
|
@@ -2316,8 +2320,30 @@ def relations_user_databases_map(self) -> dict: | |
user, current_host=self.is_connectivity_enabled | ||
) | ||
) | ||
|
||
# Copy relations users directly instead of waiting for them to be created | ||
for relation in self.model.relations[self.postgresql_client_relation.relation_name]: | ||
user = f"relation_id_{relation.id}" | ||
if user not in user_database_map and ( | ||
database := self.postgresql_client_relation.database_provides.fetch_relation_field( | ||
relation.id, "database" | ||
) | ||
): | ||
user_database_map[user] = database | ||
return user_database_map | ||
|
||
@property | ||
def generate_user_hash(self) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried to hash the |
||
"""Generate expected user and database hash.""" | ||
user_db_pairs = {} | ||
for relation in self.model.relations[self.postgresql_client_relation.relation_name]: | ||
if database := self.postgresql_client_relation.database_provides.fetch_relation_field( | ||
relation.id, "database" | ||
): | ||
user = f"relation_id_{relation.id}" | ||
user_db_pairs[user] = database | ||
return shake_128(str(user_db_pairs).encode()).hexdigest(16) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dict key order should be deterministic for units on the same platform. |
||
|
||
def override_patroni_on_failure_condition( | ||
self, new_condition: str, repeat_cause: str | None | ||
) -> bool: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also copy the legacy users as well here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be ideal, but I don't know if apps connecting through the legacy relation are using replicas. I think we can keep only the new relation users for now, so we keep the same code for
16/edge
.