Skip to content

Commit

Permalink
Change github.com/pkg/errors to use errors in pkg/config
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed May 18, 2022
1 parent 4e0c644 commit d34ec66
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 51 deletions.
58 changes: 28 additions & 30 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"fmt"
"io/fs"
"os"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/containers/storage/pkg/unshare"
units "github.com/docker/go-units"
selinux "github.com/opencontainers/selinux/go-selinux"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -610,14 +610,14 @@ func NewConfig(userConfigPath string) (*Config, error) {
// Now, gather the system configs and merge them as needed.
configs, err := systemConfigs()
if err != nil {
return nil, errors.Wrap(err, "finding config on system")
return nil, fmt.Errorf("finding config on system: %w", err)
}
for _, path := range configs {
// Merge changes in later configs with the previous configs.
// Each config file that specified fields, will override the
// previous fields.
if err = readConfigFromFile(path, config); err != nil {
return nil, errors.Wrapf(err, "reading system config %q", path)
return nil, fmt.Errorf("reading system config %q: %w", path, err)
}
logrus.Debugf("Merged system config %q", path)
logrus.Tracef("%+v", config)
Expand All @@ -630,7 +630,7 @@ func NewConfig(userConfigPath string) (*Config, error) {
// readConfigFromFile reads in container config in the specified
// file and then merge changes with the current default.
if err = readConfigFromFile(userConfigPath, config); err != nil {
return nil, errors.Wrapf(err, "reading user config %q", userConfigPath)
return nil, fmt.Errorf("reading user config %q: %w", userConfigPath, err)
}
logrus.Debugf("Merged user config %q", userConfigPath)
logrus.Tracef("%+v", config)
Expand All @@ -656,7 +656,7 @@ func readConfigFromFile(path string, config *Config) error {
logrus.Tracef("Reading configuration file %q", path)
meta, err := toml.DecodeFile(path, config)
if err != nil {
return errors.Wrapf(err, "decode configuration %v", path)
return fmt.Errorf("decode configuration %v: %w", path, err)
}
keys := meta.Undecoded()
if len(keys) > 0 {
Expand Down Expand Up @@ -710,7 +710,7 @@ func systemConfigs() ([]string, error) {
path := os.Getenv("CONTAINERS_CONF")
if path != "" {
if _, err := os.Stat(path); err != nil {
return nil, errors.Wrap(err, "CONTAINERS_CONF file")
return nil, fmt.Errorf("CONTAINERS_CONF file: %w", err)
}
return append(configs, path), nil
}
Expand Down Expand Up @@ -785,19 +785,19 @@ func (c *Config) addCAPPrefix() {
// Validate is the main entry point for library configuration validation.
func (c *Config) Validate() error {
if err := c.Containers.Validate(); err != nil {
return errors.Wrap(err, "validating containers config")
return fmt.Errorf("validating containers config: %w", err)
}

if !c.Containers.EnableLabeling {
selinux.SetDisabled()
}

if err := c.Engine.Validate(); err != nil {
return errors.Wrap(err, "validating engine configs")
return fmt.Errorf("validating engine configs: %w", err)
}

if err := c.Network.Validate(); err != nil {
return errors.Wrap(err, "validating network configs")
return fmt.Errorf("validating network configs %w", err)
}

return nil
Expand Down Expand Up @@ -834,7 +834,7 @@ func (c *EngineConfig) Validate() error {
// if it is invalid returns the error
pullPolicy := strings.ToLower(c.PullPolicy)
if _, err := ValidatePullPolicy(pullPolicy); err != nil {
return errors.Wrapf(err, "invalid pull type from containers.conf %q", c.PullPolicy)
return fmt.Errorf("invalid pull type from containers.conf %q: %w", c.PullPolicy, err)
}
return nil
}
Expand All @@ -860,11 +860,11 @@ func (c *ContainersConfig) Validate() error {
}

if c.LogSizeMax >= 0 && c.LogSizeMax < OCIBufSize {
return errors.Errorf("log size max should be negative or >= %d", OCIBufSize)
return fmt.Errorf("log size max should be negative or >= %d", OCIBufSize)
}

if _, err := units.FromHumanSize(c.ShmSize); err != nil {
return errors.Errorf("invalid --shm-size %s, %q", c.ShmSize, err)
return fmt.Errorf("invalid --shm-size %s, %q", c.ShmSize, err)
}

return nil
Expand All @@ -878,11 +878,11 @@ func (c *NetworkConfig) Validate() error {
if &c.DefaultSubnetPools != &DefaultSubnetPools {
for _, pool := range c.DefaultSubnetPools {
if pool.Base.IP.To4() == nil {
return errors.Errorf("invalid subnet pool ip %q", pool.Base.IP)
return fmt.Errorf("invalid subnet pool ip %q", pool.Base.IP)
}
ones, _ := pool.Base.IPNet.Mask.Size()
if ones > pool.Size {
return errors.Errorf("invalid subnet pool, size is bigger than subnet %q", &pool.Base.IPNet)
return fmt.Errorf("invalid subnet pool, size is bigger than subnet %q", &pool.Base.IPNet)
}
if pool.Size > 32 {
return errors.New("invalid subnet pool size, must be between 0-32")
Expand All @@ -900,7 +900,7 @@ func (c *NetworkConfig) Validate() error {
}
}

return errors.Errorf("invalid cni_plugin_dirs: %s", strings.Join(c.CNIPluginDirs, ","))
return fmt.Errorf("invalid cni_plugin_dirs: %s", strings.Join(c.CNIPluginDirs, ","))
}

// FindConmon iterates over (*Config).ConmonPath and returns the path
Expand Down Expand Up @@ -937,14 +937,12 @@ func (c *Config) FindConmon() (string, error) {
}

if foundOutdatedConmon {
return "", errors.Wrapf(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, ErrConmonOutdated)
}

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

// GetDefaultEnv returns the environment variables for the container.
Expand Down Expand Up @@ -1001,7 +999,7 @@ func Device(device string) (src, dst, permissions string, err error) {
switch len(split) {
case 3:
if !IsValidDeviceMode(split[2]) {
return "", "", "", errors.Errorf("invalid device mode: %s", split[2])
return "", "", "", fmt.Errorf("invalid device mode: %s", split[2])
}
permissions = split[2]
fallthrough
Expand All @@ -1010,18 +1008,18 @@ func Device(device string) (src, dst, permissions string, err error) {
permissions = split[1]
} else {
if split[1] == "" || split[1][0] != '/' {
return "", "", "", errors.Errorf("invalid device mode: %s", split[1])
return "", "", "", fmt.Errorf("invalid device mode: %s", split[1])
}
dst = split[1]
}
fallthrough
case 1:
if !strings.HasPrefix(split[0], "/dev/") {
return "", "", "", errors.Errorf("invalid device mode: %s", split[0])
return "", "", "", fmt.Errorf("invalid device mode: %s", split[0])
}
src = split[0]
default:
return "", "", "", errors.Errorf("invalid device specification: %s", device)
return "", "", "", fmt.Errorf("invalid device specification: %s", device)
}

if dst == "" {
Expand Down Expand Up @@ -1204,14 +1202,14 @@ func (c *Config) ActiveDestination() (uri, identity string, err error) {
case connEnv != "":
d, found := c.Engine.ServiceDestinations[connEnv]
if !found {
return "", "", errors.Errorf("environment variable CONTAINER_CONNECTION=%q service destination not found", connEnv)
return "", "", fmt.Errorf("environment variable CONTAINER_CONNECTION=%q service destination not found", connEnv)
}
return d.URI, d.Identity, nil

case c.Engine.ActiveService != "":
d, found := c.Engine.ServiceDestinations[c.Engine.ActiveService]
if !found {
return "", "", errors.Errorf("%q service destination not found", c.Engine.ActiveService)
return "", "", fmt.Errorf("%q service destination not found", c.Engine.ActiveService)
}
return d.URI, d.Identity, nil
case c.Engine.RemoteURI != "":
Expand Down Expand Up @@ -1241,9 +1239,9 @@ func (c *Config) FindHelperBinary(name string, searchPATH bool) (string, error)
}
configHint := "To resolve this error, set the helper_binaries_dir key in the `[engine]` section of containers.conf to the directory containing your helper binaries."
if len(c.Engine.HelperBinariesDir) == 0 {
return "", errors.Errorf("could not find %q because there are no helper binary directories configured. %s", name, configHint)
return "", fmt.Errorf("could not find %q because there are no helper binary directories configured. %s", name, configHint)
}
return "", errors.Errorf("could not find %q in one of %v. %s", name, c.Engine.HelperBinariesDir, configHint)
return "", fmt.Errorf("could not find %q in one of %v. %s", name, c.Engine.HelperBinariesDir, configHint)
}

// ImageCopyTmpDir default directory to store temporary image files during copy
Expand All @@ -1262,7 +1260,7 @@ func (c *Config) ImageCopyTmpDir() (string, error) {
}
}

return "", errors.Errorf("invalid image_copy_tmp_dir value %q (relative paths are not accepted)", c.Engine.ImageCopyTmpDir)
return "", fmt.Errorf("invalid image_copy_tmp_dir value %q (relative paths are not accepted)", c.Engine.ImageCopyTmpDir)
}

// setupEnv sets the environment variables for the engine
Expand Down
14 changes: 7 additions & 7 deletions pkg/config/config_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
package config

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"syscall"

units "github.com/docker/go-units"
"github.com/pkg/errors"
)

// isDirectory tests whether the given path exists and is a directory. It
Expand Down Expand Up @@ -44,13 +44,13 @@ func (c *EngineConfig) validatePaths() error {
// shift between runs or even parts of the program. - The OCI runtime
// uses a different working directory than we do, for example.
if c.StaticDir != "" && !filepath.IsAbs(c.StaticDir) {
return errors.Errorf("static directory must be an absolute path - instead got %q", c.StaticDir)
return fmt.Errorf("static directory must be an absolute path - instead got %q", c.StaticDir)
}
if c.TmpDir != "" && !filepath.IsAbs(c.TmpDir) {
return errors.Errorf("temporary directory must be an absolute path - instead got %q", c.TmpDir)
return fmt.Errorf("temporary directory must be an absolute path - instead got %q", c.TmpDir)
}
if c.VolumePath != "" && !filepath.IsAbs(c.VolumePath) {
return errors.Errorf("volume path must be an absolute path - instead got %q", c.VolumePath)
return fmt.Errorf("volume path must be an absolute path - instead got %q", c.VolumePath)
}
return nil
}
Expand All @@ -69,7 +69,7 @@ func (c *ContainersConfig) validateUlimits() error {
for _, u := range c.DefaultUlimits {
ul, err := units.ParseUlimit(u)
if err != nil {
return errors.Wrapf(err, "unrecognized ulimit %s", u)
return fmt.Errorf("unrecognized ulimit %s: %w", u, err)
}
_, err = ul.GetRlimit()
if err != nil {
Expand Down Expand Up @@ -97,7 +97,7 @@ func (c *ContainersConfig) validateTZ() error {
}
}

return errors.Errorf(
return fmt.Errorf(
"find timezone %s in paths: %s",
c.TZ, strings.Join(lookupPaths, ", "),
)
Expand All @@ -106,7 +106,7 @@ func (c *ContainersConfig) validateTZ() error {
func (c *ContainersConfig) validateUmask() error {
validUmask := regexp.MustCompile(`^[0-7]{1,4}$`)
if !validUmask.MatchString(c.Umask) {
return errors.Errorf("not a valid umask %s", c.Umask)
return fmt.Errorf("not a valid umask %s", c.Umask)
}
return nil
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"bytes"
"errors"
"fmt"
"net"
"os"
Expand All @@ -19,7 +20,6 @@ import (
"github.com/containers/storage/pkg/unshare"
"github.com/containers/storage/types"
"github.com/opencontainers/selinux/go-selinux"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand All @@ -35,7 +35,7 @@ const (

// _conmonVersionFormatErr is used when the expected versio-format of conmon
// has changed.
_conmonVersionFormatErr = "conmon version changed format"
_conmonVersionFormatErr = "conmon version changed format: %w"

// _defaultGraphRoot points to the default path of the graph root.
_defaultGraphRoot = "/var/lib/containers/storage"
Expand Down Expand Up @@ -83,7 +83,7 @@ var (
"CAP_SYS_CHROOT",
}

// It may seem a bit unconventional, but it is necessary to do so.
// Search these locations in which CNIPlugins can be installed.
DefaultCNIPluginDirs = []string{
"/usr/local/libexec/cni",
"/usr/libexec/cni",
Expand Down Expand Up @@ -151,7 +151,7 @@ const (
DefaultRootlessSignaturePolicyPath = "containers/policy.json"
// DefaultShmSize is the default upper limit on the size of tmpfs mounts.
DefaultShmSize = "65536k"
// DefaultUserNSSize default value.
// DefaultUserNSSize indicates the default number of UIDs allocated for user namespace within a container.
DefaultUserNSSize = 65536
// OCIBufSize limits maximum LogSizeMax.
OCIBufSize = 8192
Expand Down Expand Up @@ -416,7 +416,7 @@ func defaultTmpDir() (string, error) {
return "", err
} else if err := os.Chmod(libpodRuntimeDir, 0o700|os.ModeSticky); err != nil {
// The directory already exists, so we try to make sure it's private and has the sticky bit set on it.
return "", errors.Wrap(err, "set sticky bit on")
return "", fmt.Errorf("set sticky bit on: %w", err)
}
}
return filepath.Join(libpodRuntimeDir, "tmp"), nil
Expand All @@ -439,7 +439,7 @@ func probeConmon(conmonBinary string) error {
}
major, err := strconv.Atoi(matches[1])
if err != nil {
return errors.Wrap(err, _conmonVersionFormatErr)
return fmt.Errorf(_conmonVersionFormatErr, err)
}
if major < _conmonMinMajorVersion {
return ErrConmonOutdated
Expand All @@ -450,7 +450,7 @@ func probeConmon(conmonBinary string) error {

minor, err := strconv.Atoi(matches[2])
if err != nil {
return errors.Wrap(err, _conmonVersionFormatErr)
return fmt.Errorf(_conmonVersionFormatErr, err)
}
if minor < _conmonMinMinorVersion {
return ErrConmonOutdated
Expand All @@ -461,7 +461,7 @@ func probeConmon(conmonBinary string) error {

patch, err := strconv.Atoi(matches[3])
if err != nil {
return errors.Wrap(err, _conmonVersionFormatErr)
return fmt.Errorf(_conmonVersionFormatErr, err)
}
if patch < _conmonMinPatchVersion {
return ErrConmonOutdated
Expand Down Expand Up @@ -623,10 +623,10 @@ func machineVolumes(volumes []string) ([]string, error) {
vol := os.ExpandEnv(v)
split := strings.Split(vol, ":")
if len(split) < 2 || len(split) > 3 {
return nil, errors.Errorf("invalid machine volume %s, 2 or 3 fields required", v)
return nil, fmt.Errorf("invalid machine volume %s, 2 or 3 fields required", v)
}
if split[0] == "" || split[1] == "" {
return nil, errors.Errorf("invalid machine volume %s, fields must container data", v)
return nil, fmt.Errorf("invalid machine volume %s, fields must container data", v)
}
translatedVolumes = append(translatedVolumes, vol)
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/config/pull_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package config

import (
"fmt"

"github.com/pkg/errors"
)

// PullPolicy determines how and which images are being pulled from a container
Expand Down Expand Up @@ -63,7 +61,7 @@ func (p PullPolicy) Validate() error {
case PullPolicyAlways, PullPolicyMissing, PullPolicyNewer, PullPolicyNever:
return nil
default:
return errors.Errorf("unsupported pull policy %d", p)
return fmt.Errorf("unsupported pull policy %d", p)
}
}

Expand All @@ -85,7 +83,7 @@ func ParsePullPolicy(s string) (PullPolicy, error) {
case "never", "Never":
return PullPolicyNever, nil
default:
return PullPolicyUnsupported, errors.Errorf("unsupported pull policy %q", s)
return PullPolicyUnsupported, fmt.Errorf("unsupported pull policy %q", s)
}
}

Expand Down

0 comments on commit d34ec66

Please sign in to comment.