move image-related functions out of cmd/kpod/common.go and into libkpod/image

Signed-off-by: Ryan Cole <rcyoalne@gmail.com>
This commit is contained in:
Ryan Cole 2017-07-21 16:49:50 -04:00
parent 95e17b4a73
commit a68a981d0b
5 changed files with 60 additions and 74 deletions

View file

@ -9,7 +9,6 @@ import (
is "github.com/containers/image/storage"
"github.com/containers/image/types"
"github.com/containers/storage"
"github.com/kubernetes-incubator/cri-o/libkpod/image"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
@ -67,25 +66,6 @@ func getStore(c *cli.Context) (storage.Store, error) {
return store, nil
}
func findImage(store storage.Store, image string) (*storage.Image, error) {
var img *storage.Image
ref, err := is.Transport.ParseStoreReference(store, image)
if err == nil {
img, err = is.Transport.GetStoreImage(store, ref)
}
if err != nil {
img2, err2 := store.Image(image)
if err2 != nil {
if ref == nil {
return nil, errors.Wrapf(err, "error parsing reference to image %q", image)
}
return nil, errors.Wrapf(err, "unable to locate image %q", image)
}
img = img2
}
return img, nil
}
func getCopyOptions(reportWriter io.Writer, signaturePolicyPath string, srcDockerRegistry, destDockerRegistry *dockerRegistryOptions, signing signingOptions) *cp.Options {
if srcDockerRegistry == nil {
srcDockerRegistry = &dockerRegistryOptions{}
@ -219,34 +199,6 @@ func isValidBool(str string) bool {
return isTrue(str) || isFalse(str)
}
func getImageSize(img storage.Image, store storage.Store) (int64, error) {
is.Transport.SetStore(store)
storeRef, err := is.Transport.ParseStoreReference(store, "@"+img.ID)
if err != nil {
return -1, err
}
imgRef, err := storeRef.NewImage(nil)
if err != nil {
return -1, err
}
imgSize, err := imgRef.Size()
if err != nil {
return -1, err
}
return imgSize, nil
}
func getImageTopLayer(img storage.Image) (string, error) {
metadata, err := image.ParseMetadata(img)
if err != nil {
return "", err
}
// Get the digest of the first blob
digest := string(metadata.Blobs[0].Digest)
// Return the first layer associated with the given digest
return metadata.Layers[digest][0], nil
}
func getPolicyContext(path string) (*signature.PolicyContext, error) {
policy, err := signature.DefaultPolicy(&types.SystemContext{SignaturePolicyPath: path})
if err != nil {

View file

@ -7,7 +7,6 @@ import (
"flag"
is "github.com/containers/image/storage"
"github.com/containers/storage"
"github.com/urfave/cli"
)
@ -30,28 +29,6 @@ func TestGetStore(t *testing.T) {
}
}
func TestGetSize(t *testing.T) {
// Make sure the tests are running as root
failTestIfNotRoot(t)
store, err := storage.GetStore(storage.DefaultStoreOptions)
if err != nil {
t.Fatal(err)
} else if store != nil {
is.Transport.SetStore(store)
}
images, err := store.Images()
if err != nil {
t.Fatalf("Error reading images: %v", err)
}
_, err = getImageSize(images[0], store)
if err != nil {
t.Error(err)
}
}
func failTestIfNotRoot(t *testing.T) {
u, err := user.Current()
if err != nil {

View file

@ -101,7 +101,7 @@ func getImageData(store storage.Store, name string) (*imageData, error) {
return nil, err
}
topLayerID, err := getImageTopLayer(*img)
topLayerID, err := image.GetTopLayerID(*img)
if err != nil {
return nil, err
}
@ -123,7 +123,7 @@ func getImageData(store storage.Store, name string) (*imageData, error) {
return nil, err
}
virtualSize, err := getImageSize(*img, store)
virtualSize, err := image.Size(store, *img)
if err != nil {
return nil, err
}

View file

@ -201,7 +201,7 @@ func outputImages(images []storage.Image, format string, store storage.Store, fi
if len(imageMetadata.Blobs) > 0 {
digest = string(imageMetadata.Blobs[0].Digest)
}
size, _ := getImageSize(img, store)
size, _ := image.Size(store, img)
names := []string{""}
if len(img.Names) > 0 {

View file

@ -1 +1,58 @@
package image
import (
is "github.com/containers/image/storage"
"github.com/containers/storage"
"github.com/kubernetes-incubator/cri-o/libkpod/image"
"github.com/pkg/errors"
)
// FindImage searches for an image with a matching the given name or ID in the given store
func FindImage(store storage.Store, image string) (*storage.Image, error) {
var img *storage.Image
ref, err := is.Transport.ParseStoreReference(store, image)
if err == nil {
img, err = is.Transport.GetStoreImage(store, ref)
}
if err != nil {
img2, err2 := store.Image(image)
if err2 != nil {
if ref == nil {
return nil, errors.Wrapf(err, "error parsing reference to image %q", image)
}
return nil, errors.Wrapf(err, "unable to locate image %q", image)
}
img = img2
}
return img, nil
}
// Size returns the size of the image in the given store
func Size(store storage.Store, img storage.Image) (int64, error) {
is.Transport.SetStore(store)
storeRef, err := is.Transport.ParseStoreReference(store, "@"+img.ID)
if err != nil {
return -1, err
}
imgRef, err := storeRef.NewImage(nil)
if err != nil {
return -1, err
}
imgSize, err := imgRef.Size()
if err != nil {
return -1, err
}
return imgSize, nil
}
// TopLayer returns the ID of the top layer of the image
func getTopLayerID(img storage.Image) (string, error) {
metadata, err := image.ParseMetadata(img)
if err != nil {
return "", err
}
// Get the digest of the first blob
digest := string(metadata.Blobs[0].Digest)
// Return the first layer associated with the given digest
return metadata.Layers[digest][0], nil
}