Skip to content

Commit a0eb160

Browse files
fix(cleanup): keep relatives for keepImagesBuiltWithinLastNHours policy
Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
1 parent 195f159 commit a0eb160

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

pkg/cleaning/cleanup.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,23 @@ func (m *cleanupManager) run(ctx context.Context) error {
152152
}
153153
}
154154

155+
// Built within last N hours policy.
156+
{
157+
keepImagesBuiltWithinLastNHours := m.ConfigMetaCleanup.KeepImagesBuiltWithinLastNHours
158+
if m.KeepStagesBuiltWithinLastNHours != 0 {
159+
keepImagesBuiltWithinLastNHours = m.KeepStagesBuiltWithinLastNHours
160+
}
161+
162+
if !(m.ConfigMetaCleanup.DisableBuiltWithinLastNHoursPolicy || keepImagesBuiltWithinLastNHours == 0) {
163+
reason := fmt.Sprintf("built within last %d hours", keepImagesBuiltWithinLastNHours)
164+
for stageDescToDelete := range m.stageManager.GetStageDescSet().Iter() {
165+
if (time.Since(stageDescToDelete.Info.GetCreatedAt()).Hours()) <= float64(keepImagesBuiltWithinLastNHours) {
166+
m.stageManager.MarkStageDescAsProtected(stageDescToDelete, reason)
167+
}
168+
}
169+
}
170+
}
171+
155172
if err := logboek.Context(ctx).LogProcess("Cleanup unused stages").DoError(func() error {
156173
return m.cleanupUnusedStages(ctx)
157174
}); err != nil {
@@ -735,31 +752,6 @@ func (m *cleanupManager) cleanupUnusedStages(ctx context.Context) error {
735752
})
736753
}
737754

738-
keepImagesBuiltWithinLastNHours := m.ConfigMetaCleanup.KeepImagesBuiltWithinLastNHours
739-
if m.KeepStagesBuiltWithinLastNHours != 0 {
740-
keepImagesBuiltWithinLastNHours = m.KeepStagesBuiltWithinLastNHours
741-
}
742-
743-
if !(m.ConfigMetaCleanup.DisableBuiltWithinLastNHoursPolicy || keepImagesBuiltWithinLastNHours == 0) {
744-
var excludedSDList image.StageDescSet
745-
for _, sd := range stageDescSetToDelete {
746-
if (time.Since(sd.Info.GetCreatedAt()).Hours()) <= float64(keepImagesBuiltWithinLastNHours) {
747-
var excludedRelativesSDList image.StageDescSet
748-
stageDescSetToDelete, excludedRelativesSDList = m.excludeStageAndRelativesByImage(stageDescSetToDelete, sd.Info)
749-
excludedSDList = append(excludedSDList, excludedRelativesSDList...)
750-
}
751-
}
752-
753-
if len(excludedSDList) != 0 {
754-
logboek.Context(ctx).Default().LogBlock("Saved stages that were built within last %d hours (%d/%d)", keepImagesBuiltWithinLastNHours, len(excludedSDList), len(stageDescSet)).Do(func() {
755-
for _, stage := range excludedSDList {
756-
logboek.Context(ctx).Default().LogFDetails(" tag: %s\n", stage.Info.Tag)
757-
logboek.Context(ctx).LogOptionalLn()
758-
}
759-
})
760-
}
761-
}
762-
763755
if len(stageDescSetToDelete) != 0 {
764756
if err := logboek.Context(ctx).Default().LogProcess("Deleting stages tags (%d/%d)", len(stageDescSetToDelete), stageDescSetCount).DoError(func() error {
765757
return m.deleteStages(ctx, stageDescSetToDelete, false)

0 commit comments

Comments
 (0)