{"record":{"id":"ad44c3a3eeabc3e1","repo":"apache/dolphinscheduler","slug":"k8s-namespace-exist","errorCode":"K8S_NAMESPACE_EXIST","errorMessage":"K8S_NAMESPACE_EXIST","messagePattern":"K8S_NAMESPACE_EXIST","errorType":"error_code","errorClass":"ServiceException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java","lineNumber":127,"sourceCode":"    @Override\n    public K8sNamespace registerK8sNamespace(User loginUser, String namespace, Long clusterCode) {\n        if (isNotAdmin(loginUser)) {\n            throw new ServiceException(Status.USER_NO_OPERATION_PERM);\n        }\n\n        if (StringUtils.isEmpty(namespace)) {\n            log.warn(\"Parameter namespace is empty.\");\n            throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.NAMESPACE);\n        }\n\n        if (clusterCode == null) {\n            log.warn(\"Parameter clusterCode is null.\");\n            throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.CLUSTER);\n        }\n\n        if (checkNamespaceExistInDb(namespace, clusterCode)) {\n            log.warn(\"K8S namespace already exists.\");\n            throw new ServiceException(Status.K8S_NAMESPACE_EXIST, namespace, clusterCode);\n        }\n\n        Cluster cluster = clusterDao.queryByClusterCode(clusterCode);\n        if (cluster == null) {\n            log.error(\"Cluster does not exist, clusterCode:{}\", clusterCode);\n            throw new ServiceException(Status.CLUSTER_NOT_EXISTS, namespace, clusterCode);\n        }\n\n        long code = CodeGenerateUtils.genCode();\n        cluster.setCode(code);\n\n        K8sNamespace k8sNamespaceObj = new K8sNamespace();\n        Date now = new Date();\n\n        k8sNamespaceObj.setCode(code);\n        k8sNamespaceObj.setNamespace(namespace);\n        k8sNamespaceObj.setClusterCode(clusterCode);\n        k8sNamespaceObj.setUserId(loginUser.getId());","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java#L109-L145","documentation":"K8S_NAMESPACE_EXIST is thrown by K8sNamespaceServiceImpl.registerK8sNamespace when a K8s namespace with the same name and clusterCode already exists in the DolphinScheduler database. The service calls checkNamespaceExistInDb(namespace, clusterCode) before registering and rejects duplicates to keep the t_ds_k8s_namespace table unique per (namespace, cluster) pair. It is a user-facing duplicate-registration guard, not a Kubernetes-side failure.","triggerScenarios":"Calling the create-namespace API (POST /k8s-namespace) with a namespace name that is already registered for the same clusterCode; re-submitting a create request after a previous successful registration; attempting to register the same namespace against a cluster it was already registered on.","commonSituations":"Idempotent automation scripts that don't check before creating; retrying a request that actually succeeded (e.g. after a client timeout); UI double-submit; importing namespace definitions that were already registered on the same cluster.","solutions":["Query the existing namespaces (GET /k8s-namespace/list) and reuse the existing record instead of creating a new one.","Delete the existing namespace registration (deleteNamespaceById) before re-registering, if re-creation is intended.","Choose a different namespace name or register against a different clusterCode.","Make creation scripts idempotent by checking existence first or treating this error as success."],"exampleFix":"// before\nk8sNamespaceService.registerK8sNamespace(loginUser, namespace, clusterCode);\n\n// after\nif (!k8sNamespaceService.checkNamespaceExistInDb(namespace, clusterCode)) {\n    k8sNamespaceService.registerK8sNamespace(loginUser, namespace, clusterCode);\n} else {\n    log.info(\"namespace {} already registered on cluster {}\", namespace, clusterCode);\n}","handlingStrategy":"validation","validationCode":"// Java caller-side pre-check\nif (k8sNamespaceService.checkNamespaceExistInDb(namespace, clusterCode)) {\n    throw new IllegalStateException(\"namespace already registered: \" + namespace);\n}","typeGuard":null,"tryCatchPattern":"try {\n    service.registerK8sNamespace(loginUser, namespace, clusterCode);\n} catch (ServiceException e) {\n    if (Status.K8S_NAMESPACE_EXIST.getCode() == e.getCode()) {\n        log.info(\"namespace {} already registered on cluster {}; treating as success\", namespace, clusterCode);\n    } else {\n        throw e;\n    }\n}","preventionTips":["Always list existing namespaces for the cluster before registering.","Make create scripts idempotent by treating K8S_NAMESPACE_EXIST as a no-op success.","Avoid double-submit in UIs by disabling the create button after the first click."],"tags":["duplicate-resource","kubernetes","namespace"],"backgroundTag":"file-already-exists","analyzedSha":"02eac45a1b6676e639fcbfb4be2243de5771b05d","analyzedAt":"2026-09-06T17:43:00.555Z","contentChangedAt":"2026-09-06T17:43:00.555Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}