/
cleanup.go
245 lines (196 loc) · 8.31 KB
/
cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package cleanup
import (
"fmt"
"github.com/spf13/cobra"
"github.com/werf/kubedog/pkg/kube"
"github.com/werf/logboek"
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/cleaning"
"github.com/werf/werf/pkg/container_runtime"
"github.com/werf/werf/pkg/docker"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/storage"
"github.com/werf/werf/pkg/storage/lrumeta"
"github.com/werf/werf/pkg/storage/manager"
"github.com/werf/werf/pkg/tmp_manager"
"github.com/werf/werf/pkg/true_git"
"github.com/werf/werf/pkg/werf"
)
var commonCmdData common.CmdData
var cmdData struct {
GitHistoryBasedCleanup bool
GitHistoryBasedCleanupV12 bool
}
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cleanup",
DisableFlagsInUseLine: true,
Short: "Safely cleanup unused project images and stages",
Long: common.GetLongCommandDescription(`Safely cleanup unused project images and stages.
First step is 'werf images cleanup' command, which will delete unused images from images repo. Second step is 'werf stages cleanup' command, which will delete unused stages from stages storage to be in sync with the images repo.
It is safe to run this command periodically (daily is enough) by automated cleanup job in parallel with other werf commands such as build, deploy and host cleanup.`),
Example: ` $ werf cleanup --stages-storage :local --images-repo registry.mydomain.com/myproject`,
RunE: func(cmd *cobra.Command, args []string) error {
defer werf.PrintGlobalWarnings(common.BackgroundContext())
if err := common.ProcessLogOptions(&commonCmdData); err != nil {
common.PrintHelp(cmd)
return err
}
common.LogVersion()
return common.LogRunningTime(func() error {
return runCleanup()
})
},
}
common.SetupDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupTmpDir(&commonCmdData, cmd)
common.SetupHomeDir(&commonCmdData, cmd)
common.SetupStagesStorageOptions(&commonCmdData, cmd)
common.SetupImagesRepoOptions(&commonCmdData, cmd)
common.SetupParallelOptions(&commonCmdData, cmd, common.DefaultCleanupParallelTasksLimit)
common.SetupDockerConfig(&commonCmdData, cmd, "Command needs granted permissions to read, pull and delete images from the specified stages storage and images repo")
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupImagesCleanupPolicies(&commonCmdData, cmd)
common.SetupGitHistorySynchronization(&commonCmdData, cmd)
common.SetupAllowGitShallowClone(&commonCmdData, cmd)
cmd.Flags().BoolVarP(&cmdData.GitHistoryBasedCleanup, "git-history-based-cleanup", "", common.GetBoolEnvironmentDefaultTrue("WERF_GIT_HISTORY_BASED_CLEANUP"), "Use git history based cleanup (default $WERF_GIT_HISTORY_BASED_CLEANUP)")
cmd.Flags().BoolVarP(&cmdData.GitHistoryBasedCleanupV12, "git-history-based-cleanup-v1.2", "", common.GetBoolEnvironmentDefaultTrue("WERF_GIT_HISTORY_BASED_CLEANUP_v1_2"), "Use git history based cleanup and delete images tags without related image metadata (default $WERF_GIT_HISTORY_BASED_CLEANUP_v1_2)")
common.SetupScanContextNamespaceOnly(&commonCmdData, cmd)
common.SetupDryRun(&commonCmdData, cmd)
common.SetupLogOptions(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
common.SetupSynchronization(&commonCmdData, cmd)
common.SetupKubeConfig(&commonCmdData, cmd)
common.SetupKubeConfigBase64(&commonCmdData, cmd)
common.SetupKubeContext(&commonCmdData, cmd)
common.SetupWithoutKube(&commonCmdData, cmd)
return cmd
}
func runCleanup() error {
tmp_manager.AutoGCEnabled = true
ctx := common.BackgroundContext()
if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
}
if err := true_git.Init(true_git.Options{LiveGitOutput: *commonCmdData.LogVerbose || *commonCmdData.LogDebug}); err != nil {
return err
}
if err := image.Init(); err != nil {
return err
}
if err := lrumeta.Init(); err != nil {
return err
}
if err := common.DockerRegistryInit(&commonCmdData); err != nil {
return err
}
if err := docker.Init(ctx, *commonCmdData.DockerConfig, *commonCmdData.LogVerbose, *commonCmdData.LogDebug); err != nil {
return err
}
ctxWithDockerCli, err := docker.NewContext(ctx)
if err != nil {
return err
}
ctx = ctxWithDockerCli
if err := kube.Init(kube.InitOptions{KubeConfigOptions: kube.KubeConfigOptions{
Context: *commonCmdData.KubeContext,
ConfigPath: *commonCmdData.KubeConfig,
ConfigDataBase64: *commonCmdData.KubeConfigBase64,
ConfigPathMergeList: *commonCmdData.KubeConfigPathMergeList,
}}); err != nil {
return fmt.Errorf("cannot initialize kube: %s", err)
}
if err := common.InitKubedog(ctx); err != nil {
return fmt.Errorf("cannot init kubedog: %s", err)
}
projectDir, err := common.GetProjectDir(&commonCmdData)
if err != nil {
return fmt.Errorf("getting project dir failed: %s", err)
}
common.ProcessLogProjectDir(&commonCmdData, projectDir)
projectTmpDir, err := tmp_manager.CreateProjectDir(ctx)
if err != nil {
return fmt.Errorf("getting project tmp dir failed: %s", err)
}
defer tmp_manager.ReleaseProjectDir(projectTmpDir)
werfConfig, err := common.GetRequiredWerfConfig(ctx, projectDir, &commonCmdData, true)
if err != nil {
return fmt.Errorf("unable to load werf config: %s", err)
}
projectName := werfConfig.Meta.Project
containerRuntime := &container_runtime.LocalDockerServerRuntime{} // TODO
stagesStorage, err := common.GetStagesStorage(containerRuntime, &commonCmdData)
if err != nil {
return err
}
synchronization, err := common.GetSynchronization(ctx, &commonCmdData, projectName, stagesStorage)
if err != nil {
return err
}
stagesStorageCache, err := common.GetStagesStorageCache(synchronization)
if err != nil {
return err
}
storageLockManager, err := common.GetStorageLockManager(ctx, synchronization)
if err != nil {
return err
}
storageManager := manager.NewStorageManager(projectName, storageLockManager, stagesStorageCache)
if err := storageManager.UseStagesStorage(ctx, stagesStorage); err != nil {
return err
}
if stagesStorage.Address() != storage.LocalStorageAddress && *commonCmdData.Parallel {
storageManager.StagesStorageManager.EnableParallel(int(*commonCmdData.ParallelTasksLimit))
}
imagesRepo, err := common.GetImagesRepo(ctx, projectName, &commonCmdData)
if err != nil {
return err
}
storageManager.SetImageRepo(imagesRepo)
if *commonCmdData.Parallel {
storageManager.ImagesRepoManager.EnableParallel(int(*commonCmdData.ParallelTasksLimit))
}
imagesNames, err := common.GetManagedImagesNames(ctx, projectName, stagesStorage, werfConfig)
if err != nil {
return err
}
logboek.Debug().LogF("Managed images names: %v\n", imagesNames)
localGitRepo, err := common.GetLocalGitRepoForImagesCleanup(projectDir, &commonCmdData)
if err != nil {
return err
}
policies, err := common.GetImagesCleanupPolicies(&commonCmdData)
if err != nil {
return err
}
kubernetesContextClients, err := common.GetKubernetesContextClients(&commonCmdData)
if err != nil {
return fmt.Errorf("unable to get Kubernetes clusters connections: %s", err)
}
cleanupOptions := cleaning.CleanupOptions{
ImagesCleanupOptions: cleaning.ImagesCleanupOptions{
ImageNameList: imagesNames,
LocalGit: localGitRepo,
KubernetesContextClients: kubernetesContextClients,
KubernetesNamespaceRestrictionByContext: common.GetKubernetesNamespaceRestrictionByContext(&commonCmdData, kubernetesContextClients),
WithoutKube: *commonCmdData.WithoutKube,
Policies: policies,
GitHistoryBasedCleanup: cmdData.GitHistoryBasedCleanup,
GitHistoryBasedCleanupV12: cmdData.GitHistoryBasedCleanupV12,
GitHistoryBasedCleanupOptions: werfConfig.Meta.Cleanup,
DryRun: *commonCmdData.DryRun,
},
StagesCleanupOptions: cleaning.StagesCleanupOptions{
ImageNameList: imagesNames,
DryRun: *commonCmdData.DryRun,
},
}
logboek.LogOptionalLn()
if err := cleaning.Cleanup(ctx, projectName, storageManager, storageLockManager, cleanupOptions); err != nil {
return err
}
return nil
}