kubernetes/kops · error

cannot load kubecfg settings for %q: %w

Error message

cannot load kubecfg settings for %q: %w

What it means

kops wraps the error returned by client-go's genericclioptions ConfigFlags.ToRESTConfig() when it fails to build a REST config from the local kubeconfig for the cluster's context (the cluster name is used as the context name). This happens inside getKubeClientFromKubeconfig, used by shell completion in `kops delete instance`. The wrapped error typically comes from client-go's DeferredLoadingClientConfig: the kubeconfig file is missing, unreadable, or does not contain the requested context.

Source

Thrown at cmd/kops/delete_instance.go:399

				completions = appendInstance(completions, instance, longestGroup)
			}
		}

		return completions, cobra.ShellCompDirectiveNoFileComp
	}
}

// getKubeClientFromKubeconfig returns a kubernetes client from the kubeconfig,
// assuming it has already been exported.  This is not ideal, but is reasonable
// for command completion.
func getKubeClientFromKubeconfig(ctx context.Context, cluster *kopsapi.Cluster) (kubernetes.Interface, error) {
	contextName := cluster.ObjectMeta.Name
	clientGetter := genericclioptions.NewConfigFlags(true)
	clientGetter.Context = &contextName

	config, err := clientGetter.ToRESTConfig()
	if err != nil {
		return nil, fmt.Errorf("cannot load kubecfg settings for %q: %w", contextName, err)
	}

	k8sClient, err := kubernetes.NewForConfig(config)
	if err != nil {
		return nil, fmt.Errorf("cannot build kube client for %q: %w", contextName, err)
	}
	return k8sClient, nil
}

func appendInstance(completions []string, instance *cloudinstances.CloudInstance, longestGroup int) []string {
	completion := instance.ID
	if instance.CloudInstanceGroup.InstanceGroup != nil {
		completion += "\t" + instance.CloudInstanceGroup.InstanceGroup.Name

		if instance.Node != nil {
			padding := strings.Repeat(" ", longestGroup+1-len(instance.CloudInstanceGroup.InstanceGroup.Name))
			completion += padding + instance.Node.Name
			completions = append(completions, instance.Node.Name+"\t"+instance.CloudInstanceGroup.InstanceGroup.Name+padding+instance.ID)

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Export the cluster's kubeconfig first: `kops export kubecfg <cluster-name>`.
  2. Verify the context exists: `kubectl config get-contexts` and confirm a context matching the kops cluster name.
  3. Check KUBECONFIG points at the right file, or pass the correct --kubeconfig path.
  4. Re-run with the exact cluster name used as the kops --name value.

Example fix

// before
$ kops delete instance --name prod.example.com --instance i-abc123
error: cannot load kubecfg settings for "prod.example.com": context "prod.example.com" does not exist
// after
$ kops export kubecfg prod.example.com
$ kops delete instance --name prod.example.com --instance i-abc123
Defensive patterns

Strategy: validation

Validate before calling

contextName="$(kops get cluster --name "$CLUSTER" -o name)"
if ! kubectl config get-contexts -o name | grep -qx "$CLUSTER"; then
  kops export kubecfg "$CLUSTER"
fi

Prevention

When it happens

Trigger: Running `kops delete instance` (or shell completion for it) when KUBECONFIG/~/.kube/config does not contain a context named exactly <cluster-name>, the kubeconfig path is wrong (no file found, or --kubeconfig not set), the context exists but is missing user/cluster credentials, or the file is malformed YAML.

Common situations: Developer never ran `kops export kubecfg <cluster>` so the context is absent; KUBECONFIG env var points at a different file; running on a machine/CI without the exported admin kubeconfig; context renamed after cluster name change; typo'd cluster name.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/d61a2e624de9a9be. Report an issue: GitHub.