Skip to content

Commit eb78253

Browse files
authored
Merge pull request #3055 from tonistiigi/history-queryrecord
history: generalize query loading
2 parents b88423b + c1e2ae5 commit eb78253

File tree

7 files changed

+56
-97
lines changed

7 files changed

+56
-97
lines changed

commands/history/inspect.go

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,7 @@ func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions)
173173
}
174174
}
175175

176-
var offset *int
177-
if strings.HasPrefix(opts.ref, "^") {
178-
off, err := strconv.Atoi(opts.ref[1:])
179-
if err != nil {
180-
return errors.Wrapf(err, "invalid offset %q", opts.ref)
181-
}
182-
offset = &off
183-
opts.ref = ""
184-
}
185-
186-
recs, err := queryRecords(ctx, opts.ref, nodes)
176+
recs, err := queryRecords(ctx, opts.ref, nodes, nil)
187177
if err != nil {
188178
return err
189179
}
@@ -195,26 +185,7 @@ func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions)
195185
return errors.Errorf("no record found for ref %q", opts.ref)
196186
}
197187

198-
var rec *historyRecord
199-
if opts.ref == "" {
200-
slices.SortFunc(recs, func(a, b historyRecord) int {
201-
return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime())
202-
})
203-
for _, r := range recs {
204-
if offset != nil {
205-
if *offset > 0 {
206-
*offset--
207-
continue
208-
}
209-
}
210-
rec = &r
211-
break
212-
}
213-
if offset != nil && *offset > 0 {
214-
return errors.Errorf("no completed build found with offset %d", *offset)
215-
}
216-
}
217-
188+
rec := &recs[0]
218189
c, err := rec.node.Driver.Client(ctx)
219190
if err != nil {
220191
return err

commands/history/inspect_attachment.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package history
33
import (
44
"context"
55
"io"
6-
"slices"
76

87
"github.com/containerd/containerd/v2/core/content/proxy"
98
"github.com/containerd/platforms"
@@ -42,7 +41,7 @@ func runAttachment(ctx context.Context, dockerCli command.Cli, opts attachmentOp
4241
}
4342
}
4443

45-
recs, err := queryRecords(ctx, opts.ref, nodes)
44+
recs, err := queryRecords(ctx, opts.ref, nodes, nil)
4645
if err != nil {
4746
return err
4847
}
@@ -54,12 +53,6 @@ func runAttachment(ctx context.Context, dockerCli command.Cli, opts attachmentOp
5453
return errors.Errorf("no record found for ref %q", opts.ref)
5554
}
5655

57-
if opts.ref == "" {
58-
slices.SortFunc(recs, func(a, b historyRecord) int {
59-
return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime())
60-
})
61-
}
62-
6356
rec := &recs[0]
6457

6558
c, err := rec.node.Driver.Client(ctx)

commands/history/logs.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"io"
66
"os"
7-
"slices"
87

98
"github.com/docker/buildx/builder"
109
"github.com/docker/buildx/util/cobrautil/completion"
@@ -39,7 +38,7 @@ func runLogs(ctx context.Context, dockerCli command.Cli, opts logsOptions) error
3938
}
4039
}
4140

42-
recs, err := queryRecords(ctx, opts.ref, nodes)
41+
recs, err := queryRecords(ctx, opts.ref, nodes, nil)
4342
if err != nil {
4443
return err
4544
}
@@ -51,12 +50,6 @@ func runLogs(ctx context.Context, dockerCli command.Cli, opts logsOptions) error
5150
return errors.Errorf("no record found for ref %q", opts.ref)
5251
}
5352

54-
if opts.ref == "" {
55-
slices.SortFunc(recs, func(a, b historyRecord) int {
56-
return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime())
57-
})
58-
}
59-
6053
rec := &recs[0]
6154
c, err := rec.node.Driver.Client(ctx)
6255
if err != nil {

commands/history/ls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func runLs(ctx context.Context, dockerCli command.Cli, opts lsOptions) error {
5656
}
5757
}
5858

59-
out, err := queryRecords(ctx, "", nodes)
59+
out, err := queryRecords(ctx, "", nodes, nil)
6060
if err != nil {
6161
return err
6262
}

commands/history/open.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package history
33
import (
44
"context"
55
"fmt"
6-
"slices"
76

87
"github.com/docker/buildx/builder"
98
"github.com/docker/buildx/util/cobrautil/completion"
@@ -35,7 +34,7 @@ func runOpen(ctx context.Context, dockerCli command.Cli, opts openOptions) error
3534
}
3635
}
3736

38-
recs, err := queryRecords(ctx, opts.ref, nodes)
37+
recs, err := queryRecords(ctx, opts.ref, nodes, nil)
3938
if err != nil {
4039
return err
4140
}
@@ -47,12 +46,6 @@ func runOpen(ctx context.Context, dockerCli command.Cli, opts openOptions) error
4746
return errors.Errorf("no record found for ref %q", opts.ref)
4847
}
4948

50-
if opts.ref == "" {
51-
slices.SortFunc(recs, func(a, b historyRecord) int {
52-
return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime())
53-
})
54-
}
55-
5649
rec := &recs[0]
5750

5851
url := desktop.BuildURL(fmt.Sprintf("%s/%s/%s", rec.node.Builder, rec.node.Name, rec.Ref))

commands/history/trace.go

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import (
88
"io"
99
"net"
1010
"os"
11-
"slices"
12-
"strconv"
13-
"strings"
1411
"time"
1512

1613
"github.com/containerd/console"
@@ -37,51 +34,20 @@ type traceOptions struct {
3734
}
3835

3936
func loadTrace(ctx context.Context, ref string, nodes []builder.Node) (string, []byte, error) {
40-
var offset *int
41-
if strings.HasPrefix(ref, "^") {
42-
off, err := strconv.Atoi(ref[1:])
43-
if err != nil {
44-
return "", nil, errors.Wrapf(err, "invalid offset %q", ref)
45-
}
46-
offset = &off
47-
ref = ""
48-
}
49-
50-
recs, err := queryRecords(ctx, ref, nodes)
37+
recs, err := queryRecords(ctx, ref, nodes, &queryOptions{
38+
CompletedOnly: true,
39+
})
5140
if err != nil {
5241
return "", nil, err
5342
}
5443

55-
var rec *historyRecord
56-
57-
if ref == "" {
58-
slices.SortFunc(recs, func(a, b historyRecord) int {
59-
return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime())
60-
})
61-
for _, r := range recs {
62-
if r.CompletedAt != nil {
63-
if offset != nil {
64-
if *offset > 0 {
65-
*offset--
66-
continue
67-
}
68-
}
69-
rec = &r
70-
break
71-
}
72-
}
73-
if offset != nil && *offset > 0 {
74-
return "", nil, errors.Errorf("no completed build found with offset %d", *offset)
75-
}
76-
} else {
77-
rec = &recs[0]
78-
}
79-
if rec == nil {
44+
if len(recs) == 0 {
8045
if ref == "" {
8146
return "", nil, errors.New("no records found")
8247
}
8348
return "", nil, errors.Errorf("no record found for ref %q", ref)
8449
}
50+
rec := &recs[0]
8551

8652
if rec.CompletedAt == nil {
8753
return "", nil, errors.Errorf("build %q is not completed, only completed builds can be traced", rec.Ref)
@@ -103,7 +69,9 @@ func loadTrace(ctx context.Context, ref string, nodes []builder.Node) (string, [
10369
return "", nil, err
10470
}
10571

106-
recs, err := queryRecords(ctx, rec.Ref, []builder.Node{*rec.node})
72+
recs, err := queryRecords(ctx, rec.Ref, []builder.Node{*rec.node}, &queryOptions{
73+
CompletedOnly: true,
74+
})
10775
if err != nil {
10876
return "", nil, err
10977
}

commands/history/utils.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"io"
77
"path/filepath"
8+
"slices"
9+
"strconv"
810
"strings"
911
"sync"
1012
"time"
@@ -106,10 +108,24 @@ type historyRecord struct {
106108
name string
107109
}
108110

109-
func queryRecords(ctx context.Context, ref string, nodes []builder.Node) ([]historyRecord, error) {
111+
type queryOptions struct {
112+
CompletedOnly bool
113+
}
114+
115+
func queryRecords(ctx context.Context, ref string, nodes []builder.Node, opts *queryOptions) ([]historyRecord, error) {
110116
var mu sync.Mutex
111117
var out []historyRecord
112118

119+
var offset *int
120+
if strings.HasPrefix(ref, "^") {
121+
off, err := strconv.Atoi(ref[1:])
122+
if err != nil {
123+
return nil, errors.Wrapf(err, "invalid offset %q", ref)
124+
}
125+
offset = &off
126+
ref = ""
127+
}
128+
113129
eg, ctx := errgroup.WithContext(ctx)
114130
for _, node := range nodes {
115131
node := node
@@ -153,6 +169,10 @@ func queryRecords(ctx context.Context, ref string, nodes []builder.Node) ([]hist
153169
if he.Type == controlapi.BuildHistoryEventType_DELETED || he.Record == nil {
154170
continue
155171
}
172+
if opts != nil && opts.CompletedOnly && he.Type != controlapi.BuildHistoryEventType_COMPLETE {
173+
continue
174+
}
175+
156176
records = append(records, historyRecord{
157177
BuildHistoryRecord: he.Record,
158178
currentTimestamp: ts,
@@ -169,6 +189,27 @@ func queryRecords(ctx context.Context, ref string, nodes []builder.Node) ([]hist
169189
if err := eg.Wait(); err != nil {
170190
return nil, err
171191
}
192+
193+
slices.SortFunc(out, func(a, b historyRecord) int {
194+
return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime())
195+
})
196+
197+
if offset != nil {
198+
var filtered []historyRecord
199+
for _, r := range out {
200+
if *offset > 0 {
201+
*offset--
202+
continue
203+
}
204+
filtered = append(filtered, r)
205+
break
206+
}
207+
if *offset > 0 {
208+
return nil, errors.Errorf("no completed build found with offset %d", *offset)
209+
}
210+
out = filtered
211+
}
212+
172213
return out, nil
173214
}
174215

0 commit comments

Comments
 (0)