Move functions in libkpod to ContainerServer

Signed-off-by: Ryan Cole <rcyoalne@gmail.com>
This commit is contained in:
Ryan Cole 2017-08-01 11:28:50 -04:00
parent da176cd379
commit b1eb754ef5
3 changed files with 39 additions and 116 deletions

View file

@ -81,7 +81,7 @@ func inspectCmd(c *cli.Context) error {
if err != nil {
return errors.Wrapf(err, "Could not get config")
}
store, err := getStore(config)
server, err := libkpod.New(config)
if err != nil {
return err
}
@ -89,19 +89,19 @@ func inspectCmd(c *cli.Context) error {
var data interface{}
switch itemType {
case inspectTypeContainer:
data, err = libkpod.GetContainerData(store, name, size)
data, err = server.GetContainerData(name, size)
if err != nil {
return errors.Wrapf(err, "error parsing container data")
}
case inspectTypeImage:
data, err = libkpodimage.GetImageData(store, name)
data, err = libkpodimage.GetImageData(server.Store(), name)
if err != nil {
return errors.Wrapf(err, "error parsing image data")
}
case inspectAll:
ctrData, err := libkpod.GetContainerData(store, name, size)
ctrData, err := server.GetContainerData(name, size)
if err != nil {
imgData, err := libkpodimage.GetImageData(store, name)
imgData, err := libkpodimage.GetImageData(server.Store(), name)
if err != nil {
return errors.Wrapf(err, "error parsing image data")
}

View file

@ -10,14 +10,18 @@ import (
"github.com/pkg/errors"
)
// 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) {
return store.Container(container)
// GetStorageContainer searches for a container with the given name or ID in the given store
func (c *ContainerServer) GetStorageContainer(container string) (*cstorage.Container, error) {
ociCtr, err := c.LookupContainer(container)
if err != nil {
return nil, err
}
return c.store.Container(ociCtr.ID())
}
// 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)
func (c *ContainerServer) GetContainerTopLayerID(containerID string) (string, error) {
ctr, err := c.GetStorageContainer(containerID)
if err != nil {
return "", err
}
@ -25,8 +29,8 @@ 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) {
container, err := store.Container(containerID)
func (c *ContainerServer) GetContainerRwSize(containerID string) (int64, error) {
container, err := c.store.Container(containerID)
if err != nil {
return 0, err
}
@ -34,42 +38,42 @@ 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 := store.Layer(container.LayerID)
layer, err := c.store.Layer(container.LayerID)
if err != nil {
return 0, err
}
return store.DiffSize(layer.Parent, layer.ID)
return c.store.DiffSize(layer.Parent, layer.ID)
}
// GetContainerRootFsSize gets the size of the container's root filesystem
// A container FS is split into two parts. The first is the top layer, a
// 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) {
container, err := store.Container(containerID)
func (c *ContainerServer) GetContainerRootFsSize(containerID string) (int64, error) {
container, err := c.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 := store.Layer(container.LayerID)
rwLayer, err := c.store.Layer(container.LayerID)
if err != nil {
return 0, err
}
layer, err := store.Layer(rwLayer.Parent)
layer, err := c.store.Layer(rwLayer.Parent)
if err != nil {
return 0, err
}
size := int64(0)
for layer.Parent != "" {
layerSize, err := store.DiffSize(layer.Parent, layer.ID)
layerSize, err := c.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 = store.Layer(layer.Parent)
layer, err = c.store.Layer(layer.Parent)
if err != nil {
return 0, err
}
@ -77,7 +81,7 @@ 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 := store.DiffSize(layer.Parent, layer.ID)
layerSize, err := c.store.DiffSize(layer.Parent, layer.ID)
return size + layerSize, err
}

View file

@ -3,17 +3,13 @@ package libkpod
import (
"encoding/json"
"os"
"time"
"k8s.io/apimachinery/pkg/fields"
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
"github.com/containers/storage"
"github.com/kubernetes-incubator/cri-o/libkpod/common"
"github.com/kubernetes-incubator/cri-o/libkpod/driver"
libkpodimage "github.com/kubernetes-incubator/cri-o/libkpod/image"
"github.com/kubernetes-incubator/cri-o/oci"
"github.com/kubernetes-incubator/cri-o/pkg/annotations"
"github.com/opencontainers/image-spec/specs-go/v1"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
@ -55,12 +51,12 @@ type driverData struct {
// GetContainerData gets the ContainerData for a container with the given name in the given store.
// If size is set to true, it will also determine the size of the container
func GetContainerData(store storage.Store, name string, size bool) (*ContainerData, error) {
ctr, err := inspectContainer(store, name)
func (c *ContainerServer) GetContainerData(name string, size bool) (*ContainerData, error) {
ctr, err := c.inspectContainer(name)
if err != nil {
return nil, errors.Wrapf(err, "error reading build container %q", name)
}
container, err := store.Container(name)
container, err := c.store.Container(name)
if err != nil {
return nil, errors.Wrapf(err, "error reading container data")
}
@ -68,7 +64,7 @@ func GetContainerData(store storage.Store, name string, size bool) (*ContainerDa
// The runtime configuration won't exist if the container has never been started by cri-o or kpod,
// so treat a not-exist error as non-fatal.
m := getBlankSpec()
config, err := store.FromContainerDirectory(ctr.ID(), "config.json")
config, err := c.store.FromContainerDirectory(ctr.ID(), "config.json")
if err != nil && !os.IsNotExist(errors.Cause(err)) {
return nil, err
}
@ -81,24 +77,24 @@ func GetContainerData(store storage.Store, name string, size bool) (*ContainerDa
if container.ImageID == "" {
return nil, errors.Errorf("error reading container image data: container is not based on an image")
}
imageData, err := libkpodimage.GetImageData(store, container.ImageID)
imageData, err := libkpodimage.GetImageData(c.store, container.ImageID)
if err != nil {
return nil, errors.Wrapf(err, "error reading container image data")
}
driverName, err := driver.GetDriverName(store)
driverName, err := driver.GetDriverName(c.store)
if err != nil {
return nil, err
}
topLayer, err := GetContainerTopLayerID(store, ctr.ID())
topLayer, err := c.GetContainerTopLayerID(ctr.ID())
if err != nil {
return nil, err
}
layer, err := store.Layer(topLayer)
layer, err := c.store.Layer(topLayer)
if err != nil {
return nil, err
}
driverMetadata, err := driver.GetDriverMetadata(store, topLayer)
driverMetadata, err := driver.GetDriverMetadata(c.store, topLayer)
if err != nil {
return nil, err
}
@ -138,13 +134,13 @@ func GetContainerData(store storage.Store, name string, size bool) (*ContainerDa
}
if size {
sizeRootFs, err := GetContainerRootFsSize(store, data.ID)
sizeRootFs, err := c.GetContainerRootFsSize(data.ID)
if err != nil {
return nil, errors.Wrapf(err, "error reading size for container %q", name)
}
data.SizeRootFs = uint(sizeRootFs)
sizeRw, err := GetContainerRwSize(store, data.ID)
sizeRw, err := c.GetContainerRwSize(data.ID)
if err != nil {
return nil, errors.Wrapf(err, "error reading RWSize for container %q", name)
}
@ -155,18 +151,13 @@ func GetContainerData(store storage.Store, name string, size bool) (*ContainerDa
}
// Get an oci.Container and update its status
func inspectContainer(store storage.Store, container string) (*oci.Container, error) {
ociCtr, err := getOCIContainer(store, container)
if err != nil {
return nil, err
}
// call oci.New() to get the runtime
runtime, err := getOCIRuntime(store, container)
func (c *ContainerServer) inspectContainer(container string) (*oci.Container, error) {
ociCtr, err := c.LookupContainer(container)
if err != nil {
return nil, err
}
// call runtime.UpdateStatus()
err = runtime.UpdateStatus(ociCtr)
err = c.Runtime().UpdateStatus(ociCtr)
if err != nil {
return nil, err
}
@ -185,75 +176,3 @@ func getBlankSpec() specs.Spec {
Windows: &specs.Windows{},
}
}
// 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)
if err != nil {
return nil, err
}
// The runtime configuration won't exist if the container has never been started by cri-o or kpod,
// so treat a not-exist error as non-fatal.
m := getBlankSpec()
config, err := store.FromContainerDirectory(ctr.ID, "config.json")
if err != nil && !os.IsNotExist(errors.Cause(err)) {
return nil, err
}
if len(config) > 0 {
if err = json.Unmarshal(config, &m); err != nil {
return nil, err
}
}
labels := make(map[string]string)
err = json.Unmarshal([]byte(m.Annotations[annotations.Labels]), &labels)
if len(m.Annotations[annotations.Labels]) > 0 && err != nil {
return nil, err
}
name := ctr.Names[0]
var metadata pb.ContainerMetadata
err = json.Unmarshal([]byte(m.Annotations[annotations.Metadata]), &metadata)
if len(m.Annotations[annotations.Metadata]) > 0 && err != nil {
return nil, err
}
tty := common.IsTrue(m.Annotations[annotations.TTY])
stdin := common.IsTrue(m.Annotations[annotations.Stdin])
stdinOnce := common.IsTrue(m.Annotations[annotations.StdinOnce])
containerPath, err := store.ContainerRunDirectory(ctr.ID)
if err != nil {
return nil, err
}
containerDir, err := store.ContainerDirectory(ctr.ID)
if err != nil {
return nil, err
}
img, _ := m.Annotations[annotations.Image]
kubeAnnotations := make(map[string]string)
err = json.Unmarshal([]byte(m.Annotations[annotations.Annotations]), &kubeAnnotations)
if len(m.Annotations[annotations.Annotations]) > 0 && err != nil {
return nil, err
}
created := time.Time{}
if len(m.Annotations[annotations.Created]) > 0 {
created, err = time.Parse(time.RFC3339Nano, m.Annotations[annotations.Created])
if err != nil {
return nil, err
}
}
// create a new OCI Container. kpod currently doesn't deal with pod sandboxes, so the fields for netns, privileged, and trusted are left empty
return oci.NewContainer(ctr.ID, name, containerPath, m.Annotations[annotations.LogPath], nil, labels, kubeAnnotations, img, &metadata, ctr.ImageID, tty, stdin, stdinOnce, false, false, containerDir, created, m.Annotations["org.opencontainers.image.stopSignal"])
}
func getOCIRuntime(store storage.Store, container string) (*oci.Runtime, error) {
// TODO: Move server default config out of server so that it can be used instead of this
return oci.New("/usr/bin/runc", "", "runtime", "/usr/local/libexec/crio/conmon", []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, "cgroupfs")
}