Skip to content

Commit 7a8c38e

Browse files
committed
fix: fix load image failed
Signed-off-by: ningmingxiao <ning.mingxiao@zte.com.cn> Signed-off-by: 宁明晓10296073 <ning.mingxiao@zte.com.cn>
1 parent 0b388bb commit 7a8c38e

1 file changed

Lines changed: 8 additions & 13 deletions

File tree

pkg/imgutil/load/load.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"os"
2424
"strings"
25+
"sync"
2526

2627
containerd "github.com/containerd/containerd/v2/client"
2728
"github.com/containerd/containerd/v2/core/images"
@@ -60,14 +61,6 @@ func FromArchive(ctx context.Context, client *containerd.Client, options types.I
6061
}
6162

6263
imageService := client.ImageService()
63-
beforeImages, err := imageService.List(ctx)
64-
if err != nil {
65-
return nil, err
66-
}
67-
beforeSet := make(map[string]bool)
68-
for _, img := range beforeImages {
69-
beforeSet[img.Name] = true
70-
}
7164

7265
var storeOpts []transferimage.StoreOpt
7366
platUnpack := platforms.DefaultSpec()
@@ -86,16 +79,16 @@ func FromArchive(ctx context.Context, client *containerd.Client, options types.I
8679

8780
var loadedImages []images.Image
8881
pf, done := transferutil.ProgressHandler(ctx, options.Stdout)
89-
90-
err = client.Transfer(ctx,
82+
var mu sync.Mutex
83+
err := client.Transfer(ctx,
9184
tarchive.NewImageImportStream(options.Stdin, ""),
9285
transferimage.NewStore("", storeOpts...),
9386
transfer.WithProgress(func(p transfer.Progress) {
87+
mu.Lock()
88+
defer mu.Unlock()
9489
if p.Event == "saved" {
9590
if img, err := imageService.Get(ctx, p.Name); err == nil {
96-
if !beforeSet[img.Name] {
97-
loadedImages = append(loadedImages, img)
98-
}
91+
loadedImages = append(loadedImages, img)
9992
}
10093
}
10194
pf(p)
@@ -105,9 +98,11 @@ func FromArchive(ctx context.Context, client *containerd.Client, options types.I
10598
done()
10699

107100
if !options.Quiet {
101+
mu.Lock()
108102
for _, img := range loadedImages {
109103
fmt.Fprintf(options.Stdout, "Loaded image: %s\n", img.Name)
110104
}
105+
mu.Unlock()
111106
}
112107

113108
return loadedImages, err

0 commit comments

Comments
 (0)