Avoid using lower-level storage APIs

Switch from using the lower-level storage APIs (accessing LayerStore,
ImageStore, and ContainerStore types directly) in favor of the
higher-level ones that take care of synchronization and locking for us.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
Nalin Dahyabhai 2017-07-24 11:14:18 -04:00
parent 065960386f
commit 2e50006f1c
5 changed files with 31 additions and 95 deletions

View file

@ -7,11 +7,7 @@ import (
// 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)
return store.Container(container)
}
// GetContainerTopLayerID gets the ID of the top layer of the given container
@ -25,15 +21,7 @@ func GetContainerTopLayerID(store cstorage.Store, containerID string) (string, e
// 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()
container, err := store.Container(containerID)
if err != nil {
return 0, err
}
@ -41,11 +29,11 @@ func GetContainerRwSize(store cstorage.Store, containerID string) (int64, error)
// 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)
layer, err := store.Layer(container.LayerID)
if err != nil {
return 0, err
}
return lstore.DiffSize(layer.Parent, layer.ID)
return store.DiffSize(layer.Parent, layer.ID)
}
// GetContainerRootFsSize gets the size of the container's root filesystem
@ -53,38 +41,30 @@ func GetContainerRwSize(store cstorage.Store, containerID string) (int64, error)
// mutable layer, and the rest is the RootFS: the set of immutable layers
// that make up the image on which the container is based
func GetContainerRootFsSize(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()
container, err := store.Container(containerID)
if err != nil {
return 0, err
}
// Ignore the size of the top layer. The top layer is a mutable RW layer
// and is not considered a part of the rootfs
rwLayer, err := lstore.Get(container.LayerID)
rwLayer, err := store.Layer(container.LayerID)
if err != nil {
return 0, err
}
layer, err := lstore.Get(rwLayer.Parent)
layer, err := store.Layer(rwLayer.Parent)
if err != nil {
return 0, err
}
size := int64(0)
for layer.Parent != "" {
layerSize, err := lstore.DiffSize(layer.Parent, layer.ID)
layerSize, err := store.DiffSize(layer.Parent, layer.ID)
if err != nil {
return size, errors.Wrapf(err, "getting diffsize of layer %q and its parent %q", layer.ID, layer.Parent)
}
size += layerSize
layer, err = lstore.Get(layer.Parent)
layer, err = store.Layer(layer.Parent)
if err != nil {
return 0, err
}
@ -92,6 +72,6 @@ func GetContainerRootFsSize(store cstorage.Store, containerID string) (int64, er
// Get the size of the last layer. Has to be outside of the loop
// because the parent of the last layer is "", andlstore.Get("")
// will return an error
layerSize, err := lstore.DiffSize(layer.Parent, layer.ID)
layerSize, err := store.DiffSize(layer.Parent, layer.ID)
return size + layerSize, err
}