{"record":{"id":"981f389543376a83","repo":"GoogleContainerTools/skaffold","slug":"retrieving-services-for-automatic-port-forwarding","errorCode":null,"errorMessage":"retrieving services for automatic port forwarding: %w","messagePattern":"retrieving services for automatic port forwarding: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/kubernetes/portforward/resource_forwarder.go","lineNumber":103,"sourceCode":"\t\tvar validResources []*latest.PortForwardResource\n\t\tfor _, pf := range p.userDefinedResources {\n\t\t\tif pf.Namespace != \"\" {\n\t\t\t\tif err := applyWithTemplate(pf); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\tvalidResources = append(validResources, pf)\n\t\t\t} else {\n\t\t\t\tlog.Entry(ctx).Warnf(\"Skipping the port forwarding resource %s/%s because namespace is not specified\", pf.Type, pf.Name)\n\t\t\t}\n\t\t}\n\t\tp.userDefinedResources = validResources\n\t}\n\n\tvar serviceResources []*latest.PortForwardResource\n\tif p.services {\n\t\tfound, err := retrieveServices(ctx, p.label, namespaces, p.kubeContext)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"retrieving services for automatic port forwarding: %w\", err)\n\t\t}\n\t\tserviceResources = found\n\t}\n\tp.portForwardResources(ctx, append(p.userDefinedResources, serviceResources...))\n\treturn nil\n}\n\nfunc applyWithTemplate(resource *latest.PortForwardResource) error {\n\tif resource.Namespace != \"\" {\n\t\tnamespace, err := util.ExpandEnvTemplateOrFail(resource.Namespace, nil)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"cannot parse the namespace template on user defined port forwarder: %w\", err)\n\t\t}\n\t\tresource.Namespace = namespace\n\t}\n\tname, err := util.ExpandEnvTemplateOrFail(resource.Name, nil)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"cannot parse the name template on user defined port forwarder: %w\", err)","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/kubernetes/portforward/resource_forwarder.go#L85-L121","documentation":"ResourceForwarder.Start optionally discovers cluster Services matching a label so their ports can be auto-forwarded. If retrieveServices fails, the error is wrapped as \"retrieving services for automatic port forwarding\" and Start aborts. It signals that service discovery — not the forwarding itself — failed.","triggerScenarios":"retrieveServices(ctx, p.label, namespaces, p.kubeContext) returns an error during Start when p.services is true; internally it fetches a Kubernetes client and lists Services per namespace.","commonSituations":"No kubeconfig / wrong KUBECONFIG path; unreachable or wrong kubeContext; user lacks RBAC permission to list services in the target namespaces; cluster unreachable (VPN down, wrong context after switching projects).","solutions":["Verify kubeconfig is valid and the kubeContext exists: kubectl config get-contexts and kubectl --context <ctx> get svc","Check RBAC: the user/serviceaccount needs list permission on services in the configured namespaces","Confirm network connectivity to the cluster (VPN, firewall)","If service auto-forwarding is not needed, disable the services option so Start skips discovery"],"exampleFix":"// before (skaffold.yaml)\nportForward:\n  - resourceType: service\n// after (disable auto service discovery if RBAC is the blocker)\n# remove the auto-forward config or run with a context that has list-services RBAC\n# verify first:\n# kubectl --context <kubeContext> auth can-i list services --namespace <ns>","handlingStrategy":"try-catch","validationCode":"// before calling Start with services enabled:\nif err := exec.Command(\"kubectl\", \"--context\", kubeContext, \"get\", \"svc\").Run(); err != nil {\n    return fmt.Errorf(\"cluster/context not usable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := forwarder.Start(ctx); err != nil {\n    if strings.Contains(err.Error(), \"retrieving services\") {\n        log.Warnf(\"service auto-forward unavailable: %v; continuing with user-defined forwards\", err)\n        return nil\n    }\n    return err\n}","preventionTips":["Run `kubectl config get-contexts` and `kubectl auth can-i list services` before skaffold dev","Keep KUBECONFIG set explicitly in CI","Disable services auto-forwarding if RBAC cannot be granted"],"tags":["kubernetes","port-forward","service-discovery"],"backgroundTag":"kubeconfig-context-unreachable","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"}