{"record":{"id":"2790e4549e6df0b9","repo":"kubernetes/kops","slug":"building-kubernetes-client-w-2790e4","errorCode":null,"errorMessage":"building kubernetes client: %w","messagePattern":"building kubernetes client: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/toolbox_enroll.go","lineNumber":228,"sourceCode":"\n\treturn host, nil\n}\n\nfunc enrollHost(ctx context.Context, ig *kops.InstanceGroup, bootstrapData *BootstrapData, restConfig *rest.Config, hostData *v1alpha2.Host, sshTarget *SSHHost) error {\n\tscheme := runtime.NewScheme()\n\tif err := v1alpha2.AddToScheme(scheme); err != nil {\n\t\treturn fmt.Errorf(\"building kubernetes scheme: %w\", err)\n\t}\n\t// Ensure that we don't try to use proto with our CRD\n\trestConfigNoProto := rest.CopyConfig(restConfig)\n\trestConfigNoProto.ContentType = runtime.ContentTypeJSON\n\trestConfigNoProto.AcceptContentTypes = runtime.ContentTypeJSON\n\n\tkubeClient, err := client.New(restConfigNoProto, client.Options{\n\t\tScheme: scheme,\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"building kubernetes client: %w\", err)\n\t}\n\n\t// We can't create the host resource in the API server for control-plane nodes,\n\t// because the API server (likely) isn't running yet.\n\tif !ig.IsControlPlane() {\n\t\tif err := kubeClient.Create(ctx, hostData); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to create host %s/%s: %w\", hostData.Namespace, hostData.Name, err)\n\t\t}\n\t}\n\n\tfor k, v := range bootstrapData.NodeupScriptAdditionalFiles {\n\t\tif err := sshTarget.writeFile(ctx, k, bytes.NewReader(v)); err != nil {\n\t\t\treturn fmt.Errorf(\"writing file %q over SSH: %w\", k, err)\n\t\t}\n\t}\n\n\tif len(bootstrapData.NodeupScript) != 0 {\n\t\tif _, err := sshTarget.runScript(ctx, string(bootstrapData.NodeupScript), ExecOptions{Echo: true}); err != nil {","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/commands/toolbox_enroll.go#L210-L246","documentation":"enrollHost builds a controller-runtime kubernetes client with a scheme registering the kops v1alpha2 Host CRD. client.New fails before any API call if the rest.Config is malformed or the client/scheme cannot be constructed (e.g. invalid REST config options). This wraps that construction failure.","triggerScenarios":"Running kops toolbox enroll when the supplied rest.Config is invalid or client.New cannot build a client from it — e.g. a nil/empty host in the kubeconfig, unsupported config fields, or a bad content-type/negotiation setting.","commonSituations":"Stale or corrupt kubeconfig (empty server URL), using an in-cluster config outside a cluster, or passing a rest.Config produced by a failed config-loading step.","solutions":["Verify the kubeconfig pointed at by --kubeconfig / KUBECONFIG has a valid server URL and CA data: kubectl cluster-info","Re-fetch cluster credentials (kops export kubeconfig <cluster>) and retry","Enable KUBERNETES_SERVICE_HOST/PORT or use a valid kubeconfig if relying on in-cluster/rest.InClusterConfig","Check the underlying wrapped error message for the exact invalid field"],"exampleFix":"// before\nrestConfig, err := clientcmd.BuildConfigFromFlags(\"\", \"\")\nkubeClient, _ := client.New(restConfig, client.Options{Scheme: scheme})\n// after\nrestConfig, err := clientcmd.BuildConfigFromFlags(\"\", kubeconfigPath)\nif err != nil { return fmt.Errorf(\"loading kubeconfig: %w\", err) }\nif restConfig.Host == \"\" { return fmt.Errorf(\"kubeconfig has empty server URL\") }\nkubeClient, err := client.New(restConfig, client.Options{Scheme: scheme})\nif err != nil { return fmt.Errorf(\"building kubernetes client: %w\", err) }","handlingStrategy":"validation","validationCode":"if restConfig == nil || restConfig.Host == \"\" {\n    return fmt.Errorf(\"rest.Config is nil or has empty Host; export a valid kubeconfig first\")\n}\nif err := v1alpha2.AddToScheme(runtime.NewScheme()); err != nil {\n    return fmt.Errorf(\"scheme registration broken: %w\", err)\n}","typeGuard":"func validRestConfig(c *rest.Config) bool { return c != nil && c.Host != \"\" }","tryCatchPattern":"kubeClient, err := client.New(restConfigNoProto, client.Options{Scheme: scheme})\nif err != nil {\n    return fmt.Errorf(\"building kubernetes client: %w\", err)\n}","preventionTips":["Always validate kubeconfig with kubectl cluster-info before running enroll","Regain fresh credentials via kops export kubeconfig after credential rotation","Check the wrapped error text; it names the exact invalid config field"],"tags":["kubernetes","client","config"],"backgroundTag":"kubernetes-client-init-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}