From 1e3132741855e819f0739da922377538423328ec Mon Sep 17 00:00:00 2001 From: Gallardot Date: Fri, 21 Jul 2023 10:28:20 +0800 Subject: [PATCH] [Improvement][K8S] Disable delete namespaces in the K8S cluster (#14597) * [Improvement][K8S] Disable all namespaces operations in the K8S cluster Signed-off-by: Gallardot --------- Signed-off-by: Gallardot Co-authored-by: Eric Gao --- .../api/k8s/K8sClientService.java | 20 +++++++------------ .../service/impl/K8SNamespaceServiceImpl.java | 11 +--------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/k8s/K8sClientService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/k8s/K8sClientService.java index 393f440ee4..76b875ad77 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/k8s/K8sClientService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/k8s/K8sClientService.java @@ -44,7 +44,11 @@ public class K8sClientService { public ResourceQuota upsertNamespaceAndResourceToK8s(K8sNamespace k8sNamespace, String yamlStr) throws RemotingException { - upsertNamespaceToK8s(k8sNamespace.getNamespace(), k8sNamespace.getClusterCode()); + if (!checkNamespaceToK8s(k8sNamespace.getNamespace(), k8sNamespace.getClusterCode())) { + throw new RemotingException(String.format( + "namespace %s does not exist in k8s cluster, please create namespace in k8s cluster first", + k8sNamespace.getNamespace())); + } return upsertNamespacedResourceToK8s(k8sNamespace, yamlStr); } @@ -101,19 +105,9 @@ public class K8sClientService { return list; } - private Namespace upsertNamespaceToK8s(String name, Long clusterCode) throws RemotingException { + private boolean checkNamespaceToK8s(String name, Long clusterCode) throws RemotingException { Optional result = getNamespaceFromK8s(name, clusterCode); - // if not exist create - if (!result.isPresent()) { - KubernetesClient client = k8sManager.getK8sClient(clusterCode); - Namespace body = new Namespace(); - ObjectMeta meta = new ObjectMeta(); - meta.setNamespace(name); - meta.setName(name); - body.setMetadata(meta); - return client.namespaces().create(body); - } - return result.get(); + return result.isPresent(); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java index 194feb9d77..3d6050e62c 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java @@ -29,7 +29,6 @@ import org.apache.dolphinscheduler.dao.entity.K8sNamespace; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.mapper.ClusterMapper; import org.apache.dolphinscheduler.dao.mapper.K8sNamespaceMapper; -import org.apache.dolphinscheduler.remote.exceptions.RemotingException; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -322,15 +321,7 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames putMsg(result, Status.K8S_NAMESPACE_NOT_EXIST, id); return result; } - if (!Constants.K8S_LOCAL_TEST_CLUSTER_CODE.equals(k8sNamespaceObj.getClusterCode())) { - try { - k8sClientService.deleteNamespaceToK8s(k8sNamespaceObj.getNamespace(), k8sNamespaceObj.getClusterCode()); - } catch (RemotingException e) { - log.error("Namespace delete in k8s error, namespaceId:{}.", id, e); - putMsg(result, Status.K8S_CLIENT_OPS_ERROR, id); - return result; - } - } + k8sNamespaceMapper.deleteById(id); log.info("K8s namespace delete complete, namespace:{}.", k8sNamespaceObj.getNamespace()); putMsg(result, Status.SUCCESS);