Skip to content

Commit ed82245

Browse files
committed
Centralize failed subhandler check in StateBasedSubHandler
1 parent b6cc48a commit ed82245

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

crate/operator/restore_backup.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import abc
2323
import asyncio
24-
import json
2524
import logging
2625
import re
2726
from contextlib import asynccontextmanager
@@ -44,7 +43,6 @@
4443
from crate.operator.constants import (
4544
API_GROUP,
4645
GC_USERNAME,
47-
KOPF_STATE_STORE_PREFIX,
4846
RESOURCE_CRATEDB,
4947
SYSTEM_USERNAME,
5048
BackupStorageProvider,
@@ -925,21 +923,9 @@ async def handle( # type: ignore
925923
"""
926924

927925
# check whether any dependent handlers have failed.
928-
failed_handlers = []
929926
annotations = kwargs["annotations"]
930927
logger.info("RestoreInternalTablesSubHandler checking handler status...")
931-
for dep in self.depends_on:
932-
key = kopf.AnnotationsProgressStorage(
933-
v1=False, prefix=KOPF_STATE_STORE_PREFIX
934-
).make_v2_key(dep)
935-
936-
status_str = annotations.get(key)
937-
logger.info(f"[{dep}] annotation raw value: {status_str}")
938-
if status_str:
939-
parsed = json.loads(status_str)
940-
if not parsed.get("success"):
941-
failed_handlers.append(dep)
942-
logger.info(f"Failed handlers: {failed_handlers}")
928+
failed_handlers = self._get_failed_dependent_handlers(annotations, logger)
943929

944930
async with GlobalApiClient() as api_client:
945931
core = CoreV1Api(api_client)

crate/operator/rollback.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,7 @@ async def handle(
4242

4343
logger.info("Evaluating rollback conditions...")
4444

45-
failed_handlers = []
46-
for dep in self.depends_on:
47-
key = kopf.AnnotationsProgressStorage(
48-
v1=False, prefix=KOPF_STATE_STORE_PREFIX
49-
).make_v2_key(dep)
50-
51-
status_str = annotations.get(key)
52-
logger.info(f"[{dep}] annotation raw value: {status_str}")
53-
if status_str:
54-
parsed = json.loads(status_str)
55-
if not parsed.get("success"):
56-
failed_handlers.append(dep)
45+
failed_handlers = self._get_failed_dependent_handlers(annotations, logger)
5746

5847
if failed_handlers:
5948
logger.info(f"Rollback triggered due to failed handlers: {failed_handlers}")

crate/operator/utils/kopf.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import logging
2626
from datetime import datetime, timezone
2727
from functools import wraps
28-
from typing import Any, Callable, Optional, TypedDict
28+
from typing import Any, Callable, List, Optional, TypedDict
2929

3030
import kopf
3131

@@ -271,6 +271,41 @@ def _init_handler_starttime(
271271
"ref": self.ref,
272272
}
273273

274+
def _get_failed_dependent_handlers(
275+
self, annotations: dict, logger: logging.Logger
276+
) -> List[str]:
277+
"""
278+
Determine which dependent handlers have failed based on kopf annotations.
279+
280+
This inspects the progress annotations stored by kopf for each dependency in
281+
``self.depends_on`` and returns a list of handlers that did not succeed.
282+
283+
Used by subhandlers that need to decide whether to perform cleanup or rollback
284+
actions depending on the outcome of previous handlers.
285+
"""
286+
failed_handlers: List[str] = []
287+
logger.info("Checking dependent handler status...")
288+
289+
for dep in self.depends_on:
290+
key = kopf.AnnotationsProgressStorage(
291+
v1=False, prefix=KOPF_STATE_STORE_PREFIX
292+
).make_v2_key(dep)
293+
294+
status_str = annotations.get(key)
295+
logger.info(f"[{dep}] annotation raw value: {status_str}")
296+
if not status_str:
297+
continue
298+
299+
try:
300+
parsed = json.loads(status_str)
301+
if not parsed.get("success"):
302+
failed_handlers.append(dep)
303+
except json.JSONDecodeError as e:
304+
logger.warning(f"Failed to parse status annotation for {dep}: {e}")
305+
306+
logger.info(f"Failed handlers detected: {failed_handlers}")
307+
return failed_handlers
308+
274309

275310
async def send_webhook_notification(
276311
namespace: str,

0 commit comments

Comments
 (0)