2017-07-23 23:01:37 +00:00
|
|
|
package image
|
2017-06-29 19:16:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/containers/storage"
|
2017-07-21 20:43:30 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/libkpod/driver"
|
2017-06-29 19:16:06 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
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
|
|
|
|
Names []string
|
|
|
|
Digests []digest.Digest
|
|
|
|
Parent string
|
|
|
|
Comment string
|
|
|
|
Created *time.Time
|
|
|
|
Container string
|
|
|
|
ContainerConfig containerConfig
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type containerConfig struct {
|
|
|
|
Hostname string
|
|
|
|
Domainname string
|
|
|
|
User string
|
|
|
|
AttachStdin bool
|
|
|
|
AttachStdout bool
|
|
|
|
AttachStderr bool
|
|
|
|
Tty bool
|
|
|
|
OpenStdin bool
|
|
|
|
StdinOnce bool
|
|
|
|
Env []string
|
|
|
|
Cmd []string
|
|
|
|
ArgsEscaped bool
|
|
|
|
Image digest.Digest
|
|
|
|
Volumes map[string]interface{}
|
|
|
|
WorkingDir string
|
|
|
|
Entrypoint []string
|
|
|
|
Labels interface{}
|
|
|
|
OnBuild []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type rootFS struct {
|
|
|
|
Type string
|
|
|
|
Layers []string
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
digests, err := getDigests(*img)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var bigData interface{}
|
|
|
|
ctrConfig := containerConfig{}
|
|
|
|
container := ""
|
|
|
|
if len(digests) > 0 {
|
|
|
|
bd, err := store.ImageBigData(img.ID, string(digests[len(digests)-1]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(bd, &bigData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
container = (bigData.(map[string]interface{})["container"]).(string)
|
|
|
|
cc, err := json.MarshalIndent((bigData.(map[string]interface{})["container_config"]).(map[string]interface{}), "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(cc, &ctrConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-23 23:01:37 +00:00
|
|
|
topLayerID, err := GetTopLayerID(*img)
|
2017-06-29 19:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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-23 23:01:37 +00:00
|
|
|
virtualSize, err := Size(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,
|
|
|
|
Names: img.Names,
|
|
|
|
Digests: digests,
|
|
|
|
Parent: string(cid.Docker.Parent),
|
|
|
|
Comment: cid.OCIv1.History[0].Comment,
|
|
|
|
Created: cid.OCIv1.Created,
|
|
|
|
Container: container,
|
|
|
|
ContainerConfig: ctrConfig,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDigests(img storage.Image) ([]digest.Digest, error) {
|
2017-07-23 23:01:37 +00:00
|
|
|
metadata, err := ParseMetadata(img)
|
2017-06-29 19:16:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
digests := []digest.Digest{}
|
|
|
|
for _, blob := range metadata.Blobs {
|
|
|
|
digests = append(digests, blob.Digest)
|
|
|
|
}
|
|
|
|
return digests, nil
|
|
|
|
}
|