Add errors listing to libpod

Signed-off-by: Matthew Heon <mheon@redhat.com>
This commit is contained in:
Matthew Heon 2017-09-11 11:27:40 -04:00
parent f8e48aad29
commit ae5634f8dd
6 changed files with 105 additions and 60 deletions

View file

@ -1,20 +1,15 @@
package libpod package libpod
import ( import (
"fmt"
"sync" "sync"
"github.com/containers/storage" "github.com/containers/storage"
"github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/stringid"
spec "github.com/opencontainers/runtime-spec/specs-go" spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/ulule/deepcopier" "github.com/ulule/deepcopier"
) )
var (
// ErrNotImplemented indicates that functionality is not yet implemented
errNotImplemented = fmt.Errorf("NOT IMPLEMENTED")
)
// Container is a single OCI container // Container is a single OCI container
type Container struct { type Container struct {
id string id string
@ -60,7 +55,7 @@ func (c *Container) Spec() *spec.Spec {
// Make a new container // Make a new container
func newContainer(rspec *spec.Spec) (*Container, error) { func newContainer(rspec *spec.Spec) (*Container, error) {
if rspec == nil { if rspec == nil {
return nil, fmt.Errorf("must provide a valid spec to construct container") return nil, errors.Wrapf(ErrInvalidArg, "must provide a valid runtime spec to create container")
} }
ctr := new(Container) ctr := new(Container)
@ -75,52 +70,52 @@ func newContainer(rspec *spec.Spec) (*Container, error) {
// Create creates a container in the OCI runtime // Create creates a container in the OCI runtime
func (c *Container) Create() error { func (c *Container) Create() error {
return errNotImplemented return ErrNotImplemented
} }
// Start starts a container // Start starts a container
func (c *Container) Start() error { func (c *Container) Start() error {
return errNotImplemented return ErrNotImplemented
} }
// Stop stops a container // Stop stops a container
func (c *Container) Stop() error { func (c *Container) Stop() error {
return errNotImplemented return ErrNotImplemented
} }
// Kill sends a signal to a container // Kill sends a signal to a container
func (c *Container) Kill(signal uint) error { func (c *Container) Kill(signal uint) error {
return errNotImplemented return ErrNotImplemented
} }
// Exec starts a new process inside the container // Exec starts a new process inside the container
// Returns fully qualified URL of streaming server for executed process // Returns fully qualified URL of streaming server for executed process
func (c *Container) Exec(cmd []string, tty bool, stdin bool) (string, error) { func (c *Container) Exec(cmd []string, tty bool, stdin bool) (string, error) {
return "", errNotImplemented return "", ErrNotImplemented
} }
// Attach attaches to a container // Attach attaches to a container
// Returns fully qualified URL of streaming server for the container // Returns fully qualified URL of streaming server for the container
func (c *Container) Attach(stdin, tty bool) (string, error) { func (c *Container) Attach(stdin, tty bool) (string, error) {
return "", errNotImplemented return "", ErrNotImplemented
} }
// Mount mounts a container's filesystem on the host // Mount mounts a container's filesystem on the host
// The path where the container has been mounted is returned // The path where the container has been mounted is returned
func (c *Container) Mount() (string, error) { func (c *Container) Mount() (string, error) {
return "", errNotImplemented return "", ErrNotImplemented
} }
// Status gets a container's status // Status gets a container's status
// TODO this should return relevant information about container state // TODO this should return relevant information about container state
func (c *Container) Status() error { func (c *Container) Status() error {
return errNotImplemented return ErrNotImplemented
} }
// Export exports a container's root filesystem as a tar archive // Export exports a container's root filesystem as a tar archive
// The archive will be saved as a file at the given path // The archive will be saved as a file at the given path
func (c *Container) Export(path string) error { func (c *Container) Export(path string) error {
return errNotImplemented return ErrNotImplemented
} }
// Commit commits the changes between a container and its image, creating a new // Commit commits the changes between a container and its image, creating a new
@ -130,5 +125,5 @@ func (c *Container) Export(path string) error {
// a new base image will be created from the contents of the container's // a new base image will be created from the contents of the container's
// filesystem // filesystem
func (c *Container) Commit() (*storage.Image, error) { func (c *Container) Commit() (*storage.Image, error) {
return nil, errNotImplemented return nil, ErrNotImplemented
} }

53
libpod/errors.go Normal file
View file

@ -0,0 +1,53 @@
package libpod
import (
"errors"
)
var (
// ErrNoSuchCtr indicates the requested container does not exist
ErrNoSuchCtr = errors.New("no such container")
// ErrNoSuchPod indicates the requested pod does not exist
ErrNoSuchPod = errors.New("no such pod")
// ErrNoSuchImage indicates the requested image does not exist
ErrNoSuchImage = errors.New("no such image")
// ErrCtrExists indicates a container with the same name or ID already
// exists
ErrCtrExists = errors.New("container already exists")
// ErrPodExists indicates a pod with the same name or ID already exists
ErrPodExists = errors.New("pod already exists")
// ErrImageExists indicated an image with the same ID already exists
ErrImageExists = errors.New("image already exists")
// ErrRuntimeFinalized indicates that the runtime has already been
// created and cannot be modified
ErrRuntimeFinalized = errors.New("runtime has been finalized")
// ErrCtrFinalized indicates that the container has already been created
// and cannot be modified
ErrCtrFinalized = errors.New("container has been finalized")
// ErrPodFinalized indicates that the pod has already been created and
// cannot be modified
ErrPodFinalized = errors.New("pod has been finalized")
// ErrInvalidArg indicates that an invalid argument was passed
ErrInvalidArg = errors.New("invalid argument")
// ErrRuntimeStopped indicates that the runtime has already been shut
// down and no further operations can be performed on it
ErrRuntimeStopped = errors.New("runtime has already been stopped")
// ErrCtrStopped indicates that the requested container is not running
// and the requested operation cannot be performed until it is started
ErrCtrStopped = errors.New("container is stopped")
// ErrCtrRemoved indicates that the container has already been removed
// and no further operations can be performed on it
ErrCtrRemoved = errors.New("container has already been removed")
// ErrPodRemoved indicates that the pod has already been removed and no
// further operations can be performed on it
ErrPodRemoved = errors.New("pod has already been removed")
// ErrNotImplemented indicates that the requested functionality is not
// yet present
ErrNotImplemented = errors.New("not yet implemented")
)

View file

@ -353,10 +353,10 @@ func (r *Runtime) GetImageRef(image string) (types.Image, error) {
// output. Multiple filters are handled by ANDing their output, so only images // output. Multiple filters are handled by ANDing their output, so only images
// matching all filters are included // matching all filters are included
func (r *Runtime) GetImages(filter ...ImageFilter) ([]*storage.Image, error) { func (r *Runtime) GetImages(filter ...ImageFilter) ([]*storage.Image, error) {
return nil, errNotImplemented return nil, ErrNotImplemented
} }
// ImportImage imports an OCI format image archive into storage as an image // ImportImage imports an OCI format image archive into storage as an image
func (r *Runtime) ImportImage(path string) (*storage.Image, error) { func (r *Runtime) ImportImage(path string) (*storage.Image, error) {
return nil, errNotImplemented return nil, ErrNotImplemented
} }

View file

@ -9,9 +9,7 @@ import (
) )
var ( var (
errRuntimeFinalized = fmt.Errorf("runtime has already been finalized") ctrNotImplemented = func(c *Container) error {
errCtrFinalized = fmt.Errorf("container has already been finalized")
ctrNotImplemented = func(c *Container) error {
return fmt.Errorf("NOT IMPLEMENTED") return fmt.Errorf("NOT IMPLEMENTED")
} }
) )
@ -39,7 +37,7 @@ const (
func WithStorageConfig(config storage.StoreOptions) RuntimeOption { func WithStorageConfig(config storage.StoreOptions) RuntimeOption {
return func(rt *Runtime) error { return func(rt *Runtime) error {
if rt.valid { if rt.valid {
return errRuntimeFinalized return ErrRuntimeFinalized
} }
rt.config.StorageConfig.RunRoot = config.RunRoot rt.config.StorageConfig.RunRoot = config.RunRoot
@ -65,7 +63,7 @@ func WithStorageConfig(config storage.StoreOptions) RuntimeOption {
func WithImageConfig(defaultTransport string, insecureRegistries, registries []string) RuntimeOption { func WithImageConfig(defaultTransport string, insecureRegistries, registries []string) RuntimeOption {
return func(rt *Runtime) error { return func(rt *Runtime) error {
if rt.valid { if rt.valid {
return errRuntimeFinalized return ErrRuntimeFinalized
} }
rt.config.ImageDefaultTransport = defaultTransport rt.config.ImageDefaultTransport = defaultTransport
@ -87,7 +85,7 @@ func WithImageConfig(defaultTransport string, insecureRegistries, registries []s
func WithSignaturePolicy(path string) RuntimeOption { func WithSignaturePolicy(path string) RuntimeOption {
return func(rt *Runtime) error { return func(rt *Runtime) error {
if rt.valid { if rt.valid {
return errRuntimeFinalized return ErrRuntimeFinalized
} }
rt.config.SignaturePolicyPath = path rt.config.SignaturePolicyPath = path
@ -100,7 +98,7 @@ func WithSignaturePolicy(path string) RuntimeOption {
func WithOCIRuntime(runtimePath string) RuntimeOption { func WithOCIRuntime(runtimePath string) RuntimeOption {
return func(rt *Runtime) error { return func(rt *Runtime) error {
if rt.valid { if rt.valid {
return errRuntimeFinalized return ErrRuntimeFinalized
} }
rt.config.RuntimePath = runtimePath rt.config.RuntimePath = runtimePath
@ -114,7 +112,7 @@ func WithOCIRuntime(runtimePath string) RuntimeOption {
func WithConmonPath(path string) RuntimeOption { func WithConmonPath(path string) RuntimeOption {
return func(rt *Runtime) error { return func(rt *Runtime) error {
if rt.valid { if rt.valid {
return errRuntimeFinalized return ErrRuntimeFinalized
} }
rt.config.ConmonPath = path rt.config.ConmonPath = path
@ -127,7 +125,7 @@ func WithConmonPath(path string) RuntimeOption {
func WithConmonEnv(environment []string) RuntimeOption { func WithConmonEnv(environment []string) RuntimeOption {
return func(rt *Runtime) error { return func(rt *Runtime) error {
if rt.valid { if rt.valid {
return errRuntimeFinalized return ErrRuntimeFinalized
} }
rt.config.ConmonEnvVars = make([]string, len(environment)) rt.config.ConmonEnvVars = make([]string, len(environment))
@ -142,7 +140,7 @@ func WithConmonEnv(environment []string) RuntimeOption {
func WithCgroupManager(manager string) RuntimeOption { func WithCgroupManager(manager string) RuntimeOption {
return func(rt *Runtime) error { return func(rt *Runtime) error {
if rt.valid { if rt.valid {
return errRuntimeFinalized return ErrRuntimeFinalized
} }
rt.config.CgroupManager = manager rt.config.CgroupManager = manager
@ -155,7 +153,7 @@ func WithCgroupManager(manager string) RuntimeOption {
func WithSELinux() RuntimeOption { func WithSELinux() RuntimeOption {
return func(rt *Runtime) error { return func(rt *Runtime) error {
if rt.valid { if rt.valid {
return errRuntimeFinalized return ErrRuntimeFinalized
} }
rt.config.SelinuxEnabled = true rt.config.SelinuxEnabled = true
@ -169,7 +167,7 @@ func WithSELinux() RuntimeOption {
func WithPidsLimit(limit int64) RuntimeOption { func WithPidsLimit(limit int64) RuntimeOption {
return func(rt *Runtime) error { return func(rt *Runtime) error {
if rt.valid { if rt.valid {
return errRuntimeFinalized return ErrRuntimeFinalized
} }
rt.config.PidsLimit = limit rt.config.PidsLimit = limit
@ -206,7 +204,7 @@ func WithSharedNamespaces(from *Container, namespaces map[string]string) CtrCrea
func (r *Runtime) WithPod(pod *Pod) CtrCreateOption { func (r *Runtime) WithPod(pod *Pod) CtrCreateOption {
return func(ctr *Container) error { return func(ctr *Container) error {
if !ctr.valid { if !ctr.valid {
return errCtrFinalized return ErrCtrFinalized
} }
if ctr.pod != nil { if ctr.pod != nil {
@ -217,7 +215,7 @@ func (r *Runtime) WithPod(pod *Pod) CtrCreateOption {
if err != nil { if err != nil {
return errors.Wrapf(err, "error searching state for pod %s", pod.ID()) return errors.Wrapf(err, "error searching state for pod %s", pod.ID())
} else if !exists { } else if !exists {
return fmt.Errorf("pod with id %s not found in state") return errors.Wrapf(ErrNoSuchPod, "pod %s cannot be found in state", pod.ID())
} }
if err := pod.addContainer(ctr); err != nil { if err := pod.addContainer(ctr); err != nil {
@ -244,7 +242,7 @@ func WithAnnotations(annotations map[string]string) CtrCreateOption {
func WithName(name string) CtrCreateOption { func WithName(name string) CtrCreateOption {
return func(ctr *Container) error { return func(ctr *Container) error {
if !ctr.valid { if !ctr.valid {
return errCtrFinalized return ErrCtrFinalized
} }
ctr.name = name ctr.name = name
@ -264,7 +262,7 @@ func WithStopSignal(signal uint) CtrCreateOption {
func WithPodName(name string) PodCreateOption { func WithPodName(name string) PodCreateOption {
return func(pod *Pod) error { return func(pod *Pod) error {
if pod.valid { if pod.valid {
return fmt.Errorf("pod already finalized") return ErrPodFinalized
} }
pod.name = name pod.name = name

View file

@ -1,10 +1,10 @@
package libpod package libpod
import ( import (
"fmt"
"sync" "sync"
"github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/stringid"
"github.com/pkg/errors"
) )
// Pod represents a group of containers that may share namespaces // Pod represents a group of containers that may share namespaces
@ -47,11 +47,11 @@ func (p *Pod) addContainer(ctr *Container) error {
defer p.lock.Unlock() defer p.lock.Unlock()
if !p.valid { if !p.valid {
return fmt.Errorf("pod has already been removed") return ErrPodRemoved
} }
if _, ok := p.containers[ctr.id]; ok { if _, ok := p.containers[ctr.id]; ok {
return fmt.Errorf("container with id %s already exists in pod %s", ctr.id, p.id) return errors.Wrapf(ErrCtrExists, "container with ID %s already exists in pod %s", ctr.id, p.id)
} }
p.containers[ctr.id] = ctr p.containers[ctr.id] = ctr
@ -66,11 +66,11 @@ func (p *Pod) removeContainer(ctr *Container) error {
defer p.lock.Unlock() defer p.lock.Unlock()
if !p.valid { if !p.valid {
return fmt.Errorf("pod has already been removed") return ErrPodRemoved
} }
if _, ok := p.containers[ctr.id]; !ok { if _, ok := p.containers[ctr.id]; !ok {
return fmt.Errorf("container with id %s does not exist in pod %s", ctr.id, p.id) return errors.Wrapf(ErrNoSuchCtr, "no container with id %s in pod %s", ctr.id, p.id)
} }
delete(p.containers, ctr.id) delete(p.containers, ctr.id)
@ -80,17 +80,17 @@ func (p *Pod) removeContainer(ctr *Container) error {
// Start starts all containers within a pod that are not already running // Start starts all containers within a pod that are not already running
func (p *Pod) Start() error { func (p *Pod) Start() error {
return errNotImplemented return ErrNotImplemented
} }
// Stop stops all containers within a pod that are not already stopped // Stop stops all containers within a pod that are not already stopped
func (p *Pod) Stop() error { func (p *Pod) Stop() error {
return errNotImplemented return ErrNotImplemented
} }
// Kill sends a signal to all running containers within a pod // Kill sends a signal to all running containers within a pod
func (p *Pod) Kill(signal uint) error { func (p *Pod) Kill(signal uint) error {
return errNotImplemented return ErrNotImplemented
} }
// GetContainers retrieves the containers in the pod // GetContainers retrieves the containers in the pod
@ -99,7 +99,7 @@ func (p *Pod) GetContainers() ([]*Container, error) {
defer p.lock.RUnlock() defer p.lock.RUnlock()
if !p.valid { if !p.valid {
return nil, fmt.Errorf("pod has already been removed") return nil, ErrPodRemoved
} }
ctrs := make([]*Container, 0, len(p.containers)) ctrs := make([]*Container, 0, len(p.containers))
@ -113,5 +113,5 @@ func (p *Pod) GetContainers() ([]*Container, error) {
// Status gets the status of all containers in the pod // Status gets the status of all containers in the pod
// TODO This should return a summary of the states of all containers in the pod // TODO This should return a summary of the states of all containers in the pod
func (p *Pod) Status() error { func (p *Pod) Status() error {
return errNotImplemented return ErrNotImplemented
} }

View file

@ -1,7 +1,6 @@
package libpod package libpod
import ( import (
"fmt"
"sync" "sync"
"github.com/containers/image/types" "github.com/containers/image/types"
@ -124,7 +123,7 @@ func (r *Runtime) Shutdown(force bool) error {
defer r.lock.Unlock() defer r.lock.Unlock()
if !r.valid { if !r.valid {
return fmt.Errorf("runtime has already been shut down") return ErrRuntimeStopped
} }
r.valid = false r.valid = false
@ -150,7 +149,7 @@ func (r *Runtime) NewContainer(spec *spec.Spec, options ...CtrCreateOption) (*Co
defer r.lock.Unlock() defer r.lock.Unlock()
if !r.valid { if !r.valid {
return nil, fmt.Errorf("runtime has already been shut down") return nil, ErrRuntimeStopped
} }
ctr, err := newContainer(spec) ctr, err := newContainer(spec)
@ -187,7 +186,7 @@ func (r *Runtime) NewContainer(spec *spec.Spec, options ...CtrCreateOption) (*Co
// If force is specified, the container will be stopped first // If force is specified, the container will be stopped first
// Otherwise, RemoveContainer will return an error if the container is running // Otherwise, RemoveContainer will return an error if the container is running
func (r *Runtime) RemoveContainer(c *Container, force bool) error { func (r *Runtime) RemoveContainer(c *Container, force bool) error {
return errNotImplemented return ErrNotImplemented
} }
// GetContainer retrieves a container by its ID // GetContainer retrieves a container by its ID
@ -196,7 +195,7 @@ func (r *Runtime) GetContainer(id string) (*Container, error) {
defer r.lock.RUnlock() defer r.lock.RUnlock()
if !r.valid { if !r.valid {
return nil, fmt.Errorf("runtime has already been shut down") return nil, ErrRuntimeStopped
} }
return r.state.GetContainer(id) return r.state.GetContainer(id)
@ -208,7 +207,7 @@ func (r *Runtime) HasContainer(id string) (bool, error) {
defer r.lock.RUnlock() defer r.lock.RUnlock()
if !r.valid { if !r.valid {
return false, fmt.Errorf("runtime has already been shut down") return false, ErrRuntimeStopped
} }
return r.state.HasContainer(id) return r.state.HasContainer(id)
@ -221,7 +220,7 @@ func (r *Runtime) LookupContainer(idOrName string) (*Container, error) {
defer r.lock.RUnlock() defer r.lock.RUnlock()
if !r.valid { if !r.valid {
return nil, fmt.Errorf("runtime has already been shut down") return nil, ErrRuntimeStopped
} }
return r.state.LookupContainer(idOrName) return r.state.LookupContainer(idOrName)
@ -236,7 +235,7 @@ func (r *Runtime) Containers(filters ...ContainerFilter) ([]*Container, error) {
defer r.lock.RUnlock() defer r.lock.RUnlock()
if !r.valid { if !r.valid {
return nil, fmt.Errorf("runtime has already been shut down") return nil, ErrRuntimeStopped
} }
ctrs, err := r.state.GetAllContainers() ctrs, err := r.state.GetAllContainers()
@ -277,7 +276,7 @@ func (r *Runtime) NewPod(options ...PodCreateOption) (*Pod, error) {
defer r.lock.Unlock() defer r.lock.Unlock()
if !r.valid { if !r.valid {
return nil, fmt.Errorf("runtime has already been shut down") return nil, ErrRuntimeStopped
} }
pod, err := newPod() pod, err := newPod()
@ -297,7 +296,7 @@ func (r *Runtime) NewPod(options ...PodCreateOption) (*Pod, error) {
return nil, errors.Wrapf(err, "error adding pod to state") return nil, errors.Wrapf(err, "error adding pod to state")
} }
return nil, errNotImplemented return nil, ErrNotImplemented
} }
// RemovePod removes a pod and all containers in it // RemovePod removes a pod and all containers in it
@ -305,7 +304,7 @@ func (r *Runtime) NewPod(options ...PodCreateOption) (*Pod, error) {
// Otherwise, RemovePod will return an error if any container in the pod is running // Otherwise, RemovePod will return an error if any container in the pod is running
// Remove acts atomically, removing all containers or no containers // Remove acts atomically, removing all containers or no containers
func (r *Runtime) RemovePod(p *Pod, force bool) error { func (r *Runtime) RemovePod(p *Pod, force bool) error {
return errNotImplemented return ErrNotImplemented
} }
// GetPod retrieves a pod by its ID // GetPod retrieves a pod by its ID
@ -314,7 +313,7 @@ func (r *Runtime) GetPod(id string) (*Pod, error) {
defer r.lock.RUnlock() defer r.lock.RUnlock()
if !r.valid { if !r.valid {
return nil, fmt.Errorf("runtime has already been shut down") return nil, ErrRuntimeStopped
} }
return r.state.GetPod(id) return r.state.GetPod(id)
@ -326,7 +325,7 @@ func (r *Runtime) HasPod(id string) (bool, error) {
defer r.lock.RUnlock() defer r.lock.RUnlock()
if !r.valid { if !r.valid {
return false, fmt.Errorf("runtime has already been shut down") return false, ErrRuntimeStopped
} }
return r.state.HasPod(id) return r.state.HasPod(id)
@ -339,7 +338,7 @@ func (r *Runtime) LookupPod(idOrName string) (*Pod, error) {
defer r.lock.RUnlock() defer r.lock.RUnlock()
if !r.valid { if !r.valid {
return nil, fmt.Errorf("runtime has already been shut down") return nil, ErrRuntimeStopped
} }
return r.state.LookupPod(idOrName) return r.state.LookupPod(idOrName)
@ -354,7 +353,7 @@ func (r *Runtime) Pods(filters ...PodFilter) ([]*Pod, error) {
defer r.lock.RUnlock() defer r.lock.RUnlock()
if !r.valid { if !r.valid {
return nil, fmt.Errorf("runtime has already been shut down") return nil, ErrRuntimeStopped
} }
pods, err := r.state.GetAllPods() pods, err := r.state.GetAllPods()