{"record":{"id":"7172db08f45744e5","repo":"GoogleContainerTools/skaffold","slug":"getting-kubernetes-client-w-7172db","errorCode":null,"errorMessage":"getting Kubernetes client: %w","messagePattern":"getting Kubernetes client: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/kubernetes/portforward/resource_forwarder.go","lineNumber":178,"sourceCode":"\t\treturn entry\n\t}\n\n\t// Try to request matching local port *providing* that it is not a system port.\n\t// https://github.com/GoogleContainerTools/skaffold/pull/5554#issuecomment-803270340\n\trequestPort := resource.LocalPort\n\tif requestPort == 0 && resource.Port.IntVal >= 1024 {\n\t\trequestPort = resource.Port.IntVal\n\t}\n\tentry.localPort = retrieveAvailablePort(resource.Address, requestPort, &p.entryManager.forwardedPorts)\n\treturn entry\n}\n\n// retrieveServiceResources retrieves all services in the cluster matching the given label\n// as a list of PortForwardResources\nfunc retrieveServiceResources(ctx context.Context, label string, namespaces []string, kubeContext string) ([]*latest.PortForwardResource, error) {\n\tclient, err := kubernetesclient.Client(kubeContext)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"getting Kubernetes client: %w\", err)\n\t}\n\n\tvar resources []*latest.PortForwardResource\n\tfor _, ns := range namespaces {\n\t\tservices, err := client.CoreV1().Services(ns).List(ctx, metav1.ListOptions{\n\t\t\tLabelSelector: label,\n\t\t})\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"selecting services by label %q: %w\", label, err)\n\t\t}\n\t\tfor _, s := range services.Items {\n\t\t\tfor _, p := range s.Spec.Ports {\n\t\t\t\tresources = append(resources, &latest.PortForwardResource{\n\t\t\t\t\tType:      constants.Service,\n\t\t\t\t\tName:      s.Name,\n\t\t\t\t\tNamespace: s.Namespace,\n\t\t\t\t\tPort:      schemautil.FromInt(int(p.Port)),\n\t\t\t\t\tAddress:   constants.DefaultPortForwardAddress,","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/kubernetes/portforward/resource_forwarder.go#L160-L196","documentation":"retrieveServiceResources obtains a Kubernetes clientset via kubernetesclient.Client(kubeContext) to list services for auto port-forwarding. If client construction fails, the error is wrapped as \"getting Kubernetes client\". Client construction typically loads kubeconfig, resolves the context, and builds REST config, so failures are configuration- or connectivity-related.","triggerScenarios":"kubernetesclient.Client(kubeContext) returns an error because kubeconfig is missing/unreadable, the kubeContext name does not exist, or the REST config is invalid — raised while retrieveServiceResources runs inside retrieveServices during Start.","commonSituations":"KUBECONFIG pointing at a nonexistent file; stale context after cluster deletion (e.g. expired kind/minikube/GKE context); malformed kubeconfig YAML; running in a container without the kubeconfig mounted.","solutions":["Run kubectl config current-context and kubectl cluster-info to validate the kubeconfig and context","Point KUBECONFIG at a valid file or recreate the cluster context (e.g. minikube/kind/GKE auth login)","Fix kubeconfig syntax errors reported by kubectl","Ensure the kubeconfig is available where skaffold runs (mount it in CI containers)"],"exampleFix":"// diagnostic before running\n//   KUBECONFIG=~/.kube/config kubectl config get-contexts\n// before (skaffold.yaml / shell)\nkubeContext: old-deleted-cluster\n// after\nexport KUBECONFIG=~/.kube/config\nkubeContext: kind-my-cluster  # must exist in `kubectl config get-contexts`","handlingStrategy":"validation","validationCode":"func kubeContextUsable(kubeContext string) error {\n    out, err := exec.Command(\"kubectl\", \"--context\", kubeContext, \"cluster-info\").CombinedOutput()\n    if err != nil {\n        return fmt.Errorf(\"context %q unusable: %v: %s\", kubeContext, err, out)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"client, err := kubernetesclient.Client(kubeContext)\nif err != nil {\n    return nil, fmt.Errorf(\"check KUBECONFIG and context %q: %w\", kubeContext, err)\n}","preventionTips":["Validate kubeconfig with kubectl cluster-info before running skaffold","Recreate contexts for ephemeral clusters (kind/minikube) after restarts","Mount kubeconfig in CI containers and set KUBECONFIG explicitly"],"tags":["kubernetes","kubeconfig","client"],"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"}