@@ -17,13 +17,14 @@ limitations under the License.
17
17
package main
18
18
19
19
import (
20
- "time "
20
+ "bufio "
21
21
"flag"
22
22
"os"
23
- "bufio "
24
- "github.com/golang/glog"
23
+ "time "
24
+
25
25
"github.com/docker/docker/api/types"
26
26
"github.com/docker/docker/client"
27
+ "github.com/golang/glog"
27
28
"golang.org/x/net/context"
28
29
)
29
30
@@ -34,70 +35,72 @@ func readFileLines(path string) ([]string, error) {
34
35
}
35
36
file , err := os .Open (path )
36
37
if err != nil {
37
- return nil , err
38
+ return nil , err
38
39
}
39
40
defer file .Close ()
40
41
41
42
scanner := bufio .NewScanner (file )
42
43
for scanner .Scan () {
43
- lines = append (lines , scanner .Text ())
44
+ lines = append (lines , scanner .Text ())
44
45
}
45
46
return lines , scanner .Err ()
46
47
}
47
48
48
49
var dryRun * bool
50
+
49
51
const (
50
- cmdImages = "images"
51
-
52
- statusFound = "found"
53
- statusRemoved = "removed"
54
- statusRetainedByList = "retainedByList"
55
- statusRetainedByDate = "retainedByDate"
56
- statusChildRetained = "childRetained"
57
- statusChildFailedToRemove = "childFailedToRemove"
58
- statusFailedToRemove = "failedToRemove"
52
+ cmdImages = "images"
53
+
54
+ statusFound = "found"
55
+ statusRemoved = "removed"
56
+ statusRetainedByList = "retainedByList"
57
+ statusRetainedByDate = "retainedByDate"
58
+ statusChildRetained = "childRetained"
59
+ statusChildFailedToRemove = "childFailedToRemove"
60
+ statusFailedToRemove = "failedToRemove"
59
61
)
60
62
61
63
func _stringInList (list []string , s string ) bool {
62
64
for _ , a := range list {
63
- if a == s {
64
- return true
65
- }
66
- }
67
- return false
65
+ if a == s {
66
+ return true
67
+ }
68
+ }
69
+ return false
68
70
}
69
71
70
72
func cleanImages (retainedImagesList []string , retainPeriod int64 ) {
71
73
glog .Infof ("Entering cleanImages, length of retainedImagesList = %d" , len (retainedImagesList ))
72
74
if os .Getenv ("DOCKER_API_VERSION" ) == "" {
73
75
os .Setenv ("DOCKER_API_VERSION" , "1.35" )
74
76
}
75
-
76
- cli , err := client .NewEnvClient ()
77
+
78
+ cli , err := client .NewClientWithOpts (
79
+ client .FromEnv ,
80
+ )
77
81
if err != nil {
78
82
panic (err )
79
83
}
80
84
81
85
type imageToCleanStruct = struct {
82
- ID string
83
- Created int64
84
- ParentID string
85
- status string
86
- tags []string
86
+ ID string
87
+ Created int64
88
+ ParentID string
89
+ status string
90
+ tags []string
87
91
childrenIDs map [string ]string
88
- size int64
92
+ size int64
89
93
}
90
94
91
-
92
95
/*
93
- Purpose: remove images starting from first child excluding ids in retainedImagesList
94
- Logic:
95
- 1. get All images (with All=true)
96
- 2. fill map of imageToCleanStruct - for each image fill its children in the map of [id]"status"
97
- 3. find images with no children
98
- 4. loop by found images with no children and delete them, then update childrenList of whole map of imageToCleanStruct.
99
- Skip deletion for images in retainedImagesList
100
- --- Repeat 3-4 until images to delete found
96
+ Purpose: remove images starting from first child excluding ids in retainedImagesList
97
+ Logic:
98
+ 1. get All images (with All=true)
99
+ 2. fill map of imageToCleanStruct - for each image fill its children in the map of [id]"status"
100
+ 3. find images with no children
101
+ 4. loop by found images with no children and delete them, then update childrenList of whole map of imageToCleanStruct.
102
+ Skip deletion for images in retainedImagesList
103
+ --- Repeat 3-4 until images to delete found
101
104
102
105
*/
103
106
@@ -115,12 +118,12 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
115
118
images := make (map [string ]* imageToCleanStruct )
116
119
for _ , img := range imagesFullList {
117
120
images [img .ID ] = & imageToCleanStruct {
118
- ID : img .ID ,
119
- Created : img .Created ,
120
- ParentID : img .ParentID ,
121
- status : statusFound ,
122
- tags : img .RepoTags ,
123
- size : img .Size ,
121
+ ID : img .ID ,
122
+ Created : img .Created ,
123
+ ParentID : img .ParentID ,
124
+ status : statusFound ,
125
+ tags : img .RepoTags ,
126
+ size : img .Size ,
124
127
childrenIDs : make (map [string ]string ),
125
128
}
126
129
}
@@ -131,7 +134,7 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
131
134
parentImage , parentImageInList := images [img .ParentID ]
132
135
if parentImageInList {
133
136
parentImage .childrenIDs [imageID ] = statusFound
134
- }
137
+ }
135
138
}
136
139
}
137
140
@@ -164,8 +167,8 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
164
167
glog .Infof (" Skiping image %s - %v , it appears in retained list" , imageID , images [imageID ].tags )
165
168
images [imageID ].status = statusRetainedByList
166
169
} else if retainPeriod > 0 && images [imageID ].Created > 0 && images [imageID ].Created < currentTs &&
167
- currentTs - images [imageID ].Created < retainPeriod {
168
-
170
+ currentTs - images [imageID ].Created < retainPeriod {
171
+
169
172
glog .Infof (" Skiping image %s - %v , its created more than retainPeriod %d seconds ago" , imageID , images [imageID ].tags , retainPeriod )
170
173
images [imageID ].status = statusRetainedByDate
171
174
} else {
@@ -175,9 +178,9 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
175
178
if ! * dryRun {
176
179
_ , err = cli .ImageRemove (ctx , imageID , types.ImageRemoveOptions {Force : true , PruneChildren : false })
177
180
} else {
178
- glog .Infof ( "DRY RUN - do not actually delete" )
181
+ glog .Infof ("DRY RUN - do not actually delete" )
179
182
}
180
-
183
+
181
184
if err == nil {
182
185
glog .Infof (" image %s - %v has been deleted" , imageID , images [imageID ].tags )
183
186
images [imageID ].status = statusRemoved
@@ -187,17 +190,17 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
187
190
}
188
191
}
189
192
190
- glog .Infof (" setting image status to %s" , images [imageID ].status )
193
+ glog .Infof (" setting image status to %s" , images [imageID ].status )
191
194
for _ , img := range images {
192
195
if _ , ok := img .childrenIDs [imageID ]; ok {
193
196
if images [imageID ].status == statusRemoved {
194
197
glog .Infof (" deleting the child from parent image %s - %v" , img .ID , img .tags )
195
198
delete (img .childrenIDs , imageID )
196
- } else if images [imageID ].status == statusRetainedByList || images [imageID ].status == statusRetainedByDate {
199
+ } else if images [imageID ].status == statusRetainedByList || images [imageID ].status == statusRetainedByDate {
197
200
glog .Infof (" setting child status %s for image %s - %v" , images [imageID ].status , img .ID , img .tags )
198
201
img .childrenIDs [imageID ] = images [imageID ].status
199
202
img .status = statusChildRetained
200
-
203
+
201
204
} else if images [imageID ].status == statusFailedToRemove {
202
205
glog .Infof (" setting child status %s and deleting the from parent image %s - %v" , images [imageID ].status , img .ID , img .tags )
203
206
delete (img .childrenIDs , imageID )
@@ -215,7 +218,7 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
215
218
for childID , childStatus := range img .childrenIDs {
216
219
glog .Infof (" Child: %s - %s (grandchild retained)" , childID , childStatus )
217
220
}
218
-
221
+
219
222
totalImagesSize += img .size
220
223
switch img .status {
221
224
case statusRemoved :
@@ -229,17 +232,17 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
229
232
}
230
233
}
231
234
232
- glog .Infof ("\n -----------\n " +
233
- " total images shared size: %.3f Mb \n " +
234
- " removed shared size: %.3f Mb \n " +
235
- "retained shared by list size: %.3f Mb \n " +
236
- "retained shared by date size: %.3f Mb \n " +
237
- " failed to remove size: %.3f Mb " ,
238
- float64 (totalImagesSize )/ 1024 / 1024.0 ,
239
- float64 (removedSize )/ 1024 / 1024.0 ,
240
- float64 (retainedByListSize )/ 1024 / 1024.0 ,
241
- float64 (retainedByDateSize )/ 1024 / 1024.0 ,
242
- float64 (failedToRemoveSize )/ 1024 / 1024.0 )
235
+ glog .Infof ("\n -----------\n " +
236
+ " total images shared size: %.3f Mb \n " +
237
+ " removed shared size: %.3f Mb \n " +
238
+ "retained shared by list size: %.3f Mb \n " +
239
+ "retained shared by date size: %.3f Mb \n " +
240
+ " failed to remove size: %.3f Mb " ,
241
+ float64 (totalImagesSize )/ 1024 / 1024.0 ,
242
+ float64 (removedSize )/ 1024 / 1024.0 ,
243
+ float64 (retainedByListSize )/ 1024 / 1024.0 ,
244
+ float64 (retainedByDateSize )/ 1024 / 1024.0 ,
245
+ float64 (failedToRemoveSize )/ 1024 / 1024.0 )
243
246
}
244
247
245
248
func main () {
@@ -254,18 +257,18 @@ Commands:
254
257
flag .Set ("v" , "4" )
255
258
flag .Set ("alsologtostderr" , "true" )
256
259
validCommands := []string {"images" }
257
- if len (os .Args ) < 2 {
260
+ if len (os .Args ) < 2 {
258
261
glog .Errorf ("%s" , usage )
259
262
os .Exit (2 )
260
- } else if ! _stringInList (validCommands ,os .Args [1 ]) {
263
+ } else if ! _stringInList (validCommands , os .Args [1 ]) {
261
264
glog .Errorf ("Invalid command %s\n %s" , os .Args [1 ], usage )
262
265
os .Exit (2 )
263
266
}
264
267
265
268
imagesCommand := flag .NewFlagSet ("images" , flag .ExitOnError )
266
269
retainedImagesListFile := imagesCommand .String ("retained-images-file" , "" , "Retained images list file" )
267
270
imageRetainPeriod := imagesCommand .Int64 ("image-retain-period" , 86400 , "image retain period" )
268
-
271
+
269
272
dryRun = imagesCommand .Bool ("dry-run" , false , "dry run - only print actions" )
270
273
271
274
switch os .Args [1 ] {
@@ -281,13 +284,12 @@ Commands:
281
284
* dryRun = true
282
285
}
283
286
284
-
285
287
glog .Infof ("\n ----------------\n Started dind-cleaner" )
286
-
287
- glog .Infof ("First verson - only image cleaner. " +
288
- "retainedImagesListFile = %s " +
289
- "retainedImagesPeriod = %d " +
290
- "dry-run = %t" , * retainedImagesListFile , * imageRetainPeriod , * dryRun )
288
+
289
+ glog .Infof ("First verson - only image cleaner. " +
290
+ "retainedImagesListFile = %s " +
291
+ "retainedImagesPeriod = %d " +
292
+ "dry-run = %t" , * retainedImagesListFile , * imageRetainPeriod , * dryRun )
291
293
292
294
retainedImagesList , err := readFileLines (* retainedImagesListFile )
293
295
if err != nil {
0 commit comments