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

@ -18,12 +18,12 @@ package cephfs
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/pkg/errors"
"github.com/ceph/ceph-csi/internal/util"
)
@ -124,13 +124,13 @@ func getClusterInformation(options map[string]string) (*util.ClusterInfo, error)
monitors, err := util.Mons(csiConfigFile, clusterID)
if err != nil {
err = errors.Wrapf(err, "failed to fetch monitor list using clusterID (%s)", clusterID)
err = fmt.Errorf("failed to fetch monitor list using clusterID (%s): %w", clusterID, err)
return nil, err
}
subvolumeGroup, err := util.CephFSSubvolumeGroup(csiConfigFile, clusterID)
if err != nil {
err = errors.Wrapf(err, "failed to fetch subvolumegroup using clusterID (%s)", clusterID)
err = fmt.Errorf("failed to fetch subvolumegroup using clusterID (%s): %w", clusterID, err)
return nil, err
}
clusterData := &util.ClusterInfo{
@ -238,11 +238,11 @@ func newVolumeOptionsFromVolID(ctx context.Context, volID string, volOpt, secret
volOptions.FscID = vi.LocationID
if volOptions.Monitors, err = util.Mons(csiConfigFile, vi.ClusterID); err != nil {
return nil, nil, errors.Wrapf(err, "failed to fetch monitor list using clusterID (%s)", vi.ClusterID)
return nil, nil, fmt.Errorf("failed to fetch monitor list using clusterID (%s): %w", vi.ClusterID, err)
}
if volOptions.SubvolumeGroup, err = util.CephFSSubvolumeGroup(csiConfigFile, vi.ClusterID); err != nil {
return nil, nil, errors.Wrapf(err, "failed to fetch subvolumegroup list using clusterID (%s)", vi.ClusterID)
return nil, nil, fmt.Errorf("failed to fetch subvolumegroup list using clusterID (%s): %w", vi.ClusterID, err)
}
cr, err := util.NewAdminCredentials(secrets)

View File

@ -20,13 +20,13 @@ import (
"context"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"strings"
"github.com/ceph/ceph-csi/internal/util"
"github.com/pborman/uuid"
"github.com/pkg/errors"
"k8s.io/klog"
)

View File

@ -18,11 +18,11 @@ package rbd
import (
"context"
"errors"
"fmt"
"github.com/ceph/ceph-csi/internal/util"
"github.com/pkg/errors"
"k8s.io/klog"
)

View File

@ -19,6 +19,7 @@ package rbd
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
@ -36,7 +37,6 @@ import (
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/pborman/uuid"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/cloud-provider/volume/helpers"
"k8s.io/klog"
@ -189,7 +189,7 @@ func createImage(ctx context.Context, pOpts *rbdVolume, cr *util.Credentials) er
logMsg += fmt.Sprintf(", data pool %s", pOpts.DataPool)
err := options.SetString(librbd.RbdImageOptionDataPool, pOpts.DataPool)
if err != nil {
return errors.Wrapf(err, "failed to set data pool")
return fmt.Errorf("failed to set data pool: %w", err)
}
}
klog.V(4).Infof(util.Log(ctx, logMsg),
@ -198,7 +198,7 @@ func createImage(ctx context.Context, pOpts *rbdVolume, cr *util.Credentials) er
if pOpts.imageFeatureSet != 0 {
err := options.SetUint64(librbd.RbdImageOptionFeatures, uint64(pOpts.imageFeatureSet))
if err != nil {
return errors.Wrapf(err, "failed to set image features")
return fmt.Errorf("failed to set image features: %w", err)
}
}
@ -209,13 +209,13 @@ func createImage(ctx context.Context, pOpts *rbdVolume, cr *util.Credentials) er
err = pOpts.openIoctx()
if err != nil {
return errors.Wrapf(err, "failed to get IOContext")
return fmt.Errorf("failed to get IOContext: %w", err)
}
err = librbd.CreateImage(pOpts.ioctx, pOpts.RbdImageName,
uint64(util.RoundOffVolSize(pOpts.VolSize)*helpers.MiB), options)
if err != nil {
return errors.Wrapf(err, "failed to create rbd image")
return fmt.Errorf("failed to create rbd image: %w", err)
}
return nil
@ -657,7 +657,7 @@ func getMonsAndClusterID(ctx context.Context, options map[string]string) (monito
if monitors, err = util.Mons(csiConfigFile, clusterID); err != nil {
klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err)
err = errors.Wrapf(err, "failed to fetch monitor list using clusterID (%s)", clusterID)
err = fmt.Errorf("failed to fetch monitor list using clusterID (%s): %w", clusterID, err)
return
}
@ -847,7 +847,7 @@ func (rv *rbdVolume) deleteSnapshot(ctx context.Context, pOpts *rbdSnapshot) err
snap := image.GetSnapshot(pOpts.RbdSnapName)
if snap == nil {
return errors.Errorf("snapshot value is nil for %s", pOpts.RbdSnapName)
return fmt.Errorf("snapshot value is nil for %s", pOpts.RbdSnapName)
}
err = snap.Remove()
if err == librbd.ErrNotFound {
@ -866,23 +866,23 @@ func (rv *rbdVolume) cloneRbdImageFromSnapshot(ctx context.Context, pSnapOpts *r
if rv.imageFeatureSet != 0 {
err = options.SetUint64(librbd.RbdImageOptionFeatures, uint64(rv.imageFeatureSet))
if err != nil {
return errors.Wrapf(err, "failed to set image features")
return fmt.Errorf("failed to set image features: %w", err)
}
}
err = options.SetUint64(imageOptionCloneFormat, 2)
if err != nil {
return errors.Wrapf(err, "failed to set image features")
return fmt.Errorf("failed to set image features: %w", err)
}
err = rv.openIoctx()
if err != nil {
return errors.Wrapf(err, "failed to get IOContext")
return fmt.Errorf("failed to get IOContext: %w", err)
}
err = librbd.CloneImage(rv.ioctx, pSnapOpts.RbdImageName, pSnapOpts.RbdSnapName, rv.ioctx, rv.RbdImageName, options)
if err != nil {
return errors.Wrapf(err, "failed to create rbd clone")
return fmt.Errorf("failed to create rbd clone: %w", err)
}
return nil
@ -1089,7 +1089,7 @@ func resizeRBDImage(rbdVol *rbdVolume, cr *util.Credentials) error {
output, err := execCommand("rbd", args)
if err != nil {
return errors.Wrapf(err, "failed to resize rbd image, command output: %s", string(output))
return fmt.Errorf("failed to resize rbd image (%w), command output: %s", err, string(output))
}
return nil

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
}