cleanup: use standard Golang errors package

"github.com/pkg/errors" does not offer more functionlity than that we
need from the standard "errors" package. With Golang v1.13 errors can be
wrapped with `fmt.Errorf("... %w", err)`. `errors.Is()` and
`errors.As()` are available as well.

See-also: https://tip.golang.org/doc/go1.13#error_wrapping
Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos
2020-06-25 13:30:04 +02:00
committed by mergify[bot]
parent 8effa0cd3e
commit 92aae4834e
10 changed files with 52 additions and 52 deletions

View File

@ -23,7 +23,6 @@ import (
"time"
"github.com/ceph/go-ceph/rados"
"github.com/pkg/errors"
)
type connEntry struct {
@ -99,7 +98,7 @@ func (cp *ConnPool) generateUniqueKey(monitors, user, keyfile string) (string, e
// the keyfile can be unique for operations, contents will be the same
key, err := ioutil.ReadFile(keyfile) // nolint: gosec, #nosec
if err != nil {
return "", errors.Wrapf(err, "could not open keyfile %s", keyfile)
return "", fmt.Errorf("could not open keyfile %s: %w", keyfile, err)
}
return fmt.Sprintf("%s|%s|%s", monitors, user, string(key)), nil
@ -123,7 +122,7 @@ func (cp *ConnPool) getConn(unique string) *rados.Conn {
func (cp *ConnPool) Get(monitors, user, keyfile string) (*rados.Conn, error) {
unique, err := cp.generateUniqueKey(monitors, user, keyfile)
if err != nil {
return nil, errors.Wrapf(err, "failed to generate unique for connection")
return nil, fmt.Errorf("failed to generate unique for connection: %w", err)
}
cp.lock.RLock()
@ -137,16 +136,16 @@ func (cp *ConnPool) Get(monitors, user, keyfile string) (*rados.Conn, error) {
args := []string{"-m", monitors, "--keyfile=" + keyfile}
conn, err = rados.NewConnWithUser(user)
if err != nil {
return nil, errors.Wrapf(err, "creating a new connection failed")
return nil, fmt.Errorf("creating a new connection failed: %w", err)
}
err = conn.ParseCmdLineArgs(args)
if err != nil {
return nil, errors.Wrapf(err, "parsing cmdline args (%v) failed", args)
return nil, fmt.Errorf("parsing cmdline args (%v) failed: %w", args, err)
}
err = conn.Connect()
if err != nil {
return nil, errors.Wrapf(err, "connecting failed")
return nil, fmt.Errorf("connecting failed: %w", err)
}
ce := &connEntry{

View File

@ -17,10 +17,11 @@ limitations under the License.
package util
import (
"errors"
"fmt"
"time"
"github.com/ceph/go-ceph/rados"
"github.com/pkg/errors"
)
type ClusterConnection struct {
@ -46,7 +47,7 @@ func (cc *ClusterConnection) Connect(monitors string, cr *Credentials) error {
if cc.conn == nil {
conn, err := connPool.Get(monitors, cr.ID, cr.KeyFile)
if err != nil {
return errors.Wrapf(err, "failed to get connection")
return fmt.Errorf("failed to get connection: %w", err)
}
cc.conn = conn
@ -75,7 +76,7 @@ func (cc *ClusterConnection) GetIoctx(pool string) (*rados.IOContext, error) {
if err == rados.ErrNotFound {
err = ErrPoolNotFound{pool, err}
} else {
err = errors.Wrapf(err, "failed to open IOContext for pool %s", pool)
err = fmt.Errorf("failed to open IOContext for pool %s: %w", pool, err)
}
return nil, err
}

View File

@ -20,13 +20,12 @@ import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"path"
"strings"
"github.com/pkg/errors"
"crypto/rand"
"k8s.io/klog"
@ -186,7 +185,7 @@ func VolumeMapper(volumeID string) (mapperFile, mapperFilePath string) {
func EncryptVolume(ctx context.Context, devicePath, passphrase string) error {
klog.V(4).Infof(Log(ctx, "Encrypting device %s with LUKS"), devicePath)
if _, _, err := LuksFormat(devicePath, passphrase); err != nil {
return errors.Wrapf(err, "failed to encrypt device %s with LUKS", devicePath)
return fmt.Errorf("failed to encrypt device %s with LUKS: %w", devicePath, err)
}
return nil
}

View File

@ -23,7 +23,6 @@ import (
"os"
"regexp"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -97,7 +96,7 @@ func (k8scm *K8sCMCache) ForAll(pattern string, destObj interface{}, f ForAllFun
listOpts := metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", csiMetadataLabelAttr, cmLabel)}
cms, err := k8scm.Client.CoreV1().ConfigMaps(k8scm.Namespace).List(context.TODO(), listOpts)
if err != nil {
return errors.Wrap(err, "k8s-cm-cache: failed to list metadata configmaps")
return fmt.Errorf("k8s-cm-cache: failed to list metadata configmaps: %w", err)
}
for i := range cms.Items {
@ -110,7 +109,7 @@ func (k8scm *K8sCMCache) ForAll(pattern string, destObj interface{}, f ForAllFun
continue
}
if err = json.Unmarshal([]byte(data), destObj); err != nil {
return errors.Wrapf(err, "k8s-cm-cache: JSON unmarshaling failed for configmap %s", cms.Items[i].ObjectMeta.Name)
return fmt.Errorf("k8s-cm-cache: JSON unmarshaling failed for configmap %s: %w", cms.Items[i].ObjectMeta.Name, err)
}
if err = f(cms.Items[i].ObjectMeta.Name); err != nil {
return err
@ -128,7 +127,7 @@ func (k8scm *K8sCMCache) Create(identifier string, data interface{}) error {
}
dataJSON, err := json.Marshal(data)
if err != nil {
return errors.Wrapf(err, "k8s-cm-cache: JSON marshaling failed for configmap %s", identifier)
return fmt.Errorf("k8s-cm-cache: JSON marshaling failed for configmap %s: %w", identifier, err)
}
cm = &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
@ -148,7 +147,7 @@ func (k8scm *K8sCMCache) Create(identifier string, data interface{}) error {
klog.V(4).Infof("k8s-cm-cache: configmap %s already exists", identifier)
return nil
}
return errors.Wrapf(err, "k8s-cm-cache: couldn't persist %s metadata as configmap", identifier)
return fmt.Errorf("k8s-cm-cache: couldn't persist %s metadata as configmap: %w", identifier, err)
}
klog.V(4).Infof("k8s-cm-cache: configmap %s successfully created", identifier)
@ -167,7 +166,7 @@ func (k8scm *K8sCMCache) Get(identifier string, data interface{}) error {
}
err = json.Unmarshal([]byte(cm.Data[cmDataKey]), data)
if err != nil {
return errors.Wrapf(err, "k8s-cm-cache: JSON unmarshaling failed for configmap %s", identifier)
return fmt.Errorf("k8s-cm-cache: JSON unmarshaling failed for configmap %s: %w", identifier, err)
}
return nil
}
@ -181,7 +180,7 @@ func (k8scm *K8sCMCache) Delete(identifier string) error {
return nil
}
return errors.Wrapf(err, "k8s-cm-cache: couldn't delete metadata configmap %s", identifier)
return fmt.Errorf("k8s-cm-cache: couldn't delete metadata configmap %s: %w", identifier, err)
}
klog.V(4).Infof("k8s-cm-cache: successfully deleted metadata configmap %s", identifier)
return nil

View File

@ -18,6 +18,8 @@ package util
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
@ -25,7 +27,6 @@ import (
"regexp"
"strings"
"github.com/pkg/errors"
"k8s.io/klog"
)
@ -43,7 +44,7 @@ func (nc *NodeCache) EnsureCacheDirectory(cacheDir string) error {
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
// #nosec
if err := os.Mkdir(fullPath, 0755); err != nil {
return errors.Wrapf(err, "node-cache: failed to create %s folder", fullPath)
return fmt.Errorf("node-cache: failed to create %s folder: %w", fullPath, err)
}
}
return nil
@ -53,11 +54,11 @@ func (nc *NodeCache) EnsureCacheDirectory(cacheDir string) error {
func (nc *NodeCache) ForAll(pattern string, destObj interface{}, f ForAllFunc) error {
err := nc.EnsureCacheDirectory(nc.CacheDir)
if err != nil {
return errors.Wrap(err, "node-cache: couldn't ensure cache directory exists")
return fmt.Errorf("node-cache: couldn't ensure cache directory exists: %w", err)
}
files, err := ioutil.ReadDir(path.Join(nc.BasePath, nc.CacheDir))
if err != nil {
return errors.Wrapf(err, "node-cache: failed to read %s folder", nc.BasePath)
return fmt.Errorf("node-cache: failed to read %s folder: %w", nc.BasePath, err)
}
cachePath := path.Join(nc.BasePath, nc.CacheDir)
for _, file := range files {
@ -91,9 +92,9 @@ func decodeObj(fpath, pattern string, file os.FileInfo, destObj interface{}) err
decoder := json.NewDecoder(fp)
if err = decoder.Decode(destObj); err != nil {
if err = fp.Close(); err != nil {
return errors.Wrapf(err, "failed to close file %s", file.Name())
return fmt.Errorf("failed to close file %s: %w", file.Name(), err)
}
return errors.Wrapf(err, "node-cache: couldn't decode file %s", file.Name())
return fmt.Errorf("node-cache: couldn't decode file %s: %w", file.Name(), err)
}
return nil
}
@ -103,7 +104,7 @@ func (nc *NodeCache) Create(identifier string, data interface{}) error {
file := path.Join(nc.BasePath, nc.CacheDir, identifier+".json")
fp, err := os.Create(file)
if err != nil {
return errors.Wrapf(err, "node-cache: failed to create metadata storage file %s\n", file)
return fmt.Errorf("node-cache: failed to create metadata storage file %s: %w", file, err)
}
defer func() {
@ -114,7 +115,7 @@ func (nc *NodeCache) Create(identifier string, data interface{}) error {
encoder := json.NewEncoder(fp)
if err = encoder.Encode(data); err != nil {
return errors.Wrapf(err, "node-cache: failed to encode metadata for file: %s\n", file)
return fmt.Errorf("node-cache: failed to encode metadata for file: %s: %w", file, err)
}
klog.V(4).Infof("node-cache: successfully saved metadata into file: %s\n", file)
return nil
@ -126,11 +127,11 @@ func (nc *NodeCache) Get(identifier string, data interface{}) error {
// #nosec
fp, err := os.Open(file)
if err != nil {
if os.IsNotExist(errors.Cause(err)) {
if errors.Is(err, os.ErrNotExist) {
return &CacheEntryNotFound{err}
}
return errors.Wrapf(err, "node-cache: open error for %s", file)
return fmt.Errorf("node-cache: open error for %s: %w", file, err)
}
defer func() {
@ -141,7 +142,7 @@ func (nc *NodeCache) Get(identifier string, data interface{}) error {
decoder := json.NewDecoder(fp)
if err = decoder.Decode(data); err != nil {
return errors.Wrap(err, "rbd: decode error")
return fmt.Errorf("rbd: decode error: %w", err)
}
return nil
@ -157,7 +158,7 @@ func (nc *NodeCache) Delete(identifier string) error {
return nil
}
return errors.Wrapf(err, "node-cache: error removing file %s", file)
return fmt.Errorf("node-cache: error removing file %s: %w", file, err)
}
klog.V(4).Infof("node-cache: successfully deleted metadata storage file at: %+v\n", file)
return nil

View File

@ -18,6 +18,8 @@ package util
import (
"context"
"errors"
"fmt"
"math"
"os"
"path"
@ -25,7 +27,6 @@ import (
"strings"
"time"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@ -145,7 +146,7 @@ func ValidateDriverName(driverName string) error {
err = errors.New(msg)
continue
}
err = errors.Wrap(err, msg)
err = fmt.Errorf("%s: %w", msg, err)
}
return err
}