GoogleContainerTools/skaffold · critical

getting Kubernetes client: %w

Error message

getting Kubernetes client: %w

What it means

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.

Source

Thrown at pkg/skaffold/kubernetes/portforward/kubectl_forwarder.go:263

				}
				return
			} else if strings.Contains(s, "Forwarding from") {
				select {
				case err <- nil:
				default:
				}
			}
		}
	}
}

// findNewestPodForService queries the cluster to find a pod that fulfills the given service, giving
// preference to pods that were most recently created.  This is in contrast to the selection algorithm
// used by kubectl (see https://github.com/GoogleContainerTools/skaffold/issues/4522 for details).
func findNewestPodForService(ctx context.Context, kubeContext, ns, serviceName string, servicePort schemautil.IntOrString) (string, int, error) {
	client, err := kubernetesclient.Client(kubeContext)
	if err != nil {
		return "", -1, fmt.Errorf("getting Kubernetes client: %w", err)
	}
	svc, err := client.CoreV1().Services(ns).Get(ctx, serviceName, metav1.GetOptions{})
	if err != nil {
		return "", -1, fmt.Errorf("getting service %s/%s: %w", ns, serviceName, err)
	}
	svcPort, err := findServicePort(*svc, servicePort)
	if err != nil {
		return "", -1, err
	}

	// Look for pods with matching selectors and that are not terminated.
	// We cannot use field selectors as they are only supported in 1.16
	// https://github.com/flant/shell-operator/blob/8fa3c3b8cfeb1ddb37b070b7a871561fdffe788b/HOOKS.md#fieldselector
	set := labels.Set(svc.Spec.Selector)
	listOptions := metav1.ListOptions{
		LabelSelector: set.AsSelector().String(),
	}
	podsList, err := client.CoreV1().Pods(ns).List(ctx, listOptions)

View on GitHub (pinned to a1189de023)

Solutions

  1. Run `kubectl config get-contexts` and verify the kubeContext name exists, then `kubectl config use-context <name>` or set it in skaffold.yaml.
  2. Verify KUBECONFIG points to a readable, valid kubeconfig file (`kubectl cluster-info` succeeds with the same config).
  3. Recreate or re-login to the cluster so credentials/kubeconfig are regenerated (e.g. `gcloud container clusters get-credentials`).
  4. If using a remote/preview environment, ensure the cluster is running and network-reachable.

Example fix

// before: skaffold.yaml with a context that does not exist
kubectlContext: staging-ctx
// after
kubectlContext: gke_project_us-central1_cluster
Defensive patterns

Strategy: validation

Validate before calling

// before enabling kubectl port-forwarding, verify the kube context works
ctxName := kubeContext
if ctxName == "" { ctxName = "current" }
out, err := exec.CommandContext(ctx, "kubectl", "config", "get-contexts",
    "--output=name").CombinedOutput()
if err != nil || !strings.Contains(string(out), ctxName) {
    return fmt.Errorf("kube context %q not found in kubeconfig: %s", ctxName, out)
}

Try / catch

// wrap and surface the wrapped cause
pod, port, err := findNewestPodForService(ctx, kubeContext, ns, svc, port)
if err != nil {
    var cmdErr *cmdutil.ExitError
    if errors.As(err, &cmdErr) { /* kubeconfig/context problem */ }
    log.Entry(ctx).Fatalf("cannot start port-forward: %v", err)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of GoogleContainerTools/skaffold@a1189de023 (2026-09-05). Data as JSON: /api/errors/0810c92b6d199b0a. Report an issue: GitHub.