@@ -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 }
0 commit comments