kubernetes/kops · error

building kubernetes client: %w

Error message

building kubernetes client: %w

What it means

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.

Source

Thrown at pkg/commands/toolbox_enroll.go:228

	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)
		}
	}

	for k, v := range bootstrapData.NodeupScriptAdditionalFiles {
		if err := sshTarget.writeFile(ctx, k, bytes.NewReader(v)); err != nil {
			return fmt.Errorf("writing file %q over SSH: %w", k, err)
		}
	}

	if len(bootstrapData.NodeupScript) != 0 {
		if _, err := sshTarget.runScript(ctx, string(bootstrapData.NodeupScript), ExecOptions{Echo: true}); err != nil {

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Verify the kubeconfig pointed at by --kubeconfig / KUBECONFIG has a valid server URL and CA data: kubectl cluster-info
  2. Re-fetch cluster credentials (kops export kubeconfig <cluster>) and retry
  3. Enable KUBERNETES_SERVICE_HOST/PORT or use a valid kubeconfig if relying on in-cluster/rest.InClusterConfig
  4. Check the underlying wrapped error message for the exact invalid field

Example fix

// before
restConfig, err := clientcmd.BuildConfigFromFlags("", "")
kubeClient, _ := client.New(restConfig, client.Options{Scheme: scheme})
// after
restConfig, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
if err != nil { return fmt.Errorf("loading kubeconfig: %w", err) }
if restConfig.Host == "" { return fmt.Errorf("kubeconfig has empty server URL") }
kubeClient, err := client.New(restConfig, client.Options{Scheme: scheme})
if err != nil { return fmt.Errorf("building kubernetes client: %w", err) }
Defensive patterns

Strategy: validation

Validate before calling

if restConfig == nil || restConfig.Host == "" {
    return fmt.Errorf("rest.Config is nil or has empty Host; export a valid kubeconfig first")
}
if err := v1alpha2.AddToScheme(runtime.NewScheme()); err != nil {
    return fmt.Errorf("scheme registration broken: %w", err)
}

Type guard

func validRestConfig(c *rest.Config) bool { return c != nil && c.Host != "" }

Try / catch

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

Prevention

When it happens

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

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

Related errors


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