{"record":{"id":"d61a2e624de9a9be","repo":"kubernetes/kops","slug":"cannot-load-kubecfg-settings-for-q-w","errorCode":null,"errorMessage":"cannot load kubecfg settings for %q: %w","messagePattern":"cannot load kubecfg settings for %q: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/delete_instance.go","lineNumber":399,"sourceCode":"\t\t\t\tcompletions = appendInstance(completions, instance, longestGroup)\n\t\t\t}\n\t\t}\n\n\t\treturn completions, cobra.ShellCompDirectiveNoFileComp\n\t}\n}\n\n// getKubeClientFromKubeconfig returns a kubernetes client from the kubeconfig,\n// assuming it has already been exported.  This is not ideal, but is reasonable\n// for command completion.\nfunc getKubeClientFromKubeconfig(ctx context.Context, cluster *kopsapi.Cluster) (kubernetes.Interface, error) {\n\tcontextName := cluster.ObjectMeta.Name\n\tclientGetter := genericclioptions.NewConfigFlags(true)\n\tclientGetter.Context = &contextName\n\n\tconfig, err := clientGetter.ToRESTConfig()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"cannot load kubecfg settings for %q: %w\", contextName, err)\n\t}\n\n\tk8sClient, err := kubernetes.NewForConfig(config)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"cannot build kube client for %q: %w\", contextName, err)\n\t}\n\treturn k8sClient, nil\n}\n\nfunc appendInstance(completions []string, instance *cloudinstances.CloudInstance, longestGroup int) []string {\n\tcompletion := instance.ID\n\tif instance.CloudInstanceGroup.InstanceGroup != nil {\n\t\tcompletion += \"\\t\" + instance.CloudInstanceGroup.InstanceGroup.Name\n\n\t\tif instance.Node != nil {\n\t\t\tpadding := strings.Repeat(\" \", longestGroup+1-len(instance.CloudInstanceGroup.InstanceGroup.Name))\n\t\t\tcompletion += padding + instance.Node.Name\n\t\t\tcompletions = append(completions, instance.Node.Name+\"\\t\"+instance.CloudInstanceGroup.InstanceGroup.Name+padding+instance.ID)","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/delete_instance.go#L381-L417","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n$ kops delete instance --name prod.example.com --instance i-abc123\nerror: cannot load kubecfg settings for \"prod.example.com\": context \"prod.example.com\" does not exist\n// after\n$ kops export kubecfg prod.example.com\n$ kops delete instance --name prod.example.com --instance i-abc123","handlingStrategy":"validation","validationCode":"contextName=\"$(kops get cluster --name \"$CLUSTER\" -o name)\"\nif ! kubectl config get-contexts -o name | grep -qx \"$CLUSTER\"; then\n  kops export kubecfg \"$CLUSTER\"\nfi","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["kubernetes","kubeconfig","cli","client-go"],"backgroundTag":"kubeconfig-context-not-found","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}