From aabfcea7172d6241906e207578009645abe5daec Mon Sep 17 00:00:00 2001 From: Alexey Igrychev Date: Wed, 27 Apr 2022 15:02:24 +0100 Subject: [PATCH] feat(cleanup): add cleanup.keepBuiltWithinLastNHours directive in werf.yaml Signed-off-by: Alexey Igrychev --- docs/_data/werf_yaml.yml | 8 ++++++- pkg/cleaning/cleanup.go | 38 ++++++++++++++++++---------------- pkg/config/meta_cleanup.go | 1 + pkg/config/raw_meta_cleanup.go | 8 +++++++ 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/docs/_data/werf_yaml.yml b/docs/_data/werf_yaml.yml index c6248ea049..2a1bcf904d 100644 --- a/docs/_data/werf_yaml.yml +++ b/docs/_data/werf_yaml.yml @@ -95,7 +95,13 @@ sections: value: "bool" description: en: Disable a cleanup policy that allows not to remove images built in last hours (keepImagesBuiltWithinLastNHours) - ru: Отключить политику очистки, которая позволяет не удалять образы собранные в рамках заданного периода времени (keepImagesBuiltWithinLastNHours) + ru: Отключить политику очистки, которая позволяет не удалять образы, собранные в рамках заданного периода времени (keepImagesBuiltWithinLastNHours) + - name: keepImagesBuiltWithinLastNHours + value: "uint" + description: + en: The minimum number of hours that must elapse since the image is built + ru: Минимальное количество часов, которое должно пройти с момента сборки образа + default: "2" - name: keepPolicies description: en: Set of policies to select relevant images using the Git history diff --git a/pkg/cleaning/cleanup.go b/pkg/cleaning/cleanup.go index 37cc8b1e61..f83785f914 100644 --- a/pkg/cleaning/cleanup.go +++ b/pkg/cleaning/cleanup.go @@ -625,27 +625,29 @@ func (m *cleanupManager) cleanupUnusedStages(ctx context.Context) error { }) } - // skip stages and their relatives based on KeepStagesBuiltWithinLastNHours policy - { - if m.KeepStagesBuiltWithinLastNHours != 0 { - var excludedSDList []*image.StageDescription - for _, sd := range stageDescriptionListToDelete { - if (time.Since(sd.Info.GetCreatedAt()).Hours()) <= float64(m.KeepStagesBuiltWithinLastNHours) { - var excludedRelativesSDList []*image.StageDescription - stageDescriptionListToDelete, excludedRelativesSDList = m.excludeStageAndRelativesByImageID(stageDescriptionListToDelete, sd.Info.ID) - excludedSDList = append(excludedSDList, excludedRelativesSDList...) - } - } + keepImagesBuiltWithinLastNHours := m.ConfigMetaCleanup.KeepImagesBuiltWithinLastNHours + if m.KeepStagesBuiltWithinLastNHours != 0 { + keepImagesBuiltWithinLastNHours = m.KeepStagesBuiltWithinLastNHours + } - if len(excludedSDList) != 0 { - logboek.Context(ctx).Default().LogBlock("Saved stages that were built within last %d hours (%d/%d)", m.KeepStagesBuiltWithinLastNHours, len(excludedSDList), len(stageDescriptionList)).Do(func() { - for _, stage := range excludedSDList { - logboek.Context(ctx).Default().LogFDetails(" tag: %s\n", stage.Info.Tag) - logboek.Context(ctx).LogOptionalLn() - } - }) + if !(m.ConfigMetaCleanup.DisableBuiltWithinLastNHoursPolicy || keepImagesBuiltWithinLastNHours == 0) { + var excludedSDList []*image.StageDescription + for _, sd := range stageDescriptionListToDelete { + if (time.Since(sd.Info.GetCreatedAt()).Hours()) <= float64(keepImagesBuiltWithinLastNHours) { + var excludedRelativesSDList []*image.StageDescription + stageDescriptionListToDelete, excludedRelativesSDList = m.excludeStageAndRelativesByImageID(stageDescriptionListToDelete, sd.Info.ID) + 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(stageDescriptionList)).Do(func() { + for _, stage := range excludedSDList { + logboek.Context(ctx).Default().LogFDetails(" tag: %s\n", stage.Info.Tag) + logboek.Context(ctx).LogOptionalLn() + } + }) + } } if len(stageDescriptionListToDelete) != 0 { diff --git a/pkg/config/meta_cleanup.go b/pkg/config/meta_cleanup.go index aa6a0c2b13..5ae1091f17 100644 --- a/pkg/config/meta_cleanup.go +++ b/pkg/config/meta_cleanup.go @@ -11,6 +11,7 @@ type MetaCleanup struct { DisableKubernetesBasedPolicy bool DisableGitHistoryBasedPolicy bool DisableBuiltWithinLastNHoursPolicy bool + KeepImagesBuiltWithinLastNHours uint64 KeepPolicies []*MetaCleanupKeepPolicy } diff --git a/pkg/config/raw_meta_cleanup.go b/pkg/config/raw_meta_cleanup.go index a45420c333..5d05bad021 100644 --- a/pkg/config/raw_meta_cleanup.go +++ b/pkg/config/raw_meta_cleanup.go @@ -12,6 +12,7 @@ type rawMetaCleanup struct { DisableGitHistoryBasedPolicy bool `yaml:"disableGitHistoryBasedPolicy,omitempty"` DisableBuiltWithinLastNHoursPolicy bool `yaml:"disableBuiltWithinLastNHoursPolicy,omitempty"` KeepPolicies []*rawMetaCleanupKeepPolicy `yaml:"keepPolicies,omitempty"` + KeepImagesBuiltWithinLastNHours *uint64 `yaml:"keepImagesBuiltWithinLastNHours,omitempty"` rawMeta *rawMeta UnsupportedAttributes map[string]interface{} `yaml:",inline"` @@ -65,6 +66,11 @@ func (c *rawMetaCleanup) UnmarshalYAML(unmarshal func(interface{}) error) error return err } + if c.KeepImagesBuiltWithinLastNHours == nil { + defaultKeepImagesBuiltWithinLastNHours := uint64(2) + c.KeepImagesBuiltWithinLastNHours = &defaultKeepImagesBuiltWithinLastNHours + } + return nil } @@ -192,6 +198,8 @@ func (c *rawMetaCleanup) toMetaCleanup() MetaCleanup { metaCleanup.KeepPolicies = append(metaCleanup.KeepPolicies, policy.toMetaCleanupKeepPolicy()) } + metaCleanup.KeepImagesBuiltWithinLastNHours = *c.KeepImagesBuiltWithinLastNHours + return metaCleanup }