{"record":{"id":"c4c53570e02ddc02","repo":"cilium/cilium","slug":"failed-to-lookup-container-port-with-service-port","errorCode":null,"errorMessage":"failed to lookup container port with service port %d: %w","messagePattern":"failed to lookup container port with service port (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/k8s/portforward/portforward.go","lineNumber":157,"sourceCode":"\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to get service %q: %w\", name, err)\n\t}\n\n\tpod, err := pf.getFirstPodForService(ctx, svc)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to get service %q: %w\", name, err)\n\t}\n\n\tif svcPort == 0 {\n\t\tif len(svc.Spec.Ports) == 0 {\n\t\t\treturn nil, fmt.Errorf(\"service %q doesn't have any ports\", name)\n\t\t}\n\t\tsvcPort = svc.Spec.Ports[0].Port\n\t}\n\n\tcontainerPort, err := kutil.LookupContainerPortNumberByServicePort(*svc, *pod, svcPort)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to lookup container port with service port %d: %w\", svcPort, err)\n\t}\n\n\tp := PortForwardParameters{\n\t\tNamespace:  pod.Namespace,\n\t\tPod:        pod.Name,\n\t\tPorts:      []string{fmt.Sprintf(\"%d:%d\", localPort, containerPort)},\n\t\tAddresses:  nil, // default is localhost\n\t\tOutWriters: OutWriters{Out: nil, ErrOut: nil},\n\t}\n\n\tres, err := pf.PortForward(ctx, p)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to port forward: %w\", err)\n\t}\n\n\treturn &PortForwardServiceResult{\n\t\tForwardedPort: res.ForwardedPorts[0],\n\t}, nil","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/pkg/k8s/portforward/portforward.go#L139-L175","documentation":"PortForwardService resolves the service port to a container port via kutil.LookupContainerPortNumberByServicePort, matching the service port to a pod container's targetPort. If no container on the selected pod exposes that port, this wrapped error is returned.","triggerScenarios":"Calling PortForwardService with a svcPort that does not correspond to any containerPort on the pod behind the service — e.g. service port 80 maps to targetPort 8080 but the pod's containers don't declare 8080, or a numeric targetPort mismatch.","commonSituations":"Container port changed in a new image version while the service still points at the old targetPort; forwarding to the wrong service port number; pods whose containerPort declarations were omitted; multi-container pods where the port belongs to a different container.","solutions":["Compare service spec.ports[].targetPort with the container's containerPort: kubectl get svc,pod -o yaml","Update the service targetPort to match the container's exposed port","Pass the correct svcPort that exists on the service and maps to a declared container port","If multi-container, ensure the container holding the port declares containerPort"],"exampleFix":"// before\n// service: port 80 -> targetPort 8080, but container exposes 9090\nres, err := pf.PortForwardService(ctx, ns, name, 8080, 80) // lookup fails\n// after\n// fix container spec: containerPort: 8080 (or change service targetPort to 9090)\nres, err := pf.PortForwardService(ctx, ns, name, 8080, 80)","handlingStrategy":"validation","validationCode":"svc, _ := clientset.CoreV1().Services(ns).Get(ctx, name, metav1.GetOptions{})\npod, _ := getFirstPod(clientset, svc)\nfor _, p := range svc.Spec.Ports {\n  if p.Port == svcPort && containerDeclaresPort(pod, p.TargetPort) {\n    return nil // safe to forward\n  }\n}\nreturn fmt.Errorf(\"svcPort %d has no matching container port\", svcPort)","typeGuard":"func containerDeclaresPort(pod *corev1.Pod, port intstr.IntOrString) bool {\n  for _, c := range pod.Spec.Containers {\n    for _, cp := range c.Ports {\n      if int(cp.ContainerPort) == port.IntValue() { return true }\n    }\n  }\n  return false\n}","tryCatchPattern":null,"preventionTips":["Keep service targetPort aligned with the container's declared containerPort","Update both service and deployment together when changing ports","Declare containerPort in container specs explicitly","Double-check svcPort values passed to PortForwardService"],"tags":["kubernetes","port-forward","port-mapping"],"backgroundTag":"container-port-not-found","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}