Skip to content

Commit

Permalink
fix(cleanup): handle --keep-stages-built-within-last-n-hours=0 properly
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 Jan 28, 2025
1 parent 0695c5d commit f6f1b9a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
6 changes: 3 additions & 3 deletions cmd/werf/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.LogVersion()

return common.LogRunningTime(func() error {
return runCleanup(ctx)
return runCleanup(ctx, cmd)
})
},
})
Expand Down Expand Up @@ -102,7 +102,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
return cmd
}

func runCleanup(ctx context.Context) error {
func runCleanup(ctx context.Context, cmd *cobra.Command) error {
commonManager, ctx, err := common.InitCommonComponents(ctx, common.InitCommonComponentsOptions{
Cmd: &commonCmdData,
InitTrueGitWithOptions: &common.InitTrueGitOptions{
Expand Down Expand Up @@ -220,7 +220,7 @@ It is worth noting that auto-cleaning is enabled by default, and manual use is u
KubernetesNamespaceRestrictionByContext: kubernetesNamespaceRestrictionByContext,
WithoutKube: *commonCmdData.WithoutKube,
ConfigMetaCleanup: werfConfig.Meta.Cleanup,
KeepStagesBuiltWithinLastNHours: *commonCmdData.KeepStagesBuiltWithinLastNHours,
KeepStagesBuiltWithinLastNHours: common.GetKeepStagesBuiltWithinLastNHours(&commonCmdData, cmd),
DryRun: *commonCmdData.DryRun,
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/common/cmd_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type CmdData struct {
KubeToken *string
InsecureHelmDependencies *bool
DryRun *bool
KeepStagesBuiltWithinLastNHours *uint64
keepStagesBuiltWithinLastNHours *uint64
WithoutKube *bool
KubeVersion *string
ContainerRegistryMirror *[]string
Expand Down
18 changes: 11 additions & 7 deletions cmd/werf/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,22 +363,26 @@ func SetupWithoutKube(cmdData *CmdData, cmd *cobra.Command) {
cmd.Flags().BoolVarP(cmdData.WithoutKube, "without-kube", "", util.GetBoolEnvironmentDefaultFalse("WERF_WITHOUT_KUBE"), "Do not skip deployed Kubernetes images (default $WERF_WITHOUT_KUBE)")
}

const flagNameKeepStagesBuiltWithinLastNHours = "keep-stages-built-within-last-n-hours"

func SetupKeepStagesBuiltWithinLastNHours(cmdData *CmdData, cmd *cobra.Command) {
cmdData.KeepStagesBuiltWithinLastNHours = new(uint64)
cmdData.keepStagesBuiltWithinLastNHours = new(uint64)
cmd.Flags().Uint64VarP(cmdData.keepStagesBuiltWithinLastNHours, flagNameKeepStagesBuiltWithinLastNHours, "", config.DefaultKeepImagesBuiltWithinLastNHours, fmt.Sprintf("Keep stages that were built within last hours (default $WERF_KEEP_STAGES_BUILT_WITHIN_LAST_N_HOURS or %d)", config.DefaultKeepImagesBuiltWithinLastNHours))
}

func GetKeepStagesBuiltWithinLastNHours(cmdData *CmdData, cmd *cobra.Command) *uint64 {
envValue, err := util.GetUint64EnvVar("WERF_KEEP_STAGES_BUILT_WITHIN_LAST_N_HOURS")
if err != nil {
TerminateWithError(err.Error(), 1)
}

var defaultValue uint64
if envValue != nil {
defaultValue = *envValue
if cmd.Flags().Changed(flagNameKeepStagesBuiltWithinLastNHours) {
return cmdData.keepStagesBuiltWithinLastNHours
} else if envValue != nil {
return envValue
} else {
defaultValue = 2
return nil
}

cmd.Flags().Uint64VarP(cmdData.KeepStagesBuiltWithinLastNHours, "keep-stages-built-within-last-n-hours", "", defaultValue, "Keep stages that were built within last hours (default $WERF_KEEP_STAGES_BUILT_WITHIN_LAST_N_HOURS or 2)")
}

func SetupEnvironment(cmdData *CmdData, cmd *cobra.Command) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/cleaning/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type CleanupOptions struct {
KubernetesNamespaceRestrictionByContext map[string]string
WithoutKube bool // TODO: remove this legacy logic in v3.
ConfigMetaCleanup config.MetaCleanup
KeepStagesBuiltWithinLastNHours uint64
KeepStagesBuiltWithinLastNHours *uint64
DryRun bool
}

Expand Down Expand Up @@ -70,7 +70,7 @@ type cleanupManager struct {
KubernetesNamespaceRestrictionByContext map[string]string
WithoutKube bool
ConfigMetaCleanup config.MetaCleanup
KeepStagesBuiltWithinLastNHours uint64
KeepStagesBuiltWithinLastNHours *uint64
DryRun bool
}

Expand Down Expand Up @@ -156,8 +156,8 @@ 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.KeepStagesBuiltWithinLastNHours != nil {
keepImagesBuiltWithinLastNHours = *m.KeepStagesBuiltWithinLastNHours
}
stage_manager.ProtectionReasonBuiltWithinLastNHoursPolicy.SetDescription(fmt.Sprintf("built within last %d hours", keepImagesBuiltWithinLastNHours))

Expand Down
6 changes: 2 additions & 4 deletions pkg/config/raw_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type rawMeta struct {
Project *string `yaml:"project,omitempty"`
Build *rawMetaBuild `yaml:"build,omitempty"`
Deploy *rawMetaDeploy `yaml:"deploy,omitempty"`
Cleanup *rawMetaCleanup `yaml:"cleanup,omitempty"`
Cleanup rawMetaCleanup `yaml:"cleanup,omitempty"`
GitWorktree *rawMetaGitWorktree `yaml:"gitWorktree,omitempty"`

doc *doc `yaml:"-"` // parent
Expand Down Expand Up @@ -64,9 +64,7 @@ func (c *rawMeta) toMeta() *Meta {
}
}

if c.Cleanup != nil {
meta.Cleanup = c.Cleanup.toMetaCleanup()
}
meta.Cleanup = c.Cleanup.toMetaCleanup()

if c.Build != nil {
meta.Build = c.Build.toMetaBuild()
Expand Down
13 changes: 7 additions & 6 deletions pkg/config/raw_meta_cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"
)

const DefaultKeepImagesBuiltWithinLastNHours uint64 = 2

type rawMetaCleanup struct {
DisableKubernetesBasedPolicy bool `yaml:"disableKubernetesBasedPolicy,omitempty"`
DisableGitHistoryBasedPolicy bool `yaml:"disableGitHistoryBasedPolicy,omitempty"`
Expand Down Expand Up @@ -66,11 +68,6 @@ 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 @@ -195,7 +192,11 @@ func (c *rawMetaCleanup) toMetaCleanup() MetaCleanup {
metaCleanup.KeepPolicies = append(metaCleanup.KeepPolicies, policy.toMetaCleanupKeepPolicy())
}

metaCleanup.KeepImagesBuiltWithinLastNHours = *c.KeepImagesBuiltWithinLastNHours
if c.KeepImagesBuiltWithinLastNHours != nil {
metaCleanup.KeepImagesBuiltWithinLastNHours = *c.KeepImagesBuiltWithinLastNHours
} else {
metaCleanup.KeepImagesBuiltWithinLastNHours = DefaultKeepImagesBuiltWithinLastNHours
}

return metaCleanup
}
Expand Down

0 comments on commit f6f1b9a

Please sign in to comment.