retention

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2026 License: AGPL-3.0, AGPL-3.0-only Imports: 6 Imported by: 0

Documentation

Overview

Package retention implements a restic-style retention engine that works with any named+timestamped items (registry tags, forge releases, etc). Policies are additive — an item survives if ANY rule wants to keep it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyPolicies

func ApplyPolicies(candidates []Item, policy config.RetentionPolicy) []bool

ApplyPolicies evaluates all retention rules and returns a keep/prune decision for each candidate. candidates must be sorted newest-first. Policies are additive: an item is kept if ANY rule marks it.

func ApplyTimeBucket

func ApplyTimeBucket(candidates []Item, keepSet []bool, count int, bucket BucketFn)

ApplyTimeBucket keeps the newest item in each of the last N distinct time buckets. candidates must be sorted newest-first.

func TemplateToPattern

func TemplateToPattern(tmpl string) string

TemplateToPattern converts a single tag template to a regex pattern.

func TemplatesToPatterns

func TemplatesToPatterns(templates []string) []string

TemplatesToPatterns converts StageFreight tag templates into regex patterns suitable for config.MatchPatterns.

Template variables like {sha:8}, {version}, {branch} are replaced with regex wildcards (.+) so the pattern matches any resolved value.

Examples:

"dev-{sha:8}"    → "^dev-.+$"
"{version}"      → "^.+$"
"latest"         → "^latest$"
"!{branch}-rc"   → "!^.+-rc$"

func TruncateToDay

func TruncateToDay(t time.Time) time.Time

TruncateToDay truncates a time to the start of its day.

func TruncateToMonth

func TruncateToMonth(t time.Time) time.Time

TruncateToMonth truncates a time to the first day of its month.

func TruncateToWeek

func TruncateToWeek(t time.Time) time.Time

TruncateToWeek truncates a time to the start of its ISO week (Monday).

func TruncateToYear

func TruncateToYear(t time.Time) time.Time

TruncateToYear truncates a time to the first day of its year.

Types

type BucketFn

type BucketFn func(time.Time) time.Time

BucketFn truncates a time to the start of its bucket period.

type Item

type Item struct {
	Name      string
	CreatedAt time.Time
}

Item is a named, timestamped entity that can be pruned (tag, release, etc).

type Result

type Result struct {
	Matched int      // items matching the pattern set
	Kept    int      // items kept by policy
	Deleted []string // items successfully deleted
	Skipped []string // items skipped (digest shared with protected item)
	Errors  []error  // errors from individual deletes
}

Result captures what the retention engine did.

func Apply

func Apply(ctx context.Context, store Store, patterns []string, policy config.RetentionPolicy) (*Result, error)

Apply lists all items from the store, filters them by patterns (using config.MatchPatterns), sorts by creation time descending, applies restic-style retention policies, and deletes items not kept.

patterns uses the same syntax as branches/git_tags in the config:

["^dev-"]              → only items starting with "dev-"
["^dev-", "!^dev-keep"]→ dev- items, excluding dev-keep*
[]                     → ALL items are candidates

type Store

type Store interface {
	List(ctx context.Context) ([]Item, error)
	Delete(ctx context.Context, name string) error
}

Store abstracts listing and deleting items so the same engine works for registry tags, forge releases, or any other prunable resource.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL