Skip to content

Commit

Permalink
Merge pull request containers#14789 from saschagrunert/libpod-errors
Browse files Browse the repository at this point in the history
libpod/runtime: switch to golang native error wrapping
  • Loading branch information
openshift-ci[bot] authored Jul 5, 2022
2 parents 3e7e66e + 597de7a commit 773eead
Show file tree
Hide file tree
Showing 19 changed files with 391 additions and 318 deletions.
108 changes: 54 additions & 54 deletions libpod/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -40,7 +41,6 @@ import (
"github.com/containers/storage/pkg/unshare"
"github.com/docker/docker/pkg/namesgenerator"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -146,7 +146,7 @@ func SetXdgDirs() error {
}
}
if err := os.Setenv("XDG_RUNTIME_DIR", runtimeDir); err != nil {
return errors.Wrapf(err, "cannot set XDG_RUNTIME_DIR")
return fmt.Errorf("cannot set XDG_RUNTIME_DIR: %w", err)
}

if rootless.IsRootless() && os.Getenv("DBUS_SESSION_BUS_ADDRESS") == "" {
Expand All @@ -163,7 +163,7 @@ func SetXdgDirs() error {
return err
}
if err := os.Setenv("XDG_CONFIG_HOME", cfgHomeDir); err != nil {
return errors.Wrapf(err, "cannot set XDG_CONFIG_HOME")
return fmt.Errorf("cannot set XDG_CONFIG_HOME: %w", err)
}
}
return nil
Expand Down Expand Up @@ -214,7 +214,7 @@ func newRuntimeFromConfig(conf *config.Config, options ...RuntimeOption) (*Runti
// Overwrite config with user-given configuration options
for _, opt := range options {
if err := opt(runtime); err != nil {
return nil, errors.Wrapf(err, "error configuring runtime")
return nil, fmt.Errorf("error configuring runtime: %w", err)
}
}

Expand All @@ -225,12 +225,12 @@ func newRuntimeFromConfig(conf *config.Config, options ...RuntimeOption) (*Runti
}
os.Exit(1)
return nil
}); err != nil && errors.Cause(err) != shutdown.ErrHandlerExists {
}); err != nil && !errors.Is(err, shutdown.ErrHandlerExists) {
logrus.Errorf("Registering shutdown handler for libpod: %v", err)
}

if err := shutdown.Start(); err != nil {
return nil, errors.Wrapf(err, "error starting shutdown signal handler")
return nil, fmt.Errorf("error starting shutdown signal handler: %w", err)
}

if err := makeRuntime(runtime); err != nil {
Expand All @@ -256,10 +256,10 @@ func getLockManager(runtime *Runtime) (lock.Manager, error) {
lockPath := filepath.Join(runtime.config.Engine.TmpDir, "locks")
manager, err = lock.OpenFileLockManager(lockPath)
if err != nil {
if os.IsNotExist(errors.Cause(err)) {
if errors.Is(err, os.ErrNotExist) {
manager, err = lock.NewFileLockManager(lockPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to get new file lock manager")
return nil, fmt.Errorf("failed to get new file lock manager: %w", err)
}
} else {
return nil, err
Expand All @@ -275,19 +275,19 @@ func getLockManager(runtime *Runtime) (lock.Manager, error) {
manager, err = lock.OpenSHMLockManager(lockPath, runtime.config.Engine.NumLocks)
if err != nil {
switch {
case os.IsNotExist(errors.Cause(err)):
case errors.Is(err, os.ErrNotExist):
manager, err = lock.NewSHMLockManager(lockPath, runtime.config.Engine.NumLocks)
if err != nil {
return nil, errors.Wrapf(err, "failed to get new shm lock manager")
return nil, fmt.Errorf("failed to get new shm lock manager: %w", err)
}
case errors.Cause(err) == syscall.ERANGE && runtime.doRenumber:
case errors.Is(err, syscall.ERANGE) && runtime.doRenumber:
logrus.Debugf("Number of locks does not match - removing old locks")

// ERANGE indicates a lock numbering mismatch.
// Since we're renumbering, this is not fatal.
// Remove the earlier set of locks and recreate.
if err := os.Remove(filepath.Join("/dev/shm", lockPath)); err != nil {
return nil, errors.Wrapf(err, "error removing libpod locks file %s", lockPath)
return nil, fmt.Errorf("error removing libpod locks file %s: %w", lockPath, err)
}

manager, err = lock.NewSHMLockManager(lockPath, runtime.config.Engine.NumLocks)
Expand All @@ -299,7 +299,7 @@ func getLockManager(runtime *Runtime) (lock.Manager, error) {
}
}
default:
return nil, errors.Wrapf(define.ErrInvalidArg, "unknown lock type %s", runtime.config.Engine.LockType)
return nil, fmt.Errorf("unknown lock type %s: %w", runtime.config.Engine.LockType, define.ErrInvalidArg)
}
return manager, nil
}
Expand All @@ -315,17 +315,17 @@ func makeRuntime(runtime *Runtime) (retErr error) {
runtime.conmonPath = cPath

if runtime.noStore && runtime.doReset {
return errors.Wrapf(define.ErrInvalidArg, "cannot perform system reset if runtime is not creating a store")
return fmt.Errorf("cannot perform system reset if runtime is not creating a store: %w", define.ErrInvalidArg)
}
if runtime.doReset && runtime.doRenumber {
return errors.Wrapf(define.ErrInvalidArg, "cannot perform system reset while renumbering locks")
return fmt.Errorf("cannot perform system reset while renumbering locks: %w", define.ErrInvalidArg)
}

// Make the static files directory if it does not exist
if err := os.MkdirAll(runtime.config.Engine.StaticDir, 0700); err != nil {
// The directory is allowed to exist
if !os.IsExist(err) {
return errors.Wrap(err, "error creating runtime static files directory")
if !errors.Is(err, os.ErrExist) {
return fmt.Errorf("error creating runtime static files directory: %w", err)
}
}

Expand All @@ -337,9 +337,9 @@ func makeRuntime(runtime *Runtime) (retErr error) {
// package.
switch runtime.config.Engine.StateType {
case config.InMemoryStateStore:
return errors.Wrapf(define.ErrInvalidArg, "in-memory state is currently disabled")
return fmt.Errorf("in-memory state is currently disabled: %w", define.ErrInvalidArg)
case config.SQLiteStateStore:
return errors.Wrapf(define.ErrInvalidArg, "SQLite state is currently disabled")
return fmt.Errorf("SQLite state is currently disabled: %w", define.ErrInvalidArg)
case config.BoltDBStateStore:
dbPath := filepath.Join(runtime.config.Engine.StaticDir, "bolt_state.db")

Expand All @@ -349,7 +349,7 @@ func makeRuntime(runtime *Runtime) (retErr error) {
}
runtime.state = state
default:
return errors.Wrapf(define.ErrInvalidArg, "unrecognized state type passed (%v)", runtime.config.Engine.StateType)
return fmt.Errorf("unrecognized state type passed (%v): %w", runtime.config.Engine.StateType, define.ErrInvalidArg)
}

// Grab config from the database so we can reset some defaults
Expand All @@ -369,7 +369,7 @@ func makeRuntime(runtime *Runtime) (retErr error) {
}
}

return errors.Wrapf(err, "error retrieving runtime configuration from database")
return fmt.Errorf("error retrieving runtime configuration from database: %w", err)
}

runtime.mergeDBConfig(dbConfig)
Expand Down Expand Up @@ -412,7 +412,7 @@ func makeRuntime(runtime *Runtime) (retErr error) {
}

if err := runtime.state.SetNamespace(runtime.config.Engine.Namespace); err != nil {
return errors.Wrapf(err, "error setting libpod namespace in state")
return fmt.Errorf("error setting libpod namespace in state: %w", err)
}
logrus.Debugf("Set libpod namespace to %q", runtime.config.Engine.Namespace)

Expand Down Expand Up @@ -468,16 +468,16 @@ func makeRuntime(runtime *Runtime) (retErr error) {
// Create the tmpDir
if err := os.MkdirAll(runtime.config.Engine.TmpDir, 0751); err != nil {
// The directory is allowed to exist
if !os.IsExist(err) {
return errors.Wrap(err, "error creating tmpdir")
if !errors.Is(err, os.ErrExist) {
return fmt.Errorf("error creating tmpdir: %w", err)
}
}

// Create events log dir
if err := os.MkdirAll(filepath.Dir(runtime.config.Engine.EventsLogFilePath), 0700); err != nil {
// The directory is allowed to exist
if !os.IsExist(err) {
return errors.Wrap(err, "error creating events dirs")
if !errors.Is(err, os.ErrExist) {
return fmt.Errorf("error creating events dirs: %w", err)
}
}

Expand Down Expand Up @@ -514,7 +514,7 @@ func makeRuntime(runtime *Runtime) (retErr error) {
} else {
ociRuntime, ok := runtime.ociRuntimes[runtime.config.Engine.OCIRuntime]
if !ok {
return errors.Wrapf(define.ErrInvalidArg, "default OCI runtime %q not found", runtime.config.Engine.OCIRuntime)
return fmt.Errorf("default OCI runtime %q not found: %w", runtime.config.Engine.OCIRuntime, define.ErrInvalidArg)
}
runtime.defaultOCIRuntime = ociRuntime
}
Expand All @@ -523,19 +523,19 @@ func makeRuntime(runtime *Runtime) (retErr error) {

// Do we have at least one valid OCI runtime?
if len(runtime.ociRuntimes) == 0 {
return errors.Wrapf(define.ErrInvalidArg, "no OCI runtime has been configured")
return fmt.Errorf("no OCI runtime has been configured: %w", define.ErrInvalidArg)
}

// Do we have a default runtime?
if runtime.defaultOCIRuntime == nil {
return errors.Wrapf(define.ErrInvalidArg, "no default OCI runtime was configured")
return fmt.Errorf("no default OCI runtime was configured: %w", define.ErrInvalidArg)
}

// Make the per-boot files directory if it does not exist
if err := os.MkdirAll(runtime.config.Engine.TmpDir, 0755); err != nil {
// The directory is allowed to exist
if !os.IsExist(err) {
return errors.Wrapf(err, "error creating runtime temporary files directory")
if !errors.Is(err, os.ErrExist) {
return fmt.Errorf("error creating runtime temporary files directory: %w", err)
}
}

Expand All @@ -556,7 +556,7 @@ func makeRuntime(runtime *Runtime) (retErr error) {
runtimeAliveFile := filepath.Join(runtime.config.Engine.TmpDir, "alive")
aliveLock, err := storage.GetLockfile(runtimeAliveLock)
if err != nil {
return errors.Wrapf(err, "error acquiring runtime init lock")
return fmt.Errorf("error acquiring runtime init lock: %w", err)
}
// Acquire the lock and hold it until we return
// This ensures that no two processes will be in runtime.refresh at once
Expand Down Expand Up @@ -586,7 +586,7 @@ func makeRuntime(runtime *Runtime) (retErr error) {
aliveLock.Unlock() // Unlock to avoid deadlock as BecomeRootInUserNS will reexec.
pausePid, err := util.GetRootlessPauseProcessPidPathGivenDir(runtime.config.Engine.TmpDir)
if err != nil {
return errors.Wrapf(err, "could not get pause process pid file path")
return fmt.Errorf("could not get pause process pid file path: %w", err)
}
became, ret, err := rootless.BecomeRootInUserNS(pausePid)
if err != nil {
Expand All @@ -607,10 +607,10 @@ func makeRuntime(runtime *Runtime) (retErr error) {
// This will trigger on first use as well, but refreshing an
// empty state only creates a single file
// As such, it's not really a performance concern
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
doRefresh = true
} else {
return errors.Wrapf(err, "error reading runtime status file %s", runtimeAliveFile)
return fmt.Errorf("error reading runtime status file %s: %w", runtimeAliveFile, err)
}
}

Expand Down Expand Up @@ -704,14 +704,14 @@ func findConmon(conmonPaths []string) (string, error) {
}

if foundOutdatedConmon {
return "", errors.Wrapf(define.ErrConmonOutdated,
"please update to v%d.%d.%d or later",
conmonMinMajorVersion, conmonMinMinorVersion, conmonMinPatchVersion)
return "", fmt.Errorf(
"please update to v%d.%d.%d or later: %w",
conmonMinMajorVersion, conmonMinMinorVersion, conmonMinPatchVersion, define.ErrConmonOutdated)
}

return "", errors.Wrapf(define.ErrInvalidArg,
"could not find a working conmon binary (configured options: %v)",
conmonPaths)
return "", fmt.Errorf(
"could not find a working conmon binary (configured options: %v): %w",
conmonPaths, define.ErrInvalidArg)
}

// probeConmon calls conmon --version and verifies it is a new enough version for
Expand All @@ -728,11 +728,11 @@ func probeConmon(conmonBinary string) error {

matches := r.FindStringSubmatch(out.String())
if len(matches) != 4 {
return errors.Wrap(err, define.ErrConmonVersionFormat)
return fmt.Errorf("%v: %w", define.ErrConmonVersionFormat, err)
}
major, err := strconv.Atoi(matches[1])
if err != nil {
return errors.Wrap(err, define.ErrConmonVersionFormat)
return fmt.Errorf("%v: %w", define.ErrConmonVersionFormat, err)
}
if major < conmonMinMajorVersion {
return define.ErrConmonOutdated
Expand All @@ -743,7 +743,7 @@ func probeConmon(conmonBinary string) error {

minor, err := strconv.Atoi(matches[2])
if err != nil {
return errors.Wrap(err, define.ErrConmonVersionFormat)
return fmt.Errorf("%v: %w", define.ErrConmonVersionFormat, err)
}
if minor < conmonMinMinorVersion {
return define.ErrConmonOutdated
Expand All @@ -754,7 +754,7 @@ func probeConmon(conmonBinary string) error {

patch, err := strconv.Atoi(matches[3])
if err != nil {
return errors.Wrap(err, define.ErrConmonVersionFormat)
return fmt.Errorf("%v: %w", define.ErrConmonVersionFormat, err)
}
if patch < conmonMinPatchVersion {
return define.ErrConmonOutdated
Expand Down Expand Up @@ -798,7 +798,7 @@ func (r *Runtime) GetConfig() (*config.Config, error) {

// Copy so the caller won't be able to modify the actual config
if err := JSONDeepCopy(rtConfig, config); err != nil {
return nil, errors.Wrapf(err, "error copying config")
return nil, fmt.Errorf("error copying config: %w", err)
}

return config, nil
Expand Down Expand Up @@ -909,7 +909,7 @@ func (r *Runtime) Shutdown(force bool) error {

// Note that the libimage runtime shuts down the store.
if err := r.libimageRuntime.Shutdown(force); err != nil {
lastError = errors.Wrapf(err, "error shutting down container storage")
lastError = fmt.Errorf("error shutting down container storage: %w", err)
}
}
if err := r.state.Close(); err != nil {
Expand Down Expand Up @@ -941,15 +941,15 @@ func (r *Runtime) refresh(alivePath string) error {
// Containers, pods, and volumes must also reacquire their locks.
ctrs, err := r.state.AllContainers()
if err != nil {
return errors.Wrapf(err, "error retrieving all containers from state")
return fmt.Errorf("error retrieving all containers from state: %w", err)
}
pods, err := r.state.AllPods()
if err != nil {
return errors.Wrapf(err, "error retrieving all pods from state")
return fmt.Errorf("error retrieving all pods from state: %w", err)
}
vols, err := r.state.AllVolumes()
if err != nil {
return errors.Wrapf(err, "error retrieving all volumes from state")
return fmt.Errorf("error retrieving all volumes from state: %w", err)
}
// No locks are taken during pod, volume, and container refresh.
// Furthermore, the pod/volume/container refresh() functions are not
Expand Down Expand Up @@ -977,7 +977,7 @@ func (r *Runtime) refresh(alivePath string) error {
// Create a file indicating the runtime is alive and ready
file, err := os.OpenFile(alivePath, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
return errors.Wrap(err, "error creating runtime status file")
return fmt.Errorf("error creating runtime status file: %w", err)
}
defer file.Close()

Expand All @@ -998,13 +998,13 @@ func (r *Runtime) generateName() (string, error) {
// Make sure container with this name does not exist
if _, err := r.state.LookupContainer(name); err == nil {
continue
} else if errors.Cause(err) != define.ErrNoSuchCtr {
} else if !errors.Is(err, define.ErrNoSuchCtr) {
return "", err
}
// Make sure pod with this name does not exist
if _, err := r.state.LookupPod(name); err == nil {
continue
} else if errors.Cause(err) != define.ErrNoSuchPod {
} else if !errors.Is(err, define.ErrNoSuchPod) {
return "", err
}
return name, nil
Expand Down Expand Up @@ -1205,7 +1205,7 @@ func (r *Runtime) getVolumePlugin(volConfig *VolumeConfig) (*plugin.VolumePlugin

pluginPath, ok := r.config.Engine.VolumePlugins[name]
if !ok {
return nil, errors.Wrapf(define.ErrMissingPlugin, "no volume plugin with name %s available", name)
return nil, fmt.Errorf("no volume plugin with name %s available: %w", name, define.ErrMissingPlugin)
}

return plugin.GetVolumePlugin(name, pluginPath, timeout)
Expand Down
Loading

0 comments on commit 773eead

Please sign in to comment.