Skip to content

feat(prune): add --target flag support to tk prune#2032

Open
zalegrala wants to merge 2 commits into
grafana:mainfrom
zalegrala:feat/prune-target-filtering
Open

feat(prune): add --target flag support to tk prune#2032
zalegrala wants to merge 2 commits into
grafana:mainfrom
zalegrala:feat/prune-target-filtering

Conversation

@zalegrala

@zalegrala zalegrala commented May 13, 2026

Copy link
Copy Markdown

Adds --target / -t to tk prune, bringing it in line with tk apply, tk delete, and tk show. See the updated docs/garbage-collection.md for usage and examples.

Efficiency note

Without --target, tk prune lists every resource type that supports LIST before reconciling UIDs — expensive in environments with many CRDs. When the filter specifies a literal kind name (no regex metacharacters in the kind position), Orphaned() restricts the cluster query to only that resource type, avoiding the cost of listing everything else.

tk prune now accepts the same --target / -t flag as tk apply, tk delete,
and tk show. This lets operators restrict pruning to a subset of resource
kinds and names using the standard kind/name RE2 regex syntax, e.g.:

  tk prune -t 'statefulset/.*' .
  tk prune -t 'statefulset/live-store.*' .
  tk prune -t '!deployment/.*' .

When a literal kind name is given in the filter (no regex metacharacters
in the kind position), Orphaned() restricts the Kubernetes API query to
only that resource type. This avoids listing every resource kind in large
environments and makes targeted prune operations significantly cheaper.

Changes:
- process: add Matchers.KindsFor() + kindFromFilterPattern() to extract
  literal kind names from compiled filter expressions
- kubernetes: add OrphanedOpts, thread Filters through Orphaned() for
  both kind-level pre-filtering of API queries and post-filtering of results
- tanka: pass opts.Filters into Orphaned() via OrphanedOpts
- cmd/tk: wire workflowFlags (--target, --name, --jsonnet-implementation)
  into pruneCmd, matching the pattern used by applyCmd and deleteCmd
- docs: document --target usage on tk prune in garbage-collection.md
@cla-assistant

cla-assistant Bot commented May 13, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cla-assistant

cla-assistant Bot commented May 13, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@cla-assistant

cla-assistant Bot commented May 13, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

- gofmt: reformat apply_test.go and filter_test.go
- staticcheck QF1012: replace WriteString(fmt.Sprintf(...)) with
  fmt.Fprintf in find_importers.go and pkg/kubernetes/util/diff.go
@zalegrala zalegrala marked this pull request as ready for review May 13, 2026 20:51
@zalegrala zalegrala requested a review from a team as a code owner May 13, 2026 20:51

var sb strings.Builder
for _, importer := range importersList {
sb.WriteString(fmt.Sprintf("%s: %d\n", importer.File, importer.Count))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes showed up in the lint when running locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant