@@ -17,13 +17,14 @@ limitations under the License.
1717package main
1818
1919import (
20- "time "
20+ "bufio "
2121 "flag"
2222 "os"
23- "bufio "
24- "github.com/golang/glog"
23+ "time "
24+
2525 "github.com/docker/docker/api/types"
2626 "github.com/docker/docker/client"
27+ "github.com/golang/glog"
2728 "golang.org/x/net/context"
2829)
2930
@@ -34,70 +35,72 @@ func readFileLines(path string) ([]string, error) {
3435 }
3536 file , err := os .Open (path )
3637 if err != nil {
37- return nil , err
38+ return nil , err
3839 }
3940 defer file .Close ()
4041
4142 scanner := bufio .NewScanner (file )
4243 for scanner .Scan () {
43- lines = append (lines , scanner .Text ())
44+ lines = append (lines , scanner .Text ())
4445 }
4546 return lines , scanner .Err ()
4647}
4748
4849var dryRun * bool
50+
4951const (
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"
5961)
6062
6163func _stringInList (list []string , s string ) bool {
6264 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
6870}
6971
7072func cleanImages (retainedImagesList []string , retainPeriod int64 ) {
7173 glog .Infof ("Entering cleanImages, length of retainedImagesList = %d" , len (retainedImagesList ))
7274 if os .Getenv ("DOCKER_API_VERSION" ) == "" {
7375 os .Setenv ("DOCKER_API_VERSION" , "1.35" )
7476 }
75-
76- cli , err := client .NewEnvClient ()
77+
78+ cli , err := client .NewClientWithOpts (
79+ client .FromEnv ,
80+ )
7781 if err != nil {
7882 panic (err )
7983 }
8084
8185 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
8791 childrenIDs map [string ]string
88- size int64
92+ size int64
8993 }
9094
91-
9295 /*
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
101104
102105 */
103106
@@ -115,12 +118,12 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
115118 images := make (map [string ]* imageToCleanStruct )
116119 for _ , img := range imagesFullList {
117120 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 ,
124127 childrenIDs : make (map [string ]string ),
125128 }
126129 }
@@ -131,7 +134,7 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
131134 parentImage , parentImageInList := images [img .ParentID ]
132135 if parentImageInList {
133136 parentImage .childrenIDs [imageID ] = statusFound
134- }
137+ }
135138 }
136139 }
137140
@@ -164,8 +167,8 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
164167 glog .Infof (" Skiping image %s - %v , it appears in retained list" , imageID , images [imageID ].tags )
165168 images [imageID ].status = statusRetainedByList
166169 } 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+
169172 glog .Infof (" Skiping image %s - %v , its created more than retainPeriod %d seconds ago" , imageID , images [imageID ].tags , retainPeriod )
170173 images [imageID ].status = statusRetainedByDate
171174 } else {
@@ -175,9 +178,9 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
175178 if ! * dryRun {
176179 _ , err = cli .ImageRemove (ctx , imageID , types.ImageRemoveOptions {Force : true , PruneChildren : false })
177180 } else {
178- glog .Infof ( "DRY RUN - do not actually delete" )
181+ glog .Infof ("DRY RUN - do not actually delete" )
179182 }
180-
183+
181184 if err == nil {
182185 glog .Infof (" image %s - %v has been deleted" , imageID , images [imageID ].tags )
183186 images [imageID ].status = statusRemoved
@@ -187,17 +190,17 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
187190 }
188191 }
189192
190- glog .Infof (" setting image status to %s" , images [imageID ].status )
193+ glog .Infof (" setting image status to %s" , images [imageID ].status )
191194 for _ , img := range images {
192195 if _ , ok := img .childrenIDs [imageID ]; ok {
193196 if images [imageID ].status == statusRemoved {
194197 glog .Infof (" deleting the child from parent image %s - %v" , img .ID , img .tags )
195198 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 {
197200 glog .Infof (" setting child status %s for image %s - %v" , images [imageID ].status , img .ID , img .tags )
198201 img .childrenIDs [imageID ] = images [imageID ].status
199202 img .status = statusChildRetained
200-
203+
201204 } else if images [imageID ].status == statusFailedToRemove {
202205 glog .Infof (" setting child status %s and deleting the from parent image %s - %v" , images [imageID ].status , img .ID , img .tags )
203206 delete (img .childrenIDs , imageID )
@@ -215,7 +218,7 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
215218 for childID , childStatus := range img .childrenIDs {
216219 glog .Infof (" Child: %s - %s (grandchild retained)" , childID , childStatus )
217220 }
218-
221+
219222 totalImagesSize += img .size
220223 switch img .status {
221224 case statusRemoved :
@@ -229,17 +232,17 @@ func cleanImages(retainedImagesList []string, retainPeriod int64) {
229232 }
230233 }
231234
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 )
243246}
244247
245248func main () {
@@ -254,18 +257,18 @@ Commands:
254257 flag .Set ("v" , "4" )
255258 flag .Set ("alsologtostderr" , "true" )
256259 validCommands := []string {"images" }
257- if len (os .Args ) < 2 {
260+ if len (os .Args ) < 2 {
258261 glog .Errorf ("%s" , usage )
259262 os .Exit (2 )
260- } else if ! _stringInList (validCommands ,os .Args [1 ]) {
263+ } else if ! _stringInList (validCommands , os .Args [1 ]) {
261264 glog .Errorf ("Invalid command %s\n %s" , os .Args [1 ], usage )
262265 os .Exit (2 )
263266 }
264267
265268 imagesCommand := flag .NewFlagSet ("images" , flag .ExitOnError )
266269 retainedImagesListFile := imagesCommand .String ("retained-images-file" , "" , "Retained images list file" )
267270 imageRetainPeriod := imagesCommand .Int64 ("image-retain-period" , 86400 , "image retain period" )
268-
271+
269272 dryRun = imagesCommand .Bool ("dry-run" , false , "dry run - only print actions" )
270273
271274 switch os .Args [1 ] {
@@ -281,13 +284,12 @@ Commands:
281284 * dryRun = true
282285 }
283286
284-
285287 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 )
291293
292294 retainedImagesList , err := readFileLines (* retainedImagesListFile )
293295 if err != nil {
0 commit comments