GoogleContainerTools/skaffold · error

error getting Kubernetes dynamic client: %w

Error message

error getting Kubernetes dynamic client: %w

What it means

Skaffold's deploy labeling step applies skaffold watermark labels to all deployed Kubernetes objects using the dynamic client. This error wraps any failure from kubernetesclient.DynamicClient(kubeContext), which builds a dynamic.Interface from the kubeconfig for the given context. It means the dynamic Kubernetes client could not be constructed, almost always because the kubeconfig is missing, malformed, or references an unknown context.

Source

Thrown at pkg/skaffold/deploy/label/labels.go:54

	"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
)

// retry 3 times to give the object time to propagate to the API server
const (
	tries     = 3
	sleeptime = 300 * time.Millisecond
)

// Apply applies all provided labels to the created Kubernetes resources
func Apply(ctx context.Context, labels map[string]string, results []deploy.Artifact, kubeContext string) error {
	if len(labels) == 0 {
		return nil
	}

	// use the kubectl client to update all k8s objects with a skaffold watermark
	dynClient, err := kubernetesclient.DynamicClient(kubeContext)
	if err != nil {
		return fmt.Errorf("error getting Kubernetes dynamic client: %w", err)
	}

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

	for _, res := range results {
		err = nil
		for i := 0; i < tries; i++ {
			if err = updateRuntimeObject(ctx, dynClient, client.Discovery(), labels, res, kubeContext); err == nil {
				break
			}
			time.Sleep(sleeptime)
		}
		if err != nil {
			log.Entry(ctx).Warnf("error adding label to runtime object: %s", err.Error())
		}

View on GitHub (pinned to a1189de023)

Solutions

  1. Verify the kubeContext exists: run `kubectl config get-contexts` and use one of the listed names
  2. Check KUBECONFIG points to a valid file and `kubectl config view` parses without error
  3. Regenerate kubeconfig (e.g. re-login with your cloud CLI: gcloud, aws eks update-kubeconfig, az aks get-credentials)
  4. If running in-cluster, ensure a ServiceAccount token is mounted and the context is set correctly

Example fix

// before
skaffold deploy --kubeconfig /stale/path/config
// after
export KUBECONFIG=$HOME/.kube/config
skaffold deploy --kubeconfig $HOME/.kube/config
Defensive patterns

Strategy: validation

Validate before calling

out, err := exec.Command("kubectl", "config", "get-contexts").CombinedOutput()
if err != nil || !strings.Contains(string(out), kubeContext) {
    return fmt.Errorf("kubeContext %q not found in kubeconfig: %s", kubeContext, out)
}

Try / catch

if _, err := kubernetesclient.DynamicClient(kubeContext); err != nil {
    log.Fatalf("cannot build dynamic client for context %q: %v — check KUBECONFIG and context name", kubeContext, err)
}

Prevention

When it happens

Trigger: Apply() is called after a deploy with a kubeContext that does not exist in kubeconfig; KUBECONFIG env var points to a missing or unparseable file; kubeconfig YAML is invalid; clientcmd fails to build a RESTConfig for the context.

Common situations: CI environments with no ~/.kube/config; users switching clusters and removing a context; KUBECONFIG pointing at a stale path; malformed kubeconfig after manual edits.

Related errors


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