-
Notifications
You must be signed in to change notification settings - Fork 12
/
load.go
109 lines (90 loc) · 2.67 KB
/
load.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package nix2container
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"github.com/containerd/containerd"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/images/archive"
"github.com/containerd/containerd/log"
tarchive "github.com/containerd/containerd/pkg/transfer/archive"
"github.com/containerd/containerd/pkg/transfer/image"
"github.com/containerd/containerd/platforms"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
func Load(ctx context.Context, client *containerd.Client, store content.Store, archivePath string) (containerd.Image, error) {
dt, err := os.ReadFile(archivePath)
if err != nil {
return nil, err
}
src := tarchive.NewImageImportStream(bytes.NewReader(dt), ocispec.MediaTypeImageIndex)
platSpec := platforms.DefaultSpec()
storeOpts := []image.StoreOpt{
image.WithUnpack(platSpec, "nix"),
image.WithPlatforms(platSpec),
}
target, err := archive.ImportIndex(ctx, store, bytes.NewReader(dt))
if err != nil {
return nil, err
}
ref, err := refFromArchive(ctx, store, target)
if err != nil {
return nil, err
}
dest := image.NewStore(ref, storeOpts...)
// pf, done := images.ProgressHandler(ctx, os.Stderr)
// defer done()
log.G(ctx).WithField("ref", ref).Info("Importing image")
err = client.Transfer(ctx, src, dest)
if err != nil {
return nil, err
}
log.G(ctx).WithField("ref", ref).Info("Creating image")
img := images.Image{Name: ref, Target: target}
_, err = createImage(ctx, client.ImageService(), img)
if err != nil {
return nil, err
}
log.G(ctx).WithField("ref", ref).Info("Created image")
return client.GetImage(ctx, ref)
}
func refFromArchive(ctx context.Context, store content.Store, target ocispec.Descriptor) (ref string, err error) {
blob, err := content.ReadBlob(ctx, store, target)
if err != nil {
return
}
var idx ocispec.Index
if err = json.Unmarshal(blob, &idx); err != nil {
return
}
if len(idx.Manifests) != 1 {
return "", fmt.Errorf("OCI index had %d manifests", len(idx.Manifests))
}
mfst := idx.Manifests[0]
return mfst.Annotations[images.AnnotationImageName], nil
}
func createImage(ctx context.Context, store images.Store, img images.Image) (images.Image, error) {
for {
if created, err := store.Create(ctx, img); err != nil {
if !errdefs.IsAlreadyExists(err) {
return images.Image{}, err
}
updated, err := store.Update(ctx, img)
if err != nil {
// if image was removed, try create again
if errdefs.IsNotFound(err) {
continue
}
return images.Image{}, err
}
img = updated
} else {
img = created
}
return img, nil
}
}