Skip to content

Commit

Permalink
Fixed image dangling logic and added InUse field to images
Browse files Browse the repository at this point in the history
  • Loading branch information
salilponde committed Dec 30, 2023
1 parent bcc9db3 commit d1b83ca
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pkg/dockerapi/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dockerapi
import (
"context"
"errors"
"slices"
"sort"
"strings"

Expand All @@ -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
Expand All @@ -26,9 +32,10 @@ func ImageList(req *DockerImageList) (*DockerImageListResponse, error) {
for i, item := range dimages {
name := "<none>"
tag := "<none>"
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]
}
Expand All @@ -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,
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dockerapi/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions web/src/app/images/image-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export default function ImageList() {
<TableHead scope="col">Id</TableHead>
<TableHead scope="col">Name</TableHead>
<TableHead scope="col">Tag</TableHead>
<TableHead scope="col">Status</TableHead>
<TableHead scope="col">Size</TableHead>
<TableHead scope="col">
<span className="sr-only">Actions</span>
Expand All @@ -158,6 +159,7 @@ export default function ImageList() {
""
)}
</TableCell>
<TableCell>{item.inUse ? "In use" : "Unused"}</TableCell>
<TableCell>{convertByteToMb(item.size)}</TableCell>
<TableCell className="text-right">
<TableButtonDelete
Expand Down
1 change: 1 addition & 0 deletions web/src/lib/api-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface IImage {
size: number
dangling: boolean
created: number
inUse: boolean
}

export interface IVolume {
Expand Down

0 comments on commit d1b83ca

Please sign in to comment.