From 1a37ee3bf4aaa96fcec14ce20ebd01bba9b61d1a Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Tue, 31 Jul 2018 13:14:53 -0700 Subject: [PATCH] cache: add gc style pruning support Signed-off-by: Tonis Tiigi --- api/services/control/control.pb.go | 230 +++++++++++++++++++---------- api/services/control/control.proto | 4 +- cache/manager.go | 165 +++++++++++++++++++-- client/prune.go | 20 ++- cmd/buildctl/prune.go | 13 +- control/control.go | 7 +- 6 files changed, 337 insertions(+), 102 deletions(-) diff --git a/api/services/control/control.pb.go b/api/services/control/control.pb.go index c5ac3578..48f277e4 100644 --- a/api/services/control/control.pb.go +++ b/api/services/control/control.pb.go @@ -57,8 +57,10 @@ var _ = time.Kitchen const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package type PruneRequest struct { - Filter []string `protobuf:"bytes,1,rep,name=filter" json:"filter,omitempty"` - All bool `protobuf:"varint,2,opt,name=all,proto3" json:"all,omitempty"` + Filter []string `protobuf:"bytes,1,rep,name=filter" json:"filter,omitempty"` + All bool `protobuf:"varint,2,opt,name=all,proto3" json:"all,omitempty"` + KeepDuration int64 `protobuf:"varint,3,opt,name=keepDuration,proto3" json:"keepDuration,omitempty"` + KeepBytes int64 `protobuf:"varint,4,opt,name=keepBytes,proto3" json:"keepBytes,omitempty"` } func (m *PruneRequest) Reset() { *m = PruneRequest{} } @@ -80,6 +82,20 @@ func (m *PruneRequest) GetAll() bool { return false } +func (m *PruneRequest) GetKeepDuration() int64 { + if m != nil { + return m.KeepDuration + } + return 0 +} + +func (m *PruneRequest) GetKeepBytes() int64 { + if m != nil { + return m.KeepBytes + } + return 0 +} + type DiskUsageRequest struct { Filter []string `protobuf:"bytes,1,rep,name=filter" json:"filter,omitempty"` } @@ -955,6 +971,16 @@ func (m *PruneRequest) MarshalTo(dAtA []byte) (int, error) { } i++ } + if m.KeepDuration != 0 { + dAtA[i] = 0x18 + i++ + i = encodeVarintControl(dAtA, i, uint64(m.KeepDuration)) + } + if m.KeepBytes != 0 { + dAtA[i] = 0x20 + i++ + i = encodeVarintControl(dAtA, i, uint64(m.KeepBytes)) + } return i, nil } @@ -1690,6 +1716,12 @@ func (m *PruneRequest) Size() (n int) { if m.All { n += 2 } + if m.KeepDuration != 0 { + n += 1 + sovControl(uint64(m.KeepDuration)) + } + if m.KeepBytes != 0 { + n += 1 + sovControl(uint64(m.KeepBytes)) + } return n } @@ -2089,6 +2121,44 @@ func (m *PruneRequest) Unmarshal(dAtA []byte) error { } } m.All = bool(v != 0) + case 3: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field KeepDuration", wireType) + } + m.KeepDuration = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowControl + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.KeepDuration |= (int64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + case 4: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field KeepBytes", wireType) + } + m.KeepBytes = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowControl + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.KeepBytes |= (int64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipControl(dAtA[iNdEx:]) @@ -4709,81 +4779,83 @@ var ( func init() { proto.RegisterFile("control.proto", fileDescriptorControl) } var fileDescriptorControl = []byte{ - // 1206 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x56, 0x4f, 0x6f, 0x1b, 0x45, - 0x14, 0x67, 0x6d, 0xc7, 0x7f, 0x9e, 0x9d, 0x2a, 0x0c, 0x50, 0xad, 0x16, 0x48, 0xcc, 0x02, 0x92, - 0x55, 0xb5, 0xbb, 0x6d, 0xa0, 0x52, 0x15, 0xa1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xd8, - 0xaa, 0x05, 0xda, 0x42, 0x6b, 0x50, 0x0c, 0xe3, 0xd8, 0x2e, 0x34, 0xad, 0x56, 0x35, 0x10, 0x47, - 0xf7, 0x0e, 0xac, 0x75, 0x22, 0x76, 0x7c, 0xc0, 0xc2, 0xe1, 0x32, 0x6f, 0xf7, 0x39, 0xbc, 0x6b, - 0x60, 0x59, 0x4a, 0x12, 0x86, 0xd1, 0x43, 0x28, 0x53, 0xdc, 0x27, 0x74, 0x20, 0xc1, 0xf5, 0xcd, - 0x8f, 0xbd, 0xf3, 0xc5, 0xf4, 0xb4, 0x83, 0x00, 0x05, 0x1a, 0xec, 0xfe, 0x57, 0x80, 0xba, 0x71, - 0x8f, 0x6e, 0x41, 0x61, 0xa7, 0x63, 0x5b, 0x4d, 0xab, 0x55, 0x0b, 0x0a, 0x3b, 0x1d, 0x64, 0x43, - 0x65, 0x2f, 0xe3, 0x61, 0x2f, 0xc6, 0x3a, 0xda, 0xdc, 0x44, 0xef, 0xc3, 0xca, 0x4e, 0x72, 0xc0, - 0xb0, 0x5d, 0x94, 0xf7, 0xca, 0x40, 0x08, 0x4a, 0xdd, 0xe8, 0x67, 0x6c, 0x97, 0x9a, 0x56, 0xab, - 0x18, 0xc8, 0xb3, 0xc8, 0xe3, 0x45, 0x48, 0x71, 0xc2, 0xed, 0x15, 0xc9, 0xab, 0x2d, 0xd4, 0x86, - 0xda, 0x36, 0xc5, 0x21, 0xc7, 0x83, 0xa7, 0xdc, 0x2e, 0x37, 0xad, 0x56, 0x7d, 0xd3, 0xf1, 0x94, - 0x82, 0x5e, 0xae, 0xa0, 0xb7, 0x9f, 0x2b, 0xd8, 0xae, 0xbe, 0x3e, 0xdd, 0x78, 0xe7, 0x97, 0x37, - 0x1b, 0x56, 0x30, 0x75, 0x43, 0x4f, 0x00, 0x76, 0x43, 0xc6, 0x0f, 0x98, 0x24, 0xa9, 0x2c, 0x25, - 0x29, 0x49, 0x02, 0xc3, 0x07, 0xad, 0x03, 0xc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0x4b, 0xff, 0xde, 0x04, 0x8f, 0x7c, 0x28, 0xc5, 0x64, 0x98, 0xab, - 0xfd, 0xe1, 0x45, 0x7e, 0xbb, 0x64, 0x18, 0x48, 0xa0, 0x7b, 0x5a, 0x80, 0xb2, 0xba, 0x43, 0xcf, - 0xa1, 0x3c, 0x88, 0x86, 0x98, 0x71, 0x95, 0x55, 0x7b, 0x53, 0x7c, 0x20, 0x7f, 0x9d, 0x6e, 0xdc, - 0x31, 0xa6, 0x21, 0x49, 0x71, 0x22, 0x66, 0x77, 0x18, 0x25, 0x98, 0x32, 0x7f, 0x48, 0xee, 0x29, - 0x17, 0xaf, 0x23, 0xff, 0x04, 0x9a, 0x41, 0x70, 0x45, 0x49, 0x9a, 0x71, 0xdd, 0x98, 0x37, 0xe3, - 0x52, 0x0c, 0x62, 0xb4, 0x24, 0xe1, 0x08, 0xeb, 0x77, 0x4d, 0x9e, 0xc5, 0xd3, 0xda, 0x17, 0x7d, - 0x3b, 0x90, 0x03, 0xa7, 0x1a, 0x68, 0x0b, 0x6d, 0x41, 0x85, 0xf1, 0x90, 0x72, 0x3c, 0x90, 0x4f, - 0xd2, 0x55, 0x66, 0x42, 0xee, 0x80, 0x1e, 0x43, 0xad, 0x4f, 0x46, 0x69, 0x8c, 0x85, 0x77, 0xf9, - 0x8a, 0xde, 0x53, 0x17, 0xd1, 0x3d, 0x98, 0x52, 0x42, 0xe5, 0x34, 0xaa, 0x05, 0xca, 0x70, 0xff, - 0x2d, 0x40, 0xc3, 0x14, 0x6b, 0x6e, 0xd2, 0x3e, 0x87, 0xb2, 0x92, 0x5e, 0x75, 0xdd, 0xcd, 0x4a, - 0xa5, 0x18, 0x16, 0x96, 0xca, 0x86, 0x4a, 0x3f, 0xa3, 0x72, 0x0c, 0xab, 0xe1, 0x9c, 0x9b, 0x22, - 0x60, 0x4e, 0x78, 0x18, 0xcb, 0x52, 0x15, 0x03, 0x65, 0x88, 0xe9, 0x3c, 0xd9, 0x9e, 0xae, 0x37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, 0x0b, 0xed, 0x4d, 0x36, 0xa9, 0x45, 0xa1, 0x99, 0x6d, 0xe0, 0x2c, 0xf9, - 0x7f, 0xcb, 0xba, 0x6f, 0xa1, 0x1f, 0xa0, 0x6e, 0x08, 0x8d, 0x16, 0x08, 0x3a, 0xdf, 0x35, 0xce, - 0xe7, 0x4b, 0x50, 0x2a, 0xd8, 0x76, 0xe3, 0xf5, 0xd9, 0xba, 0xf5, 0xe7, 0xd9, 0xba, 0xf5, 0xf7, - 0xd9, 0xba, 0xd5, 0x2b, 0xcb, 0xbe, 0xff, 0xe2, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x10, 0x8f, - 0x03, 0xa4, 0xd1, 0x0d, 0x00, 0x00, + // 1241 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x57, 0xcf, 0x6f, 0x1b, 0xc5, + 0x17, 0xef, 0xda, 0x8e, 0xed, 0x7d, 0x76, 0xaa, 0x7c, 0xe7, 0x0b, 0xd5, 0x6a, 0x81, 0xc4, 0x2c, + 0x20, 0x59, 0x55, 0xbb, 0xdb, 0x06, 0x2a, 0xa1, 0x08, 0x55, 0xad, 0xe3, 0x22, 0x12, 0x25, 0xa2, + 0xac, 0x13, 0x2a, 0x71, 0x5b, 0xdb, 0x13, 0x67, 0xe5, 0xf5, 0xce, 0x32, 0x33, 0x1b, 0x6a, 0xfe, + 0x00, 0xce, 0x1c, 0xf8, 0x4f, 0x38, 0xf0, 0x17, 0x20, 0xe5, 0xc8, 0x99, 0x43, 0x8a, 0x72, 0x87, + 0x3b, 0x37, 0x34, 0x3f, 0xd6, 0x5e, 0xc7, 0x4e, 0x9c, 0xa4, 0xa7, 0xcc, 0x7b, 0xfe, 0xbc, 0xcf, + 0xbe, 0x5f, 0x33, 0xef, 0x05, 0x56, 0x7b, 0x24, 0xe6, 0x94, 0x44, 0x6e, 0x42, 0x09, 0x27, 0x68, +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f, 0x87, 0x65, 0x09, 0x89, 0x19, 0x46, 0x4f, 0xa0, + 0x4c, 0x71, 0x8f, 0xd0, 0xbe, 0x04, 0xd7, 0x36, 0x3f, 0x70, 0x2f, 0xd6, 0xc6, 0xd5, 0x06, 0x02, + 0xe4, 0x6b, 0xb0, 0xf3, 0x6f, 0x01, 0x6a, 0x39, 0x3d, 0xba, 0x0b, 0x85, 0x9d, 0xb6, 0x65, 0x34, + 0x8c, 0xa6, 0xe9, 0x17, 0x76, 0xda, 0xc8, 0x82, 0xca, 0x7e, 0xca, 0x83, 0x6e, 0x84, 0x75, 0xec, + 0x99, 0x88, 0xde, 0x81, 0x95, 0x9d, 0xf8, 0x90, 0x61, 0x19, 0x78, 0xd5, 0x57, 0x02, 0x42, 0x50, + 0xea, 0x84, 0x3f, 0x62, 0x15, 0xa6, 0x2f, 0xcf, 0x22, 0x8e, 0x97, 0x01, 0xc5, 0x31, 0xb7, 0x56, + 0x24, 0xaf, 0x96, 0x50, 0x0b, 0xcc, 0x6d, 0x8a, 0x03, 0x8e, 0xfb, 0xcf, 0xb9, 0x55, 0x6e, 0x18, + 0xcd, 0xda, 0xa6, 0xed, 0xaa, 0x86, 0x70, 0xb3, 0x86, 0x70, 0x0f, 0xb2, 0x86, 0x68, 0x55, 0x4f, + 0xcf, 0x36, 0xee, 0xfc, 0xfc, 0x46, 0xe4, 0x6d, 0x62, 0x86, 0x9e, 0x01, 0xec, 0x05, 0x8c, 0x1f, + 0x32, 0x49, 0x52, 0x59, 0x4a, 0x52, 0x92, 0x04, 0x39, 0x1b, 0xb4, 0x0e, 0x20, 0x13, 0xb0, 0x4d, + 0xd2, 0x98, 0x5b, 0x55, 0xe9, 0x77, 0x4e, 0x83, 0x1a, 0x50, 0x6b, 0x63, 0xd6, 0xa3, 0x61, 0x22, + 0xcb, 0x6c, 0xca, 0x10, 0xf2, 0x2a, 0xc1, 0xa0, 0xb2, 0x77, 0x30, 0x4e, 0xb0, 0x05, 0x12, 0x90, + 0xd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 0xeb, 0xc6, 0x1f, 0xe7, + 0xeb, 0xc6, 0x5f, 0xe7, 0xeb, 0x46, 0xb7, 0x2c, 0xfb, 0xfe, 0xd3, 0xff, 0x02, 0x00, 0x00, 0xff, + 0xff, 0x61, 0x35, 0x4d, 0x35, 0x20, 0x0e, 0x00, 0x00, } diff --git a/api/services/control/control.proto b/api/services/control/control.proto index 2dcd0fcb..63a682d9 100644 --- a/api/services/control/control.proto +++ b/api/services/control/control.proto @@ -25,8 +25,10 @@ service Control { } message PruneRequest { - repeated string filter = 1; // FIXME: not implemented + repeated string filter = 1; bool all = 2; + int64 keepDuration = 3 [(gogoproto.nullable) = true]; + int64 keepBytes = 4 [(gogoproto.nullable) = true]; } message DiskUsageRequest { diff --git a/cache/manager.go b/cache/manager.go index 08d8aeb7..a6a3f86e 100644 --- a/cache/manager.go +++ b/cache/manager.go @@ -2,6 +2,7 @@ package cache import ( "context" + "sort" "sync" "time" @@ -321,14 +322,50 @@ func (cm *cacheManager) Prune(ctx context.Context, ch chan client.UsageInfo, opt check = c } - return cm.prune(ctx, ch, filter, opt.All, check) + totalSize := int64(0) + if opt.KeepBytes != 0 { + du, err := cm.DiskUsage(ctx, client.DiskUsageInfo{}) + if err != nil { + return err + } + for _, ui := range du { + if check != nil { + if check.Exists(ui.ID) { + continue + } + } + totalSize += ui.Size + } + } + + return cm.prune(ctx, ch, pruneOpt{ + filter: filter, + all: opt.All, + checkShared: check, + keepDuration: opt.KeepDuration, + keepBytes: opt.KeepBytes, + totalSize: totalSize, + }) } -func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, filter filters.Filter, all bool, checkShared ExternalRefChecker) error { - var toDelete []*cacheRecord +func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, opt pruneOpt) error { + var toDelete []*deleteRecord + + if opt.keepBytes != 0 && opt.totalSize < opt.keepBytes { + return nil + } + cm.mu.Lock() + gcMode := opt.keepBytes != 0 + cutOff := time.Now().Add(-opt.keepDuration) + + locked := map[*cacheRecord]struct{}{} + for _, cr := range cm.records { + if _, ok := locked[cr]; ok { + continue + } cr.mu.Lock() // ignore duplicates that share data @@ -349,11 +386,11 @@ func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, fil } shared := false - if checkShared != nil { - shared = checkShared.Exists(cr.ID()) + if opt.checkShared != nil { + shared = opt.checkShared.Exists(cr.ID()) } - if !all { + if !opt.all { if recordType == client.UsageRecordTypeInternal || recordType == client.UsageRecordTypeFrontend || shared { cr.mu.Unlock() continue @@ -367,23 +404,59 @@ func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, fil Shared: shared, } - if filter.Match(adaptUsageInfo(c)) { - cr.dead = true + usageCount, lastUsedAt := getLastUsed(cr.md) + c.LastUsedAt = lastUsedAt + c.UsageCount = usageCount - toDelete = append(toDelete, cr) - - // mark metadata as deleted in case we crash before cleanup finished - if err := setDeleted(cr.md); err != nil { + if opt.keepDuration != 0 { + if lastUsedAt != nil && lastUsedAt.After(cutOff) { cr.mu.Unlock() - cm.mu.Unlock() - return err + continue + } + } + + if opt.filter.Match(adaptUsageInfo(c)) { + toDelete = append(toDelete, &deleteRecord{ + cacheRecord: cr, + lastUsedAt: c.LastUsedAt, + usageCount: c.UsageCount, + }) + if !gcMode { + cr.dead = true + + // mark metadata as deleted in case we crash before cleanup finished + if err := setDeleted(cr.md); err != nil { + cr.mu.Unlock() + cm.mu.Unlock() + return err + } + } else { + locked[cr] = struct{}{} + continue // leave the record locked } } } - cr.mu.Unlock() } + if gcMode && len(toDelete) > 0 { + sortDeleteRecords(toDelete) + var err error + for i, cr := range toDelete { + // only remove single record at a time + if i == 0 { + cr.dead = true + err = setDeleted(cr.md) + } + cr.mu.Unlock() + } + if err != nil { + return err + } + toDelete = toDelete[:1] + opt.totalSize -= getSize(toDelete[0].md) + } + cm.mu.Unlock() if len(toDelete) == 0 { @@ -443,7 +516,7 @@ func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, fil case <-ctx.Done(): return ctx.Err() default: - return cm.prune(ctx, ch, filter, all, checkShared) + return cm.prune(ctx, ch, opt) } } @@ -705,3 +778,63 @@ func adaptUsageInfo(info *client.UsageInfo) filters.Adaptor { return "", false }) } + +type pruneOpt struct { + filter filters.Filter + all bool + checkShared ExternalRefChecker + keepDuration time.Duration + keepBytes int64 + totalSize int64 +} + +type deleteRecord struct { + *cacheRecord + lastUsedAt *time.Time + usageCount int + lastUsedAtIndex int + usageCountIndex int +} + +func sortDeleteRecords(toDelete []*deleteRecord) { + sort.Slice(toDelete, func(i, j int) bool { + if toDelete[i].lastUsedAt == nil { + return true + } + if toDelete[j].lastUsedAt == nil { + return false + } + return toDelete[i].lastUsedAt.Before(*toDelete[j].lastUsedAt) + }) + + maxLastUsedIndex := 0 + var val time.Time + for _, v := range toDelete { + if v.lastUsedAt != nil && v.lastUsedAt.After(val) { + val = *v.lastUsedAt + maxLastUsedIndex++ + } + v.lastUsedAtIndex = maxLastUsedIndex + } + + sort.Slice(toDelete, func(i, j int) bool { + return toDelete[i].usageCount < toDelete[j].usageCount + }) + + maxUsageCountIndex := 0 + var count int + for _, v := range toDelete { + if v.usageCount != count { + count = v.usageCount + maxUsageCountIndex++ + } + v.usageCountIndex = maxUsageCountIndex + } + + sort.Slice(toDelete, func(i, j int) bool { + return float64(toDelete[i].lastUsedAtIndex)/float64(maxLastUsedIndex)+ + float64(toDelete[i].usageCountIndex)/float64(maxUsageCountIndex) < + float64(toDelete[j].lastUsedAtIndex)/float64(maxLastUsedIndex)+ + float64(toDelete[j].usageCountIndex)/float64(maxUsageCountIndex) + }) +} diff --git a/client/prune.go b/client/prune.go index 96cf6fff..27fe5dd8 100644 --- a/client/prune.go +++ b/client/prune.go @@ -3,6 +3,7 @@ package client import ( "context" "io" + "time" controlapi "github.com/moby/buildkit/api/services/control" "github.com/pkg/errors" @@ -14,7 +15,11 @@ func (c *Client) Prune(ctx context.Context, ch chan UsageInfo, opts ...PruneOpti o.SetPruneOption(info) } - req := &controlapi.PruneRequest{Filter: info.Filter} + req := &controlapi.PruneRequest{ + Filter: info.Filter, + KeepDuration: int64(info.KeepDuration), + KeepBytes: int64(info.KeepBytes), + } if info.All { req.All = true } @@ -54,8 +59,10 @@ type PruneOption interface { } type PruneInfo struct { - Filter []string - All bool + Filter []string + All bool + KeepDuration time.Duration + KeepBytes int64 } type pruneOptionFunc func(*PruneInfo) @@ -67,3 +74,10 @@ func (f pruneOptionFunc) SetPruneOption(pi *PruneInfo) { var PruneAll = pruneOptionFunc(func(pi *PruneInfo) { pi.All = true }) + +func WithKeepOpt(duration time.Duration, bytes int64) PruneOption { + return pruneOptionFunc(func(pi *PruneInfo) { + pi.KeepDuration = duration + pi.KeepBytes = bytes + }) +} diff --git a/cmd/buildctl/prune.go b/cmd/buildctl/prune.go index 70877676..4c43cc41 100644 --- a/cmd/buildctl/prune.go +++ b/cmd/buildctl/prune.go @@ -15,6 +15,14 @@ var pruneCommand = cli.Command{ Usage: "clean up build cache", Action: prune, Flags: []cli.Flag{ + cli.DurationFlag{ + Name: "keep-duration", + Usage: "Keep data newer than this limit", + }, + cli.Float64Flag{ + Name: "keep-storage", + Usage: "Keep data below this limit (in MB)", + }, cli.StringSliceFlag{ Name: "filter, f", Usage: "Filter records", @@ -60,7 +68,10 @@ func prune(clicontext *cli.Context) error { } }() - opts := []client.PruneOption{client.WithFilter(clicontext.StringSlice("filter"))} + opts := []client.PruneOption{ + client.WithFilter(clicontext.StringSlice("filter")), + client.WithKeepOpt(clicontext.Duration("keep-duration"), int64(clicontext.Float64("keep-storage")*1e6)), + } if clicontext.Bool("all") { opts = append(opts, client.PruneAll) diff --git a/control/control.go b/control/control.go index 1dca59e3..25f076a9 100644 --- a/control/control.go +++ b/control/control.go @@ -2,6 +2,7 @@ package control import ( "context" + "time" "github.com/docker/distribution/reference" controlapi "github.com/moby/buildkit/api/services/control" @@ -120,8 +121,10 @@ func (c *Controller) Prune(req *controlapi.PruneRequest, stream controlapi.Contr func(w worker.Worker) { eg.Go(func() error { return w.Prune(ctx, ch, client.PruneInfo{ - Filter: req.Filter, - All: req.All, + Filter: req.Filter, + All: req.All, + KeepDuration: time.Duration(req.KeepDuration), + KeepBytes: req.KeepBytes, }) }) }(w)