pkg: switch to golang native error wrapping

We now use the golang error wrapping format specifier `%w` instead of
the deprecated github.com/pkg/errors package.

[NO NEW TESTS NEEDED]

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
This commit is contained in:
Sascha Grunert
2022-07-06 09:48:36 +02:00
parent 862cc42ddc
commit a46f798831
130 changed files with 879 additions and 875 deletions
go.mod
pkg
api
auth
autoupdate
bindings
channel
checkpoint
copy
domain
env
hooks
machine
parallel
rootless
seccomp
specgen
specgenutil
systemd/generate
trust
util
test/system
vendor

@ -2,6 +2,7 @@ package util
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"math"
@ -27,7 +28,6 @@ import (
stypes "github.com/containers/storage/types"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/term"
)
@ -68,7 +68,7 @@ func ParseRegistryCreds(creds string) (*types.DockerAuthConfig, error) {
fmt.Print("Password: ")
termPassword, err := term.ReadPassword(0)
if err != nil {
return nil, errors.Wrapf(err, "could not read password from terminal")
return nil, fmt.Errorf("could not read password from terminal: %w", err)
}
password = string(termPassword)
}
@ -129,7 +129,7 @@ func GetImageConfig(changes []string) (ImageConfig, error) {
if len(split) != 2 {
split = strings.SplitN(change, "=", 2)
if len(split) != 2 {
return ImageConfig{}, errors.Errorf("invalid change %q - must be formatted as KEY VALUE", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - must be formatted as KEY VALUE", change)
}
}
@ -139,7 +139,7 @@ func GetImageConfig(changes []string) (ImageConfig, error) {
case "USER":
// Assume literal contents are the user.
if value == "" {
return ImageConfig{}, errors.Errorf("invalid change %q - must provide a value to USER", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - must provide a value to USER", change)
}
config.User = value
case "EXPOSE":
@ -148,14 +148,14 @@ func GetImageConfig(changes []string) (ImageConfig, error) {
// Protocol must be "tcp" or "udp"
splitPort := strings.Split(value, "/")
if len(splitPort) > 2 {
return ImageConfig{}, errors.Errorf("invalid change %q - EXPOSE port must be formatted as PORT[/PROTO]", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - EXPOSE port must be formatted as PORT[/PROTO]", change)
}
portNum, err := strconv.Atoi(splitPort[0])
if err != nil {
return ImageConfig{}, errors.Wrapf(err, "invalid change %q - EXPOSE port must be an integer", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - EXPOSE port must be an integer: %w", change, err)
}
if portNum > 65535 || portNum <= 0 {
return ImageConfig{}, errors.Errorf("invalid change %q - EXPOSE port must be a valid port number", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - EXPOSE port must be a valid port number", change)
}
proto := "tcp"
if len(splitPort) > 1 {
@ -164,7 +164,7 @@ func GetImageConfig(changes []string) (ImageConfig, error) {
case "tcp", "udp":
proto = testProto
default:
return ImageConfig{}, errors.Errorf("invalid change %q - EXPOSE protocol must be TCP or UDP", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - EXPOSE protocol must be TCP or UDP", change)
}
}
if config.ExposedPorts == nil {
@ -188,7 +188,7 @@ func GetImageConfig(changes []string) (ImageConfig, error) {
key = splitEnv[0]
// We do need a key
if key == "" {
return ImageConfig{}, errors.Errorf("invalid change %q - ENV must have at least one argument", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - ENV must have at least one argument", change)
}
// Perfectly valid to not have a value
if len(splitEnv) == 2 {
@ -250,11 +250,11 @@ func GetImageConfig(changes []string) (ImageConfig, error) {
testUnmarshal = strings.Split(value, " ")
}
if len(testUnmarshal) == 0 {
return ImageConfig{}, errors.Errorf("invalid change %q - must provide at least one argument to VOLUME", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - must provide at least one argument to VOLUME", change)
}
for _, vol := range testUnmarshal {
if vol == "" {
return ImageConfig{}, errors.Errorf("invalid change %q - VOLUME paths must not be empty", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - VOLUME paths must not be empty", change)
}
if config.Volumes == nil {
config.Volumes = make(map[string]struct{})
@ -268,7 +268,7 @@ func GetImageConfig(changes []string) (ImageConfig, error) {
// WORKDIR c results in /A/b/c
// Just need to check it's not empty...
if value == "" {
return ImageConfig{}, errors.Errorf("invalid change %q - must provide a non-empty WORKDIR", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - must provide a non-empty WORKDIR", change)
}
config.WorkingDir = filepath.Join(config.WorkingDir, value)
case "LABEL":
@ -285,7 +285,7 @@ func GetImageConfig(changes []string) (ImageConfig, error) {
splitLabel := strings.SplitN(value, "=", 2)
// Unlike ENV, LABEL must have a value
if len(splitLabel) != 2 {
return ImageConfig{}, errors.Errorf("invalid change %q - LABEL must be formatted key=value", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - LABEL must be formatted key=value", change)
}
key = splitLabel[0]
val = splitLabel[1]
@ -298,7 +298,7 @@ func GetImageConfig(changes []string) (ImageConfig, error) {
}
// Check key after we strip quotations
if key == "" {
return ImageConfig{}, errors.Errorf("invalid change %q - LABEL must have a non-empty key", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - LABEL must have a non-empty key", change)
}
if config.Labels == nil {
config.Labels = make(map[string]string)
@ -308,17 +308,17 @@ func GetImageConfig(changes []string) (ImageConfig, error) {
// Check the provided signal for validity.
killSignal, err := ParseSignal(value)
if err != nil {
return ImageConfig{}, errors.Wrapf(err, "invalid change %q - KILLSIGNAL must be given a valid signal", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - KILLSIGNAL must be given a valid signal: %w", change, err)
}
config.StopSignal = fmt.Sprintf("%d", killSignal)
case "ONBUILD":
// Onbuild always appends.
if value == "" {
return ImageConfig{}, errors.Errorf("invalid change %q - ONBUILD must be given an argument", change)
return ImageConfig{}, fmt.Errorf("invalid change %q - ONBUILD must be given an argument", change)
}
config.OnBuild = append(config.OnBuild, value)
default:
return ImageConfig{}, errors.Errorf("invalid change %q - invalid instruction %s", change, outerKey)
return ImageConfig{}, fmt.Errorf("invalid change %q - invalid instruction %s", change, outerKey)
}
}
@ -336,7 +336,7 @@ func ParseSignal(rawSignal string) (syscall.Signal, error) {
}
// 64 is SIGRTMAX; wish we could get this from a standard Go library
if sig < 1 || sig > 64 {
return -1, errors.Errorf("valid signals are 1 through 64")
return -1, errors.New("valid signals are 1 through 64")
}
return sig, nil
}
@ -362,10 +362,10 @@ func GetKeepIDMapping() (*stypes.IDMappingOptions, int, int, error) {
uids, gids, err := rootless.GetConfiguredMappings()
if err != nil {
return nil, -1, -1, errors.Wrapf(err, "cannot read mappings")
return nil, -1, -1, fmt.Errorf("cannot read mappings: %w", err)
}
if len(uids) == 0 || len(gids) == 0 {
return nil, -1, -1, errors.Wrapf(err, "keep-id requires additional UIDs or GIDs defined in /etc/subuid and /etc/subgid to function correctly")
return nil, -1, -1, fmt.Errorf("keep-id requires additional UIDs or GIDs defined in /etc/subuid and /etc/subgid to function correctly: %w", err)
}
maxUID, maxGID := 0, 0
for _, u := range uids {
@ -403,10 +403,10 @@ func GetNoMapMapping() (*stypes.IDMappingOptions, int, int, error) {
}
uids, gids, err := rootless.GetConfiguredMappings()
if err != nil {
return nil, -1, -1, errors.Wrapf(err, "cannot read mappings")
return nil, -1, -1, fmt.Errorf("cannot read mappings: %w", err)
}
if len(uids) == 0 || len(gids) == 0 {
return nil, -1, -1, errors.Wrapf(err, "nomap requires additional UIDs or GIDs defined in /etc/subuid and /etc/subgid to function correctly")
return nil, -1, -1, fmt.Errorf("nomap requires additional UIDs or GIDs defined in /etc/subuid and /etc/subgid to function correctly: %w", err)
}
options.UIDMap, options.GIDMap = nil, nil
uid, gid := 0, 0
@ -566,7 +566,7 @@ func ParseInputTime(inputTime string, since bool) (time.Time, error) {
// input might be a duration
duration, err := time.ParseDuration(inputTime)
if err != nil {
return time.Time{}, errors.Errorf("unable to interpret time value")
return time.Time{}, errors.New("unable to interpret time value")
}
if since {
return time.Now().Add(-duration), nil
@ -607,7 +607,7 @@ func HomeDir() (string, error) {
if home == "" {
usr, err := user.LookupId(fmt.Sprintf("%d", rootless.GetRootlessUID()))
if err != nil {
return "", errors.Wrapf(err, "unable to resolve HOME directory")
return "", fmt.Errorf("unable to resolve HOME directory: %w", err)
}
home = usr.HomeDir
}
@ -645,12 +645,12 @@ func ValidateSysctls(strSlice []string) (map[string]string, error) {
foundMatch := false
arr := strings.Split(val, "=")
if len(arr) < 2 {
return nil, errors.Errorf("%s is invalid, sysctl values must be in the form of KEY=VALUE", val)
return nil, fmt.Errorf("%s is invalid, sysctl values must be in the form of KEY=VALUE", val)
}
trimmed := fmt.Sprintf("%s=%s", strings.TrimSpace(arr[0]), strings.TrimSpace(arr[1]))
if trimmed != val {
return nil, errors.Errorf("'%s' is invalid, extra spaces found", val)
return nil, fmt.Errorf("'%s' is invalid, extra spaces found", val)
}
if validSysctlMap[arr[0]] {
@ -666,7 +666,7 @@ func ValidateSysctls(strSlice []string) (map[string]string, error) {
}
}
if !foundMatch {
return nil, errors.Errorf("sysctl '%s' is not allowed", arr[0])
return nil, fmt.Errorf("sysctl '%s' is not allowed", arr[0])
}
}
return sysctl, nil
@ -680,9 +680,9 @@ func CreateCidFile(cidfile string, id string) error {
cidFile, err := OpenExclusiveFile(cidfile)
if err != nil {
if os.IsExist(err) {
return errors.Errorf("container id file exists. Ensure another container is not using it or delete %s", cidfile)
return fmt.Errorf("container id file exists. Ensure another container is not using it or delete %s", cidfile)
}
return errors.Errorf("opening cidfile %s", cidfile)
return fmt.Errorf("opening cidfile %s", cidfile)
}
if _, err = cidFile.WriteString(id); err != nil {
logrus.Error(err)