{"record":{"id":"5681ebbcfcff7763","repo":"zhisheng17/flink-learning","slug":"could-not-kill-kubernetes-cluster-clusterid","errorCode":null,"errorMessage":"Could not kill Kubernetes cluster ${clusterId}","messagePattern":"Could not kill Kubernetes cluster (.+?)","errorType":"exception","errorClass":"FlinkException","httpStatus":null,"severity":"error","filePath":"flink-learning-k8s/flink-k8s/src/main/java/org/apache/flink/kubernetes/KubernetesClusterDescriptor.java","lineNumber":268,"sourceCode":"\t\t\treturn createClusterClientProvider(clusterId);\n\t\t} catch (Exception e) {\n\t\t\ttry {\n\t\t\t\tLOG.warn(\"Failed to create the Kubernetes cluster \\\"{}\\\", try to clean up the residual resources.\", clusterId);\n\t\t\t\tclient.stopAndCleanupCluster(clusterId);\n\t\t\t} catch (Exception e1) {\n\t\t\t\tLOG.info(\"Failed to stop and clean up the Kubernetes cluster \\\"{}\\\".\", clusterId, e1);\n\t\t\t}\n\t\t\tthrow new ClusterDeploymentException(\"Could not create Kubernetes cluster \\\"\" + clusterId + \"\\\".\", e);\n\t\t}\n\t}\n\n\t@Override\n\tpublic void killCluster(String clusterId) throws FlinkException {\n\t\ttry {\n\t\t\tclient.stopAndCleanupCluster(clusterId);\n\t\t} catch (Exception e) {\n\t\t\tclient.handleException(e);\n\t\t\tthrow new FlinkException(\"Could not kill Kubernetes cluster \" + clusterId);\n\t\t}\n\t}\n\n\t@Override\n\tpublic void close() {\n\t\ttry {\n\t\t\tclient.close();\n\t\t} catch (Exception e) {\n\t\t\tclient.handleException(e);\n\t\t\tLOG.error(\"failed to close client, exception {}\", e.toString());\n\t\t}\n\t}\n}\n","sourceCodeStart":250,"sourceCodeEnd":282,"githubUrl":"https://github.com/zhisheng17/flink-learning/blob/d731cee7618021be56d132cc925102ffff8d75e6/flink-learning-k8s/flink-k8s/src/main/java/org/apache/flink/kubernetes/KubernetesClusterDescriptor.java#L250-L282","documentation":"Thrown by killCluster when client.stopAndCleanupCluster(clusterId) throws while deleting the cluster's Kubernetes resources. handleException is invoked for richer diagnostics, and the original cause is not attached to the FlinkException, so check the logs/client for the underlying reason.","triggerScenarios":"Calling killCluster(clusterId) when Kubernetes API delete calls fail — RBAC denial on delete, API server unreachable, or resources already gone/deleted concurrently.","commonSituations":"Deleting a cluster that was already removed; a service account lacking delete permissions on deployments/services/configmaps; network partition to the Kubernetes API; namespace already terminating (deletes rejected).","solutions":["Check the logged cause (handleException output) for RBAC vs connectivity vs not-found errors.","If resources are already gone, the cluster is effectively killed — verify with kubectl get deploy,svc | grep <clusterId> and ignore the failure.","Grant the service account delete permissions (deployments, services, configmaps, pods) in the namespace.","Retry once the Kubernetes API is reachable if the cause was transient connectivity.","If the namespace is terminating, wait for termination; objects cannot be deleted meanwhile."],"exampleFix":"// before\ntry {\n    descriptor.killCluster(clusterId);\n} catch (FlinkException e) {\n    throw e; // cause lost\n}\n// after\ndescriptor.killCluster(clusterId); // then verify cleanup\nkubectl get all -l app=<clusterId>  # confirm nothing residual remains","handlingStrategy":"try-catch","validationCode":"boolean stillThere = kubeClient.getService(KubernetesService.ServiceType.REST_SERVICE, clusterId).isPresent();\nif (!stillThere) { return; /* nothing to kill */ }","typeGuard":null,"tryCatchPattern":"try {\n    descriptor.killCluster(clusterId);\n} catch (FlinkException e) {\n    LOG.warn(\"Kill failed for {}\", clusterId, e); // check RBAC vs already-deleted\n}","preventionTips":["Grant delete RBAC on deployments/services/configmaps/pods","Handle double-kill idempotently","Verify namespace is not terminating before cleanup","Keep the client's handleException diagnostics in logs"],"tags":["kubernetes","flink","cluster-cleanup","kill-cluster"],"backgroundTag":"api-request-failed","analyzedSha":"d731cee7618021be56d132cc925102ffff8d75e6","analyzedAt":"2026-09-06T05:35:08.496Z","contentChangedAt":"2026-09-06T05:35:08.496Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}