Skip to content

Commit

Permalink
fix(cleanup): keep relatives for keepImagesBuiltWithinLastNHours policy
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
  • Loading branch information
alexey-igrychev committed Nov 21, 2024
1 parent 195f159 commit a0eb160
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions pkg/cleaning/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ func (m *cleanupManager) run(ctx context.Context) error {
}
}

// Built within last N hours policy.
{
keepImagesBuiltWithinLastNHours := m.ConfigMetaCleanup.KeepImagesBuiltWithinLastNHours
if m.KeepStagesBuiltWithinLastNHours != 0 {
keepImagesBuiltWithinLastNHours = m.KeepStagesBuiltWithinLastNHours
}

if !(m.ConfigMetaCleanup.DisableBuiltWithinLastNHoursPolicy || keepImagesBuiltWithinLastNHours == 0) {
reason := fmt.Sprintf("built within last %d hours", keepImagesBuiltWithinLastNHours)
for stageDescToDelete := range m.stageManager.GetStageDescSet().Iter() {
if (time.Since(stageDescToDelete.Info.GetCreatedAt()).Hours()) <= float64(keepImagesBuiltWithinLastNHours) {
m.stageManager.MarkStageDescAsProtected(stageDescToDelete, reason)
}
}
}
}

if err := logboek.Context(ctx).LogProcess("Cleanup unused stages").DoError(func() error {
return m.cleanupUnusedStages(ctx)
}); err != nil {
Expand Down Expand Up @@ -735,31 +752,6 @@ func (m *cleanupManager) cleanupUnusedStages(ctx context.Context) error {
})
}

keepImagesBuiltWithinLastNHours := m.ConfigMetaCleanup.KeepImagesBuiltWithinLastNHours
if m.KeepStagesBuiltWithinLastNHours != 0 {
keepImagesBuiltWithinLastNHours = m.KeepStagesBuiltWithinLastNHours
}

if !(m.ConfigMetaCleanup.DisableBuiltWithinLastNHoursPolicy || keepImagesBuiltWithinLastNHours == 0) {
var excludedSDList image.StageDescSet
for _, sd := range stageDescSetToDelete {
if (time.Since(sd.Info.GetCreatedAt()).Hours()) <= float64(keepImagesBuiltWithinLastNHours) {
var excludedRelativesSDList image.StageDescSet
stageDescSetToDelete, excludedRelativesSDList = m.excludeStageAndRelativesByImage(stageDescSetToDelete, sd.Info)
excludedSDList = append(excludedSDList, excludedRelativesSDList...)
}
}

if len(excludedSDList) != 0 {
logboek.Context(ctx).Default().LogBlock("Saved stages that were built within last %d hours (%d/%d)", keepImagesBuiltWithinLastNHours, len(excludedSDList), len(stageDescSet)).Do(func() {
for _, stage := range excludedSDList {
logboek.Context(ctx).Default().LogFDetails(" tag: %s\n", stage.Info.Tag)
logboek.Context(ctx).LogOptionalLn()
}
})
}
}

if len(stageDescSetToDelete) != 0 {
if err := logboek.Context(ctx).Default().LogProcess("Deleting stages tags (%d/%d)", len(stageDescSetToDelete), stageDescSetCount).DoError(func() error {
return m.deleteStages(ctx, stageDescSetToDelete, false)
Expand Down

0 comments on commit a0eb160

Please sign in to comment.