GoogleContainerTools/skaffold · error

getting Kubernetes client: %w

Error message

getting Kubernetes client: %w

What it means

containerHook.run executes lifecycle hooks inside containers of running pods. It first builds a Kubernetes client via kubernetesclient.Client for the configured kube context. If client construction fails, the error is wrapped as 'getting Kubernetes client: %w'.

Source

Thrown at pkg/skaffold/hooks/container.go:97

	}
}

// containerHook represents a lifecycle hook to be executed inside a running container
type containerHook struct {
	cfg        latest.ContainerHook
	cli        *kubectl.CLI
	selector   containerSelector
	namespaces []string
	formatter  logger.Formatter
}

// run executes the lifecycle hook inside the target container
func (h containerHook) run(ctx context.Context, out io.Writer) error {
	errs, ctx := errgroup.WithContext(ctx)

	client, err := kubernetesclient.Client(h.cli.KubeContext)
	if err != nil {
		return fmt.Errorf("getting Kubernetes client: %w", err)
	}

	for _, ns := range h.namespaces {
		pods, err := client.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{})
		if err != nil {
			return fmt.Errorf("getting pods for namespace %q: %w", ns, err)
		}

		for _, p := range pods.Items {
			for _, c := range p.Spec.Containers {
				if matched, err := h.selector(p, c); err != nil {
					return err
				} else if !matched {
					continue
				}
				args := []string{p.Name, "--namespace", p.Namespace, "-c", c.Name, "--"}
				args = append(args, h.cfg.Command...)
				cmd := h.cli.Command(ctx, "exec", args...)

View on GitHub (pinned to a1189de023)

Solutions

  1. Verify the context exists: kubectl config get-contexts, and fix the kubeContext in skaffold.yaml
  2. Check KUBECONFIG points to a valid, readable kubeconfig file
  3. Run 'kubectl --context <ctx> get pods' to confirm connectivity, then re-run skaffold
  4. Refresh credentials (e.g. gcloud auth login / aws eks update-kubeconfig) if auth is the cause

Example fix

// before (skaffold.yaml)
kubeContext: "staging-cluster"  // not in kubeconfig
// after, on the shell:
kubectl config get-contexts
skaffold deploy --kube-context staging-cluster  # or set a valid context
Defensive patterns

Strategy: retry

Validate before calling

ctxName := h.cli.KubeContext
if err := exec.Command("kubectl", "--context", ctxName, "get", "ns").Run(); err != nil {
    return fmt.Errorf("context %q unusable before hooks: %w", ctxName, err)
}

Try / catch

err := hook.Run(ctx, out)
if err != nil && strings.Contains(err.Error(), "getting Kubernetes client") {
    // brief retry for transient kubeconfig/auth issues
    return retry.Do(func() error { return hook.Run(ctx, out) }, retry.Attempts(3))
}

Prevention

When it happens

Trigger: Calling run (from RunPostHooks or RunPreHooks of a container hook) with a h.cli.KubeContext that cannot produce a client — missing/invalid kubeconfig, unreachable context, or failed REST config loading.

Common situations: KubeContext referencing a context absent from ~/.kube/config; corrupted or unreadable kubeconfig; KUBECONFIG env var pointing to a nonexistent file; cluster credentials expired/removed before hooks run.

Related errors


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