Skip to content

Commit

Permalink
fix(host-cleanup): run host cleanup without docker-server in buildah …
Browse files Browse the repository at this point in the history
…mode

Do not cleanup local docker server, but cleanup ~/.werf/local_cache in the case when WERF_BUIDLAH_MODE used.

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jun 16, 2022
1 parent 9702026 commit f1b1403
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 44 deletions.
5 changes: 1 addition & 4 deletions cmd/werf/build/main.go
Expand Up @@ -161,10 +161,7 @@ func runMain(ctx context.Context, args []string) error {
}

defer func() {
if _, match := containerBackend.(*container_backend.DockerServerBackend); !match {
return
}
if err := common.RunAutoHostCleanup(ctx, &commonCmdData); err != nil {
if err := common.RunAutoHostCleanup(ctx, &commonCmdData, containerBackend); err != nil {
logboek.Context(ctx).Error().LogF("Auto host cleanup failed: %s\n", err)
}
}()
Expand Down
6 changes: 1 addition & 5 deletions cmd/werf/bundle/export/export.go
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_backend"
"github.com/werf/werf/pkg/deploy/helm/chart_extender"
"github.com/werf/werf/pkg/deploy/helm/chart_extender/helpers"
"github.com/werf/werf/pkg/git_repo"
Expand Down Expand Up @@ -163,10 +162,7 @@ func runExport(ctx context.Context) error {
}

defer func() {
if _, match := containerBackend.(*container_backend.DockerServerBackend); !match {
return
}
if err := common.RunAutoHostCleanup(ctx, &commonCmdData); err != nil {
if err := common.RunAutoHostCleanup(ctx, &commonCmdData, containerBackend); err != nil {
logboek.Context(ctx).Error().LogF("Auto host cleanup failed: %s\n", err)
}
}()
Expand Down
6 changes: 1 addition & 5 deletions cmd/werf/bundle/publish/publish.go
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_backend"
"github.com/werf/werf/pkg/deploy/bundles"
"github.com/werf/werf/pkg/deploy/helm/chart_extender"
"github.com/werf/werf/pkg/deploy/helm/chart_extender/helpers"
Expand Down Expand Up @@ -179,10 +178,7 @@ func runPublish(ctx context.Context) error {
}

defer func() {
if _, match := containerBackend.(*container_backend.DockerServerBackend); !match {
return
}
if err := common.RunAutoHostCleanup(ctx, &commonCmdData); err != nil {
if err := common.RunAutoHostCleanup(ctx, &commonCmdData, containerBackend); err != nil {
logboek.Context(ctx).Error().LogF("Auto host cleanup failed: %s\n", err)
}
}()
Expand Down
6 changes: 1 addition & 5 deletions cmd/werf/cleanup/cleanup.go
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/werf/logboek"
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/cleaning"
"github.com/werf/werf/pkg/container_backend"
"github.com/werf/werf/pkg/git_repo"
"github.com/werf/werf/pkg/git_repo/gitdata"
"github.com/werf/werf/pkg/image"
Expand Down Expand Up @@ -135,10 +134,7 @@ func runCleanup(ctx context.Context) error {
}

defer func() {
if _, match := containerBackend.(*container_backend.DockerServerBackend); !match {
return
}
if err := common.RunAutoHostCleanup(ctx, &commonCmdData); err != nil {
if err := common.RunAutoHostCleanup(ctx, &commonCmdData, containerBackend); err != nil {
logboek.Context(ctx).Error().LogF("Auto host cleanup failed: %s\n", err)
}
}()
Expand Down
13 changes: 10 additions & 3 deletions cmd/werf/common/host_cleanup.go
Expand Up @@ -7,14 +7,20 @@ import (

"github.com/spf13/cobra"

"github.com/werf/werf/pkg/container_backend"
"github.com/werf/werf/pkg/host_cleaning"
)

func RunAutoHostCleanup(ctx context.Context, cmdData *CmdData) error {
func RunAutoHostCleanup(ctx context.Context, cmdData *CmdData, containerBackend container_backend.ContainerBackend) error {
if *cmdData.DisableAutoHostCleanup {
return nil
}

cleanupDockerServer := false
if _, match := containerBackend.(*container_backend.DockerServerBackend); match {
cleanupDockerServer = true
}

if *cmdData.AllowedDockerStorageVolumeUsageMargin >= *cmdData.AllowedDockerStorageVolumeUsage {
return fmt.Errorf("incompatible params --allowed-docker-storage-volume-usage=%d and --allowed-docker-storage-volume-usage-margin=%d: margin percentage should be less than allowed volume usage level percentage", *cmdData.AllowedDockerStorageVolumeUsage, *cmdData.AllowedDockerStorageVolumeUsageMargin)
}
Expand All @@ -25,8 +31,9 @@ func RunAutoHostCleanup(ctx context.Context, cmdData *CmdData) error {

return host_cleaning.RunAutoHostCleanup(ctx, host_cleaning.AutoHostCleanupOptions{
HostCleanupOptions: host_cleaning.HostCleanupOptions{
DryRun: false,
Force: false,
DryRun: false,
Force: false,
CleanupDockerServer: cleanupDockerServer,
AllowedDockerStorageVolumeUsagePercentage: cmdData.AllowedDockerStorageVolumeUsage,
AllowedDockerStorageVolumeUsageMarginPercentage: cmdData.AllowedDockerStorageVolumeUsageMargin,
AllowedLocalCacheVolumeUsagePercentage: cmdData.AllowedLocalCacheVolumeUsage,
Expand Down
5 changes: 1 addition & 4 deletions cmd/werf/compose/main.go
Expand Up @@ -323,10 +323,7 @@ func runMain(ctx context.Context, dockerComposeCmdName string, cmdData composeCm
}

defer func() {
if _, match := containerBackend.(*container_backend.DockerServerBackend); !match {
return
}
if err := common.RunAutoHostCleanup(ctx, &commonCmdData); err != nil {
if err := common.RunAutoHostCleanup(ctx, &commonCmdData, containerBackend); err != nil {
logboek.Context(ctx).Error().LogF("Auto host cleanup failed: %s\n", err)
}
}()
Expand Down
5 changes: 1 addition & 4 deletions cmd/werf/converge/converge.go
Expand Up @@ -205,10 +205,7 @@ func runMain(ctx context.Context) error {
}

defer func() {
if _, match := containerBackend.(*container_backend.DockerServerBackend); !match {
return
}
if err := common.RunAutoHostCleanup(ctx, &commonCmdData); err != nil {
if err := common.RunAutoHostCleanup(ctx, &commonCmdData, containerBackend); err != nil {
logboek.Context(ctx).Error().LogF("Auto host cleanup failed: %s\n", err)
}
}()
Expand Down
15 changes: 12 additions & 3 deletions cmd/werf/host/cleanup/cleanup.go
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/werf/logboek"
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/container_backend"
"github.com/werf/werf/pkg/git_repo"
"github.com/werf/werf/pkg/git_repo/gitdata"
"github.com/werf/werf/pkg/host_cleaning"
Expand Down Expand Up @@ -76,14 +77,16 @@ It is safe to run this command periodically by automated cleanup job in parallel
common.SetupAllowedLocalCacheVolumeUsageMargin(&commonCmdData, cmd)
common.SetupDockerServerStoragePath(&commonCmdData, cmd)
common.SetupPlatform(&commonCmdData, cmd)
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)

cmd.Flags().BoolVarP(&cmdData.Force, "force", "", common.GetBoolEnvironmentDefaultFalse("WERF_FORCE"), "Force deletion of images which are being used by some containers (default $WERF_FORCE)")

return cmd
}

func runCleanup(ctx context.Context) error {
_, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData)
containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,9 +120,15 @@ func runCleanup(ctx context.Context) error {

logboek.LogOptionalLn()

cleanupDockerServer := false
if _, match := containerBackend.(*container_backend.DockerServerBackend); match {
cleanupDockerServer = true
}

hostCleanupOptions := host_cleaning.HostCleanupOptions{
DryRun: *commonCmdData.DryRun,
Force: cmdData.Force,
DryRun: *commonCmdData.DryRun,
Force: cmdData.Force,
CleanupDockerServer: cleanupDockerServer,
AllowedDockerStorageVolumeUsagePercentage: commonCmdData.AllowedDockerStorageVolumeUsage,
AllowedDockerStorageVolumeUsageMarginPercentage: commonCmdData.AllowedDockerStorageVolumeUsageMargin,
AllowedLocalCacheVolumeUsagePercentage: commonCmdData.AllowedLocalCacheVolumeUsage,
Expand Down
27 changes: 16 additions & 11 deletions pkg/host_cleaning/host_cleanup.go
Expand Up @@ -26,8 +26,9 @@ type HostCleanupOptions struct {
AllowedLocalCacheVolumeUsageMarginPercentage *uint
DockerServerStoragePath *string

DryRun bool
Force bool
CleanupDockerServer bool
DryRun bool
Force bool
}

type AutoHostCleanupOptions struct {
Expand Down Expand Up @@ -137,17 +138,21 @@ func RunHostCleanup(ctx context.Context, options HostCleanupOptions) error {
return err
}

dockerServerStoragePath, err := getDockerServerStoragePath(ctx, options.DockerServerStoragePath)
if err != nil {
return fmt.Errorf("error getting local docker server storage path: %w", err)
if options.CleanupDockerServer {
dockerServerStoragePath, err := getDockerServerStoragePath(ctx, options.DockerServerStoragePath)
if err != nil {
return fmt.Errorf("error getting local docker server storage path: %w", err)
}

return logboek.Context(ctx).Default().LogProcess("Running GC for local docker server").DoError(func() error {
if err := RunGCForLocalDockerServer(ctx, allowedDockerStorageVolumeUsagePercentage, allowedDockerStorageVolumeUsageMarginPercentage, dockerServerStoragePath, options.Force, options.DryRun); err != nil {
return fmt.Errorf("local docker server GC failed: %w", err)
}
return nil
})
}

return logboek.Context(ctx).Default().LogProcess("Running GC for local docker server").DoError(func() error {
if err := RunGCForLocalDockerServer(ctx, allowedDockerStorageVolumeUsagePercentage, allowedDockerStorageVolumeUsageMarginPercentage, dockerServerStoragePath, options.Force, options.DryRun); err != nil {
return fmt.Errorf("local docker server GC failed: %w", err)
}
return nil
})
return nil
}

func ShouldRunAutoHostCleanup(ctx context.Context, options HostCleanupOptions) (bool, error) {
Expand Down

0 comments on commit f1b1403

Please sign in to comment.