Skip to content

Commit c461e70

Browse files
authored
Merge pull request #3716 from crazy-max/history-fix-finalize
history: finalize export records on their owning node
2 parents 3b87097 + 5245d50 commit c461e70

File tree

6 files changed

+154
-123
lines changed

6 files changed

+154
-123
lines changed

commands/history/export.go

Lines changed: 32 additions & 18 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/containerd/console"
109
"github.com/containerd/platforms"
@@ -53,37 +52,52 @@ func runExport(ctx context.Context, dockerCli command.Cli, opts exportOptions) e
5352
return errors.Errorf("no record found for ref %q", ref)
5453
}
5554

55+
toExport := recs
56+
if !opts.all && ref == "" {
57+
latestRef := recs[0].Ref
58+
recCount := 0
59+
for _, rec := range recs {
60+
if rec.Ref != latestRef {
61+
break
62+
}
63+
recCount++
64+
}
65+
toExport = recs[:recCount]
66+
}
5667
if opts.finalize {
68+
seen := make(map[string]struct{}, len(toExport))
5769
var finalized bool
58-
for _, rec := range recs {
59-
if rec.Trace == nil {
60-
finalized = true
61-
if err := finalizeRecord(ctx, rec.Ref, nodes); err != nil {
62-
return err
63-
}
70+
for _, rec := range toExport {
71+
if rec.node == nil || rec.Trace != nil {
72+
continue
73+
}
74+
key := rec.node.Builder + "\x00" + rec.node.Name + "\x00" + rec.Ref
75+
if _, ok := seen[key]; ok {
76+
continue
77+
}
78+
seen[key] = struct{}{}
79+
finalized = true
80+
if err := finalizeRecord(ctx, rec.Ref, *rec.node); err != nil {
81+
return err
6482
}
6583
}
6684
if finalized {
67-
recs, err = queryRecords(ctx, ref, nodes, &queryOptions{
85+
queryRef := ref
86+
if !opts.all {
87+
queryRef = toExport[0].Ref
88+
}
89+
recs, err = queryRecords(ctx, queryRef, nodes, &queryOptions{
6890
CompletedOnly: true,
6991
})
7092
if err != nil {
7193
return err
7294
}
95+
toExport = recs
7396
}
7497
}
75-
76-
if ref == "" {
77-
slices.SortFunc(recs, func(a, b historyRecord) int {
78-
return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime())
79-
})
80-
}
81-
98+
res = append(res, toExport...)
8299
if opts.all {
83-
res = append(res, recs...)
84100
break
85-
} else {
86-
res = append(res, recs[0])
87101
}
88102
}
89103

commands/history/trace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func loadTrace(ctx context.Context, ref string, nodes []builder.Node) (string, [
5656
// build is complete but no trace yet. try to finalize the trace
5757
time.Sleep(1 * time.Second) // give some extra time for last parts of trace to be written
5858

59-
err := finalizeRecord(ctx, rec.Ref, []builder.Node{*rec.node})
59+
err := finalizeRecord(ctx, rec.Ref, *rec.node)
6060
if err != nil {
6161
return "", nil, err
6262
}

commands/history/utils.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -257,25 +257,19 @@ func queryRecords(ctx context.Context, ref string, nodes []builder.Node, opts *q
257257
return out, nil
258258
}
259259

260-
func finalizeRecord(ctx context.Context, ref string, nodes []builder.Node) error {
261-
eg, ctx := errgroup.WithContext(ctx)
262-
for _, node := range nodes {
263-
eg.Go(func() error {
264-
if node.Driver == nil {
265-
return nil
266-
}
267-
c, err := node.Driver.Client(ctx)
268-
if err != nil {
269-
return err
270-
}
271-
_, err = c.ControlClient().UpdateBuildHistory(ctx, &controlapi.UpdateBuildHistoryRequest{
272-
Ref: ref,
273-
Finalize: true,
274-
})
275-
return err
276-
})
260+
func finalizeRecord(ctx context.Context, ref string, node builder.Node) error {
261+
if node.Driver == nil {
262+
return nil
277263
}
278-
return eg.Wait()
264+
c, err := node.Driver.Client(ctx)
265+
if err != nil {
266+
return err
267+
}
268+
_, err = c.ControlClient().UpdateBuildHistory(ctx, &controlapi.UpdateBuildHistoryRequest{
269+
Ref: ref,
270+
Finalize: true,
271+
})
272+
return err
279273
}
280274

281275
func formatDuration(d time.Duration) string {

hack/test-driver

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ buildxCmd build ${buildPlatformFlag} \
134134
--metadata-file="${context}/metadata-build.json" \
135135
"${context}"
136136
cat "${context}/metadata-build.json"
137+
buildRef=$(awk -F'"' '/"buildx.build.ref"/ {print $4; exit}' "${context}/metadata-build.json")
138+
buildID=${buildRef##*/}
137139

138140
# load to docker store
139141
if [ "$DRIVER" != "docker" ]; then
@@ -143,6 +145,15 @@ if [ "$DRIVER" != "docker" ]; then
143145
"${context}"
144146
fi
145147

148+
# list build records
149+
buildxCmd --builder="${builderName}" history ls
150+
151+
# export build records
152+
buildxCmd --builder="${builderName}" history export --finalize "${buildID}" --output "${context}/record.dockerbuild"
153+
file "${context}/record.dockerbuild"
154+
buildxCmd --builder="${builderName}" history export --finalize --all --output "${context}/records.dockerbuild"
155+
file "${context}/records.dockerbuild"
156+
146157
# create bake def
147158
cat > "${bakedef}" <<EOL
148159
group "default" {

0 commit comments

Comments
 (0)