From b225258496f3ec8b6906b3fc6a3a2c76e50502d9 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Tue, 15 May 2018 10:02:16 -0700 Subject: [PATCH] builder: export build cache records Signed-off-by: Tonis Tiigi --- api/server/router/system/system.go | 9 +++-- api/server/router/system/system_routes.go | 44 ++++++++++++++++++++--- api/types/types.go | 17 ++++++++- builder/builder-next/builder.go | 25 +++++++++++++ cmd/dockerd/daemon.go | 6 ++-- 5 files changed, 90 insertions(+), 11 deletions(-) diff --git a/api/server/router/system/system.go b/api/server/router/system/system.go index ebb840a899..11370584ef 100644 --- a/api/server/router/system/system.go +++ b/api/server/router/system/system.go @@ -2,6 +2,7 @@ package system // import "github.com/docker/docker/api/server/router/system" import ( "github.com/docker/docker/api/server/router" + buildkit "github.com/docker/docker/builder/builder-next" "github.com/docker/docker/builder/fscache" ) @@ -11,15 +12,17 @@ type systemRouter struct { backend Backend cluster ClusterBackend routes []router.Route - builder *fscache.FSCache + fscache *fscache.FSCache // legacy + builder *buildkit.Builder } // NewRouter initializes a new system router -func NewRouter(b Backend, c ClusterBackend, fscache *fscache.FSCache) router.Router { +func NewRouter(b Backend, c ClusterBackend, fscache *fscache.FSCache, builder *buildkit.Builder) router.Router { r := &systemRouter{ backend: b, cluster: c, - builder: fscache, + fscache: fscache, + builder: builder, } r.routes = []router.Route{ diff --git a/api/server/router/system/system_routes.go b/api/server/router/system/system_routes.go index 573496886c..1bea813bcf 100644 --- a/api/server/router/system/system_routes.go +++ b/api/server/router/system/system_routes.go @@ -17,6 +17,7 @@ import ( "github.com/docker/docker/pkg/ioutils" pkgerrors "github.com/pkg/errors" "github.com/sirupsen/logrus" + "golang.org/x/sync/errgroup" ) func optionsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { @@ -69,15 +70,48 @@ func (s *systemRouter) getVersion(ctx context.Context, w http.ResponseWriter, r } func (s *systemRouter) getDiskUsage(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - du, err := s.backend.SystemDiskUsage(ctx) - if err != nil { + eg, ctx := errgroup.WithContext(ctx) + + var du *types.DiskUsage + eg.Go(func() error { + var err error + du, err = s.backend.SystemDiskUsage(ctx) + if err != nil { + return err + } + return nil + }) + + var builderSize int64 // legacy + eg.Go(func() error { + var err error + builderSize, err = s.fscache.DiskUsage(ctx) + if err != nil { + return pkgerrors.Wrap(err, "error getting fscache build cache usage") + } + return nil + }) + + var buildCache []*types.BuildCache + eg.Go(func() error { + var err error + buildCache, err = s.builder.DiskUsage(ctx) + if err != nil { + return pkgerrors.Wrap(err, "error getting fscache build cache usage") + } + return nil + }) + + if err := eg.Wait(); err != nil { return err } - builderSize, err := s.builder.DiskUsage(ctx) - if err != nil { - return pkgerrors.Wrap(err, "error getting build cache usage") + + for _, b := range buildCache { + builderSize += b.Size } + du.BuilderSize = builderSize + du.BuildCache = buildCache return httputils.WriteJSON(w, http.StatusOK, du) } diff --git a/api/types/types.go b/api/types/types.go index 729f4eb6c4..06c0ca3a69 100644 --- a/api/types/types.go +++ b/api/types/types.go @@ -512,7 +512,8 @@ type DiskUsage struct { Images []*ImageSummary Containers []*Container Volumes []*Volume - BuilderSize int64 + BuildCache []*BuildCache + BuilderSize int64 // deprecated } // ContainersPruneReport contains the response for Engine API: @@ -585,3 +586,17 @@ type PushResult struct { type BuildResult struct { ID string } + +// BuildCache contains information about a build cache record +type BuildCache struct { + ID string + Mutable bool + InUse bool + Size int64 + + CreatedAt time.Time + LastUsedAt *time.Time + UsageCount int + Parent string + Description string +} diff --git a/builder/builder-next/builder.go b/builder/builder-next/builder.go index d67bd41166..397c9ca4a2 100644 --- a/builder/builder-next/builder.go +++ b/builder/builder-next/builder.go @@ -8,6 +8,7 @@ import ( "sync" "github.com/containerd/containerd/content" + "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/backend" "github.com/docker/docker/builder" "github.com/docker/docker/daemon/images" @@ -55,6 +56,30 @@ func (b *Builder) Cancel(ctx context.Context, id string) error { return nil } +func (b *Builder) DiskUsage(ctx context.Context) ([]*types.BuildCache, error) { + duResp, err := b.controller.DiskUsage(ctx, &controlapi.DiskUsageRequest{}) + if err != nil { + return nil, err + } + + var items []*types.BuildCache + for _, r := range duResp.Record { + items = append(items, &types.BuildCache{ + ID: r.ID, + Mutable: r.Mutable, + InUse: r.InUse, + Size: r.Size_, + + CreatedAt: r.CreatedAt, + LastUsedAt: r.LastUsedAt, + UsageCount: int(r.UsageCount), + Parent: r.Parent, + Description: r.Description, + }) + } + return items, nil +} + func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.Result, error) { var out builder.Result diff --git a/cmd/dockerd/daemon.go b/cmd/dockerd/daemon.go index 084cfb9d0f..efefaa1ac3 100644 --- a/cmd/dockerd/daemon.go +++ b/cmd/dockerd/daemon.go @@ -239,7 +239,8 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) { type routerOptions struct { sessionManager *session.Manager buildBackend *buildbackend.Backend - buildCache *fscache.FSCache + buildCache *fscache.FSCache // legacy + buildkit *buildkit.Builder daemon *daemon.Daemon api *apiserver.Server cluster *cluster.Cluster @@ -289,6 +290,7 @@ func newRouterOptions(config *config.Config, daemon *daemon.Daemon) (routerOptio sessionManager: sm, buildBackend: bb, buildCache: buildCache, + buildkit: buildkit, daemon: daemon, }, nil } @@ -462,7 +464,7 @@ func initRouter(opts routerOptions) { checkpointrouter.NewRouter(opts.daemon, decoder), container.NewRouter(opts.daemon, decoder), image.NewRouter(opts.daemon.ImageService()), - systemrouter.NewRouter(opts.daemon, opts.cluster, opts.buildCache), + systemrouter.NewRouter(opts.daemon, opts.cluster, opts.buildCache, opts.buildkit), volume.NewRouter(opts.daemon.VolumesService()), build.NewRouter(opts.buildBackend, opts.daemon), sessionrouter.NewRouter(opts.sessionManager),