{"record":{"id":"1c9a16df819a634e","repo":"kubernetes/kops","slug":"building-kubernetes-scheme-w","errorCode":null,"errorMessage":"building kubernetes scheme: %w","messagePattern":"building kubernetes scheme: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/toolbox_enroll.go","lineNumber":217,"sourceCode":"\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\thost := &v1alpha2.Host{}\n\thost.SetGroupVersionKind(v1alpha2.SchemeGroupVersion.WithKind(\"Host\"))\n\thost.Namespace = \"kops-system\"\n\thost.Name = hostname\n\thost.Spec.InstanceGroup = options.InstanceGroup\n\thost.Spec.PublicKey = string(publicKeyBytes)\n\thost.Spec.PodCIDRs = options.PodCIDRs\n\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)","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/commands/toolbox_enroll.go#L199-L235","documentation":"enrollHost builds a Kubernetes client scheme that includes the Cluster API provider's v1alpha2 Host CRD before creating a REST client to register the host. If `v1alpha2.AddToScheme` fails, the error is wrapped as \"building kubernetes scheme\". This almost always indicates a problem with the compiled-in scheme/registry rather than the cluster itself.","triggerScenarios":"Calling enrollHost when the v1alpha2 API package cannot register its types into a fresh runtime.Scheme — typically due to duplicate/schematically invalid type registrations, or a build with mismatched k8s.io/apimachinery / cluster-api provider versions.","commonSituations":"Custom kops builds mixing incompatible dependency versions (go.mod conflicts between sigs.k8s.io/cluster-api providers); running a kops binary too old/new for the CRDs installed in the cluster.","solutions":["Rebuild kops cleanly (`make kops`) so dependency versions in go.mod are consistent","Align your cluster's installed Host CRD version with what your kops binary supports (check `kops version` and CRD apiVersion v1alpha2)","Read the wrapped inner error for the exact conflicting type and resolve duplicate registrations in any custom code"],"exampleFix":"// before: mixed versions in go.mod\n// after\ngo mod tidy && make kops","handlingStrategy":"try-catch","validationCode":"// verify the CRD exists and kops binary version matches your build\nkubectl get crd hosts.v1alpha2.<provider-group> || echo \"Host CRD missing\"\nkops version","typeGuard":null,"tryCatchPattern":"if err := RunToolboxEnroll(...); err != nil && strings.Contains(err.Error(), \"building kubernetes scheme\") {\n    log.Printf(\"scheme registration failed; rebuild kops with consistent deps: %v\", err)\n    return err\n}","preventionTips":["Build kops from a clean checkout with `go mod tidy`","Keep Host CRDs on the apiVersion your kops binary supports","Avoid patching kops with ad-hoc dependency overrides"],"tags":["kubernetes","scheme","cluster-api","bare-metal"],"backgroundTag":"scheme-registration-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"}