kubernetes/kops · error

building kubernetes scheme: %w

Error message

building kubernetes scheme: %w

What it means

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.

Source

Thrown at pkg/commands/toolbox_enroll.go:217

	if err != nil {
		return nil, err
	}

	host := &v1alpha2.Host{}
	host.SetGroupVersionKind(v1alpha2.SchemeGroupVersion.WithKind("Host"))
	host.Namespace = "kops-system"
	host.Name = hostname
	host.Spec.InstanceGroup = options.InstanceGroup
	host.Spec.PublicKey = string(publicKeyBytes)
	host.Spec.PodCIDRs = options.PodCIDRs

	return host, nil
}

func enrollHost(ctx context.Context, ig *kops.InstanceGroup, bootstrapData *BootstrapData, restConfig *rest.Config, hostData *v1alpha2.Host, sshTarget *SSHHost) error {
	scheme := runtime.NewScheme()
	if err := v1alpha2.AddToScheme(scheme); err != nil {
		return fmt.Errorf("building kubernetes scheme: %w", err)
	}
	// Ensure that we don't try to use proto with our CRD
	restConfigNoProto := rest.CopyConfig(restConfig)
	restConfigNoProto.ContentType = runtime.ContentTypeJSON
	restConfigNoProto.AcceptContentTypes = runtime.ContentTypeJSON

	kubeClient, err := client.New(restConfigNoProto, client.Options{
		Scheme: scheme,
	})
	if err != nil {
		return fmt.Errorf("building kubernetes client: %w", err)
	}

	// We can't create the host resource in the API server for control-plane nodes,
	// because the API server (likely) isn't running yet.
	if !ig.IsControlPlane() {
		if err := kubeClient.Create(ctx, hostData); err != nil {
			return fmt.Errorf("failed to create host %s/%s: %w", hostData.Namespace, hostData.Name, err)

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Rebuild kops cleanly (`make kops`) so dependency versions in go.mod are consistent
  2. Align your cluster's installed Host CRD version with what your kops binary supports (check `kops version` and CRD apiVersion v1alpha2)
  3. Read the wrapped inner error for the exact conflicting type and resolve duplicate registrations in any custom code

Example fix

// before: mixed versions in go.mod
// after
go mod tidy && make kops
Defensive patterns

Strategy: try-catch

Validate before calling

// verify the CRD exists and kops binary version matches your build
kubectl get crd hosts.v1alpha2.<provider-group> || echo "Host CRD missing"
kops version

Try / catch

if err := RunToolboxEnroll(...); err != nil && strings.Contains(err.Error(), "building kubernetes scheme") {
    log.Printf("scheme registration failed; rebuild kops with consistent deps: %v", err)
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/1c9a16df819a634e. Report an issue: GitHub.