refactor: remove docker image list reference filter (#1501)
* refactor: remove docker reference filter * make it work * solve logic failure * Another mistake * another one * revert signature of ImageExistsLocally It is better to keep two return values
This commit is contained in:
parent
8b4f210872
commit
b2fb9e64ac
1 changed files with 18 additions and 49 deletions
|
@ -7,7 +7,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ImageExistsLocally returns a boolean indicating if an image with the
|
// ImageExistsLocally returns a boolean indicating if an image with the
|
||||||
|
@ -19,33 +19,15 @@ func ImageExistsLocally(ctx context.Context, imageName string, platform string)
|
||||||
}
|
}
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
filters := filters.NewArgs()
|
inspectImage, _, err := cli.ImageInspectWithRaw(ctx, imageName)
|
||||||
filters.Add("reference", imageName)
|
if client.IsErrNotFound(err) {
|
||||||
|
|
||||||
imageListOptions := types.ImageListOptions{
|
|
||||||
Filters: filters,
|
|
||||||
}
|
|
||||||
|
|
||||||
images, err := cli.ImageList(ctx, imageListOptions)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(images) > 0 {
|
|
||||||
if platform == "any" || platform == "" {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
for _, v := range images {
|
|
||||||
inspectImage, _, err := cli.ImageInspectWithRaw(ctx, v.ID)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if fmt.Sprintf("%s/%s", inspectImage.Os, inspectImage.Architecture) == platform {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false, nil
|
return false, nil
|
||||||
|
} else if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if platform == "" || platform == "any" || fmt.Sprintf("%s/%s", inspectImage.Os, inspectImage.Architecture) == platform {
|
||||||
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, nil
|
return false, nil
|
||||||
|
@ -54,38 +36,25 @@ func ImageExistsLocally(ctx context.Context, imageName string, platform string)
|
||||||
// RemoveImage removes image from local store, the function is used to run different
|
// RemoveImage removes image from local store, the function is used to run different
|
||||||
// container image architectures
|
// container image architectures
|
||||||
func RemoveImage(ctx context.Context, imageName string, force bool, pruneChildren bool) (bool, error) {
|
func RemoveImage(ctx context.Context, imageName string, force bool, pruneChildren bool) (bool, error) {
|
||||||
if exists, err := ImageExistsLocally(ctx, imageName, "any"); !exists {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cli, err := GetDockerClient(ctx)
|
cli, err := GetDockerClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
defer cli.Close()
|
||||||
|
|
||||||
filters := filters.NewArgs()
|
inspectImage, _, err := cli.ImageInspectWithRaw(ctx, imageName)
|
||||||
filters.Add("reference", imageName)
|
if client.IsErrNotFound(err) {
|
||||||
|
return false, nil
|
||||||
imageListOptions := types.ImageListOptions{
|
} else if err != nil {
|
||||||
Filters: filters,
|
|
||||||
}
|
|
||||||
|
|
||||||
images, err := cli.ImageList(ctx, imageListOptions)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(images) > 0 {
|
if _, err = cli.ImageRemove(ctx, inspectImage.ID, types.ImageRemoveOptions{
|
||||||
for _, v := range images {
|
|
||||||
if _, err = cli.ImageRemove(ctx, v.ID, types.ImageRemoveOptions{
|
|
||||||
Force: force,
|
Force: force,
|
||||||
PruneChildren: pruneChildren,
|
PruneChildren: pruneChildren,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue