Skip to content

Commit 066792d

Browse files
authored
Retry getting secrets from juju3 (#224)
1 parent f28418f commit 066792d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/charm.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
)
4444
from ops.pebble import ChangeError, Layer, PathError, ProtocolError, ServiceStatus
4545
from requests import ConnectionError
46-
from tenacity import RetryError
46+
from tenacity import RetryError, Retrying, stop_after_attempt, wait_fixed
4747

4848
from backups import PostgreSQLBackups
4949
from constants import (
@@ -192,12 +192,16 @@ def _juju_secrets_get(self, scope: str) -> Optional[bool]:
192192
return
193193

194194
if SECRET_CACHE_LABEL not in self.secrets[scope]:
195-
try:
196-
# NOTE: Secret contents are not yet available!
197-
secret = self.model.get_secret(id=peer_data[SECRET_INTERNAL_LABEL])
198-
except SecretNotFoundError as e:
199-
logging.debug(f"No secret found for ID {peer_data[SECRET_INTERNAL_LABEL]}, {e}")
200-
return
195+
for attempt in Retrying(stop=stop_after_attempt(3), wait=wait_fixed(1), reraise=True):
196+
with attempt:
197+
try:
198+
# NOTE: Secret contents are not yet available!
199+
secret = self.model.get_secret(id=peer_data[SECRET_INTERNAL_LABEL])
200+
except SecretNotFoundError as e:
201+
logging.debug(
202+
f"No secret found for ID {peer_data[SECRET_INTERNAL_LABEL]}, {e}"
203+
)
204+
return
201205

202206
logging.debug(f"Secret {peer_data[SECRET_INTERNAL_LABEL]} downloaded")
203207

@@ -242,7 +246,7 @@ def get_secret(self, scope: str, key: str) -> Optional[str]:
242246
if juju_version.has_secrets:
243247
return self._juju_secret_get_key(scope, key)
244248

245-
def _juju_secret_set(self, scope: str, key: str, value: str) -> str:
249+
def _juju_secret_set(self, scope: str, key: str, value: str) -> Optional[str]:
246250
"""Helper function setting Juju secret."""
247251
if scope == UNIT_SCOPE:
248252
peer_data = self.unit_peer_data
@@ -270,6 +274,7 @@ def _juju_secret_set(self, scope: str, key: str, value: str) -> str:
270274
f"Error in attempt to set {scope}:{key}. "
271275
f"Existing keys were: {list(secret_cache.keys())}. {error}"
272276
)
277+
return
273278
logging.debug(f"Secret {scope}:{key} was {key} set")
274279

275280
# We need to create a brand-new secret for this scope

0 commit comments

Comments
 (0)