zhisheng17/flink-learning · error · RuntimeException

Failed to find port "{}" in Service "{}"

Error message

Failed to find port "{}" in Service "{}"

What it means

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.

Source

Thrown at flink-learning-k8s/flink-k8s/src/main/java/org/apache/flink/kubernetes/kubeclient/Fabric8FlinkKubeClient.java:415

			.withKind(deployment.getKind())
			.withController(true)
			.withBlockOwnerDeletion(true)
			.build();
		resources.forEach(resource ->
			resource.getMetadata().setOwnerReferences(Collections.singletonList(deploymentOwnerReference)));
	}

	/**
	 * Get rest port from the external Service.
	 */
	private int getRestPortFromExternalService(Service externalService) {
		final List<ServicePort> servicePortCandidates = externalService.getSpec().getPorts()
			.stream()
			.filter(x -> x.getName().equals(Constants.REST_PORT_NAME))
			.collect(Collectors.toList());

		if (servicePortCandidates.isEmpty()) {
			throw new RuntimeException("Failed to find port \"" + Constants.REST_PORT_NAME + "\" in Service \"" +
				ExternalServiceDecorator.getExternalServiceName(this.clusterId) + "\"");
		}

		final ServicePort externalServicePort = servicePortCandidates.get(0);

		final KubernetesConfigOptions.ServiceExposedType externalServiceType =
			KubernetesConfigOptions.ServiceExposedType.valueOf(externalService.getSpec().getType());

		switch (externalServiceType) {
			case ClusterIP:
			case LoadBalancer:
				return externalServicePort.getPort();
			case NodePort:
				return externalServicePort.getNodePort();
			default:
				throw new RuntimeException("Unrecognized Service type: " + externalServiceType);
		}
	}

View on GitHub (pinned to d731cee761)

Solutions

  1. 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"}]'.
  2. Recreate the Service via Flink's ExternalServiceDecorator instead of a custom manifest.
  3. Inspect the Service's spec.ports (kubectl get svc -o yaml) and align names with Constants.REST_PORT_NAME.
  4. If using custom Helm charts, add name: rest to the REST port definition.

Example fix

// before: unnamed REST port in service manifest
ports:
  - port: 8081
// after: named so Flink can locate it
ports:
  - name: rest
    port: 8081
Defensive patterns

Strategy: validation

Validate before calling

// confirm the 'rest' named port before resolving the REST endpoint
Service svc = externalService.get();
boolean hasRestPort = svc.getSpec().getPorts().stream()
    .anyMatch(p -> Constants.REST_PORT_NAME.equals(p.getName()));
if (!hasRestPort) throw new IllegalStateException("Service missing 'rest' port");

Try / catch

try {
    int port = client.getRestPortFromExternalService();
} catch (RuntimeException e) {
    if (e.getMessage() != null && e.getMessage().contains("Failed to find port")) {
        // fall back to default 8081 or repair the Service manifest
    }
}

Prevention

When it happens

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

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

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of zhisheng17/flink-learning@d731cee761 (2026-09-06). Data as JSON: /api/errors/c0fe5e23483ea439. Report an issue: GitHub.