move container-related functions out of kpod and into libkpod

Signed-off-by: Ryan Cole <rcyoalne@gmail.com>
This commit is contained in:
Ryan Cole 2017-07-21 22:07:40 -04:00
parent a68a981d0b
commit 2c1fd1ad3f
8 changed files with 65 additions and 54 deletions

View file

@ -84,22 +84,6 @@ func getCopyOptions(reportWriter io.Writer, signaturePolicyPath string, srcDocke
}
}
func findContainer(store storage.Store, container string) (*storage.Container, error) {
ctrStore, err := store.ContainerStore()
if err != nil {
return nil, err
}
return ctrStore.Get(container)
}
func getContainerTopLayerID(store storage.Store, containerID string) (string, error) {
ctr, err := findContainer(store, containerID)
if err != nil {
return "", err
}
return ctr.LayerID, nil
}
func getSystemContext(signaturePolicyPath string) *types.SystemContext {
sc := &types.SystemContext{}
if signaturePolicyPath != "" {
@ -163,30 +147,6 @@ func getRootFsSize(store storage.Store, containerID string) (int64, error) {
return size + layerSize, err
}
func getContainerRwSize(store storage.Store, containerID string) (int64, error) {
ctrStore, err := store.ContainerStore()
if err != nil {
return 0, err
}
container, err := ctrStore.Get(containerID)
if err != nil {
return 0, err
}
lstore, err := store.LayerStore()
if err != nil {
return 0, err
}
// Get the size of the top layer by calculating the size of the diff
// between the layer and its parent. The top layer of a container is
// the only RW layer, all others are immutable
layer, err := lstore.Get(container.LayerID)
if err != nil {
return 0, err
}
return lstore.DiffSize(layer.Parent, layer.ID)
}
func isTrue(str string) bool {
return str == "true"
}

View file

@ -9,6 +9,7 @@ import (
"github.com/containers/storage"
"github.com/kubernetes-incubator/cri-o/cmd/kpod/docker"
"github.com/kubernetes-incubator/cri-o/libkpod"
"github.com/kubernetes-incubator/cri-o/libkpod/driver"
"github.com/kubernetes-incubator/cri-o/oci"
"github.com/kubernetes-incubator/cri-o/pkg/annotations"
@ -76,7 +77,7 @@ func getContainerData(store storage.Store, name string, size bool) (*containerDa
if err != nil {
return nil, err
}
topLayer, err := getContainerTopLayerID(store, ctr.ID())
topLayer, err := libkpod.GetContainerTopLayerID(store, ctr.ID())
if err != nil {
return nil, err
}
@ -122,7 +123,7 @@ func getContainerData(store storage.Store, name string, size bool) (*containerDa
return nil, errors.Wrapf(err, "error reading size for container %q", name)
}
data.SizeRootFs = uint(sizeRootFs)
sizeRw, err := getContainerRwSize(store, data.ID)
sizeRw, err := libkpod.GetContainerRwSize(store, data.ID)
if err != nil {
return nil, errors.Wrapf(err, "error reading RWSize for container %q", name)
}
@ -153,7 +154,7 @@ func inspectContainer(store storage.Store, container string) (*oci.Container, er
// get an oci.Container instance for a given container ID
func getOCIContainer(store storage.Store, container string) (*oci.Container, error) {
ctr, err := findContainer(store, container)
ctr, err := libkpod.FindContainer(store, container)
if err != nil {
return nil, err
}

View file

@ -16,6 +16,7 @@ import (
"github.com/containers/storage/pkg/archive"
"github.com/docker/docker/pkg/ioutils"
"github.com/kubernetes-incubator/cri-o/cmd/kpod/docker"
libkpodimage "github.com/kubernetes-incubator/cri-o/libkpod/image"
digest "github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go/v1"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
@ -378,7 +379,7 @@ func openImage(store storage.Store, image string) (*containerImageData, error) {
if image == "" {
return nil, errors.Errorf("image name must be specified")
}
img, err := findImage(store, image)
img, err := libkpodimage.FindImage(store, image)
if err != nil {
return nil, errors.Wrapf(err, "error locating image %q for importing settings", image)
}

View file

@ -6,7 +6,7 @@ import (
"github.com/containers/storage"
"github.com/kubernetes-incubator/cri-o/libkpod/driver"
"github.com/kubernetes-incubator/cri-o/libkpod/image"
libkpodimage "github.com/kubernetes-incubator/cri-o/libkpod/image"
digest "github.com/opencontainers/go-digest"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
@ -58,7 +58,7 @@ type rootFS struct {
}
func getImageData(store storage.Store, name string) (*imageData, error) {
img, err := findImage(store, name)
img, err := libkpodimage.FindImage(store, name)
if err != nil {
return nil, errors.Wrapf(err, "error reading image %q", name)
}
@ -101,7 +101,7 @@ func getImageData(store storage.Store, name string) (*imageData, error) {
return nil, err
}
topLayerID, err := image.GetTopLayerID(*img)
topLayerID, err := libkpodimage.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 := image.Size(store, *img)
virtualSize, err := libkpodimage.Size(store, *img)
if err != nil {
return nil, err
}
@ -152,7 +152,7 @@ func getImageData(store storage.Store, name string) (*imageData, error) {
}
func getDigests(img storage.Image) ([]digest.Digest, error) {
metadata, err := image.ParseMetadata(img)
metadata, err := libkpodimage.ParseMetadata(img)
if err != nil {
return nil, err
}

View file

@ -12,6 +12,7 @@ import (
"github.com/containers/image/types"
"github.com/containers/storage"
"github.com/containers/storage/pkg/archive"
libkpodimage "github.com/kubernetes-incubator/cri-o/libkpod/image"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
@ -166,7 +167,7 @@ func pushImage(srcName, destName string, options pushOptions) error {
defer policyContext.Destroy()
// Look up the image name and its layer, then build the imagePushData from
// the image
img, err := findImage(options.Store, srcName)
img, err := libkpodimage.FindImage(options.Store, srcName)
if err != nil {
return errors.Wrapf(err, "error locating image %q for importing settings", srcName)
}

View file

@ -3,6 +3,7 @@ package main
import (
"github.com/containers/image/docker/reference"
"github.com/containers/storage"
libkpodimage "github.com/kubernetes-incubator/cri-o/libkpod/image"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
@ -27,7 +28,7 @@ func tagCmd(c *cli.Context) error {
if err != nil {
return err
}
img, err := findImage(store, args[0])
img, err := libkpodimage.FindImage(store, args[0])
if err != nil {
return err
}

48
libkpod/container.go Normal file
View file

@ -0,0 +1,48 @@
package libkpod
import (
cstorage "github.com/containers/storage"
)
// FindContainer searches for a container with the given name or ID in the given store
func FindContainer(store cstorage.Store, container string) (*cstorage.Container, error) {
ctrStore, err := store.ContainerStore()
if err != nil {
return nil, err
}
return ctrStore.Get(container)
}
// GetContainerTopLayerID gets the ID of the top layer of the given container
func GetContainerTopLayerID(store cstorage.Store, containerID string) (string, error) {
ctr, err := FindContainer(store, containerID)
if err != nil {
return "", err
}
return ctr.LayerID, nil
}
// GetContainerRwSize Gets the size of the mutable top layer of the container
func GetContainerRwSize(store cstorage.Store, containerID string) (int64, error) {
ctrStore, err := store.ContainerStore()
if err != nil {
return 0, err
}
container, err := ctrStore.Get(containerID)
if err != nil {
return 0, err
}
lstore, err := store.LayerStore()
if err != nil {
return 0, err
}
// Get the size of the top layer by calculating the size of the diff
// between the layer and its parent. The top layer of a container is
// the only RW layer, all others are immutable
layer, err := lstore.Get(container.LayerID)
if err != nil {
return 0, err
}
return lstore.DiffSize(layer.Parent, layer.ID)
}

View file

@ -3,7 +3,6 @@ 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"
)
@ -46,8 +45,8 @@ func Size(store storage.Store, img storage.Image) (int64, error) {
}
// TopLayer returns the ID of the top layer of the image
func getTopLayerID(img storage.Image) (string, error) {
metadata, err := image.ParseMetadata(img)
func GetTopLayerID(img storage.Image) (string, error) {
metadata, err := ParseMetadata(img)
if err != nil {
return "", err
}