Skip to content

Commit

Permalink
use retry logic for kcpt recreation
Browse files Browse the repository at this point in the history
  • Loading branch information
okozachenko1203 committed Sep 10, 2024
1 parent f0ee5a2 commit 4d17fae
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions magnum_cluster_api/hacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""

import pykube
from tenacity import Retrying, retry_if_result, stop_after_delay, wait_fixed

from magnum_cluster_api import objects, utils

Expand All @@ -38,17 +39,31 @@ def set_certificate_expiry_days(
rollout_before = kcpt.obj["spec"]["template"]["spec"].get(
"rolloutBefore", {}
)
if "certificatesExpiryDays" not in rollout_before:
kcpt.obj["spec"]["template"]["spec"].setdefault("rolloutBefore", {})
kcpt.obj["spec"]["template"]["spec"]["rolloutBefore"][
"certificatesExpiryDays"
] = 21

# NOTE(mnaser): Since the KubeadmControlPlaneTemplate is immutable, we need to
# delete the object and re-create it.
kcpt.delete()
del kcpt.obj["metadata"]["uid"]

utils.kube_apply_patch(kcpt)
if "certificatesExpiryDays" in rollout_before:
continue

# NOTE(mnaser): Since the KubeadmControlPlaneTemplate is immutable, we need to
# delete the object and re-create it.
kcpt.delete()

del kcpt.obj["metadata"]["uid"]
kcpt.obj["spec"]["template"]["spec"].setdefault("rolloutBefore", {})
kcpt.obj["spec"]["template"]["spec"]["rolloutBefore"][
"certificatesExpiryDays"
] = 21

# Use tenacity to wait for kcpt to be created
for attempt in Retrying(
retry=retry_if_result(lambda result: result is None),
stop=stop_after_delay(10),
wait=wait_fixed(1),
):
with attempt:
utils.kube_apply_patch(kcpt)
new_kcpt = objects.KubeadmControlPlaneTemplate.objects(
api, namespace="magnum-system"
).get(name=kcpt.obj["metadata"]["name"])
if not attempt.retry_state.outcome.failed:
attempt.retry_state.set_result(new_kcpt)

CERTIFICATE_EXPIRY_DAYS_FIX_APPLIED = True

0 comments on commit 4d17fae

Please sign in to comment.