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
- Export the cluster's kubeconfig first: `kops export kubecfg <cluster-name>`.
- Verify the context exists: `kubectl config get-contexts` and confirm a context matching the kops cluster name.
- Check KUBECONFIG points at the right file, or pass the correct --kubeconfig path.
- 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
- Always run `kops export kubecfg <cluster>` after creating or switching clusters.
- Keep KUBECONFIG pointing at the file kops wrote; avoid ad-hoc overrides.
- Verify with `kubectl config get-contexts` before kops commands that touch the API.
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
- cannot build kube client: %w
- cannot build kube client for %q: %w
- building kubernetes client: %w
- error creating pod log dumper: %w
- building kubernetes client for node labeler: %w
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/d61a2e624de9a9be.
Report an issue: GitHub.