From d1b83ca41ff2a0b807d34bcf04da4391036b374d Mon Sep 17 00:00:00 2001 From: Salil Ponde Date: Sat, 30 Dec 2023 10:05:36 +0530 Subject: [PATCH] Fixed image dangling logic and added InUse field to images --- pkg/dockerapi/image.go | 14 +++++++++++--- pkg/dockerapi/models.go | 1 + web/src/app/images/image-list.tsx | 2 ++ web/src/lib/api-models.ts | 1 + 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/dockerapi/image.go b/pkg/dockerapi/image.go index 0d66679..86d0d54 100644 --- a/pkg/dockerapi/image.go +++ b/pkg/dockerapi/image.go @@ -3,6 +3,7 @@ package dockerapi import ( "context" "errors" + "slices" "sort" "strings" @@ -17,6 +18,11 @@ func ImageList(req *DockerImageList) (*DockerImageListResponse, error) { return nil, err } + dcontainers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: req.All}) + if err != nil { + return nil, err + } + dimages, err := cli.ImageList(context.Background(), types.ImageListOptions{All: req.All}) if err != nil { return nil, err @@ -26,9 +32,10 @@ func ImageList(req *DockerImageList) (*DockerImageListResponse, error) { for i, item := range dimages { name := "" tag := "" - dangling := len(item.RepoTags) == 0 + inUse := slices.ContainsFunc(dcontainers, func(c types.Container) bool { return c.ImageID == item.ID }) + untagged := len(item.RepoTags) == 0 - if dangling { + if untagged { if len(item.RepoDigests) >= 1 { name = strings.Split(item.RepoDigests[0], "@")[0] } @@ -45,8 +52,9 @@ func ImageList(req *DockerImageList) (*DockerImageListResponse, error) { Name: name, Tag: tag, Size: item.Size, - Dangling: dangling, + Dangling: untagged && !inUse, Created: item.Created, + InUse: inUse, } } diff --git a/pkg/dockerapi/models.go b/pkg/dockerapi/models.go index 8edbe36..b84e0e1 100644 --- a/pkg/dockerapi/models.go +++ b/pkg/dockerapi/models.go @@ -63,6 +63,7 @@ type Image struct { Size int64 `json:"size"` Dangling bool `json:"dangling"` Created int64 `json:"created"` + InUse bool `json:"inUse"` } type DockerImageList struct { diff --git a/web/src/app/images/image-list.tsx b/web/src/app/images/image-list.tsx index 12959c3..1232b27 100644 --- a/web/src/app/images/image-list.tsx +++ b/web/src/app/images/image-list.tsx @@ -137,6 +137,7 @@ export default function ImageList() { Id Name Tag + Status Size Actions @@ -158,6 +159,7 @@ export default function ImageList() { "" )} + {item.inUse ? "In use" : "Unused"} {convertByteToMb(item.size)}