kubernetes/kops · error

getting rest config: %w

Error message

getting rest config: %w

What it means

When not running with --cloud-only, RunDeleteInstance needs an in-cluster Kubernetes API client to drain/delete the node. It obtains the REST config via f.RESTConfig(ctx, cluster, options.CreateKubecfgOptions); failure (missing or unusable kubeconfig, unreachable cluster admin endpoint) is wrapped as "getting rest config: ...".

Source

Thrown at cmd/kops/delete_instance.go:177

}

func RunDeleteInstance(ctx context.Context, f *util.Factory, out io.Writer, options *DeleteInstanceOptions) error {
	clientSet, err := f.KopsClient()
	if err != nil {
		return err
	}

	cluster, err := GetCluster(ctx, f, options.ClusterName)
	if err != nil {
		return err
	}

	var k8sClient kubernetes.Interface
	var restConfig *rest.Config
	if !options.CloudOnly {
		restConfig, err = f.RESTConfig(ctx, cluster, options.CreateKubecfgOptions)
		if err != nil {
			return fmt.Errorf("getting rest config: %w", err)
		}

		httpClient, err := f.HTTPClient(restConfig)
		if err != nil {
			return fmt.Errorf("getting http client: %w", err)
		}

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

	var nodes []v1.Node
	if !options.CloudOnly {
		nodes, err = getNodes(ctx, k8sClient, true)
		if err != nil {
			return err

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Run `kops export kubeconfig <cluster> --admin` (or `kops export kubecfg`) to generate a valid kubeconfig, then retry.
  2. Verify KUBECONFIG points at the right file and context exists: `kubectl config get-contexts`.
  3. If you cannot reach the cluster's API, re-run with `--cloud-only` to skip Kubernetes interaction.

Example fix

// before
kops delete instance i-0abc123 --name cluster.example.com
// after
kops export kubeconfig cluster.example.com --admin
kops delete instance i-0abc123 --name cluster.example.com  # or add --cloud-only
Defensive patterns

Strategy: validation

Validate before calling

cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
    clientcmd.NewDefaultClientConfigLoadingRules(), nil).ClientConfig()
if err != nil {
    return fmt.Errorf("run `kops export kubeconfig <cluster> --admin` first: %w", err)
}

Try / catch

if err := run(...); err != nil && strings.Contains(err.Error(), "getting rest config") {
    // regenerate kubeconfig via `kops export kubeconfig` or fall back to --cloud-only
}

Prevention

When it happens

Trigger: f.RESTConfig returns an error: no kubeconfig at the default path, context for the cluster absent, invalid credentials, or kOps cannot build a config for the cluster's API endpoint.

Common situations: KUBECONFIG pointing to an empty file; deleting an instance of a cluster whose admin kubeconfig was never fetched (`kops export kubeconfig`); running from outside the VPC where the API server is unreachable.

Related errors


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