1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

builder: export build cache records

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi 2018-05-15 10:02:16 -07:00
parent bb68c8132b
commit b225258496
5 changed files with 90 additions and 11 deletions

View file

@ -2,6 +2,7 @@ package system // import "github.com/docker/docker/api/server/router/system"
import ( import (
"github.com/docker/docker/api/server/router" "github.com/docker/docker/api/server/router"
buildkit "github.com/docker/docker/builder/builder-next"
"github.com/docker/docker/builder/fscache" "github.com/docker/docker/builder/fscache"
) )
@ -11,15 +12,17 @@ type systemRouter struct {
backend Backend backend Backend
cluster ClusterBackend cluster ClusterBackend
routes []router.Route routes []router.Route
builder *fscache.FSCache fscache *fscache.FSCache // legacy
builder *buildkit.Builder
} }
// NewRouter initializes a new system router // 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{ r := &systemRouter{
backend: b, backend: b,
cluster: c, cluster: c,
builder: fscache, fscache: fscache,
builder: builder,
} }
r.routes = []router.Route{ r.routes = []router.Route{

View file

@ -17,6 +17,7 @@ import (
"github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/ioutils"
pkgerrors "github.com/pkg/errors" pkgerrors "github.com/pkg/errors"
"github.com/sirupsen/logrus" "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 { 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 { func (s *systemRouter) getDiskUsage(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
du, err := s.backend.SystemDiskUsage(ctx) 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 { if err != nil {
return err return err
} }
builderSize, err := s.builder.DiskUsage(ctx) return nil
})
var builderSize int64 // legacy
eg.Go(func() error {
var err error
builderSize, err = s.fscache.DiskUsage(ctx)
if err != nil { if err != nil {
return pkgerrors.Wrap(err, "error getting build cache usage") 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
}
for _, b := range buildCache {
builderSize += b.Size
}
du.BuilderSize = builderSize du.BuilderSize = builderSize
du.BuildCache = buildCache
return httputils.WriteJSON(w, http.StatusOK, du) return httputils.WriteJSON(w, http.StatusOK, du)
} }

View file

@ -512,7 +512,8 @@ type DiskUsage struct {
Images []*ImageSummary Images []*ImageSummary
Containers []*Container Containers []*Container
Volumes []*Volume Volumes []*Volume
BuilderSize int64 BuildCache []*BuildCache
BuilderSize int64 // deprecated
} }
// ContainersPruneReport contains the response for Engine API: // ContainersPruneReport contains the response for Engine API:
@ -585,3 +586,17 @@ type PushResult struct {
type BuildResult struct { type BuildResult struct {
ID string 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
}

View file

@ -8,6 +8,7 @@ import (
"sync" "sync"
"github.com/containerd/containerd/content" "github.com/containerd/containerd/content"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/backend" "github.com/docker/docker/api/types/backend"
"github.com/docker/docker/builder" "github.com/docker/docker/builder"
"github.com/docker/docker/daemon/images" "github.com/docker/docker/daemon/images"
@ -55,6 +56,30 @@ func (b *Builder) Cancel(ctx context.Context, id string) error {
return nil 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) { func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.Result, error) {
var out builder.Result var out builder.Result

View file

@ -239,7 +239,8 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
type routerOptions struct { type routerOptions struct {
sessionManager *session.Manager sessionManager *session.Manager
buildBackend *buildbackend.Backend buildBackend *buildbackend.Backend
buildCache *fscache.FSCache buildCache *fscache.FSCache // legacy
buildkit *buildkit.Builder
daemon *daemon.Daemon daemon *daemon.Daemon
api *apiserver.Server api *apiserver.Server
cluster *cluster.Cluster cluster *cluster.Cluster
@ -289,6 +290,7 @@ func newRouterOptions(config *config.Config, daemon *daemon.Daemon) (routerOptio
sessionManager: sm, sessionManager: sm,
buildBackend: bb, buildBackend: bb,
buildCache: buildCache, buildCache: buildCache,
buildkit: buildkit,
daemon: daemon, daemon: daemon,
}, nil }, nil
} }
@ -462,7 +464,7 @@ func initRouter(opts routerOptions) {
checkpointrouter.NewRouter(opts.daemon, decoder), checkpointrouter.NewRouter(opts.daemon, decoder),
container.NewRouter(opts.daemon, decoder), container.NewRouter(opts.daemon, decoder),
image.NewRouter(opts.daemon.ImageService()), 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()), volume.NewRouter(opts.daemon.VolumesService()),
build.NewRouter(opts.buildBackend, opts.daemon), build.NewRouter(opts.buildBackend, opts.daemon),
sessionrouter.NewRouter(opts.sessionManager), sessionrouter.NewRouter(opts.sessionManager),