Skip to content

Commit

Permalink
Merge pull request #1305 from lcarva/EC-340
Browse files Browse the repository at this point in the history
Prune records by taking their tag into account
  • Loading branch information
lcarva authored Jan 25, 2024
2 parents 0847a82 + 4eb70e1 commit 6a48b6a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 39 deletions.
40 changes: 26 additions & 14 deletions internal/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,36 +204,48 @@ func (t *Tracker) filterBundles(prune bool) {
}

// filterRecords reduces the list of records by removing superfluous entries.
// It removes records that have the same Repository and Digest. If prune is
// It removes records that have the same Repository Digest, and Tag. If prune is
// true, it skips any record that is no longer acceptable. Any record with an
// EffectiveOn date in the future, and the record with the most recent
// EffectiveOn date *not* in the future are considered acceptable.
func filterRecords(records []bundleRecord, prune bool) []bundleRecord {
now := time.Now().UTC()

// lastDigestForTag tracks the latest digest seen for each tag.
lastDigestForTag := map[string]string{}
unique := make([]bundleRecord, 0, len(records))
last_index := len(records) - 1
for i := last_index; i >= 0; i-- {
r := records[i]
for i := len(records) - 1; i >= 0; i-- {
// NOTE: Newly added records will have a repository, but existing ones
// will not. This is expected because the output does not persist the
// repository for each record. Instead, the repository is the attribute
// which references the list of records.
if i < last_index {
previous := records[i+1]
if previous.Digest == r.Digest {
continue
}
r := records[i]

if digest, ok := lastDigestForTag[r.Tag]; ok && digest == r.Digest {
continue
}
lastDigestForTag[r.Tag] = r.Digest

unique = append([]bundleRecord{r}, unique...)
}

relevant := make([]bundleRecord, 0, len(unique))
for _, r := range unique {
relevant = append(relevant, r)
if prune && now.After(r.EffectiveOn) {
break
var relevant []bundleRecord
if prune {
// tagsToSkip tracks when records for a certain tag should start to be pruned.
tagsToSkip := map[string]bool{}
for _, r := range unique {
if tagsToSkip[r.Tag] {
continue
}
relevant = append(relevant, r)
if !tagsToSkip[r.Tag] {
if now.After(r.EffectiveOn) {
tagsToSkip[r.Tag] = true
}
}
}
} else {
relevant = unique
}

filteredCount := len(records) - len(relevant)
Expand Down
64 changes: 39 additions & 25 deletions internal/tracker/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ func TestTrack(t *testing.T) {
registry.com/one:
- digest: ` + sampleHashOne.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.9"
tag: "1.0"
`)),
output: hd.Doc(`
---
pipeline-bundles:
registry.com/one:
- digest: ` + sampleHashOne.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.9"
tag: "1.0"
`),
},
{
Expand All @@ -240,18 +240,26 @@ func TestTrack(t *testing.T) {
registry.com/mixed:
- digest: ` + sampleHashThree.String() + `
effective_on: "` + yesterday + `"
tag: "0.3"
tag: "1.0"
- digest: ` + sampleHashTwo.String() + `
effective_on: "` + yesterday + `"
tag: "0.2"
tag: "1.0"
# Unrelated tag should be ignored.
- digest: sha256:abc
effective_on: "` + yesterday + `"
tag: "0.9"
task-bundles:
registry.com/mixed:
- digest: ` + sampleHashThree.String() + `
effective_on: "` + yesterday + `"
tag: "0.3"
tag: "1.0"
- digest: ` + sampleHashTwo.String() + `
effective_on: "` + yesterday + `"
tag: "0.2"
tag: "1.0"
# Unrelated tag should be ignored.
- digest: sha256:abc
effective_on: "` + yesterday + `"
tag: "0.9"
`)),
output: hd.Doc(`
---
Expand All @@ -262,15 +270,21 @@ func TestTrack(t *testing.T) {
tag: "1.0"
- digest: ` + sampleHashThree.String() + `
effective_on: "` + yesterday + `"
tag: "0.3"
tag: "1.0"
- digest: sha256:abc
effective_on: "` + yesterday + `"
tag: "0.9"
task-bundles:
registry.com/mixed:
- digest: ` + sampleHashOne.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "1.0"
- digest: ` + sampleHashThree.String() + `
effective_on: "` + yesterday + `"
tag: "0.3"
tag: "1.0"
- digest: sha256:abc
effective_on: "` + yesterday + `"
tag: "0.9"
`),
},
{
Expand Down Expand Up @@ -348,30 +362,30 @@ func TestTrack(t *testing.T) {
registry.com/mixed:
- digest: ` + sampleHashThree.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.3"
tag: "1.0"
- digest: ` + sampleHashTwo.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.2"
tag: "1.0"
- digest: ` + sampleHashThree.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.3"
tag: "1.0"
- digest: ` + sampleHashTwo.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.2"
tag: "1.0"
task-bundles:
registry.com/mixed:
- digest: ` + sampleHashThree.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.3"
tag: "1.0"
- digest: ` + sampleHashTwo.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.2"
tag: "1.0"
- digest: ` + sampleHashThree.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.3"
tag: "1.0"
- digest: ` + sampleHashTwo.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.2"
tag: "1.0"
`)),
output: hd.Doc(`
---
Expand All @@ -382,33 +396,33 @@ func TestTrack(t *testing.T) {
tag: "1.0"
- digest: ` + sampleHashThree.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.3"
tag: "1.0"
- digest: ` + sampleHashTwo.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.2"
tag: "1.0"
- digest: ` + sampleHashThree.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.3"
tag: "1.0"
- digest: ` + sampleHashTwo.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.2"
tag: "1.0"
task-bundles:
registry.com/mixed:
- digest: ` + sampleHashOne.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "1.0"
- digest: ` + sampleHashThree.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.3"
tag: "1.0"
- digest: ` + sampleHashTwo.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.2"
tag: "1.0"
- digest: ` + sampleHashThree.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.3"
tag: "1.0"
- digest: ` + sampleHashTwo.String() + `
effective_on: "` + expectedEffectiveOn + `"
tag: "0.2"
tag: "1.0"
`),
},
{
Expand Down Expand Up @@ -436,7 +450,7 @@ func TestTrack(t *testing.T) {
},
}

for _, tt := range tests[len(tests)-1:] {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.WithValue(context.Background(), image.RemoteHead, head)

Expand Down

0 comments on commit 6a48b6a

Please sign in to comment.