|
| 1 | +# CrateDB Kubernetes Operator |
| 2 | +# |
| 3 | +# Licensed to Crate.IO GmbH ("Crate") under one or more contributor |
| 4 | +# license agreements. See the NOTICE file distributed with this work for |
| 5 | +# additional information regarding copyright ownership. Crate licenses |
| 6 | +# this file to you under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. You may |
| 8 | +# obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | +# License for the specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# |
| 18 | +# However, if you have executed another commercial license agreement |
| 19 | +# with Crate these terms will supersede the license and you may use the |
| 20 | +# software solely pursuant to the terms of the relevant commercial agreement. |
| 21 | + |
| 22 | +import logging |
| 23 | + |
| 24 | +from kubernetes_asyncio.client import ApiException, CustomObjectsApi |
| 25 | + |
| 26 | +from crate.operator.config import config |
| 27 | +from crate.operator.constants import CloudProvider |
| 28 | +from crate.operator.utils.k8s_api_client import GlobalApiClient |
| 29 | + |
| 30 | + |
| 31 | +async def delete_cratedb( |
| 32 | + namespace: str, |
| 33 | + name: str, |
| 34 | + logger: logging.Logger, |
| 35 | +) -> None: |
| 36 | + """ |
| 37 | + Clean up cluster-scoped resources that cannot be garbage-collected |
| 38 | + via Kubernetes owner references. |
| 39 | +
|
| 40 | + Namespace-scoped resources (Services, Secrets, StatefulSets, etc.) |
| 41 | + are cleaned up automatically via owner references. However, |
| 42 | + SecurityContextConstraints are cluster-scoped and require explicit |
| 43 | + deletion here. |
| 44 | + """ |
| 45 | + await _delete_crate_scc(namespace, name, logger) |
| 46 | + |
| 47 | + |
| 48 | +async def _delete_crate_scc( |
| 49 | + namespace: str, |
| 50 | + name: str, |
| 51 | + logger: logging.Logger, |
| 52 | +) -> None: |
| 53 | + """ |
| 54 | + Delete the OpenShift SecurityContextConstraint for a CrateDB cluster. |
| 55 | + """ |
| 56 | + if config.CLOUD_PROVIDER != CloudProvider.OPENSHIFT: |
| 57 | + return |
| 58 | + |
| 59 | + scc_name = f"crate-anyuid-{namespace}-{name}" |
| 60 | + |
| 61 | + async with GlobalApiClient() as api_client: |
| 62 | + custom_api = CustomObjectsApi(api_client) |
| 63 | + try: |
| 64 | + await custom_api.delete_cluster_custom_object( |
| 65 | + group="security.openshift.io", |
| 66 | + version="v1", |
| 67 | + plural="securitycontextconstraints", |
| 68 | + name=scc_name, |
| 69 | + ) |
| 70 | + logger.info("Deleted SCC %s", scc_name) |
| 71 | + except ApiException as e: |
| 72 | + if e.status == 404: |
| 73 | + logger.info("SCC %s already deleted", scc_name) |
| 74 | + else: |
| 75 | + logger.warning( |
| 76 | + "Could not delete SCC %s (status=%s reason=%s) — " |
| 77 | + "it may need to be removed manually", |
| 78 | + scc_name, |
| 79 | + e.status, |
| 80 | + e.reason, |
| 81 | + ) |
0 commit comments