From a19f92fc2a0a6e185f8f9072192d2e0b02e1f9b1 Mon Sep 17 00:00:00 2001 From: YaoZengzeng Date: Wed, 11 Apr 2018 17:08:48 +0800 Subject: [PATCH] bugfix: we should get image ID from containerd Signed-off-by: YaoZengzeng --- ctrd/image.go | 36 ++++++++++++++++++++---------------- daemon/mgr/cri.go | 8 ++++++++ daemon/mgr/spec_process.go | 4 +--- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/ctrd/image.go b/ctrd/image.go index 5331b782e2..1b3d26c192 100644 --- a/ctrd/image.go +++ b/ctrd/image.go @@ -96,6 +96,13 @@ func (c *Client) ListImages(ctx context.Context, filter ...string) ([]types.Imag continue } + desc, err := image.Config(ctx, wrapperCli.client.ContentStore(), platforms.Default()) + if err != nil { + logrus.Errorf("failed to get image id %s: %v", image.Name, err) + continue + } + id := desc.Digest.String() + refNamed, err := reference.ParseNamedReference(image.Name) // occur error, skip it if err != nil { @@ -118,13 +125,7 @@ func (c *Client) ListImages(ctx context.Context, filter ...string) ([]types.Imag continue } imageInfo.Size = size - - // generate image ID by imageInfo JSON. - imageID, err := generateID(&imageInfo) - if err != nil { - return nil, err - } - imageInfo.ID = imageID.String() + imageInfo.ID = id if refDigest, ok := refNamed.(reference.Digested); ok { imageInfo.RepoDigests = append(imageInfo.RepoDigests, refDigest.String()) @@ -206,6 +207,12 @@ func (c *Client) PullImage(ctx context.Context, ref string, authConfig *types.Au return types.ImageInfo{}, err } + desc, err := img.Config(ctx) + if err != nil { + return types.ImageInfo{}, err + } + id := desc.Digest.String() + logrus.Infof("success to pull image: %s", img.Name()) ociImage, err := c.GetOciImage(ctx, ref) @@ -217,24 +224,21 @@ func (c *Client) PullImage(ctx context.Context, ref string, authConfig *types.Au // fill struct ImageInfo { imageInfo.Size = size - // generate image ID by imageInfo JSON. - imageID, err := generateID(&imageInfo) + imageInfo.ID = id + + refNamed, err := reference.ParseNamedReference(ref) if err != nil { return types.ImageInfo{}, err } - imageInfo.ID = imageID.String() - refNamed, err := reference.ParseNamedReference(img.Name()) - if err != nil { - return types.ImageInfo{}, err + if refTag, ok := refNamed.(reference.Tagged); ok { + imageInfo.RepoTags = append(imageInfo.RepoTags, refTag.String()) } if refDigest, ok := refNamed.(reference.Digested); ok { imageInfo.RepoDigests = append(imageInfo.RepoDigests, refDigest.String()) } else { - refTagged := reference.WithDefaultTagIfMissing(refNamed).(reference.Tagged) - imageInfo.RepoTags = append(imageInfo.RepoTags, refTagged.String()) - imageInfo.RepoDigests = append(imageInfo.RepoDigests, refTagged.Name()+"@"+img.Target().Digest.String()) + imageInfo.RepoDigests = append(imageInfo.RepoDigests, refNamed.Name()+"@"+img.Target().Digest.String()) } } diff --git a/daemon/mgr/cri.go b/daemon/mgr/cri.go index 8a5e06c36c..f8015a999a 100644 --- a/daemon/mgr/cri.go +++ b/daemon/mgr/cri.go @@ -761,8 +761,15 @@ func (c *CriManager) ListImages(ctx context.Context, r *runtime.ListImagesReques return nil, err } + // We may get images with same id and different repoTag or repoDigest, + // so we need idExist to de-dup. + idExist := make(map[string]bool) + images := make([]*runtime.Image, 0, len(imageList)) for _, i := range imageList { + if _, ok := idExist[i.ID]; ok { + continue + } // NOTE: we should query image cache to get the correct image info. imageInfo, err := c.ImageMgr.GetImage(ctx, strings.TrimPrefix(i.ID, "sha256:")) if err != nil { @@ -774,6 +781,7 @@ func (c *CriManager) ListImages(ctx context.Context, r *runtime.ListImagesReques continue } images = append(images, image) + idExist[i.ID] = true } return &runtime.ListImagesResponse{Images: images}, nil diff --git a/daemon/mgr/spec_process.go b/daemon/mgr/spec_process.go index ca250683da..ba6b4d72db 100644 --- a/daemon/mgr/spec_process.go +++ b/daemon/mgr/spec_process.go @@ -54,10 +54,8 @@ func setupProcessCwd(ctx context.Context, c *ContainerMeta, spec *SpecWrapper) e func setupProcessTTY(ctx context.Context, c *ContainerMeta, spec *SpecWrapper) error { s := spec.s s.Process.Terminal = c.Config.Tty - if s.Process.Env != nil { + if c.Config.Tty { s.Process.Env = append(s.Process.Env, "TERM=xterm") - } else { - s.Process.Env = []string{"TERM=xterm"} } return nil }