diff --git a/cmd/kpod/common.go b/cmd/kpod/common.go index b5d841aa..a5dfe75b 100644 --- a/cmd/kpod/common.go +++ b/cmd/kpod/common.go @@ -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 { diff --git a/cmd/kpod/common_test.go b/cmd/kpod/common_test.go index 08e23176..711c8c3e 100644 --- a/cmd/kpod/common_test.go +++ b/cmd/kpod/common_test.go @@ -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 { diff --git a/cmd/kpod/imageData.go b/cmd/kpod/imageData.go index 502bac22..7b38d007 100644 --- a/cmd/kpod/imageData.go +++ b/cmd/kpod/imageData.go @@ -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 } diff --git a/cmd/kpod/images.go b/cmd/kpod/images.go index be58bc0b..678af48b 100644 --- a/cmd/kpod/images.go +++ b/cmd/kpod/images.go @@ -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 { diff --git a/libkpod/image/image.go b/libkpod/image/image.go index 1da0d734..cfcdb1ec 100644 --- a/libkpod/image/image.go +++ b/libkpod/image/image.go @@ -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 +}