GoogleContainerTools/skaffold · error

getting Kubernetes client: %w

Error message

getting Kubernetes client: %w

What it means

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.

Source

Thrown at pkg/skaffold/kubernetes/portforward/resource_forwarder.go:178

		return entry
	}

	// Try to request matching local port *providing* that it is not a system port.
	// https://github.com/GoogleContainerTools/skaffold/pull/5554#issuecomment-803270340
	requestPort := resource.LocalPort
	if requestPort == 0 && resource.Port.IntVal >= 1024 {
		requestPort = resource.Port.IntVal
	}
	entry.localPort = retrieveAvailablePort(resource.Address, requestPort, &p.entryManager.forwardedPorts)
	return entry
}

// retrieveServiceResources retrieves all services in the cluster matching the given label
// as a list of PortForwardResources
func retrieveServiceResources(ctx context.Context, label string, namespaces []string, kubeContext string) ([]*latest.PortForwardResource, error) {
	client, err := kubernetesclient.Client(kubeContext)
	if err != nil {
		return nil, fmt.Errorf("getting Kubernetes client: %w", err)
	}

	var resources []*latest.PortForwardResource
	for _, ns := range namespaces {
		services, err := client.CoreV1().Services(ns).List(ctx, metav1.ListOptions{
			LabelSelector: label,
		})
		if err != nil {
			return nil, fmt.Errorf("selecting services by label %q: %w", label, err)
		}
		for _, s := range services.Items {
			for _, p := range s.Spec.Ports {
				resources = append(resources, &latest.PortForwardResource{
					Type:      constants.Service,
					Name:      s.Name,
					Namespace: s.Namespace,
					Port:      schemautil.FromInt(int(p.Port)),
					Address:   constants.DefaultPortForwardAddress,

View on GitHub (pinned to a1189de023)

Solutions

  1. Run kubectl config current-context and kubectl cluster-info to validate the kubeconfig and context
  2. Point KUBECONFIG at a valid file or recreate the cluster context (e.g. minikube/kind/GKE auth login)
  3. Fix kubeconfig syntax errors reported by kubectl
  4. Ensure the kubeconfig is available where skaffold runs (mount it in CI containers)

Example fix

// diagnostic before running
//   KUBECONFIG=~/.kube/config kubectl config get-contexts
// before (skaffold.yaml / shell)
kubeContext: old-deleted-cluster
// after
export KUBECONFIG=~/.kube/config
kubeContext: kind-my-cluster  # must exist in `kubectl config get-contexts`
Defensive patterns

Strategy: validation

Validate before calling

func kubeContextUsable(kubeContext string) error {
    out, err := exec.Command("kubectl", "--context", kubeContext, "cluster-info").CombinedOutput()
    if err != nil {
        return fmt.Errorf("context %q unusable: %v: %s", kubeContext, err, out)
    }
    return nil
}

Try / catch

client, err := kubernetesclient.Client(kubeContext)
if err != nil {
    return nil, fmt.Errorf("check KUBECONFIG and context %q: %w", kubeContext, err)
}

Prevention

When it happens

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

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

Related errors


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