forked from AliyunContainerService/pouch
/
image_load.go
56 lines (46 loc) · 1.7 KB
/
image_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
package mgr
import (
"context"
"fmt"
"io"
"github.com/alibaba/pouch/pkg/multierror"
"github.com/alibaba/pouch/pkg/reference"
ociimage "github.com/containerd/containerd/images/oci"
pkgerrors "github.com/pkg/errors"
)
// LoadImage loads images by the oci.v1 format tarstream.
func (mgr *ImageManager) LoadImage(ctx context.Context, imageName string, tarstream io.ReadCloser) error {
defer tarstream.Close()
namedRef, err := reference.Parse(imageName)
if err != nil {
return pkgerrors.Wrapf(err, "failed to parse image name %s", imageName)
}
// NOTE: in the image ocispec.v1, the org.opencontainers.image.ref.name
// annotation represents a "tag" for image. For example, an image may
// have a tag for different versions or builds of the software.
// And containerd.importer will append ":" and annotation to the name
// so that we don't allow imageName to contains any digest or tag
// information, like foo/bar:latest:v1.2.
if !reference.IsNamedOnly(namedRef) {
return fmt.Errorf("the image name should not contains any digest or tag information")
}
importer := &ociimage.V1Importer{
ImageName: imageName,
}
imgs, err := mgr.client.ImportImage(ctx, importer, tarstream)
if err != nil {
return pkgerrors.Wrap(err, "failed to import image into containerd by tarstream")
}
// FIXME(fuwei): if the store fails to update reference cache, the daemon
// may fails to load after restart.
merrs := new(multierror.Multierrors)
for _, img := range imgs {
if err := mgr.storeImageReference(ctx, img); err != nil {
merrs.Append(fmt.Errorf("fail to store reference: %s: %v", img.Name(), err))
}
}
if merrs.Size() != 0 {
return fmt.Errorf("fails to load image: %v", merrs.Error())
}
return nil
}