improve calling for content

Signed-off-by: yason <yan.xuean@zte.com.cn>
This commit is contained in:
yason 2017-12-11 14:19:30 +08:00
parent c89e555150
commit 41c8763e2b
5 changed files with 29 additions and 36 deletions

View File

@ -315,7 +315,7 @@ type imageInfo struct {
} }
// getImageInfo gets image info from containerd. // getImageInfo gets image info from containerd.
func getImageInfo(ctx context.Context, image containerd.Image, provider content.Provider) (*imageInfo, error) { func getImageInfo(ctx context.Context, image containerd.Image) (*imageInfo, error) {
// Get image information. // Get image information.
diffIDs, err := image.RootFS(ctx) diffIDs, err := image.RootFS(ctx)
if err != nil { if err != nil {
@ -334,7 +334,7 @@ func getImageInfo(ctx context.Context, image containerd.Image, provider content.
} }
id := desc.Digest.String() id := desc.Digest.String()
rb, err := content.ReadBlob(ctx, provider, desc.Digest) rb, err := content.ReadBlob(ctx, image.ContentStore(), desc.Digest)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read image config from content store: %v", err) return nil, fmt.Errorf("failed to read image config from content store: %v", err)
} }

View File

@ -52,7 +52,7 @@ func (c *criContainerdService) LoadImage(ctx context.Context, r *api.LoadImageRe
glog.Warningf("Failed to unpack image %q: %v", repoTag, err) glog.Warningf("Failed to unpack image %q: %v", repoTag, err)
// Do not fail image importing. Unpack will be retried when container creation. // Do not fail image importing. Unpack will be retried when container creation.
} }
info, err := getImageInfo(ctx, image, c.client.ContentStore()) info, err := getImageInfo(ctx, image)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get image %q info: %v", repoTag, err) return nil, fmt.Errorf("failed to get image %q info: %v", repoTag, err)
} }

View File

@ -114,8 +114,15 @@ func (c *criContainerdService) PullImage(ctx context.Context, r *runtime.PullIma
// Do not fail image pulling. Unpack will be retried before container creation. // Do not fail image pulling. Unpack will be retried before container creation.
} }
// Get image information.
info, err := getImageInfo(ctx, image)
if err != nil {
return nil, fmt.Errorf("failed to get image information: %v", err)
}
imageID := info.id
repoDigest, repoTag := getRepoDigestAndTag(namedRef, image.Target().Digest, isSchema1) repoDigest, repoTag := getRepoDigestAndTag(namedRef, image.Target().Digest, isSchema1)
for _, r := range []string{repoTag, repoDigest} { for _, r := range []string{repoTag, repoDigest, imageID} {
if r == "" { if r == "" {
continue continue
} }
@ -123,16 +130,7 @@ func (c *criContainerdService) PullImage(ctx context.Context, r *runtime.PullIma
return nil, fmt.Errorf("failed to update image reference %q: %v", r, err) return nil, fmt.Errorf("failed to update image reference %q: %v", r, err)
} }
} }
// Get image information.
info, err := getImageInfo(ctx, image, c.client.ContentStore())
if err != nil {
return nil, fmt.Errorf("failed to get image information: %v", err)
}
imageID := info.id
if err := c.createImageReference(ctx, imageID, image.Target()); err != nil {
return nil, fmt.Errorf("failed to update image reference %q: %v", imageID, err)
}
glog.V(4).Infof("Pulled image %q with image id %q, repo tag %q, repo digest %q", imageRef, imageID, glog.V(4).Infof("Pulled image %q with image id %q, repo tag %q, repo digest %q", imageRef, imageID,
repoTag, repoDigest) repoTag, repoDigest)
img := imagestore.Image{ img := imagestore.Image{
@ -158,7 +156,7 @@ func (c *criContainerdService) PullImage(ctx context.Context, r *runtime.PullIma
// by someone else anytime, before/during/after we create the metadata. We should always // by someone else anytime, before/during/after we create the metadata. We should always
// check the actual state in containerd before using the image or returning status of the // check the actual state in containerd before using the image or returning status of the
// image. // image.
return &runtime.PullImageResponse{ImageRef: img.ID}, err return &runtime.PullImageResponse{ImageRef: img.ID}, nil
} }
// ParseAuth parses AuthConfig and returns username and password/secret required by containerd. // ParseAuth parses AuthConfig and returns username and password/secret required by containerd.

View File

@ -25,7 +25,6 @@ import (
"github.com/containerd/containerd" "github.com/containerd/containerd"
containerdio "github.com/containerd/containerd/cio" containerdio "github.com/containerd/containerd/cio"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
containerdimages "github.com/containerd/containerd/images" containerdimages "github.com/containerd/containerd/images"
"github.com/containerd/containerd/platforms" "github.com/containerd/containerd/platforms"
@ -99,7 +98,7 @@ func (c *criContainerdService) recover(ctx context.Context) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to list images: %v", err) return fmt.Errorf("failed to list images: %v", err)
} }
images, err := loadImages(ctx, cImages, c.client.ContentStore(), c.config.ContainerdConfig.Snapshotter) images, err := loadImages(ctx, cImages, c.config.ContainerdConfig.Snapshotter)
if err != nil { if err != nil {
return fmt.Errorf("failed to load images: %v", err) return fmt.Errorf("failed to load images: %v", err)
} }
@ -322,7 +321,7 @@ func loadSandbox(ctx context.Context, cntr containerd.Container) (sandboxstore.S
// loadImages loads images from containerd. // loadImages loads images from containerd.
// TODO(random-liu): Check whether image is unpacked, because containerd put image reference // TODO(random-liu): Check whether image is unpacked, because containerd put image reference
// into store before image is unpacked. // into store before image is unpacked.
func loadImages(ctx context.Context, cImages []containerd.Image, provider content.Provider, func loadImages(ctx context.Context, cImages []containerd.Image,
snapshotter string) ([]imagestore.Image, error) { snapshotter string) ([]imagestore.Image, error) {
// Group images by image id. // Group images by image id.
imageMap := make(map[string][]containerd.Image) imageMap := make(map[string][]containerd.Image)
@ -340,7 +339,7 @@ func loadImages(ctx context.Context, cImages []containerd.Image, provider conten
// imgs len must be > 0, or else the entry will not be created in // imgs len must be > 0, or else the entry will not be created in
// previous loop. // previous loop.
i := imgs[0] i := imgs[0]
ok, _, _, _, err := containerdimages.Check(ctx, provider, i.Target(), platforms.Default()) ok, _, _, _, err := containerdimages.Check(ctx, i.ContentStore(), i.Target(), platforms.Default())
if err != nil { if err != nil {
glog.Errorf("Failed to check image content readiness for %q: %v", i.Name(), err) glog.Errorf("Failed to check image content readiness for %q: %v", i.Name(), err)
continue continue
@ -360,7 +359,7 @@ func loadImages(ctx context.Context, cImages []containerd.Image, provider conten
// TODO(random-liu): Consider whether we should try unpack here. // TODO(random-liu): Consider whether we should try unpack here.
} }
info, err := getImageInfo(ctx, i, provider) info, err := getImageInfo(ctx, i)
if err != nil { if err != nil {
glog.Warningf("Failed to get image info for %q: %v", i.Name(), err) glog.Warningf("Failed to get image info for %q: %v", i.Name(), err)
continue continue

View File

@ -26,7 +26,6 @@ import (
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/api/services/tasks/v1" "github.com/containerd/containerd/api/services/tasks/v1"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images" "github.com/containerd/containerd/images"
"github.com/containerd/containerd/plugin" "github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/sys" "github.com/containerd/containerd/sys"
@ -96,8 +95,6 @@ type criContainerdService struct {
snapshotStore *snapshotstore.Store snapshotStore *snapshotstore.Store
// taskService is containerd tasks client. // taskService is containerd tasks client.
taskService tasks.TasksClient taskService tasks.TasksClient
// contentStoreService is the containerd content service client.
contentStoreService content.Store
// imageStoreService is the containerd service to store and track // imageStoreService is the containerd service to store and track
// image metadata. // image metadata.
imageStoreService images.Store imageStoreService images.Store
@ -131,20 +128,19 @@ func NewCRIContainerdService(config options.Config) (CRIContainerdService, error
} }
c := &criContainerdService{ c := &criContainerdService{
config: config, config: config,
apparmorEnabled: runcapparmor.IsEnabled(), apparmorEnabled: runcapparmor.IsEnabled(),
seccompEnabled: runcseccomp.IsEnabled(), seccompEnabled: runcseccomp.IsEnabled(),
os: osinterface.RealOS{}, os: osinterface.RealOS{},
sandboxStore: sandboxstore.NewStore(), sandboxStore: sandboxstore.NewStore(),
containerStore: containerstore.NewStore(), containerStore: containerstore.NewStore(),
imageStore: imagestore.NewStore(), imageStore: imagestore.NewStore(),
snapshotStore: snapshotstore.NewStore(), snapshotStore: snapshotstore.NewStore(),
sandboxNameIndex: registrar.NewRegistrar(), sandboxNameIndex: registrar.NewRegistrar(),
containerNameIndex: registrar.NewRegistrar(), containerNameIndex: registrar.NewRegistrar(),
taskService: client.TaskService(), taskService: client.TaskService(),
imageStoreService: client.ImageService(), imageStoreService: client.ImageService(),
contentStoreService: client.ContentStore(), client: client,
client: client,
} }
imageFSPath := imageFSPath(config.ContainerdConfig.RootDir, config.ContainerdConfig.Snapshotter) imageFSPath := imageFSPath(config.ContainerdConfig.RootDir, config.ContainerdConfig.Snapshotter)