GoogleContainerTools/skaffold · error

getting client config for Kubernetes client: %w

Error message

getting client config for Kubernetes client: %w

What it means

getClientset builds a Kubernetes typed clientset from a rest config produced by context.GetRestClientConfig(kubeContext). If that config construction fails (kubeconfig unreadable, context missing, in-cluster fallback failing), the wrapped error is returned. The result is memoized, so the first clientset request triggers it.

Source

Thrown at pkg/skaffold/kubernetes/client/client.go:39

	"k8s.io/client-go/dynamic"
	"k8s.io/client-go/kubernetes"
	_ "k8s.io/client-go/plugin/pkg/client/auth" // Initialize all known client auth plugins

	"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/kubernetes/context"
)

// for tests
var (
	Client        = getClientset
	DynamicClient = getDynamicClient
	DefaultClient = getDefaultClientset
)

func getClientset(kubeContext string) (kubernetes.Interface, error) {
	config, err := context.GetRestClientConfig(kubeContext)
	if err != nil {
		return nil, fmt.Errorf("getting client config for Kubernetes client: %w", err)
	}
	return kubernetes.NewForConfig(config)
}

func getDynamicClient(kubeContext string) (dynamic.Interface, error) {
	config, err := context.GetRestClientConfig(kubeContext)
	if err != nil {
		return nil, fmt.Errorf("getting client config for dynamic client: %w", err)
	}
	return dynamic.NewForConfig(config)
}

func getDefaultClientset() (kubernetes.Interface, error) {
	config, err := context.GetDefaultRestClientConfig()
	if err != nil {
		return nil, fmt.Errorf("getting client config for Kubernetes client: %w", err)
	}
	return kubernetes.NewForConfig(config)

View on GitHub (pinned to a1189de023)

Solutions

  1. Run `kubectl config get-contexts` to confirm the kubeContext exists and kubeconfig is valid
  2. Fix KUBECONFIG / --kubeconfig to point at a readable kubeconfig
  3. If no kubeconfig is intended, run inside a pod with a service account or provide valid in-cluster env

Example fix

// before: KUBECONFIG=/wrong/path -> config load fails
// after
export KUBECONFIG=$HOME/.kube/config
kubectl config current-context
Defensive patterns

Strategy: validation

Validate before calling

rules := clientcmd.NewDefaultClientConfigLoadingRules()
raw, err := rules.Load()
if err != nil {
    return fmt.Errorf("kubeconfig invalid: %w", err)
}
if _, ok := raw.Contexts[kubeContext]; !ok {
    return fmt.Errorf("kubeContext %q not found; run kubectl config get-contexts", kubeContext)
}

Type guard

func kubeContextExists(kubeContext string) bool {
    raw, _ := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
        clientcmd.NewDefaultClientConfigLoadingRules(), nil).RawConfig()
    _, ok := raw.Contexts[kubeContext]
    return ok
}

Try / catch

clientset, err := client.GetClientset(kubeContext)
if err != nil {
    return fmt.Errorf("check KUBECONFIG and `kubectl config get-contexts` for %q: %w", kubeContext, err)
}

Prevention

When it happens

Trigger: GetClientset(kubeContext) when the kubeconfig file is missing/malformed, the named kubeContext has no cluster, or when no context and no kubeconfig exist and InClusterConfig() also fails (not running in a pod).

Common situations: Wrong KUBECONFIG path, invalid YAML in ~/.kube/config, referencing a context that was deleted, running a command expecting in-cluster credentials outside a cluster.

Related errors


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