From 0dcb1fe34409a903433db5d199cdd6f7716a6201 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 7 Nov 2025 13:01:33 +0100 Subject: [PATCH] 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 --- daemon/internal/builder-next/builder.go | 21 ++++++++--- daemon/server/buildbackend/build.go | 7 ++++ daemon/server/router/system/backend.go | 4 +-- daemon/server/router/system/system_routes.go | 38 +++++++------------- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/daemon/internal/builder-next/builder.go b/daemon/internal/builder-next/builder.go index 550753faee..5a50b33513 100644 --- a/daemon/internal/builder-next/builder.go +++ b/daemon/internal/builder-next/builder.go @@ -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. diff --git a/daemon/server/buildbackend/build.go b/daemon/server/buildbackend/build.go index bc5f9b27ee..a3545c7455 100644 --- a/daemon/server/buildbackend/build.go +++ b/daemon/server/buildbackend/build.go @@ -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 diff --git a/daemon/server/router/system/backend.go b/daemon/server/router/system/backend.go index 3d1c12f1f9..a9e289f01e 100644 --- a/daemon/server/router/system/backend.go +++ b/daemon/server/router/system/backend.go @@ -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. diff --git a/daemon/server/router/system/system_routes.go b/daemon/server/router/system/system_routes.go index 9aad8ca3f6..1a2a3c0a55 100644 --- a/daemon/server/router/system/system_routes.go +++ b/daemon/server/router/system/system_routes.go @@ -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)