{"record":{"id":"c0fe5e23483ea439","repo":"zhisheng17/flink-learning","slug":"failed-to-find-port-in-service","errorCode":null,"errorMessage":"Failed to find port \"{}\" in Service \"{}\"","messagePattern":"Failed to find port \"(.+?)\" in Service \"(.+?)\"","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":415,"sourceCode":"\t\t\t.withKind(deployment.getKind())\n\t\t\t.withController(true)\n\t\t\t.withBlockOwnerDeletion(true)\n\t\t\t.build();\n\t\tresources.forEach(resource ->\n\t\t\tresource.getMetadata().setOwnerReferences(Collections.singletonList(deploymentOwnerReference)));\n\t}\n\n\t/**\n\t * Get rest port from the external Service.\n\t */\n\tprivate int getRestPortFromExternalService(Service externalService) {\n\t\tfinal List<ServicePort> servicePortCandidates = externalService.getSpec().getPorts()\n\t\t\t.stream()\n\t\t\t.filter(x -> x.getName().equals(Constants.REST_PORT_NAME))\n\t\t\t.collect(Collectors.toList());\n\n\t\tif (servicePortCandidates.isEmpty()) {\n\t\t\tthrow new RuntimeException(\"Failed to find port \\\"\" + Constants.REST_PORT_NAME + \"\\\" in Service \\\"\" +\n\t\t\t\tExternalServiceDecorator.getExternalServiceName(this.clusterId) + \"\\\"\");\n\t\t}\n\n\t\tfinal ServicePort externalServicePort = servicePortCandidates.get(0);\n\n\t\tfinal KubernetesConfigOptions.ServiceExposedType externalServiceType =\n\t\t\tKubernetesConfigOptions.ServiceExposedType.valueOf(externalService.getSpec().getType());\n\n\t\tswitch (externalServiceType) {\n\t\t\tcase ClusterIP:\n\t\t\tcase LoadBalancer:\n\t\t\t\treturn externalServicePort.getPort();\n\t\t\tcase NodePort:\n\t\t\t\treturn externalServicePort.getNodePort();\n\t\t\tdefault:\n\t\t\t\tthrow new RuntimeException(\"Unrecognized Service type: \" + externalServiceType);\n\t\t}\n\t}","sourceCodeStart":397,"sourceCodeEnd":433,"githubUrl":"https://github.com/zhisheng17/flink-learning/blob/d731cee7618021be56d132cc925102ffff8d75e6/flink-learning-k8s/flink-k8s/src/main/java/org/apache/flink/kubernetes/kubeclient/Fabric8FlinkKubeClient.java#L397-L433","documentation":"getRestPortFromExternalService() filters the external Service's ports for name 'rest' (Constants.REST_PORT_NAME) and throws this RuntimeException when no port with that name exists, so the REST endpoint port cannot be resolved.","triggerScenarios":"restPort() called on an external Service whose spec.ports lack an entry named 'rest' — e.g. the Service was created manually, port names were customized, or the ExternalServiceDecorator was bypassed.","commonSituations":"Hand-written Kubernetes Services missing the required 'rest' port name; port renamed in custom manifests; services created by other tools (Helm charts) that name the port 'http' or 'web'; older Flink deployments with unnamed ports.","solutions":["Ensure the external Service has a port named 'rest': kubectl patch svc <cluster-id>-rest -n <ns> --type json -p '[{\"op\":\"add\",\"path\":\"/spec/ports/0/name\",\"value\":\"rest\"}]'.","Recreate the Service via Flink's ExternalServiceDecorator instead of a custom manifest.","Inspect the Service's spec.ports (kubectl get svc -o yaml) and align names with Constants.REST_PORT_NAME.","If using custom Helm charts, add name: rest to the REST port definition."],"exampleFix":"// before: unnamed REST port in service manifest\nports:\n  - port: 8081\n// after: named so Flink can locate it\nports:\n  - name: rest\n    port: 8081","handlingStrategy":"validation","validationCode":"// confirm the 'rest' named port before resolving the REST endpoint\nService svc = externalService.get();\nboolean hasRestPort = svc.getSpec().getPorts().stream()\n    .anyMatch(p -> Constants.REST_PORT_NAME.equals(p.getName()));\nif (!hasRestPort) throw new IllegalStateException(\"Service missing 'rest' port\");","typeGuard":null,"tryCatchPattern":"try {\n    int port = client.getRestPortFromExternalService();\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Failed to find port\")) {\n        // fall back to default 8081 or repair the Service manifest\n    }\n}","preventionTips":["Always name the REST port 'rest' in Service manifests and Helm charts.","Provision external services via ExternalServiceDecorator, not manual YAML.","Audit existing Services with kubectl get svc -o yaml after migrations.","Pin port naming in deployment tooling/CI checks."],"tags":["kubernetes","service","port","rest"],"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"}