From bdaa194a7e6075555d1f1edfa151821c2477c99e Mon Sep 17 00:00:00 2001 From: wrfly Date: Wed, 26 Jul 2017 16:23:52 +0800 Subject: [PATCH] refector cleanImageOnNode --- cluster/calcium/image.go | 46 +++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/cluster/calcium/image.go b/cluster/calcium/image.go index 8d521587e..8bb7a9882 100644 --- a/cluster/calcium/image.go +++ b/cluster/calcium/image.go @@ -1,11 +1,12 @@ package calcium import ( - "sort" "strings" "sync" + log "github.com/Sirupsen/logrus" enginetypes "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/filters" "gitlab.ricebook.net/platform/core/types" "golang.org/x/net/context" ) @@ -61,7 +62,9 @@ func (c *calcium) cleanImage(podname, image string) error { wg.Add(1) go func(node *types.Node) { defer wg.Done() - cleanImageOnNode(node, image) + if err := cleanImageOnNode(node, image); err != nil { + log.Errorf("cleanImageOnNode error: %s", err) + } }(node) } @@ -87,39 +90,30 @@ func (x imageList) Less(i, j int) bool { return x[i].Created > x[j].Created } // 只清理同名字不同tag的 // 并且保留最新的两个 func cleanImageOnNode(node *types.Node, image string) error { - images, err := node.Engine.ImageList(context.Background(), enginetypes.ImageListOptions{}) + log.Debugf("[cleanImageOnNode] node: %s, image: %s", node.Name, strings.Split(image, ":")[0]) + imgListFilter := filters.NewArgs() + image = normalizeImage(image) + imgListFilter.Add("reference", image) // 相同repo的image + images, err := node.Engine.ImageList(context.Background(), enginetypes.ImageListOptions{Filters: imgListFilter}) if err != nil { return err } - image = normalizeImage(image) - - toDelete := imageList{} - for _, img := range images { - // 没有名字的不管 - if len(img.RepoTags) == 0 { - continue - } - tag := img.RepoTags[0] - // 别人的镜像不管 - if !strings.HasPrefix(tag, image) { - continue - } - toDelete = append(toDelete, img) + if len(images) < 2 { + return nil } - sort.Sort(toDelete) - for index, img := range toDelete { - // 如果是最新的两个(index是0或者1), 就忽略 - if index <= 1 { - continue - } - // 其他都无情删掉 - // 可能因为有容器的存在而失败, 就放他苟且吧 - node.Engine.ImageRemove(context.Background(), img.RepoTags[0], enginetypes.ImageRemoveOptions{ + images = images[2:] + log.Debugf("Delete Images: %v", images) + + for _, image := range images { + _, err := node.Engine.ImageRemove(context.Background(), image.ID, enginetypes.ImageRemoveOptions{ Force: false, PruneChildren: true, }) + if err != nil { + log.Errorf("[cleanImageOnNode] Node %s ImageRemove error: %s, imageID: %s", node.Name, err, image.ID) + } } return nil }