Skip to content

Commit 8e3ae09

Browse files
Alexandr Zaytsevalexey-igrychev
authored andcommitted
fix(host-cleanup): respect --dry-run option for gitdata gc
Signed-off-by: Alexandr Zaytsev <alexandr.zaytsev@flant.com>
1 parent 952af61 commit 8e3ae09

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

‎pkg/git_repo/gitdata/gc.go‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ func ShouldRunAutoGC(ctx context.Context, allowedVolumeUsagePercentage float64)
3535
return vu.Percentage() > allowedVolumeUsagePercentage, nil
3636
}
3737

38-
func RunGC(ctx context.Context, allowedVolumeUsagePercentage, allowedVolumeUsageMarginPercentage float64) error {
38+
type RunGCOptions struct {
39+
AllowedLocalCacheVolumeUsagePercentage float64
40+
AllowedLocalCacheVolumeUsageMarginPercentage float64
41+
DryRun bool
42+
}
43+
44+
func RunGC(ctx context.Context, options RunGCOptions) error {
3945
if lock, err := lockGC(ctx, false); err != nil {
4046
return err
4147
} else {
@@ -90,15 +96,15 @@ func RunGC(ctx context.Context, allowedVolumeUsagePercentage, allowedVolumeUsage
9096
return fmt.Errorf("error getting volume usage by path %q: %w", werf.GetLocalCacheDir(), err)
9197
}
9298

93-
targetVolumeUsagePercentage := math.Max(allowedVolumeUsagePercentage-allowedVolumeUsageMarginPercentage, 0)
99+
targetVolumeUsagePercentage := math.Max(options.AllowedLocalCacheVolumeUsagePercentage-options.AllowedLocalCacheVolumeUsageMarginPercentage, 0)
94100

95101
bytesToFree := vu.BytesToFree(targetVolumeUsagePercentage)
96102

97-
if vu.Percentage() <= allowedVolumeUsagePercentage {
103+
if vu.Percentage() <= options.AllowedLocalCacheVolumeUsagePercentage {
98104
logboek.Context(ctx).Default().LogBlock("Git data storage check").Do(func() {
99105
logboek.Context(ctx).Default().LogF("Werf local cache dir: %s\n", werf.GetLocalCacheDir())
100106
logboek.Context(ctx).Default().LogF("Volume usage: %s / %s\n", humanize.Bytes(vu.UsedBytes), humanize.Bytes(vu.TotalBytes))
101-
logboek.Context(ctx).Default().LogF("Allowed volume usage percentage: %s <= %s — %s\n", utils.GreenF("%0.2f%%", vu.Percentage()), utils.BlueF("%0.2f%%", allowedVolumeUsagePercentage), utils.GreenF("OK"))
107+
logboek.Context(ctx).Default().LogF("Allowed volume usage percentage: %s <= %s — %s\n", utils.GreenF("%0.2f%%", vu.Percentage()), utils.BlueF("%0.2f%%", options.AllowedLocalCacheVolumeUsagePercentage), utils.GreenF("OK"))
102108
})
103109

104110
return nil
@@ -107,8 +113,8 @@ func RunGC(ctx context.Context, allowedVolumeUsagePercentage, allowedVolumeUsage
107113
logboek.Context(ctx).Default().LogBlock("Git data storage check").Do(func() {
108114
logboek.Context(ctx).Default().LogF("Werf local cache dir: %s\n", werf.GetLocalCacheDir())
109115
logboek.Context(ctx).Default().LogF("Volume usage: %s / %s\n", humanize.Bytes(vu.UsedBytes), humanize.Bytes(vu.TotalBytes))
110-
logboek.Context(ctx).Default().LogF("Allowed percentage level exceeded: %s > %s — %s\n", utils.RedF("%0.2f%%", vu.Percentage()), utils.YellowF("%0.2f%%", allowedVolumeUsagePercentage), utils.RedF("HIGH VOLUME USAGE"))
111-
logboek.Context(ctx).Default().LogF("Target percentage level after cleanup: %0.2f%% - %0.2f%% (margin) = %s\n", allowedVolumeUsagePercentage, allowedVolumeUsageMarginPercentage, utils.BlueF("%0.2f%%", targetVolumeUsagePercentage))
116+
logboek.Context(ctx).Default().LogF("Allowed percentage level exceeded: %s > %s — %s\n", utils.RedF("%0.2f%%", vu.Percentage()), utils.YellowF("%0.2f%%", options.AllowedLocalCacheVolumeUsagePercentage), utils.RedF("HIGH VOLUME USAGE"))
117+
logboek.Context(ctx).Default().LogF("Target percentage level after cleanup: %0.2f%% - %0.2f%% (margin) = %s\n", options.AllowedLocalCacheVolumeUsagePercentage, options.AllowedLocalCacheVolumeUsageMarginPercentage, utils.BlueF("%0.2f%%", targetVolumeUsagePercentage))
112118
logboek.Context(ctx).Default().LogF("Needed to free: %s\n", utils.RedF("%s", humanize.Bytes(bytesToFree)))
113119
})
114120

@@ -165,6 +171,11 @@ func RunGC(ctx context.Context, allowedVolumeUsagePercentage, allowedVolumeUsage
165171
for _, entry := range gitDataEntries {
166172
for _, path := range entry.GetPaths() {
167173
logboek.Context(ctx).LogF("Removing %q inside scope %q\n", path, entry.GetCacheBasePath())
174+
175+
if options.DryRun {
176+
continue
177+
}
178+
168179
if err := RemovePathWithEmptyParentDirsInsideScope(entry.GetCacheBasePath(), path); err != nil {
169180
return fmt.Errorf("unable to remove %q: %w", path, err)
170181
}

‎pkg/host_cleaning/host_cleanup.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ func RunHostCleanup(ctx context.Context, backend container_backend.ContainerBack
114114
allowedLocalCacheVolumeUsageMarginPercentage := getOptionValueOrDefault(options.AllowedLocalCacheVolumeUsageMarginPercentage, DefaultAllowedLocalCacheVolumeUsageMarginPercentage)
115115

116116
if err := logboek.Context(ctx).Default().LogProcess("Running GC for git data").DoError(func() error {
117-
if err := gitdata.RunGC(ctx, allowedLocalCacheVolumeUsagePercentage, allowedLocalCacheVolumeUsageMarginPercentage); err != nil {
117+
if err := gitdata.RunGC(ctx, gitdata.RunGCOptions{
118+
AllowedLocalCacheVolumeUsagePercentage: allowedLocalCacheVolumeUsagePercentage,
119+
AllowedLocalCacheVolumeUsageMarginPercentage: allowedLocalCacheVolumeUsageMarginPercentage,
120+
DryRun: options.DryRun,
121+
}); err != nil {
118122
return fmt.Errorf("git repo GC failed: %w", err)
119123
}
120124
return nil

0 commit comments

Comments
 (0)