{"record":{"id":"0810c92b6d199b0a","repo":"GoogleContainerTools/skaffold","slug":"getting-kubernetes-client-w-0810c9","errorCode":null,"errorMessage":"getting Kubernetes client: %w","messagePattern":"getting Kubernetes client: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/skaffold/kubernetes/portforward/kubectl_forwarder.go","lineNumber":263,"sourceCode":"\t\t\t\t}\n\t\t\t\treturn\n\t\t\t} else if strings.Contains(s, \"Forwarding from\") {\n\t\t\t\tselect {\n\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)","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/kubernetes/portforward/kubectl_forwarder.go#L245-L281","documentation":"findNewestPodForService needs a Kubernetes client to query Services and Pods, and builds it from the given kubeContext via kubernetesclient.Client. When that client construction fails, the underlying cause (bad kubeconfig, unreachable cluster, invalid context) is wrapped with \"getting Kubernetes client: %w\". It means port-forwarding could not even start because Skaffold could not authenticate/connect to the cluster.","triggerScenarios":"kubernetesclient.Client(kubeContext) returns an error: missing or unreadable kubeconfig (KUBECONFIG / ~/.kube/config), unknown kubeContext name, invalid client credentials, or client-go failing to build a rest.Config for the context.","commonSituations":"Running skaffold dev before kubectl config is set up; pointing at a context created for a deleted GKE/EKS/KinD cluster; KUBECONFIG env var pointing to a stale path; switching contexts after cluster teardown.","solutions":["Run `kubectl config get-contexts` and verify the kubeContext name exists, then `kubectl config use-context <name>` or set it in skaffold.yaml.","Verify KUBECONFIG points to a readable, valid kubeconfig file (`kubectl cluster-info` succeeds with the same config).","Recreate or re-login to the cluster so credentials/kubeconfig are regenerated (e.g. `gcloud container clusters get-credentials`).","If using a remote/preview environment, ensure the cluster is running and network-reachable."],"exampleFix":"// before: skaffold.yaml with a context that does not exist\nkubectlContext: staging-ctx\n// after\nkubectlContext: gke_project_us-central1_cluster","handlingStrategy":"validation","validationCode":"// before enabling kubectl port-forwarding, verify the kube context works\nctxName := kubeContext\nif ctxName == \"\" { ctxName = \"current\" }\nout, err := exec.CommandContext(ctx, \"kubectl\", \"config\", \"get-contexts\",\n    \"--output=name\").CombinedOutput()\nif err != nil || !strings.Contains(string(out), ctxName) {\n    return fmt.Errorf(\"kube context %q not found in kubeconfig: %s\", ctxName, out)\n}","typeGuard":null,"tryCatchPattern":"// wrap and surface the wrapped cause\npod, port, err := findNewestPodForService(ctx, kubeContext, ns, svc, port)\nif err != nil {\n    var cmdErr *cmdutil.ExitError\n    if errors.As(err, &cmdErr) { /* kubeconfig/context problem */ }\n    log.Entry(ctx).Fatalf(\"cannot start port-forward: %v\", err)\n}","preventionTips":["Run `kubectl cluster-info` with the same KUBECONFIG before skaffold dev.","Pin the kubeContext explicitly in skaffold.yaml instead of relying on the default context.","Keep kubeconfig credentials fresh (re-run get-credentials after cluster recreation)."],"tags":["kubernetes","kubeconfig","client-init"],"backgroundTag":"kubeconfig-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"}