{"record":{"id":"312a3cbd40296c1d","repo":"zhisheng17/flink-learning","slug":"failed-to-find-deployment-named-in-namespace","errorCode":null,"errorMessage":"Failed to find Deployment named {} in namespace {}","messagePattern":"Failed to find Deployment named (.+?) in namespace (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"flink-learning-k8s/flink-k8s/src/main/java/org/apache/flink/kubernetes/kubeclient/Fabric8FlinkKubeClient.java","lineNumber":129,"sourceCode":"\t\tthis.internalClient\n\t\t\t.resourceList(accompanyingResources)\n\t\t\t.inNamespace(this.namespace)\n\t\t\t.createOrReplace();\n\t}\n\n\t@Override\n\tpublic CompletableFuture<Void> createTaskManagerPod(KubernetesPod kubernetesPod) {\n\t\treturn CompletableFuture.runAsync(\n\t\t\t() -> {\n\t\t\t\tif (masterDeploymentRef.get() == null) {\n\t\t\t\t\tfinal Deployment masterDeployment =\n\t\t\t\t\t\tthis.internalClient\n\t\t\t\t\t\t\t.apps()\n\t\t\t\t\t\t\t.deployments()\n\t\t\t\t\t\t\t.withName(KubernetesUtils.getDeploymentName(clusterId))\n\t\t\t\t\t\t\t.get();\n\t\t\t\t\tif (masterDeployment == null) {\n\t\t\t\t\t\tthrow new RuntimeException(\n\t\t\t\t\t\t\t\"Failed to find Deployment named \"\n\t\t\t\t\t\t\t\t+ clusterId\n\t\t\t\t\t\t\t\t+ \" in namespace \"\n\t\t\t\t\t\t\t\t+ this.namespace);\n\t\t\t\t\t}\n\t\t\t\t\tmasterDeploymentRef.compareAndSet(null, masterDeployment);\n\t\t\t\t}\n\n\t\t\t\t// Note that we should use the uid of the master Deployment for the OwnerReference.\n\t\t\t\tsetOwnerReference(masterDeploymentRef.get(), Collections.singletonList(kubernetesPod.getInternalResource()));\n\n\t\t\t\tLOG.debug(\n\t\t\t\t\t\"Start to create pod with spec {}{}\",\n\t\t\t\t\tSystem.lineSeparator(),\n\t\t\t\t\tKubernetesUtils.tryToGetPrettyPrintYaml(\n\t\t\t\t\t\tkubernetesPod.getInternalResource()));\n\n\t\t\t\tthis.internalClient","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/zhisheng17/flink-learning/blob/d731cee7618021be56d132cc925102ffff8d75e6/flink-learning-k8s/flink-k8s/src/main/java/org/apache/flink/kubernetes/kubeclient/Fabric8FlinkKubeClient.java#L111-L147","documentation":"createTaskManagerPod() looks up the JobManager Deployment named after the clusterId to copy its pod template, and throws this RuntimeException if the fabric8 client returns null (no such Deployment in the namespace). A TaskManager pod cannot be created without the master Deployment's spec.","triggerScenarios":"createTaskManagerPod called before the JobManager Deployment exists, after it was deleted (e.g. by killing a session-mode job), or with a clusterId that doesn't match the deployed Deployment name (getDeploymentName(clusterId) mismatch).","commonSituations":"Submitting TaskManager pods to a session cluster that was already torn down; using the wrong clusterId/namespace combination (deployed in 'flink' but client configured with 'default'); native-K8s lifecycle races during teardown.","solutions":["Verify the Deployment exists: kubectl get deployment <deployment-name-of-clusterId> -n <namespace>.","Ensure the KubeClient's namespace and clusterId match how the JobManager was deployed.","Create the JobManager Deployment (createJobManagerComponent) before requesting TaskManager pods.","If this happens during shutdown, guard pod creation against activeResourceManager teardown — do not create TMs after the deployment was removed."],"exampleFix":"// before: TM pod requested for nonexistent cluster\nclient.createTaskManagerPod(tmPodTemplate).get();\n// after: verify deployment first\nif (client.getServiceNames() != null && kubectlHasDeployment(clusterId)) {\n    client.createTaskManagerPod(tmPodTemplate).get();\n}","handlingStrategy":"validation","validationCode":"// confirm the JobManager Deployment exists before creating TM pods\nboolean exists = kubeClient\n    .listPods(Collections.emptyMap()) != null; // plus: kubectl get deployment <deployment-name(clusterId)> -n <ns>","typeGuard":null,"tryCatchPattern":"try {\n    client.createTaskManagerPod(template).get();\n} catch (Exception e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Failed to find Deployment\")) {\n        // cluster is gone: stop requesting workers\n    }\n}","preventionTips":["Create the JobManager Deployment before scaling up TaskManagers.","Match clusterId and namespace to the actual deployment.","Do not request TM pods after session-cluster teardown.","Verify with kubectl before programmatic pod creation."],"tags":["kubernetes","deployment","taskmanager","kubeclient"],"backgroundTag":"resource-not-found","analyzedSha":"d731cee7618021be56d132cc925102ffff8d75e6","analyzedAt":"2026-09-06T05:35:08.496Z","contentChangedAt":"2026-09-06T05:35:08.496Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}