daemon: remove intermediate vars when collecting diskUsage

Set values directly on the DiskUsage objects instead of using some
intermediate vars, some of which were named slightly confusing due
to them being used both for "totalSize" and "reclaimableSize".

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-11-07 14:43:32 +01:00
parent 0dcb1fe344
commit 20870f13c2

View File

@@ -26,30 +26,27 @@ func (daemon *Daemon) containerDiskUsage(ctx context.Context, verbose bool) (*ba
return nil, fmt.Errorf("failed to retrieve container list: %v", err)
}
// Remove image manifest descriptor from the result as it should not be included.
// https://github.com/moby/moby/pull/49407#discussion_r1954396666
for _, c := range containers {
c.ImageManifestDescriptor = nil
}
isActive := func(ctr *container.Summary) bool {
return ctr.State == container.StateRunning ||
ctr.State == container.StatePaused ||
ctr.State == container.StateRestarting
}
activeCount := int64(len(containers))
du := &backend.ContainerDiskUsage{TotalCount: activeCount}
du := &backend.ContainerDiskUsage{
ActiveCount: int64(len(containers)),
TotalCount: int64(len(containers)),
}
for _, ctr := range containers {
du.TotalSize += ctr.SizeRw
if !isActive(ctr) {
du.Reclaimable += ctr.SizeRw
activeCount--
du.ActiveCount--
}
}
du.ActiveCount = activeCount
// Remove image manifest descriptor from the result as it should not be included.
// https://github.com/moby/moby/pull/49407#discussion_r1954396666
ctr.ImageManifestDescriptor = nil
}
if verbose {
du.Items = sliceutil.Deref(containers)
@@ -73,29 +70,29 @@ func (daemon *Daemon) imageDiskUsage(ctx context.Context, verbose bool) (*backen
return nil, errors.Wrap(err, "failed to retrieve image list")
}
reclaimable, _, err := daemon.usageLayer.Do(ctx, struct{}{}, func(ctx context.Context) (int64, error) {
totalSize, _, err := daemon.usageLayer.Do(ctx, struct{}{}, func(ctx context.Context) (int64, error) {
return daemon.imageService.ImageDiskUsage(ctx)
})
if err != nil {
return nil, errors.Wrap(err, "failed to calculate image disk usage")
}
activeCount := int64(len(images))
du := &backend.ImageDiskUsage{TotalCount: activeCount, TotalSize: reclaimable}
du := &backend.ImageDiskUsage{
ActiveCount: int64(len(images)),
Reclaimable: totalSize,
TotalCount: int64(len(images)),
TotalSize: totalSize,
}
for _, i := range images {
if i.Containers == 0 {
activeCount--
du.ActiveCount--
if i.Size == -1 || i.SharedSize == -1 {
continue
}
reclaimable -= i.Size - i.SharedSize
du.Reclaimable -= i.Size - i.SharedSize
}
}
du.Reclaimable = reclaimable
du.ActiveCount = activeCount
if verbose {
du.Items = sliceutil.Deref(images)
}
@@ -115,21 +112,20 @@ func (daemon *Daemon) localVolumesSize(ctx context.Context, verbose bool) (*back
return nil, err
}
activeCount := int64(len(volumes))
du := &backend.VolumeDiskUsage{TotalCount: activeCount}
du := &backend.VolumeDiskUsage{
ActiveCount: int64(len(volumes)),
TotalCount: int64(len(volumes)),
}
for _, v := range volumes {
if v.UsageData.Size != -1 {
du.TotalSize += v.UsageData.Size
if v.UsageData.RefCount == 0 {
du.Reclaimable += v.UsageData.Size
activeCount--
du.ActiveCount--
}
du.TotalSize += v.UsageData.Size
}
}
du.ActiveCount = activeCount
if verbose {
du.Items = sliceutil.Deref(volumes)
}