buildctl: add filter to buildctl df
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>docker-18.09
parent
38ac00090c
commit
e3c91ee898
|
@ -2,6 +2,7 @@ package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -34,7 +35,7 @@ type Accessor interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Controller interface {
|
type Controller interface {
|
||||||
DiskUsage(ctx context.Context) ([]*client.UsageInfo, error)
|
DiskUsage(ctx context.Context, info client.DiskUsageInfo) ([]*client.UsageInfo, error)
|
||||||
Prune(ctx context.Context) (map[string]int64, error)
|
Prune(ctx context.Context) (map[string]int64, error)
|
||||||
GC(ctx context.Context) error
|
GC(ctx context.Context) error
|
||||||
}
|
}
|
||||||
|
@ -251,7 +252,7 @@ func (cm *cacheManager) GetMutable(ctx context.Context, id string) (MutableRef,
|
||||||
return rec.mref(), nil
|
return rec.mref(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *cacheManager) DiskUsage(ctx context.Context) ([]*client.UsageInfo, error) {
|
func (cm *cacheManager) DiskUsage(ctx context.Context, opt client.DiskUsageInfo) ([]*client.UsageInfo, error) {
|
||||||
cm.mu.Lock()
|
cm.mu.Lock()
|
||||||
|
|
||||||
type cacheUsageInfo struct {
|
type cacheUsageInfo struct {
|
||||||
|
@ -275,6 +276,7 @@ func (cm *cacheManager) DiskUsage(ctx context.Context) ([]*client.UsageInfo, err
|
||||||
cr.mu.Unlock()
|
cr.mu.Unlock()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
usageCount, lastUsedAt := getLastUsed(cr.md)
|
usageCount, lastUsedAt := getLastUsed(cr.md)
|
||||||
c := &cacheUsageInfo{
|
c := &cacheUsageInfo{
|
||||||
refs: len(cr.refs),
|
refs: len(cr.refs),
|
||||||
|
@ -313,6 +315,10 @@ func (cm *cacheManager) DiskUsage(ctx context.Context) ([]*client.UsageInfo, err
|
||||||
|
|
||||||
var du []*client.UsageInfo
|
var du []*client.UsageInfo
|
||||||
for id, cr := range m {
|
for id, cr := range m {
|
||||||
|
if opt.Filter != "" && !strings.HasPrefix(id, opt.Filter) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
c := &client.UsageInfo{
|
c := &client.UsageInfo{
|
||||||
ID: id,
|
ID: id,
|
||||||
Mutable: cr.mutable,
|
Mutable: cr.mutable,
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/containerd/containerd/namespaces"
|
"github.com/containerd/containerd/namespaces"
|
||||||
"github.com/containerd/containerd/snapshot/naive"
|
"github.com/containerd/containerd/snapshot/naive"
|
||||||
"github.com/moby/buildkit/cache/metadata"
|
"github.com/moby/buildkit/cache/metadata"
|
||||||
|
"github.com/moby/buildkit/client"
|
||||||
"github.com/moby/buildkit/snapshot"
|
"github.com/moby/buildkit/snapshot"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -265,7 +266,7 @@ func getCacheManager(t *testing.T, tmpdir string) Manager {
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkDiskUsage(t *testing.T, ctx context.Context, cm Manager, inuse, unused int) {
|
func checkDiskUsage(t *testing.T, ctx context.Context, cm Manager, inuse, unused int) {
|
||||||
du, err := cm.DiskUsage(ctx)
|
du, err := cm.DiskUsage(ctx, client.DiskUsageInfo{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
var inuseActual, unusedActual int
|
var inuseActual, unusedActual int
|
||||||
for _, r := range du {
|
for _, r := range du {
|
||||||
|
|
|
@ -22,8 +22,14 @@ type UsageInfo struct {
|
||||||
Description string
|
Description string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DiskUsage(ctx context.Context) ([]*UsageInfo, error) {
|
func (c *Client) DiskUsage(ctx context.Context, opts ...DiskUsageOption) ([]*UsageInfo, error) {
|
||||||
resp, err := c.controlClient().DiskUsage(ctx, &controlapi.DiskUsageRequest{})
|
info := &DiskUsageInfo{}
|
||||||
|
for _, o := range opts {
|
||||||
|
o(info)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &controlapi.DiskUsageRequest{Filter: info.Filter}
|
||||||
|
resp, err := c.controlClient().DiskUsage(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to get diskusage")
|
return nil, errors.Wrap(err, "failed to get diskusage")
|
||||||
}
|
}
|
||||||
|
@ -45,8 +51,23 @@ func (c *Client) DiskUsage(ctx context.Context) ([]*UsageInfo, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(du, func(i, j int) bool {
|
sort.Slice(du, func(i, j int) bool {
|
||||||
|
if du[i].Size == du[j].Size {
|
||||||
|
return du[i].ID > du[j].ID
|
||||||
|
}
|
||||||
return du[i].Size > du[j].Size
|
return du[i].Size > du[j].Size
|
||||||
})
|
})
|
||||||
|
|
||||||
return du, nil
|
return du, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DiskUsageOption func(*DiskUsageInfo)
|
||||||
|
|
||||||
|
type DiskUsageInfo struct {
|
||||||
|
Filter string
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithFilter(f string) DiskUsageOption {
|
||||||
|
return func(di *DiskUsageInfo) {
|
||||||
|
di.Filter = f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -9,6 +8,7 @@ import (
|
||||||
|
|
||||||
units "github.com/docker/go-units"
|
units "github.com/docker/go-units"
|
||||||
"github.com/moby/buildkit/client"
|
"github.com/moby/buildkit/client"
|
||||||
|
"github.com/moby/buildkit/util/appcontext"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,6 +17,10 @@ var diskUsageCommand = cli.Command{
|
||||||
Usage: "disk usage",
|
Usage: "disk usage",
|
||||||
Action: diskUsage,
|
Action: diskUsage,
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "filter, f",
|
||||||
|
Usage: "Filter snapshot ID",
|
||||||
|
},
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "verbose, v",
|
Name: "verbose, v",
|
||||||
Usage: "Verbose output",
|
Usage: "Verbose output",
|
||||||
|
@ -25,12 +29,12 @@ var diskUsageCommand = cli.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
func diskUsage(clicontext *cli.Context) error {
|
func diskUsage(clicontext *cli.Context) error {
|
||||||
client, err := resolveClient(clicontext)
|
c, err := resolveClient(clicontext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
du, err := client.DiskUsage(context.TODO())
|
du, err := c.DiskUsage(appcontext.Context(), client.WithFilter(clicontext.String("filter")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -43,7 +47,9 @@ func diskUsage(clicontext *cli.Context) error {
|
||||||
printTable(tw, du)
|
printTable(tw, du)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if clicontext.String("filter") == "" {
|
||||||
printSummary(tw, du)
|
printSummary(tw, du)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,8 +51,10 @@ func (c *Controller) Register(server *grpc.Server) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) DiskUsage(ctx context.Context, _ *controlapi.DiskUsageRequest) (*controlapi.DiskUsageResponse, error) {
|
func (c *Controller) DiskUsage(ctx context.Context, r *controlapi.DiskUsageRequest) (*controlapi.DiskUsageResponse, error) {
|
||||||
du, err := c.opt.CacheManager.DiskUsage(ctx)
|
du, err := c.opt.CacheManager.DiskUsage(ctx, client.DiskUsageInfo{
|
||||||
|
Filter: r.Filter,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/containerd/containerd/namespaces"
|
"github.com/containerd/containerd/namespaces"
|
||||||
"github.com/moby/buildkit/cache"
|
"github.com/moby/buildkit/cache"
|
||||||
"github.com/moby/buildkit/cache/metadata"
|
"github.com/moby/buildkit/cache/metadata"
|
||||||
|
"github.com/moby/buildkit/client"
|
||||||
"github.com/moby/buildkit/snapshot"
|
"github.com/moby/buildkit/snapshot"
|
||||||
"github.com/moby/buildkit/snapshot/blobmapping"
|
"github.com/moby/buildkit/snapshot/blobmapping"
|
||||||
"github.com/moby/buildkit/source"
|
"github.com/moby/buildkit/source"
|
||||||
|
@ -93,7 +94,7 @@ func TestControl(t *testing.T) {
|
||||||
lm.Unmount()
|
lm.Unmount()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
du, err := cm.DiskUsage(ctx)
|
du, err := cm.DiskUsage(ctx, client.DiskUsageInfo{})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// for _, d := range du {
|
// for _, d := range du {
|
||||||
|
@ -148,7 +149,7 @@ func TestControl(t *testing.T) {
|
||||||
err = snap.Release(ctx)
|
err = snap.Release(ctx)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
du2, err := cm.DiskUsage(ctx)
|
du2, err := cm.DiskUsage(ctx, client.DiskUsageInfo{})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 1, len(du2)-len(du))
|
assert.Equal(t, 1, len(du2)-len(du))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue