Skip to content

Commit dd43b68

Browse files
committed
fix(cleanup): fix "should reset storage cache" error during werf-cleanup and werf-purge
Automatically reset stages storage cache in the werf-cleanup and werf-purge commands as in werf-build.
1 parent 3dc7098 commit dd43b68

File tree

5 files changed

+43
-46
lines changed

5 files changed

+43
-46
lines changed

cmd/werf/cleanup/cleanup.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,8 @@ It is worth noting that auto-cleaning is enabled by default, and manual use is u
258258
DryRun: *commonCmdData.DryRun,
259259
}
260260

261-
logboek.LogOptionalLn()
262-
if err := cleaning.Cleanup(ctx, projectName, storageManager, storageLockManager, cleanupOptions); err != nil {
263-
return err
264-
}
265-
266-
return nil
261+
return manager.RetryOnStagesStorageCacheResetError(ctx, storageManager, func() error {
262+
logboek.LogOptionalLn()
263+
return cleaning.Cleanup(ctx, projectName, storageManager, storageLockManager, cleanupOptions)
264+
})
267265
}

cmd/werf/purge/purge.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewCmd() *cobra.Command {
2828
Use: "purge",
2929
DisableFlagsInUseLine: true,
3030
Short: "Purge all project images in the container registry",
31-
Long: common.GetLongCommandDescription(`Purge all project images in the container registry.
31+
Long: common.GetLongCommandDescription(`Purge all project images in the container registry.
3232
3333
WARNING: Images that are being used in the Kubernetes cluster will also be deleted.`),
3434
RunE: func(cmd *cobra.Command, args []string) error {
@@ -150,7 +150,7 @@ func runPurge() error {
150150

151151
stagesStorageAddress, err := common.GetStagesStorageAddress(&commonCmdData)
152152
if err != nil {
153-
logboek.Context(ctx).Default().LogLnDetails(`The "werf purge" command is only used to cleaning the container registry. In case you need to clean the runner or the localhost, use the commands of the "werf host" group.
153+
logboek.Context(ctx).Default().LogLnDetails(`The "werf purge" command is only used to cleaning the container registry. In case you need to clean the runner or the localhost, use the commands of the "werf host" group.
154154
It is worth noting that auto-cleaning is enabled by default, and manual use is usually not required (if not, we would appreciate feedback and creating an issue https://github.com/werf/werf/issues/new).`)
155155

156156
return err
@@ -194,10 +194,8 @@ It is worth noting that auto-cleaning is enabled by default, and manual use is u
194194
DryRun: *commonCmdData.DryRun,
195195
}
196196

197-
logboek.LogOptionalLn()
198-
if err := cleaning.Purge(ctx, projectName, storageManager, storageLockManager, purgeOptions); err != nil {
199-
return err
200-
}
201-
202-
return nil
197+
return manager.RetryOnStagesStorageCacheResetError(ctx, storageManager, func() error {
198+
logboek.LogOptionalLn()
199+
return cleaning.Purge(ctx, projectName, storageManager, storageLockManager, purgeOptions)
200+
})
203201
}

docs/documentation/_includes/reference/cli/werf_purge.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{% else %}
44
{% assign header = "###" %}
55
{% endif %}
6-
Purge all project images in the container registry.
6+
Purge all project images in the container registry.
77

88
WARNING: Images that are being used in the Kubernetes cluster will also be deleted.
99

pkg/build/conveyor_with_retry.go

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package build
33
import (
44
"context"
55

6-
"github.com/werf/logboek"
76
"github.com/werf/werf/pkg/config"
87
"github.com/werf/werf/pkg/container_runtime"
98
"github.com/werf/werf/pkg/giterminism_manager"
@@ -45,37 +44,22 @@ func (wrapper *ConveyorWithRetryWrapper) Terminate() error {
4544
}
4645

4746
func (wrapper *ConveyorWithRetryWrapper) WithRetryBlock(ctx context.Context, f func(c *Conveyor) error) error {
48-
Retry:
49-
newConveyor := NewConveyor(
50-
wrapper.WerfConfig,
51-
wrapper.GiterminismManager,
52-
wrapper.ImageNamesToProcess,
53-
wrapper.ProjectDir,
54-
wrapper.BaseTmpDir,
55-
wrapper.SshAuthSock,
56-
wrapper.ContainerRuntime,
57-
wrapper.StorageManager,
58-
wrapper.StorageLockManager,
59-
wrapper.ConveyorOptions,
60-
)
47+
return manager.RetryOnStagesStorageCacheResetError(ctx, wrapper.StorageManager, func() error {
48+
newConveyor := NewConveyor(
49+
wrapper.WerfConfig,
50+
wrapper.GiterminismManager,
51+
wrapper.ImageNamesToProcess,
52+
wrapper.ProjectDir,
53+
wrapper.BaseTmpDir,
54+
wrapper.SshAuthSock,
55+
wrapper.ContainerRuntime,
56+
wrapper.StorageManager,
57+
wrapper.StorageLockManager,
58+
wrapper.ConveyorOptions,
59+
)
6160

62-
if shouldRetry, err := func() (bool, error) {
6361
defer newConveyor.Terminate(ctx)
6462

65-
if err := f(newConveyor); manager.ShouldResetStagesStorageCache(err) {
66-
logboek.Context(ctx).Error().LogF("Will reset stages storage cache due to error: %s\n", err)
67-
68-
if err := newConveyor.StorageManager.ResetStagesStorageCache(ctx); err != nil {
69-
return false, err
70-
}
71-
return true, nil
72-
} else {
73-
return false, err
74-
}
75-
}(); err != nil {
76-
return err
77-
} else if shouldRetry {
78-
goto Retry
79-
}
80-
return nil
63+
return f(newConveyor)
64+
})
8165
}

pkg/storage/manager/storage_manager.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ func ShouldResetStagesStorageCache(err error) bool {
8080
return false
8181
}
8282

83+
func RetryOnStagesStorageCacheResetError(ctx context.Context, manager StorageManagerInterface, f func() error) error {
84+
Retry:
85+
err := f()
86+
87+
if ShouldResetStagesStorageCache(err) {
88+
logboek.Context(ctx).Error().LogF("Will reset stages storage cache due to error: %s\n", err)
89+
90+
if err := manager.ResetStagesStorageCache(ctx); err != nil {
91+
return fmt.Errorf("unable to reset stages storage cache: %s", err)
92+
}
93+
94+
goto Retry
95+
}
96+
97+
return err
98+
}
99+
83100
func NewStorageManager(projectName string, stagesStorage storage.StagesStorage, finalStagesStorage storage.StagesStorage, secondaryStagesStorageList []storage.StagesStorage, cacheStagesStorageList []storage.StagesStorage, storageLockManager storage.LockManager, stagesStorageCache storage.StagesStorageCache) *StorageManager {
84101
return &StorageManager{
85102
ProjectName: projectName,

0 commit comments

Comments
 (0)