Skip to content

Commit 13cf07c

Browse files
authored
Merge pull request #5979 from crazy-max/v0.22-picks-0.22.0-rc2
[v0.22] cherry-picks for v0.22.0-rc2
2 parents 141a4a6 + a3712e2 commit 13cf07c

File tree

15 files changed

+918
-484
lines changed

15 files changed

+918
-484
lines changed

api/services/control/control.pb.go

Lines changed: 442 additions & 432 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/services/control/control.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ message PruneRequest {
3636

3737
message DiskUsageRequest {
3838
repeated string filter = 1;
39+
int64 ageLimit = 2;
3940
}
4041

4142
message DiskUsageResponse {

api/services/control/control_vtproto.pb.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cache/manager.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,7 @@ func (cm *cacheManager) DiskUsage(ctx context.Context, opt client.DiskUsageInfo)
14521452
if err := cm.markShared(m); err != nil {
14531453
return nil, err
14541454
}
1455+
cutOff := time.Now().Add(-opt.AgeLimit)
14551456

14561457
var du []*client.UsageInfo
14571458
for id, cr := range m {
@@ -1468,9 +1469,15 @@ func (cm *cacheManager) DiskUsage(ctx context.Context, opt client.DiskUsageInfo)
14681469
RecordType: cr.recordType,
14691470
Shared: cr.shared,
14701471
}
1471-
if filter.Match(adaptUsageInfo(c)) {
1472-
du = append(du, c)
1472+
if !filter.Match(adaptUsageInfo(c)) {
1473+
continue
1474+
}
1475+
if opt.AgeLimit > 0 {
1476+
if c.LastUsedAt != nil && c.LastUsedAt.After(cutOff) {
1477+
continue
1478+
}
14731479
}
1480+
du = append(du, c)
14741481
}
14751482

14761483
eg, ctx := errgroup.WithContext(ctx)

client/diskusage.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (c *Client) DiskUsage(ctx context.Context, opts ...DiskUsageOption) ([]*Usa
3131
o.SetDiskUsageOption(info)
3232
}
3333

34-
req := &controlapi.DiskUsageRequest{Filter: info.Filter}
34+
req := &controlapi.DiskUsageRequest{Filter: info.Filter, AgeLimit: int64(info.AgeLimit)}
3535
resp, err := c.ControlClient().DiskUsage(ctx, req)
3636
if err != nil {
3737
return nil, errors.Wrap(err, "failed to call diskusage")
@@ -72,7 +72,8 @@ type DiskUsageOption interface {
7272
}
7373

7474
type DiskUsageInfo struct {
75-
Filter []string
75+
Filter []string
76+
AgeLimit time.Duration
7677
}
7778

7879
type UsageRecordType string
@@ -85,3 +86,15 @@ const (
8586
UsageRecordTypeCacheMount UsageRecordType = "exec.cachemount"
8687
UsageRecordTypeRegular UsageRecordType = "regular"
8788
)
89+
90+
type diskUsageOptionFunc func(*DiskUsageInfo)
91+
92+
func (f diskUsageOptionFunc) SetDiskUsageOption(info *DiskUsageInfo) {
93+
f(info)
94+
}
95+
96+
func WithAgeLimit(age time.Duration) DiskUsageOption {
97+
return diskUsageOptionFunc(func(info *DiskUsageInfo) {
98+
info.AgeLimit = age
99+
})
100+
}

client/llb/source.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,12 @@ func Git(url, ref string, opts ...GitOption) State {
322322
addCap(&gi.Constraints, pb.CapSourceGitMountSSHSock)
323323
}
324324

325+
checksum := gi.Checksum
326+
if checksum != "" {
327+
attrs[pb.AttrGitChecksum] = checksum
328+
addCap(&gi.Constraints, pb.CapSourceGitChecksum)
329+
}
330+
325331
addCap(&gi.Constraints, pb.CapSourceGit)
326332

327333
source := NewSource("git://"+id, attrs, gi.Constraints)
@@ -345,6 +351,7 @@ type GitInfo struct {
345351
addAuthCap bool
346352
KnownSSHHosts string
347353
MountSSHSock string
354+
Checksum string
348355
}
349356

350357
func KeepGitDir() GitOption {
@@ -373,6 +380,12 @@ func MountSSHSock(sshID string) GitOption {
373380
})
374381
}
375382

383+
func GitChecksum(v string) GitOption {
384+
return gitOptionFunc(func(gi *GitInfo) {
385+
gi.Checksum = v
386+
})
387+
}
388+
376389
// AuthOption can be used with either HTTP or Git sources.
377390
type AuthOption interface {
378391
GitOption

control/control.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ func (c *Controller) DiskUsage(ctx context.Context, r *controlapi.DiskUsageReque
167167
}
168168
for _, w := range workers {
169169
du, err := w.DiskUsage(ctx, client.DiskUsageInfo{
170-
Filter: r.Filter,
170+
Filter: r.Filter,
171+
AgeLimit: time.Duration(r.AgeLimit),
171172
})
172173
if err != nil {
173174
return nil, err

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -937,27 +937,21 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
937937
case *instructions.WorkdirCommand:
938938
err = dispatchWorkdir(d, c, true, &opt)
939939
case *instructions.AddCommand:
940-
var checksum digest.Digest
941-
if c.Checksum != "" {
942-
checksum, err = digest.Parse(c.Checksum)
943-
}
944-
if err == nil {
945-
err = dispatchCopy(d, copyConfig{
946-
params: c.SourcesAndDest,
947-
excludePatterns: c.ExcludePatterns,
948-
source: opt.buildContext,
949-
isAddCommand: true,
950-
cmdToPrint: c,
951-
chown: c.Chown,
952-
chmod: c.Chmod,
953-
link: c.Link,
954-
keepGitDir: c.KeepGitDir,
955-
checksum: checksum,
956-
location: c.Location(),
957-
ignoreMatcher: opt.dockerIgnoreMatcher,
958-
opt: opt,
959-
})
960-
}
940+
err = dispatchCopy(d, copyConfig{
941+
params: c.SourcesAndDest,
942+
excludePatterns: c.ExcludePatterns,
943+
source: opt.buildContext,
944+
isAddCommand: true,
945+
cmdToPrint: c,
946+
chown: c.Chown,
947+
chmod: c.Chmod,
948+
link: c.Link,
949+
keepGitDir: c.KeepGitDir,
950+
checksum: c.Checksum,
951+
location: c.Location(),
952+
ignoreMatcher: opt.dockerIgnoreMatcher,
953+
opt: opt,
954+
})
961955
if err == nil {
962956
for _, src := range c.SourcePaths {
963957
if !strings.HasPrefix(src, "http://") && !strings.HasPrefix(src, "https://") {
@@ -1470,8 +1464,8 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
14701464
if len(cfg.params.SourcePaths) != 1 {
14711465
return errors.New("checksum can't be specified for multiple sources")
14721466
}
1473-
if !isHTTPSource(cfg.params.SourcePaths[0]) {
1474-
return errors.New("checksum can't be specified for non-HTTP(S) sources")
1467+
if !isHTTPSource(cfg.params.SourcePaths[0]) && !isGitSource(cfg.params.SourcePaths[0]) {
1468+
return errors.New("checksum requires HTTP(S) or Git sources")
14751469
}
14761470
}
14771471

@@ -1519,6 +1513,9 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
15191513
if cfg.keepGitDir {
15201514
gitOptions = append(gitOptions, llb.KeepGitDir())
15211515
}
1516+
if cfg.checksum != "" {
1517+
gitOptions = append(gitOptions, llb.GitChecksum(cfg.checksum))
1518+
}
15221519
st := llb.Git(gitRef.Remote, commit, gitOptions...)
15231520
opts := append([]llb.CopyOption{&llb.CopyInfo{
15241521
Mode: chopt,
@@ -1547,7 +1544,15 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
15471544
}
15481545
}
15491546

1550-
st := llb.HTTP(src, llb.Filename(f), llb.WithCustomName(pgName), llb.Checksum(cfg.checksum), dfCmd(cfg.params))
1547+
var checksum digest.Digest
1548+
if cfg.checksum != "" {
1549+
checksum, err = digest.Parse(cfg.checksum)
1550+
if err != nil {
1551+
return err
1552+
}
1553+
}
1554+
1555+
st := llb.HTTP(src, llb.Filename(f), llb.WithCustomName(pgName), llb.Checksum(checksum), dfCmd(cfg.params))
15511556

15521557
opts := append([]llb.CopyOption{&llb.CopyInfo{
15531558
Mode: chopt,
@@ -1674,7 +1679,7 @@ type copyConfig struct {
16741679
chmod string
16751680
link bool
16761681
keepGitDir bool
1677-
checksum digest.Digest
1682+
checksum string
16781683
parents bool
16791684
location []parser.Range
16801685
ignoreMatcher *patternmatcher.PatternMatcher
@@ -2265,11 +2270,15 @@ func isHTTPSource(src string) bool {
22652270
if !strings.HasPrefix(src, "http://") && !strings.HasPrefix(src, "https://") {
22662271
return false
22672272
}
2273+
return !isGitSource(src)
2274+
}
2275+
2276+
func isGitSource(src string) bool {
22682277
// https://github.com/ORG/REPO.git is a git source, not an http source
22692278
if gitRef, gitErr := gitutil.ParseGitRef(src); gitRef != nil && gitErr == nil {
2270-
return false
2279+
return true
22712280
}
2272-
return true
2281+
return false
22732282
}
22742283

22752284
func isEnabledForStage(stage string, value string) bool {

frontend/dockerfile/dockerfile_addchecksum_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,6 @@ ADD --checksum=%s foo /tmp/foo
162162
dockerui.DefaultLocalNameContext: dir,
163163
},
164164
}, nil)
165-
require.Error(t, err, "checksum can't be specified for non-HTTP(S) sources")
165+
require.Error(t, err, "checksum requires HTTP(S) or Git sources")
166166
})
167167
}

0 commit comments

Comments
 (0)