kubernetes/kops · error

failed to prune objects of kind %s: %w

Error message

failed to prune objects of kind %s: %w

What it means

Prune iterates the PruneSpec.Kinds list and delegates to pruneObjectsOfKind for each GroupKind. Any failure inside (mapping, listing, deleting) is wrapped as `failed to prune objects of kind <gk>`, preserving the full chain (%w) so the root cause is inspectable with errors.As/Is.

Source

Thrown at channels/pkg/channels/prune.go:71

		gv, err := schema.ParseGroupVersion(object.APIVersion())
		if err != nil || gv.Version == "" {
			return fmt.Errorf("failed to parse apiVersion %q", object.APIVersion())
		}
		kind := object.Kind()
		if kind == "" {
			return fmt.Errorf("failed to find kind in object")
		}

		gvk := gv.WithKind(kind)
		gk := gvk.GroupKind()
		objectsByKind[gk] = append(objectsByKind[gk], object)
	}

	for i := range spec.Kinds {
		pruneKind := &spec.Kinds[i]
		gk := schema.GroupKind{Group: pruneKind.Group, Kind: pruneKind.Kind}
		if err := p.pruneObjectsOfKind(ctx, gk, pruneKind, objectsByKind[gk]); err != nil {
			return fmt.Errorf("failed to prune objects of kind %s: %w", gk, err)
		}
	}

	return nil
}

func (p *Pruner) pruneObjectsOfKind(ctx context.Context, gk schema.GroupKind, spec *api.PruneKindSpec, keepObjects []*kubemanifest.Object) error {
	klog.Infof("pruning objects of kind: %v", gk)

	restMapping, err := p.RESTMapper.RESTMapping(gk)
	if err != nil {
		return fmt.Errorf("unable to find resource for %s: %w", gk, err)
	}

	gvr := restMapping.Resource

	var listOptions v1.ListOptions
	listOptions.LabelSelector = spec.LabelSelector

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Unwrap the chain (errors.Unwrap / %v of inner error) to see whether mapping, listing, or deleting failed
  2. Fix the specific inner cause (see errors 56–59 for each sub-case)
  3. Correct the Group/Kind spelling in the channels PruneSpec
  4. Verify RBAC allows list and delete for the kind being pruned

Example fix

// before
kinds:
- group: apps
  kind: Deploymnets   # typo
// after
kinds:
- group: apps
  kind: Deployment
Defensive patterns

Strategy: try-catch

Validate before calling

// verify every prune kind is mappable before pruning
for _, k := range spec.Kinds {
    gk := schema.GroupKind{Group: k.Group, Kind: k.Kind}
    if _, err := restMapper.RESTMapping(gk); err != nil {
        return fmt.Errorf("prune kind %v not present in cluster: %w", gk, err)
    }
}

Try / catch

if err := pruner.Prune(ctx, manifest, spec); err != nil {
    var gkErr *fmt.WrapError // inspect chain
    klog.Errorf("prune failed: %v", err) // %w chain shows the inner mapping/list/delete cause
    return err
}

Prevention

When it happens

Trigger: Any pruneKind entry in the PruneSpec fails processing: RESTMapper cannot map the kind, list calls fail (cluster/RBAC/selector issues), or deletion of a stale object fails.

Common situations: Prune spec referencing a kind not present in the cluster (typo or removed API); RBAC denying list/delete on the group; API server errors mid-prune; network partition during prune.

Related errors


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