Avoid duplicate image configuration parsing logic

Don't bother trying to find and parse the image's configuration blob
after we've already done it; just reuse the value.  This frees us from
making the assumption that the last blob which was committed to local
storage was the image's configuration blob.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
Nalin Dahyabhai 2017-07-25 15:23:43 -04:00
parent 8957156c41
commit 7e9ac9700b

View file

@ -1,13 +1,12 @@
package image
import (
"encoding/json"
"time"
"github.com/containers/image/docker/reference"
"github.com/containers/storage"
"github.com/kubernetes-incubator/cri-o/cmd/kpod/docker"
"github.com/kubernetes-incubator/cri-o/libkpod/driver"
digest "github.com/opencontainers/go-digest"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)
@ -22,7 +21,7 @@ type ImageData struct {
Comment string
Created *time.Time
Container string
ContainerConfig containerConfig
ContainerConfig docker.Config
Author string
Config ociv1.ImageConfig
Architecture string
@ -33,27 +32,6 @@ type ImageData struct {
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
@ -96,34 +74,6 @@ func GetImageData(store storage.Store, name string) (*ImageData, error) {
if err != nil {
return nil, errors.Wrapf(err, "error reading image %q", name)
}
blobDigests, err := getDigests(*img)
if err != nil {
return nil, err
}
var bigData interface{}
ctrConfig := containerConfig{}
container := ""
if len(blobDigests) > 0 {
bd, err := store.ImageBigData(img.ID, string(blobDigests[len(blobDigests)-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
}
}
tags, digests, err := ParseImageNames(img.Names)
if err != nil {
@ -166,8 +116,8 @@ func GetImageData(store storage.Store, name string) (*ImageData, error) {
Parent: string(cid.Docker.Parent),
Comment: cid.OCIv1.History[0].Comment,
Created: cid.OCIv1.Created,
Container: container,
ContainerConfig: ctrConfig,
Container: cid.Docker.Container,
ContainerConfig: cid.Docker.ContainerConfig,
Author: cid.OCIv1.Author,
Config: cid.OCIv1.Config,
Architecture: cid.OCIv1.Architecture,
@ -181,15 +131,3 @@ func GetImageData(store storage.Store, name string) (*ImageData, error) {
RootFS: cid.OCIv1.RootFS,
}, nil
}
func getDigests(img storage.Image) ([]digest.Digest, error) {
metadata, err := ParseMetadata(img)
if err != nil {
return nil, err
}
digests := []digest.Digest{}
for _, blob := range metadata.Blobs {
digests = append(digests, blob.Digest)
}
return digests, nil
}