0
0
Fork 0
mirror of https://github.com/crazy-max/diun.git synced 2025-04-18 00:42:35 +00:00

Skip dangling images ()

This commit is contained in:
CrazyMax 2020-06-20 03:48:38 +02:00
parent 9146b78111
commit f521384ee1
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
3 changed files with 37 additions and 14 deletions
internal/provider
pkg/docker

View file

@ -36,14 +36,20 @@ func (c *Client) listContainerImage() []model.Image {
var list []model.Image
for _, ctn := range ctns {
local, err := cli.IsLocalImage(ctn.Image)
imageRaw, err := cli.RawImage(ctn.Image)
if err != nil {
c.logger.Error().Err(err).Msgf("Cannot inspect image from container %s", ctn.ID)
continue
} else if local {
c.logger.Debug().Msgf("Skip locally built image for container %s", ctn.ID)
}
if local := cli.IsLocalImage(imageRaw); local {
c.logger.Debug().Msgf("Skip locally built image from container %s", ctn.ID)
continue
}
if dangling := cli.IsDanglingImage(imageRaw); dangling {
c.logger.Debug().Msgf("Skip dangling image from container %s", ctn.ID)
continue
}
image, err := provider.ValidateContainerImage(ctn.Image, ctn.Labels, *c.config.WatchByDefault)
if err != nil {
c.logger.Error().Err(err).Msgf("Cannot get image from container %s", ctn.ID)

View file

@ -29,11 +29,17 @@ func (c *Client) listServiceImage() []model.Image {
var list []model.Image
for _, svc := range svcs {
local, _ := cli.IsLocalImage(svc.Spec.TaskTemplate.ContainerSpec.Image)
if local {
c.logger.Debug().Msgf("Skip locally built image for service %s", svc.Spec.Name)
continue
if imageRaw, err := cli.RawImage(svc.Spec.TaskTemplate.ContainerSpec.Image); err == nil {
if local := cli.IsLocalImage(imageRaw); local {
c.logger.Debug().Msgf("Skip locally built image for service %s", svc.Spec.Name)
continue
}
if dangling := cli.IsDanglingImage(imageRaw); dangling {
c.logger.Debug().Msgf("Skip dangling image for service %s", svc.Spec.Name)
continue
}
}
image, err := provider.ValidateContainerImage(svc.Spec.TaskTemplate.ContainerSpec.Image, svc.Spec.Labels, *c.config.WatchByDefault)
if err != nil {
c.logger.Error().Err(err).Msgf("Cannot get image from service %s", svc.Spec.Name)

View file

@ -1,10 +1,21 @@
package docker
// IsLocalImage checks if the image has been built locally
func (c *Client) IsLocalImage(image string) (bool, error) {
raw, _, err := c.API.ImageInspectWithRaw(c.ctx, image)
if err != nil {
return false, err
}
return len(raw.RepoDigests) == 0, nil
import "github.com/docker/docker/api/types"
// RawImage returns the image information and its raw representation
func (c *Client) RawImage(image string) (types.ImageInspect, error) {
imageRaw, _, err := c.API.ImageInspectWithRaw(c.ctx, image)
return imageRaw, err
}
// IsLocalImage checks if the image has been built locally
func (c *Client) IsLocalImage(image types.ImageInspect) bool {
return len(image.RepoDigests) == 0
}
// IsDanglingImage returns whether the given image is "dangling" which means
// that there are no repository references to the given image and it has no
// child images
func (c *Client) IsDanglingImage(image types.ImageInspect) bool {
return len(image.RepoTags) == 1 && image.RepoTags[0] == "<none>:<none>" && len(image.RepoDigests) == 1 && image.RepoDigests[0] == "<none>@<none>"
}