Skip to content

Commit

Permalink
feat(cleanup): add cleanup.keepBuiltWithinLastNHours directive in wer…
Browse files Browse the repository at this point in the history
…f.yaml

Signed-off-by: Alexey Igrychev <alexey.igrychev@flant.com>
  • Loading branch information
alexey-igrychev committed Apr 27, 2022
1 parent d860485 commit aabfcea
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
8 changes: 7 additions & 1 deletion docs/_data/werf_yaml.yml
Expand Up @@ -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
Expand Down
38 changes: 20 additions & 18 deletions pkg/cleaning/cleanup.go
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/meta_cleanup.go
Expand Up @@ -11,6 +11,7 @@ type MetaCleanup struct {
DisableKubernetesBasedPolicy bool
DisableGitHistoryBasedPolicy bool
DisableBuiltWithinLastNHoursPolicy bool
KeepImagesBuiltWithinLastNHours uint64
KeepPolicies []*MetaCleanupKeepPolicy
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/config/raw_meta_cleanup.go
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -192,6 +198,8 @@ func (c *rawMetaCleanup) toMetaCleanup() MetaCleanup {
metaCleanup.KeepPolicies = append(metaCleanup.KeepPolicies, policy.toMetaCleanupKeepPolicy())
}

metaCleanup.KeepImagesBuiltWithinLastNHours = *c.KeepImagesBuiltWithinLastNHours

return metaCleanup
}

Expand Down

0 comments on commit aabfcea

Please sign in to comment.