2017-06-08 18:17:44 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-06-30 22:54:51 +00:00
|
|
|
"sort"
|
2017-07-25 19:11:52 +00:00
|
|
|
"time"
|
2017-06-08 18:17:44 +00:00
|
|
|
|
2017-06-22 20:15:46 +00:00
|
|
|
controlapi "github.com/moby/buildkit/api/services/control"
|
2017-06-08 18:17:44 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UsageInfo struct {
|
|
|
|
ID string
|
|
|
|
Mutable bool
|
|
|
|
InUse bool
|
|
|
|
Size int64
|
2017-07-25 19:11:52 +00:00
|
|
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
LastUsedAt *time.Time
|
|
|
|
UsageCount int
|
|
|
|
Parent string
|
|
|
|
Description string
|
2018-07-26 19:07:52 +00:00
|
|
|
RecordType UsageRecordType
|
2018-07-27 00:53:48 +00:00
|
|
|
Shared bool
|
2017-06-08 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:14:46 +00:00
|
|
|
func (c *Client) DiskUsage(ctx context.Context, opts ...DiskUsageOption) ([]*UsageInfo, error) {
|
|
|
|
info := &DiskUsageInfo{}
|
|
|
|
for _, o := range opts {
|
2018-07-26 00:20:03 +00:00
|
|
|
o.SetDiskUsageOption(info)
|
2017-07-25 22:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req := &controlapi.DiskUsageRequest{Filter: info.Filter}
|
|
|
|
resp, err := c.controlClient().DiskUsage(ctx, req)
|
2017-06-08 18:17:44 +00:00
|
|
|
if err != nil {
|
2017-12-27 01:22:50 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to call diskusage")
|
2017-06-08 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var du []*UsageInfo
|
|
|
|
|
|
|
|
for _, d := range resp.Record {
|
|
|
|
du = append(du, &UsageInfo{
|
2017-07-25 19:11:52 +00:00
|
|
|
ID: d.ID,
|
|
|
|
Mutable: d.Mutable,
|
|
|
|
InUse: d.InUse,
|
|
|
|
Size: d.Size_,
|
|
|
|
Parent: d.Parent,
|
|
|
|
CreatedAt: d.CreatedAt,
|
|
|
|
Description: d.Description,
|
|
|
|
UsageCount: int(d.UsageCount),
|
|
|
|
LastUsedAt: d.LastUsedAt,
|
2018-07-26 19:07:52 +00:00
|
|
|
RecordType: UsageRecordType(d.RecordType),
|
2018-07-27 00:53:48 +00:00
|
|
|
Shared: d.Shared,
|
2017-06-08 18:17:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-30 22:54:51 +00:00
|
|
|
sort.Slice(du, func(i, j int) bool {
|
2017-07-25 22:14:46 +00:00
|
|
|
if du[i].Size == du[j].Size {
|
|
|
|
return du[i].ID > du[j].ID
|
|
|
|
}
|
2017-06-30 22:54:51 +00:00
|
|
|
return du[i].Size > du[j].Size
|
|
|
|
})
|
|
|
|
|
2017-06-08 18:17:44 +00:00
|
|
|
return du, nil
|
|
|
|
}
|
2017-07-25 22:14:46 +00:00
|
|
|
|
2018-07-26 00:20:03 +00:00
|
|
|
type DiskUsageOption interface {
|
|
|
|
SetDiskUsageOption(*DiskUsageInfo)
|
|
|
|
}
|
2017-07-25 22:14:46 +00:00
|
|
|
|
|
|
|
type DiskUsageInfo struct {
|
2018-07-26 00:01:37 +00:00
|
|
|
Filter []string
|
2017-07-25 22:14:46 +00:00
|
|
|
}
|
2018-07-26 19:07:52 +00:00
|
|
|
|
|
|
|
type UsageRecordType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
UsageRecordTypeInternal UsageRecordType = "internal"
|
|
|
|
UsageRecordTypeFrontend UsageRecordType = "frontend"
|
|
|
|
UsageRecordTypeLocalSource UsageRecordType = "source.local"
|
|
|
|
UsageRecordTypeGitCheckout UsageRecordType = "source.git.checkout"
|
|
|
|
UsageRecordTypeCacheMount UsageRecordType = "exec.cachemount"
|
|
|
|
UsageRecordTypeRegular UsageRecordType = "regular"
|
|
|
|
)
|