Skip to content

Commit

Permalink
Merge pull request #721 from zeppp/feature-add-fields-to-imageInfo
Browse files Browse the repository at this point in the history
feature: add repoTags and repoDigests
  • Loading branch information
HusterWan authored Mar 21, 2018
2 parents 92579c3 + 34442b5 commit 890bdca
Show file tree
Hide file tree
Showing 17 changed files with 372 additions and 143 deletions.
6 changes: 3 additions & 3 deletions apis/server/image_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,22 @@ func (s *Server) removeImage(ctx context.Context, rw http.ResponseWriter, req *h
}

containers, err := s.ContainerMgr.List(ctx, func(meta *mgr.ContainerMeta) bool {
return meta.Image == image.Name
return meta.Image == image.ID
}, &mgr.ContainerListOption{All: true})
if err != nil {
return err
}

isForce := httputils.BoolValue(req, "force")
if !isForce && len(containers) > 0 {
return fmt.Errorf("Unable to remove the image %q (must force) - container %s is using this image", image.Name, containers[0].ID)
return fmt.Errorf("Unable to remove the image %q (must force) - container %s is using this image", image.ID, containers[0].ID)
}

option := &mgr.ImageRemoveOption{
Force: isForce,
}

if err := s.ImageMgr.RemoveImage(ctx, image, option); err != nil {
if err := s.ImageMgr.RemoveImage(ctx, image, name, option); err != nil {
return err
}

Expand Down
19 changes: 10 additions & 9 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2382,15 +2382,16 @@ definitions:
description: "ID of an image."
type: "string"
x-nullable: false
Name:
description: "name of an image."
type: "string"
Tag:
description: "tag of an image."
type: "string"
Digest:
description: "digest of image."
type: "string"
RepoTags:
description: "repository with tag."
type: "array"
items:
type: "string"
RepoDigests:
description: "repository with digest."
type: "array"
items:
type: "string"
CreatedAt:
description: "time of image creation."
type: "string"
Expand Down
53 changes: 38 additions & 15 deletions apis/types/image_info.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 28 additions & 3 deletions cli/image_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/alibaba/pouch/pkg/reference"
"github.com/alibaba/pouch/pkg/utils"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -80,17 +81,41 @@ func (i *ImagesCommand) runImages(args []string) error {
}

for _, image := range imageList {
var name reference.Named
var digest string
if len(image.RepoTags) > 0 {
name, err = reference.ParseNamedReference(image.RepoTags[0])
if err != nil {
return err
}
digestName, err := reference.ParseNamedReference(image.RepoDigests[0])
if err != nil {
return err
}
if digestd, ok := digestName.(reference.Digested); ok {
digest = digestd.Digest()
}
} else {
name, err = reference.ParseNamedReference(image.RepoDigests[0])
if err != nil {
return err
}
if digestd, ok := name.(reference.Digested); ok {
digest = digestd.Digest()
}
}

if i.flagDigest {
display.AddRow([]string{
utils.TruncateID(image.ID),
image.Name,
image.Digest,
name.String(),
digest,
fmt.Sprintf("%s", imageSize(image.Size)),
})
} else {
display.AddRow([]string{
utils.TruncateID(image.ID),
image.Name,
name.String(),
fmt.Sprintf("%s", imageSize(image.Size)),
})
}
Expand Down
53 changes: 29 additions & 24 deletions ctrd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/remotes"
digest "github.com/opencontainers/go-digest"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go/v1"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -74,9 +74,6 @@ func (c *Client) ListImages(ctx context.Context, filter ...string) ([]types.Imag

images := make([]types.ImageInfo, 0, 32)
for _, image := range imageList {
descriptor := image.Target
digest := descriptor.Digest

size, err := image.Size(ctx, c.client.ContentStore(), platforms.Default())
// occur error, skip it
if err != nil {
Expand All @@ -90,7 +87,6 @@ func (c *Client) ListImages(ctx context.Context, filter ...string) ([]types.Imag
logrus.Errorf("failed to parse image %s: %v", image.Name, err)
continue
}
refTagged := reference.WithDefaultTagIfMissing(refNamed).(reference.Tagged)

ociImage, err := c.GetOciImage(ctx, image.Name)
// occur error, skip it
Expand All @@ -106,9 +102,6 @@ func (c *Client) ListImages(ctx context.Context, filter ...string) ([]types.Imag
logrus.Errorf("failed to convert ociImage to pouch image %s: %v", image.Name, err)
continue
}
imageInfo.Tag = refTagged.Tag()
imageInfo.Name = image.Name
imageInfo.Digest = digest.String()
imageInfo.Size = size

// generate image ID by imageInfo JSON.
Expand All @@ -118,6 +111,13 @@ func (c *Client) ListImages(ctx context.Context, filter ...string) ([]types.Imag
}
imageInfo.ID = imageID.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()+"@"+image.Target.Digest.String())
}
images = append(images, imageInfo)
}
return images, nil
Expand Down Expand Up @@ -194,24 +194,29 @@ func (c *Client) PullImage(ctx context.Context, ref string, authConfig *types.Au
}

imageInfo, err := ociImageToPouchImage(ociImage)

// fill struct ImageInfo
refNamed, err := reference.ParseNamedReference(img.Name())
if err != nil {
return types.ImageInfo{}, err
}
refTagged := reference.WithDefaultTagIfMissing(refNamed).(reference.Tagged)
imageInfo.Tag = refTagged.Tag()
imageInfo.Name = img.Name()
imageInfo.Digest = img.Target().Digest.String()
imageInfo.Size = size

// generate image ID by imageInfo JSON.
imageID, err := generateID(&imageInfo)
if err != nil {
return types.ImageInfo{}, err
{
imageInfo.Size = size
// generate image ID by imageInfo JSON.
imageID, err := generateID(&imageInfo)
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 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.ID = imageID.String()

return imageInfo, nil
}
Expand Down
25 changes: 20 additions & 5 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

"github.com/go-openapi/strfmt"
"github.com/opencontainers/image-spec/specs-go/v1"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -344,7 +344,15 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
if err != nil {
return nil, err
}
config.Image = image.Name

// FIXME: image.Name does not exist,so convert Repotags or RepoDigests to ref
ref := ""
if len(image.RepoTags) > 0 {
ref = image.RepoTags[0]
} else {
ref = image.RepoDigests[0]
}
config.Image = ref

// set container runtime
if config.HostConfig.Runtime == "" {
Expand Down Expand Up @@ -372,7 +380,7 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
Status: types.StatusCreated,
},
ID: id,
Image: image.Name,
Image: image.ID,
Name: name,
Config: &config.ContainerConfig,
Created: time.Now().UTC().Format(utils.TimeLayout),
Expand Down Expand Up @@ -767,8 +775,15 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
return err
}
// TODO Image param is duplicate in ContainerMeta
c.meta.Config.Image = image.Name
c.meta.Image = image.Name
// FIXME: image.Name does not exist,so convert Repotags or RepoDigests to ref
ref := ""
if len(image.RepoTags) > 0 {
ref = image.RepoTags[0]
} else {
ref = image.RepoDigests[0]
}
c.meta.Config.Image = ref
c.meta.Image = ref
}

if len(config.Env) != 0 {
Expand Down
7 changes: 4 additions & 3 deletions daemon/mgr/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path"
"path/filepath"
"strings"
"time"

apitypes "github.com/alibaba/pouch/apis/types"
Expand Down Expand Up @@ -748,7 +749,7 @@ func (c *CriManager) ImageStatus(ctx context.Context, r *runtime.ImageStatusRequ
return nil, err
}

imageInfo, err := c.ImageMgr.GetImage(ctx, ref.String())
imageInfo, err := c.ImageMgr.GetImage(ctx, strings.TrimPrefix(ref.String(), "sha256:"))
if err != nil {
// TODO: seperate ErrImageNotFound with others.
// Now we just return empty if the error occured.
Expand Down Expand Up @@ -795,14 +796,14 @@ func (c *CriManager) RemoveImage(ctx context.Context, r *runtime.RemoveImageRequ
return nil, err
}

imageInfo, err := c.ImageMgr.GetImage(ctx, ref.String())
imageInfo, err := c.ImageMgr.GetImage(ctx, strings.TrimPrefix(ref.String(), "sha256:"))
if err != nil {
// TODO: seperate ErrImageNotFound with others.
// Now we just return empty if the error occured.
return &runtime.RemoveImageResponse{}, nil
}

err = c.ImageMgr.RemoveImage(ctx, imageInfo, &ImageRemoveOption{})
err = c.ImageMgr.RemoveImage(ctx, imageInfo, strings.TrimPrefix(ref.String(), "sha256:"), &ImageRemoveOption{})
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 890bdca

Please sign in to comment.