kubernetes/kops · error

building kubernetes client: %w

Error message

building kubernetes client: %w

What it means

After building a REST config and HTTP client from the cluster's kubeconfig material, RunGetInstances constructs a client-go kubernetes clientset with kubernetes.NewForConfigAndClient. If clientset construction fails, the error is wrapped as "building kubernetes client: %w". This indicates malformed or incompatible client configuration (host, TLS material, content type, API version skew), not a network failure.

Source

Thrown at cmd/kops/get_instances.go:125

	cloud, err := cloudup.BuildCloud(cluster)
	if err != nil {
		return err
	}

	restConfig, err := f.RESTConfig(ctx, cluster, options.CreateKubecfgOptions)
	if err != nil {
		return err
	}

	httpClient, err := f.HTTPClient(restConfig)
	if err != nil {
		return err
	}

	k8sClient, err := kubernetes.NewForConfigAndClient(restConfig, httpClient)
	if err != nil {
		return fmt.Errorf("building kubernetes client: %w", err)
	}

	nodeList, err := k8sClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
	if err != nil {
		klog.Warningf("cannot list node names. Kubernetes API unavailable: %v", err)
	}

	igList, err := clientset.InstanceGroupsFor(cluster).List(ctx, metav1.ListOptions{})
	if err != nil {
		return err
	}

	var instanceGroups []*kops.InstanceGroup
	for i := range igList.Items {
		instanceGroups = append(instanceGroups, &igList.Items[i])
	}

	var cloudInstances []*cloudinstances.CloudInstance

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Inspect the wrapped inner error after "building kubernetes client:" to see exactly which config field is invalid
  2. Regenerate the kubeconfig via `kops export kubecfg <cluster> --admin` so TLS material and server URL are correct
  3. Validate the resulting context with `kubectl cluster-info` using the same kubeconfig
  4. Check the cluster spec (`kops get cluster -o yaml`) for a bogus/kubernetesApi endpoint value and fix or roll back the change
  5. Upgrade/rebuild kops so client-go matches the target API server version

Example fix

// before: manually pasted, possibly invalid kubeconfig
kubectl config use-context broken-context
// after
kops export kubecfg mycluster.example.com --admin
kops get instances mycluster.example.com
Defensive patterns

Strategy: validation

Validate before calling

// validate kubeconfig before running the command
if ! kubectl --context "$CTX" cluster-info >/dev/null 2>&1; then
  kops export kubecfg "$CLUSTER" --admin || exit 1
fi
kops get instances "$CLUSTER"

Type guard

func buildClientOrErr(restConfig *rest.Config, hc *http.Client) (*kubernetes.Clientset, error) {
    if restConfig == nil || restConfig.Host == "" {
        return nil, fmt.Errorf("rest config has no host")
    }
    return kubernetes.NewForConfigAndClient(restConfig, hc)
}

Try / catch

k8sClient, err := kubernetes.NewForConfigAndClient(restConfig, httpClient)
if err != nil {
    return fmt.Errorf("building kubernetes client: %w (check kubeconfig TLS material and server URL)", err)
}

Prevention

When it happens

Trigger: `kops get instances` when restConfig contains an invalid host URL, unparseable certificate/key data, or unsupported config fields, causing kubernetes.NewForConfigAndClient (get_instances.go:123) to return a non-nil err.

Common situations: Corrupt or hand-edited kubeconfig entries; empty certificate-authority-data or base64 encoding mistakes; cluster spec pointing at an unreachable/malformed API server URL; mismatched client-go version expectations; proxies injecting bad config.

Related errors


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