{"record":{"id":"afd64fe14f071650","repo":"kubernetes/kops","slug":"cannot-build-kube-client-for-q-w","errorCode":null,"errorMessage":"cannot build kube client for %q: %w","messagePattern":"cannot build kube client for %q: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/delete_instance.go","lineNumber":404,"sourceCode":"\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)\n\t\t}\n\t}\n\treturn append(completions, completion)\n}\n","sourceCodeStart":386,"sourceCodeEnd":422,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/delete_instance.go#L386-L422","documentation":"kops wraps the error from kubernetes.NewForConfig(config) when a REST config was successfully loaded from the kubeconfig but a Kubernetes clientset could not be constructed from it. This is rarer than the config-load failure and typically indicates invalid REST config content (e.g. malformed host URL, unsupported/unparseable auth scheme, invalid certificate data in the context's user entry).","triggerScenarios":"Calling getKubeClientFromKubeconfig with a kubeconfig whose context has a malformed server URL (missing scheme), corrupt certificate/base64 data, an exec auth plugin that fails config validation, or an otherwise invalid config for client-go's clientset constructor.","commonSituations":"Hand-edited ~/.kube/config with a typo'd server URL or broken certificate; kops export produced credentials for a CA that has been rotated/replaced; exec credential plugin (aws eks get-token etc.) not installed or misconfigured; version mismatch between kubeconfig fields and the client-go version.","solutions":["Inspect the wrapped error: it names the invalid field (host URL, cert data, auth plugin). Fix that field in the kubeconfig.","Re-export a fresh kubeconfig: `kops export kubecfg <cluster-name>` to regenerate valid certificate/host data.","Validate the file with `kubectl config view --raw` and try `kubectl --context <cluster-name> get nodes` to confirm the config works outside kops.","Reinstall or fix the exec credential plugin referenced by the context's user."],"exampleFix":"// before (hand-edited kubeconfig)\nserver: prod.example.com  # missing scheme -> NewForConfig fails\n// after\nserver: https://api.prod.example.com","handlingStrategy":"validation","validationCode":"kubectl config view --raw --context \"$CLUSTER\" >/dev/null && kubectl --context \"$CLUSTER\" get --raw /healthz >/dev/null || echo \"invalid kubeconfig for $CLUSTER\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never hand-edit certificate blocks in kubeconfig; re-export with kops export kubecfg.","Keep exec credential plugins (aws/azure/gcloud) installed and on PATH.","Validate with kubectl before using kops automation against the context."],"tags":["kubernetes","kubeconfig","client-go","client-construction"],"backgroundTag":"invalid-rest-config","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"}