Skip to content

Commit

Permalink
Fixed deleting a cluster while restoring/cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
juanpardo committed Jul 19, 2023
1 parent 5d523c2 commit 44e47c7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog
Unreleased
----------

* Fixed a bug that lead to the namespace not being deleted after deleting a cluster
that had a snapshot restore/clone operation in progress.

2.30.1 (2023-07-06)
-------------------

Expand Down
12 changes: 11 additions & 1 deletion crate/operator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
update_user_password_secret,
)
from crate.operator.kube_auth import login_via_kubernetes_asyncio
from crate.operator.operations import update_sql_exporter_configmap
from crate.operator.operations import (
is_namespace_terminating,
update_sql_exporter_configmap,
)
from crate.operator.restore_backup import is_valid_snapshot
from crate.operator.utils import crate
from crate.operator.webhooks import webhook_client
Expand Down Expand Up @@ -198,6 +201,13 @@ async def cluster_restore(
"""
Handles field changes which trigger restoring data from a backup.
"""

# Ensure the namespace is not terminating. Otherwise end with a permanent error.
if await is_namespace_terminating(namespace):
raise kopf.PermanentError(
"The namespace for the target operation is terminating"
)

await restore_backup(namespace, name, diff, new, patch, status, started, logger)


Expand Down
21 changes: 21 additions & 0 deletions crate/operator/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
V1CronJobList,
V1JobList,
V1JobStatus,
V1Namespace,
V1PersistentVolumeClaimList,
V1PodList,
V1Service,
Expand Down Expand Up @@ -240,6 +241,26 @@ async def get_pods_in_deployment(
return [{"uid": p.metadata.uid, "name": p.metadata.name} for p in all_pods.items]


async def get_namespace_resource(namespace_name: str) -> V1Namespace:
"""
Return the CrateDB custom resource.
:param namespace_ am e: The Kubernetes namespace name to look up.
"""
async with ApiClient() as api_client:
core = CoreV1Api(api_client)
namespaces = await core.list_namespace()
for ns in namespaces.items:
if ns.metadata.name == namespace_name:
return ns
return None


async def is_namespace_terminating(namespace_name: str) -> bool:
namespace_obj = await get_namespace_resource(namespace_name)
return namespace_obj and namespace_obj.status.phase == "Terminating"


async def get_cratedb_resource(namespace: str, name: str) -> dict:
"""
Return the CrateDB custom resource.
Expand Down

0 comments on commit 44e47c7

Please sign in to comment.