Skip to content

Add KeepLatestNDockerImagesAllVersions rule (fix #183)#184

Open
sdn2s wants to merge 1 commit into
devopshq:masterfrom
sdn2s:keep-latest-n-all-versions
Open

Add KeepLatestNDockerImagesAllVersions rule (fix #183)#184
sdn2s wants to merge 1 commit into
devopshq:masterfrom
sdn2s:keep-latest-n-all-versions

Conversation

@sdn2s

@sdn2s sdn2s commented Jun 11, 2026

Copy link
Copy Markdown

What

Adds a new Docker keep rule KeepLatestNDockerImagesAllVersions that protects the latest N image digests computed over all tags in the repository, independently of the other rules in the policy.

Fixes #183.

Why

All rules in a policy contribute to a single AQL find combined with $and (CleanupPolicy._get_aql_find_filters), and Keep* rules run their in-memory filter() on the already-narrowed result set. So when a policy mixes a filtering Delete* rule (e.g. DeleteDockerImagesNotUsed) with KeepLatestNDockerImages, the keep rule only sees the artifacts that survived the delete filter and therefore cannot protect the truly latest N versions.

Consequence: the very common retention intent — "delete images not used for N days, but always keep the latest M versions" — is not expressible in a single policy, and on many datasets the policy deletes nothing (see #183 for the detailed analysis).

How

KeepLatestNDockerImagesAllVersions:

  • issues its own AQL query for every manifest in the affected repos (items.find(...).include("*")), independently of the policy's delete filter;
  • computes the latest N digests per image by updated (same semantics as KeepLatestNDockerImages);
  • protects the candidate artifacts whose digest is among those latest N.

Usage:

- rule: DeleteDockerImagesNotUsed
  days: 120
- rule: KeepLatestNDockerImagesAllVersions
  count: 5

All rules in a policy share a single AQL `find` combined with `$and`, so a
`Keep*` rule only sees the artifacts left after a filtering `Delete*` rule.
As a result `KeepLatestNDockerImages` cannot protect the truly latest N
versions when the policy also contains e.g. `DeleteDockerImagesNotUsed`, and
the common "delete unused, but always keep the latest N" intent is not
expressible in a single policy.

This adds `KeepLatestNDockerImagesAllVersions`, which computes the latest N
digests over all manifests via its own AQL query, independently of the other
rules. Existing rules are untouched. Includes a test and README docs.

Fixes devopshq#183
@sdn2s sdn2s force-pushed the keep-latest-n-all-versions branch from b9d9b58 to 0bb0881 Compare June 11, 2026 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant