diff --git a/cmd/werf/build/main.go b/cmd/werf/build/main.go index 8bf58a4974..914d65d866 100644 --- a/cmd/werf/build/main.go +++ b/cmd/werf/build/main.go @@ -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) } }() diff --git a/cmd/werf/bundle/export/export.go b/cmd/werf/bundle/export/export.go index c9e9e09744..f572376231 100644 --- a/cmd/werf/bundle/export/export.go +++ b/cmd/werf/bundle/export/export.go @@ -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) } }() diff --git a/cmd/werf/bundle/publish/publish.go b/cmd/werf/bundle/publish/publish.go index 7975072720..8b08436ba4 100644 --- a/cmd/werf/bundle/publish/publish.go +++ b/cmd/werf/bundle/publish/publish.go @@ -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) } }() diff --git a/cmd/werf/cleanup/cleanup.go b/cmd/werf/cleanup/cleanup.go index ddc3a7fec2..dfa24290bc 100644 --- a/cmd/werf/cleanup/cleanup.go +++ b/cmd/werf/cleanup/cleanup.go @@ -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) } }() diff --git a/cmd/werf/common/host_cleanup.go b/cmd/werf/common/host_cleanup.go index de3cfcd70f..0ea63c33b1 100644 --- a/cmd/werf/common/host_cleanup.go +++ b/cmd/werf/common/host_cleanup.go @@ -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, diff --git a/cmd/werf/compose/main.go b/cmd/werf/compose/main.go index e0fdf677c9..40cd8a9e5b 100644 --- a/cmd/werf/compose/main.go +++ b/cmd/werf/compose/main.go @@ -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) } }() diff --git a/cmd/werf/converge/converge.go b/cmd/werf/converge/converge.go index e41d6e5c7c..34f9886d2a 100644 --- a/cmd/werf/converge/converge.go +++ b/cmd/werf/converge/converge.go @@ -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) } }() diff --git a/cmd/werf/host/cleanup/cleanup.go b/cmd/werf/host/cleanup/cleanup.go index 9bcf8bd40c..62c059e448 100644 --- a/cmd/werf/host/cleanup/cleanup.go +++ b/cmd/werf/host/cleanup/cleanup.go @@ -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,6 +77,8 @@ 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)") @@ -83,7 +86,7 @@ It is safe to run this command periodically by automated cleanup job in parallel } 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, diff --git a/pkg/host_cleaning/host_cleanup.go b/pkg/host_cleaning/host_cleanup.go index e45f8c1de8..18cc816642 100644 --- a/pkg/host_cleaning/host_cleanup.go +++ b/pkg/host_cleaning/host_cleanup.go @@ -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) {