apache/dolphinscheduler · error · ServiceException

K8S_NAMESPACE_EXIST

K8S_NAMESPACE_EXIST

Error message

K8S_NAMESPACE_EXIST

What it means

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.

Source

Thrown at dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java:127

    @Override
    public K8sNamespace registerK8sNamespace(User loginUser, String namespace, Long clusterCode) {
        if (isNotAdmin(loginUser)) {
            throw new ServiceException(Status.USER_NO_OPERATION_PERM);
        }

        if (StringUtils.isEmpty(namespace)) {
            log.warn("Parameter namespace is empty.");
            throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.NAMESPACE);
        }

        if (clusterCode == null) {
            log.warn("Parameter clusterCode is null.");
            throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.CLUSTER);
        }

        if (checkNamespaceExistInDb(namespace, clusterCode)) {
            log.warn("K8S namespace already exists.");
            throw new ServiceException(Status.K8S_NAMESPACE_EXIST, namespace, clusterCode);
        }

        Cluster cluster = clusterDao.queryByClusterCode(clusterCode);
        if (cluster == null) {
            log.error("Cluster does not exist, clusterCode:{}", clusterCode);
            throw new ServiceException(Status.CLUSTER_NOT_EXISTS, namespace, clusterCode);
        }

        long code = CodeGenerateUtils.genCode();
        cluster.setCode(code);

        K8sNamespace k8sNamespaceObj = new K8sNamespace();
        Date now = new Date();

        k8sNamespaceObj.setCode(code);
        k8sNamespaceObj.setNamespace(namespace);
        k8sNamespaceObj.setClusterCode(clusterCode);
        k8sNamespaceObj.setUserId(loginUser.getId());

View on GitHub (pinned to 02eac45a1b)

Solutions

  1. Query the existing namespaces (GET /k8s-namespace/list) and reuse the existing record instead of creating a new one.
  2. Delete the existing namespace registration (deleteNamespaceById) before re-registering, if re-creation is intended.
  3. Choose a different namespace name or register against a different clusterCode.
  4. Make creation scripts idempotent by checking existence first or treating this error as success.

Example fix

// before
k8sNamespaceService.registerK8sNamespace(loginUser, namespace, clusterCode);

// after
if (!k8sNamespaceService.checkNamespaceExistInDb(namespace, clusterCode)) {
    k8sNamespaceService.registerK8sNamespace(loginUser, namespace, clusterCode);
} else {
    log.info("namespace {} already registered on cluster {}", namespace, clusterCode);
}
Defensive patterns

Strategy: validation

Validate before calling

// Java caller-side pre-check
if (k8sNamespaceService.checkNamespaceExistInDb(namespace, clusterCode)) {
    throw new IllegalStateException("namespace already registered: " + namespace);
}

Try / catch

try {
    service.registerK8sNamespace(loginUser, namespace, clusterCode);
} catch (ServiceException e) {
    if (Status.K8S_NAMESPACE_EXIST.getCode() == e.getCode()) {
        log.info("namespace {} already registered on cluster {}; treating as success", namespace, clusterCode);
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of apache/dolphinscheduler@02eac45a1b (2026-09-06). Data as JSON: /api/errors/ad44c3a3eeabc3e1. Report an issue: GitHub.