daemon: align build.DiskUsage() with other disk-usages

Move calculation of the data to the builder backend, to align with
the other type of objects. This also allows us to skip the verbose
data if it's not used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-11-07 13:01:33 +01:00
parent f1a3387633
commit 0dcb1fe344
4 changed files with 37 additions and 33 deletions

View File

@@ -150,15 +150,26 @@ func (b *Builder) Cancel(ctx context.Context, id string) error {
}
// DiskUsage returns a report about space used by build cache
func (b *Builder) DiskUsage(ctx context.Context) ([]build.CacheRecord, error) {
func (b *Builder) DiskUsage(ctx context.Context, options buildbackend.DiskUsageOptions) (*buildbackend.DiskUsage, error) {
duResp, err := b.controller.DiskUsage(ctx, &controlapi.DiskUsageRequest{})
if err != nil {
return nil, err
return nil, fmt.Errorf("error getting build cache usage: %w", err)
}
var items []build.CacheRecord
var usage buildbackend.DiskUsage
for _, r := range duResp.Record {
items = append(items, build.CacheRecord{
usage.TotalCount++
usage.TotalSize += r.Size
if r.InUse {
usage.ActiveCount++
}
if !r.InUse && !r.Shared {
usage.Reclaimable += r.Size
}
if !options.Verbose {
continue
}
usage.Items = append(usage.Items, build.CacheRecord{
ID: r.ID,
Parents: r.Parents,
Type: r.RecordType,
@@ -182,7 +193,7 @@ func (b *Builder) DiskUsage(ctx context.Context) ([]build.CacheRecord, error) {
UsageCount: int(r.UsageCount),
})
}
return items, nil
return &usage, nil
}
// Prune clears all reclaimable build cache.

View File

@@ -11,6 +11,13 @@ import (
"github.com/moby/moby/v2/daemon/internal/filters"
)
type DiskUsageOptions struct {
Verbose bool
}
// DiskUsage contains disk usage for the build cache.
type DiskUsage = build.DiskUsage
type CachePruneOptions struct {
All bool
ReservedSpace int64

View File

@@ -4,13 +4,13 @@ import (
"context"
"time"
"github.com/moby/moby/api/types/build"
"github.com/moby/moby/api/types/events"
"github.com/moby/moby/api/types/registry"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/api/types/system"
"github.com/moby/moby/v2/daemon/internal/filters"
"github.com/moby/moby/v2/daemon/server/backend"
"github.com/moby/moby/v2/daemon/server/buildbackend"
)
// Backend is the methods that need to be implemented to provide
@@ -32,7 +32,7 @@ type ClusterBackend interface {
// BuildBackend provides build specific system information.
type BuildBackend interface {
DiskUsage(context.Context) ([]build.CacheRecord, error)
DiskUsage(context.Context, buildbackend.DiskUsageOptions) (*buildbackend.DiskUsage, error)
}
// StatusProvider provides methods to get the swarm status of the current node.

View File

@@ -24,6 +24,7 @@ import (
"github.com/moby/moby/v2/daemon/internal/timestamp"
"github.com/moby/moby/v2/daemon/internal/versions"
"github.com/moby/moby/v2/daemon/server/backend"
"github.com/moby/moby/v2/daemon/server/buildbackend"
"github.com/moby/moby/v2/daemon/server/httputils"
"github.com/moby/moby/v2/daemon/server/router/build"
"github.com/moby/moby/v2/pkg/ioutils"
@@ -197,11 +198,13 @@ func (s *systemRouter) getDiskUsage(ctx context.Context, w http.ResponseWriter,
})
}
var buildCache []buildtypes.CacheRecord
var buildCacheUsage *buildbackend.DiskUsage
if getBuildCache {
eg.Go(func() error {
var err error
buildCache, err = s.builder.DiskUsage(ctx)
buildCacheUsage, err = s.builder.DiskUsage(ctx, buildbackend.DiskUsageOptions{
Verbose: verbose || legacyFields,
})
if err != nil {
return errors.Wrap(err, "error getting build cache usage")
}
@@ -257,35 +260,18 @@ func (s *systemRouter) getDiskUsage(ctx context.Context, w http.ResponseWriter,
v.VolumeUsage.Items = diskUsage.Volumes.Items
}
}
if getBuildCache {
if buildCacheUsage != nil {
v.BuildCacheUsage = &buildtypes.DiskUsage{
TotalCount: int64(len(buildCache)),
ActiveCount: buildCacheUsage.ActiveCount,
Reclaimable: buildCacheUsage.Reclaimable,
TotalCount: buildCacheUsage.TotalCount,
TotalSize: buildCacheUsage.TotalSize,
}
activeCount := v.BuildCacheUsage.TotalCount
var totalSize, reclaimable int64
for _, b := range buildCache {
if versions.LessThan(version, "1.42") {
totalSize += b.Size
}
if !b.InUse {
activeCount--
}
if !b.InUse && !b.Shared {
reclaimable += b.Size
}
}
v.BuildCacheUsage.ActiveCount = activeCount
v.BuildCacheUsage.TotalSize = totalSize
v.BuildCacheUsage.Reclaimable = reclaimable
if legacyFields {
v.BuildCache = nonNilSlice(buildCache) //nolint: staticcheck,SA1019: kept to maintain backwards compatibility with API < v1.52.
v.BuildCache = nonNilSlice(buildCacheUsage.Items) //nolint: staticcheck,SA1019: kept to maintain backwards compatibility with API < v1.52.
} else if verbose {
v.BuildCacheUsage.Items = buildCache
v.BuildCacheUsage.Items = buildCacheUsage.Items
}
}
return httputils.WriteJSON(w, http.StatusOK, v)