Skip to content
Permalink
Browse files
feat(cleanup): add cleanup.keepBuiltWithinLastNHours directive in wer…
…f.yaml

Signed-off-by: Alexey Igrychev <alexey.igrychev@flant.com>
  • Loading branch information
alexey-igrychev committed Apr 27, 2022
1 parent d860485 commit aabfcea7172d6241906e207578009645abe5daec
Showing with 36 additions and 19 deletions.
  1. +7 −1 docs/_data/werf_yaml.yml
  2. +20 −18 pkg/cleaning/cleanup.go
  3. +1 −0 pkg/config/meta_cleanup.go
  4. +8 −0 pkg/config/raw_meta_cleanup.go
@@ -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
@@ -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 {
@@ -11,6 +11,7 @@ type MetaCleanup struct {
DisableKubernetesBasedPolicy bool
DisableGitHistoryBasedPolicy bool
DisableBuiltWithinLastNHoursPolicy bool
KeepImagesBuiltWithinLastNHours uint64
KeepPolicies []*MetaCleanupKeepPolicy
}

@@ -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
}

0 comments on commit aabfcea

Please sign in to comment.