Procházet zdrojové kódy

[Improvement][K8S] Disable delete namespaces in the K8S cluster (#14597)

* [Improvement][K8S] Disable all namespaces operations in the K8S cluster

Signed-off-by: Gallardot <gallardot@apache.org>

---------

Signed-off-by: Gallardot <gallardot@apache.org>
Co-authored-by: Eric Gao <ericgao.apache@gmail.com>
Gallardot před 1 rokem
rodič
revize
1e31327418

+ 7 - 13
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<Namespace> 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();
     }
 
 }

+ 1 - 10
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);