{"record":{"id":"0eb70dd04f60e46c","repo":"GoogleContainerTools/skaffold","slug":"getting-service-s-s-w","errorCode":null,"errorMessage":"getting service %s/%s: %w","messagePattern":"getting service (.+?)/(.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/kubernetes/portforward/kubectl_forwarder.go","lineNumber":267,"sourceCode":"\t\t\t\tcase err <- nil:\n\t\t\t\tdefault:\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\n// findNewestPodForService queries the cluster to find a pod that fulfills the given service, giving\n// preference to pods that were most recently created.  This is in contrast to the selection algorithm\n// used by kubectl (see https://github.com/GoogleContainerTools/skaffold/issues/4522 for details).\nfunc findNewestPodForService(ctx context.Context, kubeContext, ns, serviceName string, servicePort schemautil.IntOrString) (string, int, error) {\n\tclient, err := kubernetesclient.Client(kubeContext)\n\tif err != nil {\n\t\treturn \"\", -1, fmt.Errorf(\"getting Kubernetes client: %w\", err)\n\t}\n\tsvc, err := client.CoreV1().Services(ns).Get(ctx, serviceName, metav1.GetOptions{})\n\tif err != nil {\n\t\treturn \"\", -1, fmt.Errorf(\"getting service %s/%s: %w\", ns, serviceName, err)\n\t}\n\tsvcPort, err := findServicePort(*svc, servicePort)\n\tif err != nil {\n\t\treturn \"\", -1, err\n\t}\n\n\t// Look for pods with matching selectors and that are not terminated.\n\t// We cannot use field selectors as they are only supported in 1.16\n\t// https://github.com/flant/shell-operator/blob/8fa3c3b8cfeb1ddb37b070b7a871561fdffe788b/HOOKS.md#fieldselector\n\tset := labels.Set(svc.Spec.Selector)\n\tlistOptions := metav1.ListOptions{\n\t\tLabelSelector: set.AsSelector().String(),\n\t}\n\tpodsList, err := client.CoreV1().Pods(ns).List(ctx, listOptions)\n\tif err != nil {\n\t\treturn \"\", -1, fmt.Errorf(\"listing pods: %w\", err)\n\t}\n\tvar pods []corev1.Pod","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/kubernetes/portforward/kubectl_forwarder.go#L249-L285","documentation":"After obtaining the client, findNewestPodForService fetches the Service object named serviceName in namespace ns. If the API server Get call fails, the error is wrapped as \"getting service <ns>/<name>: <cause>\". This wraps transport, auth, or not-found errors returned by client-go when reading the Service.","triggerScenarios":"client.CoreV1().Services(ns).Get(ctx, serviceName, metav1.GetOptions{}) returns an error: service does not exist (NotFound), namespace does not exist, RBAC denies services get, or the API server is unreachable/times out.","commonSituations":"Port-forwarding a service whose manifest was never applied (or was applied in a different namespace); typo'd serviceName in kubectl port-forward resources; cluster connectivity dropped mid-session; restricted RBAC in shared clusters.","solutions":["Check the service exists: `kubectl get svc <name> -n <namespace>` with the same kubeContext; deploy it if missing.","Verify the namespace in the skaffold port-forward resource config matches where the service was created.","Fix cluster connectivity/auth (`kubectl cluster-info`, re-authenticate).","If RBAC, grant the user's identity `get` on services in that namespace."],"exampleFix":"// before: forwarding resource names a service that is not deployed\n- resourceName: my-service\n  namespace: default\n// after: deploy the service or use the correct name/namespace\n- resourceName: web-svc\n  namespace: prod","handlingStrategy":"validation","validationCode":"svc, err := client.CoreV1().Services(ns).Get(ctx, serviceName, metav1.GetOptions{})\nif apierrors.IsNotFound(err) {\n    return fmt.Errorf(\"service %s/%s does not exist yet; deploy it before port-forwarding\", ns, serviceName)\n}","typeGuard":"func serviceExists(err error) bool {\n    return err == nil || !apierrors.IsNotFound(err)\n}","tryCatchPattern":"if _, err := findNewestPodForService(ctx, kubeContext, ns, svcName, port); err != nil {\n    if strings.Contains(err.Error(), \"getting service\") {\n        log.Warnf(\"service %s/%s unreachable: %v — is the app deployed?\", ns, svcName, err)\n    }\n}","preventionTips":["Always apply the manifests (or let skaffold deploy) before local port-forwarding.","Double-check namespace and serviceName in port-forward resources with `kubectl get svc -A | grep <name>`.","Use skaffold's dependencies so port-forward resources wait for deployed services."],"tags":["kubernetes","service-not-found","api-server"],"backgroundTag":"kubernetes-resource-not-found","analyzedSha":"a1189de023efc32d4b8e11f395acc678aa555011","analyzedAt":"2026-09-05T12:09:27.064Z","contentChangedAt":"2026-09-05T12:09:27.064Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}