Delay prom_exporter user creation until password is available.

There are situations where the create_local_prometheus_exporter_user()
handler could be executed in a follower unit before the leader units
gets the chance to generate a password for the prom_exporter user and
store it in the leader databag, this has the consequence that the
prom_exporter user is created with the password 'None' (string
representation of the `None` value), this leads to the problem that the
mysql_exporter daemon can't connect to mysqld.

This patch checks that the password is available, if it isn't, then bail
out and let subsequent executions to re-evaluate this condition.

Closes-Bug: #2033306
Change-Id: I58fc489fd4cc4e06b11c293f63e73bd0ab18897f
This commit is contained in:
Felipe Reyes
2023-08-28 20:58:47 -04:00
parent 20581ffd46
commit 83bc7b36ae
2 changed files with 14 additions and 0 deletions

View File

@@ -15,6 +15,13 @@ SNAP_NAME = "mysqld-exporter"
def create_local_prometheus_exporter_user():
"""Create local exporter user in the DB."""
with charm.provide_charm_instance() as instance:
if not instance.prometheus_exporter_password:
ch_core.hookenv.log(
"Local prometheus exporter user was not created, because the "
"prometheus export password hasn't been set in the leader "
"databag.",
"WARNING")
return
if not instance.create_user(
instance.cluster_address,
instance.prometheus_exporter_user,

View File

@@ -146,6 +146,7 @@ class TestPrometheusMySQLExporterHandlers(test_utils.PatchHelper):
def test_create_local_prometheus_expoter_user(self):
self.midbc.create_user.return_value = True
self.midbc.prometheus_exporter_password = 'mypassword'
handlers.create_local_prometheus_exporter_user()
self.midbc.create_user.assert_called_once_with(
self.midbc.cluster_address,
@@ -156,6 +157,12 @@ class TestPrometheusMySQLExporterHandlers(test_utils.PatchHelper):
self.set_flag.assert_called_once_with(
"local.prom-exporter.user-created")
def test_create_local_prometheus_expoter_user_non_set_password(self):
self.midbc.create_user.return_value = True
self.midbc.prometheus_exporter_password = None
handlers.create_local_prometheus_exporter_user()
self.midbc.create_user.assert_not_called()
def test_snap_install_prometheus_exporter(self):
handlers.snap_install_prometheus_exporter()