Skip to content
Permalink
Browse files
fix(host-cleanup): run host cleanup without docker-server in buildah …
…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 f1b140316c7868e8a7bc12a52531603449d7cff3
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 44 deletions.
@@ -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)
}
}()
@@ -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"
@@ -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)
}
}()
@@ -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"
@@ -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)
}
}()
@@ -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"
@@ -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)
}
}()
@@ -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)
}
@@ -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,
@@ -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)
}
}()
@@ -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)
}
}()
@@ -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"
@@ -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
}
@@ -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,
@@ -26,8 +26,9 @@ type HostCleanupOptions struct {
AllowedLocalCacheVolumeUsageMarginPercentage *uint
DockerServerStoragePath *string

DryRun bool
Force bool
CleanupDockerServer bool
DryRun bool
Force bool
}

type AutoHostCleanupOptions struct {
@@ -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) {

0 comments on commit f1b1403

Please sign in to comment.