From ae5634f8dd53a1455d19bc5f17bdc34ff3b4b6ed Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Mon, 11 Sep 2017 11:27:40 -0400 Subject: [PATCH] Add errors listing to libpod Signed-off-by: Matthew Heon --- libpod/container.go | 29 ++++++++++--------------- libpod/errors.go | 53 +++++++++++++++++++++++++++++++++++++++++++++ libpod/image.go | 4 ++-- libpod/options.go | 30 ++++++++++++------------- libpod/pod.go | 20 ++++++++--------- libpod/runtime.go | 29 ++++++++++++------------- 6 files changed, 105 insertions(+), 60 deletions(-) create mode 100644 libpod/errors.go diff --git a/libpod/container.go b/libpod/container.go index f9496279..45ee5647 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -1,20 +1,15 @@ package libpod import ( - "fmt" "sync" "github.com/containers/storage" "github.com/docker/docker/pkg/stringid" spec "github.com/opencontainers/runtime-spec/specs-go" + "github.com/pkg/errors" "github.com/ulule/deepcopier" ) -var ( - // ErrNotImplemented indicates that functionality is not yet implemented - errNotImplemented = fmt.Errorf("NOT IMPLEMENTED") -) - // Container is a single OCI container type Container struct { id string @@ -60,7 +55,7 @@ func (c *Container) Spec() *spec.Spec { // Make a new container func newContainer(rspec *spec.Spec) (*Container, error) { 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) @@ -75,52 +70,52 @@ func newContainer(rspec *spec.Spec) (*Container, error) { // Create creates a container in the OCI runtime func (c *Container) Create() error { - return errNotImplemented + return ErrNotImplemented } // Start starts a container func (c *Container) Start() error { - return errNotImplemented + return ErrNotImplemented } // Stop stops a container func (c *Container) Stop() error { - return errNotImplemented + return ErrNotImplemented } // Kill sends a signal to a container func (c *Container) Kill(signal uint) error { - return errNotImplemented + return ErrNotImplemented } // Exec starts a new process inside the container // Returns fully qualified URL of streaming server for executed process func (c *Container) Exec(cmd []string, tty bool, stdin bool) (string, error) { - return "", errNotImplemented + return "", ErrNotImplemented } // Attach attaches to a container // Returns fully qualified URL of streaming server for the container func (c *Container) Attach(stdin, tty bool) (string, error) { - return "", errNotImplemented + return "", ErrNotImplemented } // Mount mounts a container's filesystem on the host // The path where the container has been mounted is returned func (c *Container) Mount() (string, error) { - return "", errNotImplemented + return "", ErrNotImplemented } // Status gets a container's status // TODO this should return relevant information about container state func (c *Container) Status() error { - return errNotImplemented + return ErrNotImplemented } // Export exports a container's root filesystem as a tar archive // The archive will be saved as a file at the given path func (c *Container) Export(path string) error { - return errNotImplemented + return ErrNotImplemented } // 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 // filesystem func (c *Container) Commit() (*storage.Image, error) { - return nil, errNotImplemented + return nil, ErrNotImplemented } diff --git a/libpod/errors.go b/libpod/errors.go new file mode 100644 index 00000000..d50db574 --- /dev/null +++ b/libpod/errors.go @@ -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") +) diff --git a/libpod/image.go b/libpod/image.go index affb39da..61b56254 100644 --- a/libpod/image.go +++ b/libpod/image.go @@ -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 // matching all filters are included 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 func (r *Runtime) ImportImage(path string) (*storage.Image, error) { - return nil, errNotImplemented + return nil, ErrNotImplemented } diff --git a/libpod/options.go b/libpod/options.go index 60cc1fe5..03248274 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -9,9 +9,7 @@ import ( ) var ( - errRuntimeFinalized = fmt.Errorf("runtime has already been finalized") - errCtrFinalized = fmt.Errorf("container has already been finalized") - ctrNotImplemented = func(c *Container) error { + ctrNotImplemented = func(c *Container) error { return fmt.Errorf("NOT IMPLEMENTED") } ) @@ -39,7 +37,7 @@ const ( func WithStorageConfig(config storage.StoreOptions) RuntimeOption { return func(rt *Runtime) error { if rt.valid { - return errRuntimeFinalized + return ErrRuntimeFinalized } rt.config.StorageConfig.RunRoot = config.RunRoot @@ -65,7 +63,7 @@ func WithStorageConfig(config storage.StoreOptions) RuntimeOption { func WithImageConfig(defaultTransport string, insecureRegistries, registries []string) RuntimeOption { return func(rt *Runtime) error { if rt.valid { - return errRuntimeFinalized + return ErrRuntimeFinalized } rt.config.ImageDefaultTransport = defaultTransport @@ -87,7 +85,7 @@ func WithImageConfig(defaultTransport string, insecureRegistries, registries []s func WithSignaturePolicy(path string) RuntimeOption { return func(rt *Runtime) error { if rt.valid { - return errRuntimeFinalized + return ErrRuntimeFinalized } rt.config.SignaturePolicyPath = path @@ -100,7 +98,7 @@ func WithSignaturePolicy(path string) RuntimeOption { func WithOCIRuntime(runtimePath string) RuntimeOption { return func(rt *Runtime) error { if rt.valid { - return errRuntimeFinalized + return ErrRuntimeFinalized } rt.config.RuntimePath = runtimePath @@ -114,7 +112,7 @@ func WithOCIRuntime(runtimePath string) RuntimeOption { func WithConmonPath(path string) RuntimeOption { return func(rt *Runtime) error { if rt.valid { - return errRuntimeFinalized + return ErrRuntimeFinalized } rt.config.ConmonPath = path @@ -127,7 +125,7 @@ func WithConmonPath(path string) RuntimeOption { func WithConmonEnv(environment []string) RuntimeOption { return func(rt *Runtime) error { if rt.valid { - return errRuntimeFinalized + return ErrRuntimeFinalized } rt.config.ConmonEnvVars = make([]string, len(environment)) @@ -142,7 +140,7 @@ func WithConmonEnv(environment []string) RuntimeOption { func WithCgroupManager(manager string) RuntimeOption { return func(rt *Runtime) error { if rt.valid { - return errRuntimeFinalized + return ErrRuntimeFinalized } rt.config.CgroupManager = manager @@ -155,7 +153,7 @@ func WithCgroupManager(manager string) RuntimeOption { func WithSELinux() RuntimeOption { return func(rt *Runtime) error { if rt.valid { - return errRuntimeFinalized + return ErrRuntimeFinalized } rt.config.SelinuxEnabled = true @@ -169,7 +167,7 @@ func WithSELinux() RuntimeOption { func WithPidsLimit(limit int64) RuntimeOption { return func(rt *Runtime) error { if rt.valid { - return errRuntimeFinalized + return ErrRuntimeFinalized } rt.config.PidsLimit = limit @@ -206,7 +204,7 @@ func WithSharedNamespaces(from *Container, namespaces map[string]string) CtrCrea func (r *Runtime) WithPod(pod *Pod) CtrCreateOption { return func(ctr *Container) error { if !ctr.valid { - return errCtrFinalized + return ErrCtrFinalized } if ctr.pod != nil { @@ -217,7 +215,7 @@ func (r *Runtime) WithPod(pod *Pod) CtrCreateOption { if err != nil { return errors.Wrapf(err, "error searching state for pod %s", pod.ID()) } 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 { @@ -244,7 +242,7 @@ func WithAnnotations(annotations map[string]string) CtrCreateOption { func WithName(name string) CtrCreateOption { return func(ctr *Container) error { if !ctr.valid { - return errCtrFinalized + return ErrCtrFinalized } ctr.name = name @@ -264,7 +262,7 @@ func WithStopSignal(signal uint) CtrCreateOption { func WithPodName(name string) PodCreateOption { return func(pod *Pod) error { if pod.valid { - return fmt.Errorf("pod already finalized") + return ErrPodFinalized } pod.name = name diff --git a/libpod/pod.go b/libpod/pod.go index f70c8313..451747aa 100644 --- a/libpod/pod.go +++ b/libpod/pod.go @@ -1,10 +1,10 @@ package libpod import ( - "fmt" "sync" "github.com/docker/docker/pkg/stringid" + "github.com/pkg/errors" ) // 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() if !p.valid { - return fmt.Errorf("pod has already been removed") + return ErrPodRemoved } 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 @@ -66,11 +66,11 @@ func (p *Pod) removeContainer(ctr *Container) error { defer p.lock.Unlock() if !p.valid { - return fmt.Errorf("pod has already been removed") + return ErrPodRemoved } 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) @@ -80,17 +80,17 @@ func (p *Pod) removeContainer(ctr *Container) error { // Start starts all containers within a pod that are not already running func (p *Pod) Start() error { - return errNotImplemented + return ErrNotImplemented } // Stop stops all containers within a pod that are not already stopped func (p *Pod) Stop() error { - return errNotImplemented + return ErrNotImplemented } // Kill sends a signal to all running containers within a pod func (p *Pod) Kill(signal uint) error { - return errNotImplemented + return ErrNotImplemented } // GetContainers retrieves the containers in the pod @@ -99,7 +99,7 @@ func (p *Pod) GetContainers() ([]*Container, error) { defer p.lock.RUnlock() if !p.valid { - return nil, fmt.Errorf("pod has already been removed") + return nil, ErrPodRemoved } 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 // TODO This should return a summary of the states of all containers in the pod func (p *Pod) Status() error { - return errNotImplemented + return ErrNotImplemented } diff --git a/libpod/runtime.go b/libpod/runtime.go index 08d564f2..ddbb87bc 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -1,7 +1,6 @@ package libpod import ( - "fmt" "sync" "github.com/containers/image/types" @@ -124,7 +123,7 @@ func (r *Runtime) Shutdown(force bool) error { defer r.lock.Unlock() if !r.valid { - return fmt.Errorf("runtime has already been shut down") + return ErrRuntimeStopped } r.valid = false @@ -150,7 +149,7 @@ func (r *Runtime) NewContainer(spec *spec.Spec, options ...CtrCreateOption) (*Co defer r.lock.Unlock() if !r.valid { - return nil, fmt.Errorf("runtime has already been shut down") + return nil, ErrRuntimeStopped } 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 // Otherwise, RemoveContainer will return an error if the container is running func (r *Runtime) RemoveContainer(c *Container, force bool) error { - return errNotImplemented + return ErrNotImplemented } // GetContainer retrieves a container by its ID @@ -196,7 +195,7 @@ func (r *Runtime) GetContainer(id string) (*Container, error) { defer r.lock.RUnlock() if !r.valid { - return nil, fmt.Errorf("runtime has already been shut down") + return nil, ErrRuntimeStopped } return r.state.GetContainer(id) @@ -208,7 +207,7 @@ func (r *Runtime) HasContainer(id string) (bool, error) { defer r.lock.RUnlock() if !r.valid { - return false, fmt.Errorf("runtime has already been shut down") + return false, ErrRuntimeStopped } return r.state.HasContainer(id) @@ -221,7 +220,7 @@ func (r *Runtime) LookupContainer(idOrName string) (*Container, error) { defer r.lock.RUnlock() if !r.valid { - return nil, fmt.Errorf("runtime has already been shut down") + return nil, ErrRuntimeStopped } return r.state.LookupContainer(idOrName) @@ -236,7 +235,7 @@ func (r *Runtime) Containers(filters ...ContainerFilter) ([]*Container, error) { defer r.lock.RUnlock() if !r.valid { - return nil, fmt.Errorf("runtime has already been shut down") + return nil, ErrRuntimeStopped } ctrs, err := r.state.GetAllContainers() @@ -277,7 +276,7 @@ func (r *Runtime) NewPod(options ...PodCreateOption) (*Pod, error) { defer r.lock.Unlock() if !r.valid { - return nil, fmt.Errorf("runtime has already been shut down") + return nil, ErrRuntimeStopped } 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, errNotImplemented + return nil, ErrNotImplemented } // 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 // Remove acts atomically, removing all containers or no containers func (r *Runtime) RemovePod(p *Pod, force bool) error { - return errNotImplemented + return ErrNotImplemented } // GetPod retrieves a pod by its ID @@ -314,7 +313,7 @@ func (r *Runtime) GetPod(id string) (*Pod, error) { defer r.lock.RUnlock() if !r.valid { - return nil, fmt.Errorf("runtime has already been shut down") + return nil, ErrRuntimeStopped } return r.state.GetPod(id) @@ -326,7 +325,7 @@ func (r *Runtime) HasPod(id string) (bool, error) { defer r.lock.RUnlock() if !r.valid { - return false, fmt.Errorf("runtime has already been shut down") + return false, ErrRuntimeStopped } return r.state.HasPod(id) @@ -339,7 +338,7 @@ func (r *Runtime) LookupPod(idOrName string) (*Pod, error) { defer r.lock.RUnlock() if !r.valid { - return nil, fmt.Errorf("runtime has already been shut down") + return nil, ErrRuntimeStopped } return r.state.LookupPod(idOrName) @@ -354,7 +353,7 @@ func (r *Runtime) Pods(filters ...PodFilter) ([]*Pod, error) { defer r.lock.RUnlock() if !r.valid { - return nil, fmt.Errorf("runtime has already been shut down") + return nil, ErrRuntimeStopped } pods, err := r.state.GetAllPods()