GoogleContainerTools/skaffold · error

getting current cluster context: %w

Error message

getting current cluster context: %w

What it means

After applying profiles, Skaffold checks kube-context consistency between context-activated profiles and the effective deploy.kubeContext. To do so it reads the current context from the local kubeconfig via kubectx.CurrentConfig; if the kubeconfig cannot be loaded or parsed, the failure is wrapped as `getting current cluster context: %w`. The check is skipped entirely when --kubecontext is passed on the CLI.

Source

Thrown at pkg/skaffold/schema/profiles.go:78

	}

	// remove profiles section for run modes where profiles are already merged into the main pipeline
	switch opts.Mode() {
	case cfg.RunModes.Build, cfg.RunModes.Dev, cfg.RunModes.Deploy, cfg.RunModes.Debug, cfg.RunModes.Render, cfg.RunModes.Run, cfg.RunModes.Diagnose, cfg.RunModes.Delete:
		c.Profiles = nil
	}
	return profiles, fieldsOverrodeByProfile, checkKubeContextConsistency(contextSpecificProfiles, opts.KubeContext, c.Deploy.KubeContext)
}

func checkKubeContextConsistency(contextSpecificProfiles []string, cliContext, effectiveContext string) error {
	// cli flag takes precedence
	if cliContext != "" {
		return nil
	}

	kubeConfig, err := kubectx.CurrentConfig()
	if err != nil {
		return fmt.Errorf("getting current cluster context: %w", err)
	}
	currentContext := kubeConfig.CurrentContext

	// nothing to do
	if effectiveContext == "" || effectiveContext == currentContext || len(contextSpecificProfiles) == 0 {
		return nil
	}

	return fmt.Errorf("profiles %q were activated by kube-context %q, but the effective kube-context is %q -- please revise your `profiles.activation` and `deploy.kubeContext` configurations", contextSpecificProfiles, currentContext, effectiveContext)
}

// activatedProfiles returns the activated profiles and activated profiles which are kube-context specific.
// The latter matters for error reporting when the effective kube-context changes.
func activatedProfiles(profiles []latest.Profile, opts cfg.SkaffoldOptions, namedProfiles []string) ([]string, []string, error) {
	var activated []string
	var contextSpecificProfiles []string

	if opts.ProfileAutoActivation {

View on GitHub (pinned to a1189de023)

Solutions

  1. Ensure a valid kubeconfig exists at ~/.kube/config (e.g. `gcloud container clusters get-credentials` or copy from a working machine)
  2. Check the KUBECONFIG env var points at an existing, valid file (or unset it to use the default)
  3. Pass `--kubecontext <ctx>` explicitly, which short-circuits this kubeconfig lookup
  4. Read the wrapped inner error for the concrete parse/read failure and fix the kubeconfig file

Example fix

# before
export KUBECONFIG=~/.kube/nonexistent
# after
export KUBECONFIG=$HOME/.kube/config && kubectl config current-context
Defensive patterns

Strategy: fallback

Validate before calling

kubeconfig := os.Getenv("KUBECONFIG")
if kubeconfig == "" { kubeconfig = filepath.Join(homedir, ".kube", "config") }
if _, err := os.Stat(kubeconfig); err != nil {
    return fmt.Errorf("kubeconfig %s missing: run `gcloud container clusters get-credentials` first", kubeconfig)
}

Try / catch

if _, _, err := schema.ApplyProfiles(c, overrides, opts, named); err != nil {
    if strings.Contains(err.Error(), "getting current cluster context") {
        // retry with explicit kubecontext so the kubeconfig lookup is skipped
        opts.KubeContext = "default"
        return schema.ApplyProfiles(c, overrides, opts, named)
    }
    return err
}

Prevention

When it happens

Trigger: Auto-activated context-specific profiles exist (or deploy.kubeContext is set) and no --kubecontext flag is given, so checkKubeContextConsistency calls kubectx.CurrentConfig(); this fails when KUBECONFIG points at missing/unreadable/malformed files or the default ~/.kube/config is absent/corrupt.

Common situations: Running skaffold in CI without a kubeconfig (no cluster credentials set up); KUBECONFIG env var pointing to a nonexistent path; malformed YAML in ~/.kube/config; permissions issues after switching users or containers.

Related errors


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