ahmetb/kubectx · error

failed to initialize k8s REST client: %w

Error message

failed to initialize k8s REST client: %w

What it means

newKubernetesClientSet failed to build a client-go clientset from the kubeconfig: either client-go could not load the config (broken/invalid kubeconfig content beyond what kyaml accepted) or REST config construction failed for the current context's cluster/user.

Source

Thrown at cmd/kubens/list.go:85

	for _, c := range ns {
		s := c
		if c == curNs {
			s = printer.ActiveItemColor.Sprint(c)
		}
		fmt.Fprintf(w, "%s\n", s)
	}
	return nil
}

func queryNamespaces(kc *kubeconfig.Kubeconfig) ([]string, error) {
	if os.Getenv("_MOCK_NAMESPACES") != "" {
		return []string{"ns1", "ns2"}, nil
	}

	clientset, err := newKubernetesClientSet(kc)
	if err != nil {
		return nil, fmt.Errorf("failed to initialize k8s REST client: %w", err)
	}

	var out []string
	var next string
	for {
		list, err := clientset.CoreV1().Namespaces().List(
			context.Background(),
			metav1.ListOptions{
				Limit:    500,
				Continue: next,
			})
		if err != nil {
			return nil, fmt.Errorf("failed to list namespaces from k8s API: %w", err)
		}
		next = list.Continue
		out = slices.Grow(out, len(list.Items))
		for _, it := range list.Items {
			out = append(out, it.Name)

View on GitHub (pinned to 12ad6fb22e)

Solutions

  1. Run kubectl config view to see if client-go can parse the same config
  2. Check the cluster and user entries of the current context for missing/invalid fields
  3. Regenerate the context with the provider tool (e.g. gcloud/eks ctl) if entries are corrupt
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at cmd/kubens/list.go:85 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of ahmetb/kubectx@12ad6fb22e (2026-09-02). Data as JSON: /api/errors/4d18afe729c455e3. Report an issue: GitHub.