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

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
}