2017-07-23 23:01:37 +00:00
|
|
|
package image
|
2017-06-29 19:16:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2017-07-25 19:39:16 +00:00
|
|
|
"github.com/containers/image/docker/reference"
|
2017-06-29 19:16:06 +00:00
|
|
|
"github.com/containers/storage"
|
2017-07-25 19:23:43 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/cmd/kpod/docker"
|
2017-07-21 20:43:30 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/libkpod/driver"
|
2017-07-25 19:33:41 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2017-06-29 19:16:06 +00:00
|
|
|
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-07-23 23:01:37 +00:00
|
|
|
// ImageData handles the data used when inspecting a container
|
2017-07-24 13:21:50 +00:00
|
|
|
// nolint
|
2017-07-23 23:01:37 +00:00
|
|
|
type ImageData struct {
|
2017-06-29 19:16:06 +00:00
|
|
|
ID string
|
2017-07-25 19:39:16 +00:00
|
|
|
Tags []string
|
|
|
|
Digests []string
|
2017-07-25 19:33:41 +00:00
|
|
|
Digest digest.Digest
|
2017-06-29 19:16:06 +00:00
|
|
|
Parent string
|
|
|
|
Comment string
|
|
|
|
Created *time.Time
|
|
|
|
Container string
|
2017-07-25 19:23:43 +00:00
|
|
|
ContainerConfig docker.Config
|
2017-06-29 19:16:06 +00:00
|
|
|
Author string
|
|
|
|
Config ociv1.ImageConfig
|
|
|
|
Architecture string
|
|
|
|
OS string
|
|
|
|
Size uint
|
|
|
|
VirtualSize uint
|
2017-07-23 23:01:37 +00:00
|
|
|
GraphDriver driver.Data
|
2017-06-29 19:16:06 +00:00
|
|
|
RootFS ociv1.RootFS
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:39:16 +00:00
|
|
|
// ParseImageNames parses the names we've stored with an image into a list of
|
|
|
|
// tagged references and a list of references which contain digests.
|
|
|
|
func ParseImageNames(names []string) (tags, digests []string, err error) {
|
|
|
|
for _, name := range names {
|
|
|
|
if named, err := reference.ParseNamed(name); err == nil {
|
|
|
|
if digested, ok := named.(reference.Digested); ok {
|
|
|
|
canonical, err := reference.WithDigest(named, digested.Digest())
|
|
|
|
if err == nil {
|
|
|
|
digests = append(digests, canonical.String())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if reference.IsNameOnly(named) {
|
|
|
|
named = reference.TagNameOnly(named)
|
|
|
|
}
|
|
|
|
if tagged, ok := named.(reference.Tagged); ok {
|
|
|
|
namedTagged, err := reference.WithTag(named, tagged.Tag())
|
|
|
|
if err == nil {
|
|
|
|
tags = append(tags, namedTagged.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tags, digests, nil
|
|
|
|
}
|
|
|
|
|
2017-07-23 23:01:37 +00:00
|
|
|
// GetImageData gets the ImageData for a container with the given name in the given store.
|
|
|
|
func GetImageData(store storage.Store, name string) (*ImageData, error) {
|
|
|
|
img, err := FindImage(store, name)
|
2017-06-29 19:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading image %q", name)
|
|
|
|
}
|
|
|
|
|
2017-07-23 23:01:37 +00:00
|
|
|
cid, err := GetImageCopyData(store, name)
|
2017-06-29 19:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading image %q", name)
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:39:16 +00:00
|
|
|
tags, digests, err := ParseImageNames(img.Names)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing image names for %q", name)
|
|
|
|
}
|
|
|
|
|
2017-07-21 20:43:30 +00:00
|
|
|
driverName, err := driver.GetDriverName(store)
|
2017-06-29 19:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:33:41 +00:00
|
|
|
topLayerID := img.TopLayer
|
2017-07-25 19:39:16 +00:00
|
|
|
|
2017-07-21 20:43:30 +00:00
|
|
|
driverMetadata, err := driver.GetDriverMetadata(store, topLayerID)
|
2017-06-29 19:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-24 15:14:18 +00:00
|
|
|
layer, err := store.Layer(topLayerID)
|
2017-06-29 19:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-24 15:14:18 +00:00
|
|
|
size, err := store.DiffSize(layer.Parent, layer.ID)
|
2017-06-29 19:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-27 17:35:38 +00:00
|
|
|
_, digest, virtualSize, err := InfoAndDigestAndSize(store, *img)
|
2017-06-29 19:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-23 23:01:37 +00:00
|
|
|
return &ImageData{
|
2017-06-29 19:16:06 +00:00
|
|
|
ID: img.ID,
|
2017-07-25 19:39:16 +00:00
|
|
|
Tags: tags,
|
2017-06-29 19:16:06 +00:00
|
|
|
Digests: digests,
|
2017-07-25 19:33:41 +00:00
|
|
|
Digest: digest,
|
2017-06-29 19:16:06 +00:00
|
|
|
Parent: string(cid.Docker.Parent),
|
|
|
|
Comment: cid.OCIv1.History[0].Comment,
|
|
|
|
Created: cid.OCIv1.Created,
|
2017-07-25 19:23:43 +00:00
|
|
|
Container: cid.Docker.Container,
|
|
|
|
ContainerConfig: cid.Docker.ContainerConfig,
|
2017-06-29 19:16:06 +00:00
|
|
|
Author: cid.OCIv1.Author,
|
|
|
|
Config: cid.OCIv1.Config,
|
|
|
|
Architecture: cid.OCIv1.Architecture,
|
|
|
|
OS: cid.OCIv1.OS,
|
|
|
|
Size: uint(size),
|
|
|
|
VirtualSize: uint(virtualSize),
|
2017-07-23 23:01:37 +00:00
|
|
|
GraphDriver: driver.Data{
|
2017-06-29 19:16:06 +00:00
|
|
|
Name: driverName,
|
|
|
|
Data: driverMetadata,
|
|
|
|
},
|
|
|
|
RootFS: cid.OCIv1.RootFS,
|
|
|
|
}, nil
|
|
|
|
}
|