mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-12-18 11:00:25 +00:00
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:
parent
8effa0cd3e
commit
92aae4834e
@ -18,12 +18,12 @@ package cephfs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/container-storage-interface/spec/lib/go/csi"
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
||||||
"github.com/pkg/errors"
|
|
||||||
|
|
||||||
"github.com/ceph/ceph-csi/internal/util"
|
"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)
|
monitors, err := util.Mons(csiConfigFile, clusterID)
|
||||||
if err != nil {
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
subvolumeGroup, err := util.CephFSSubvolumeGroup(csiConfigFile, clusterID)
|
subvolumeGroup, err := util.CephFSSubvolumeGroup(csiConfigFile, clusterID)
|
||||||
if err != nil {
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
clusterData := &util.ClusterInfo{
|
clusterData := &util.ClusterInfo{
|
||||||
@ -238,11 +238,11 @@ func newVolumeOptionsFromVolID(ctx context.Context, volID string, volOpt, secret
|
|||||||
volOptions.FscID = vi.LocationID
|
volOptions.FscID = vi.LocationID
|
||||||
|
|
||||||
if volOptions.Monitors, err = util.Mons(csiConfigFile, vi.ClusterID); err != nil {
|
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 {
|
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)
|
cr, err := util.NewAdminCredentials(secrets)
|
||||||
|
@ -20,13 +20,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ceph/ceph-csi/internal/util"
|
"github.com/ceph/ceph-csi/internal/util"
|
||||||
|
|
||||||
"github.com/pborman/uuid"
|
"github.com/pborman/uuid"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"k8s.io/klog"
|
"k8s.io/klog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,11 +18,11 @@ package rbd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/ceph/ceph-csi/internal/util"
|
"github.com/ceph/ceph-csi/internal/util"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"k8s.io/klog"
|
"k8s.io/klog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ package rbd
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
@ -36,7 +37,6 @@ import (
|
|||||||
"github.com/golang/protobuf/ptypes"
|
"github.com/golang/protobuf/ptypes"
|
||||||
"github.com/golang/protobuf/ptypes/timestamp"
|
"github.com/golang/protobuf/ptypes/timestamp"
|
||||||
"github.com/pborman/uuid"
|
"github.com/pborman/uuid"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/cloud-provider/volume/helpers"
|
"k8s.io/cloud-provider/volume/helpers"
|
||||||
"k8s.io/klog"
|
"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)
|
logMsg += fmt.Sprintf(", data pool %s", pOpts.DataPool)
|
||||||
err := options.SetString(librbd.RbdImageOptionDataPool, pOpts.DataPool)
|
err := options.SetString(librbd.RbdImageOptionDataPool, pOpts.DataPool)
|
||||||
if err != nil {
|
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),
|
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 {
|
if pOpts.imageFeatureSet != 0 {
|
||||||
err := options.SetUint64(librbd.RbdImageOptionFeatures, uint64(pOpts.imageFeatureSet))
|
err := options.SetUint64(librbd.RbdImageOptionFeatures, uint64(pOpts.imageFeatureSet))
|
||||||
if err != nil {
|
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()
|
err = pOpts.openIoctx()
|
||||||
if err != nil {
|
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,
|
err = librbd.CreateImage(pOpts.ioctx, pOpts.RbdImageName,
|
||||||
uint64(util.RoundOffVolSize(pOpts.VolSize)*helpers.MiB), options)
|
uint64(util.RoundOffVolSize(pOpts.VolSize)*helpers.MiB), options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to create rbd image")
|
return fmt.Errorf("failed to create rbd image: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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 {
|
if monitors, err = util.Mons(csiConfigFile, clusterID); err != nil {
|
||||||
klog.Errorf(util.Log(ctx, "failed getting mons (%s)"), err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -847,7 +847,7 @@ func (rv *rbdVolume) deleteSnapshot(ctx context.Context, pOpts *rbdSnapshot) err
|
|||||||
|
|
||||||
snap := image.GetSnapshot(pOpts.RbdSnapName)
|
snap := image.GetSnapshot(pOpts.RbdSnapName)
|
||||||
if snap == nil {
|
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()
|
err = snap.Remove()
|
||||||
if err == librbd.ErrNotFound {
|
if err == librbd.ErrNotFound {
|
||||||
@ -866,23 +866,23 @@ func (rv *rbdVolume) cloneRbdImageFromSnapshot(ctx context.Context, pSnapOpts *r
|
|||||||
if rv.imageFeatureSet != 0 {
|
if rv.imageFeatureSet != 0 {
|
||||||
err = options.SetUint64(librbd.RbdImageOptionFeatures, uint64(rv.imageFeatureSet))
|
err = options.SetUint64(librbd.RbdImageOptionFeatures, uint64(rv.imageFeatureSet))
|
||||||
if err != nil {
|
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)
|
err = options.SetUint64(imageOptionCloneFormat, 2)
|
||||||
if err != nil {
|
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()
|
err = rv.openIoctx()
|
||||||
if err != nil {
|
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)
|
err = librbd.CloneImage(rv.ioctx, pSnapOpts.RbdImageName, pSnapOpts.RbdSnapName, rv.ioctx, rv.RbdImageName, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to create rbd clone")
|
return fmt.Errorf("failed to create rbd clone: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -1089,7 +1089,7 @@ func resizeRBDImage(rbdVol *rbdVolume, cr *util.Credentials) error {
|
|||||||
output, err := execCommand("rbd", args)
|
output, err := execCommand("rbd", args)
|
||||||
|
|
||||||
if err != nil {
|
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
|
return nil
|
||||||
|
@ -23,7 +23,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ceph/go-ceph/rados"
|
"github.com/ceph/go-ceph/rados"
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type connEntry struct {
|
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
|
// the keyfile can be unique for operations, contents will be the same
|
||||||
key, err := ioutil.ReadFile(keyfile) // nolint: gosec, #nosec
|
key, err := ioutil.ReadFile(keyfile) // nolint: gosec, #nosec
|
||||||
if err != nil {
|
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
|
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) {
|
func (cp *ConnPool) Get(monitors, user, keyfile string) (*rados.Conn, error) {
|
||||||
unique, err := cp.generateUniqueKey(monitors, user, keyfile)
|
unique, err := cp.generateUniqueKey(monitors, user, keyfile)
|
||||||
if err != nil {
|
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()
|
cp.lock.RLock()
|
||||||
@ -137,16 +136,16 @@ func (cp *ConnPool) Get(monitors, user, keyfile string) (*rados.Conn, error) {
|
|||||||
args := []string{"-m", monitors, "--keyfile=" + keyfile}
|
args := []string{"-m", monitors, "--keyfile=" + keyfile}
|
||||||
conn, err = rados.NewConnWithUser(user)
|
conn, err = rados.NewConnWithUser(user)
|
||||||
if err != nil {
|
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)
|
err = conn.ParseCmdLineArgs(args)
|
||||||
if err != nil {
|
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()
|
err = conn.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "connecting failed")
|
return nil, fmt.Errorf("connecting failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ce := &connEntry{
|
ce := &connEntry{
|
||||||
|
@ -17,10 +17,11 @@ limitations under the License.
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ceph/go-ceph/rados"
|
"github.com/ceph/go-ceph/rados"
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ClusterConnection struct {
|
type ClusterConnection struct {
|
||||||
@ -46,7 +47,7 @@ func (cc *ClusterConnection) Connect(monitors string, cr *Credentials) error {
|
|||||||
if cc.conn == nil {
|
if cc.conn == nil {
|
||||||
conn, err := connPool.Get(monitors, cr.ID, cr.KeyFile)
|
conn, err := connPool.Get(monitors, cr.ID, cr.KeyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to get connection")
|
return fmt.Errorf("failed to get connection: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cc.conn = conn
|
cc.conn = conn
|
||||||
@ -75,7 +76,7 @@ func (cc *ClusterConnection) GetIoctx(pool string) (*rados.IOContext, error) {
|
|||||||
if err == rados.ErrNotFound {
|
if err == rados.ErrNotFound {
|
||||||
err = ErrPoolNotFound{pool, err}
|
err = ErrPoolNotFound{pool, err}
|
||||||
} else {
|
} 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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -20,13 +20,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
|
||||||
"k8s.io/klog"
|
"k8s.io/klog"
|
||||||
@ -186,7 +185,7 @@ func VolumeMapper(volumeID string) (mapperFile, mapperFilePath string) {
|
|||||||
func EncryptVolume(ctx context.Context, devicePath, passphrase string) error {
|
func EncryptVolume(ctx context.Context, devicePath, passphrase string) error {
|
||||||
klog.V(4).Infof(Log(ctx, "Encrypting device %s with LUKS"), devicePath)
|
klog.V(4).Infof(Log(ctx, "Encrypting device %s with LUKS"), devicePath)
|
||||||
if _, _, err := LuksFormat(devicePath, passphrase); err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
apierrs "k8s.io/apimachinery/pkg/api/errors"
|
apierrs "k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
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)}
|
listOpts := metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", csiMetadataLabelAttr, cmLabel)}
|
||||||
cms, err := k8scm.Client.CoreV1().ConfigMaps(k8scm.Namespace).List(context.TODO(), listOpts)
|
cms, err := k8scm.Client.CoreV1().ConfigMaps(k8scm.Namespace).List(context.TODO(), listOpts)
|
||||||
if err != nil {
|
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 {
|
for i := range cms.Items {
|
||||||
@ -110,7 +109,7 @@ func (k8scm *K8sCMCache) ForAll(pattern string, destObj interface{}, f ForAllFun
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err = json.Unmarshal([]byte(data), destObj); err != nil {
|
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 {
|
if err = f(cms.Items[i].ObjectMeta.Name); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -128,7 +127,7 @@ func (k8scm *K8sCMCache) Create(identifier string, data interface{}) error {
|
|||||||
}
|
}
|
||||||
dataJSON, err := json.Marshal(data)
|
dataJSON, err := json.Marshal(data)
|
||||||
if err != nil {
|
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{
|
cm = &v1.ConfigMap{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
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)
|
klog.V(4).Infof("k8s-cm-cache: configmap %s already exists", identifier)
|
||||||
return nil
|
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)
|
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)
|
err = json.Unmarshal([]byte(cm.Data[cmDataKey]), data)
|
||||||
if err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
@ -181,7 +180,7 @@ func (k8scm *K8sCMCache) Delete(identifier string) error {
|
|||||||
return nil
|
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)
|
klog.V(4).Infof("k8s-cm-cache: successfully deleted metadata configmap %s", identifier)
|
||||||
return nil
|
return nil
|
||||||
|
@ -18,6 +18,8 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -25,7 +27,6 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"k8s.io/klog"
|
"k8s.io/klog"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -43,7 +44,7 @@ func (nc *NodeCache) EnsureCacheDirectory(cacheDir string) error {
|
|||||||
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
|
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
|
||||||
// #nosec
|
// #nosec
|
||||||
if err := os.Mkdir(fullPath, 0755); err != nil {
|
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
|
return nil
|
||||||
@ -53,11 +54,11 @@ func (nc *NodeCache) EnsureCacheDirectory(cacheDir string) error {
|
|||||||
func (nc *NodeCache) ForAll(pattern string, destObj interface{}, f ForAllFunc) error {
|
func (nc *NodeCache) ForAll(pattern string, destObj interface{}, f ForAllFunc) error {
|
||||||
err := nc.EnsureCacheDirectory(nc.CacheDir)
|
err := nc.EnsureCacheDirectory(nc.CacheDir)
|
||||||
if err != nil {
|
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))
|
files, err := ioutil.ReadDir(path.Join(nc.BasePath, nc.CacheDir))
|
||||||
if err != nil {
|
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)
|
cachePath := path.Join(nc.BasePath, nc.CacheDir)
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
@ -91,9 +92,9 @@ func decodeObj(fpath, pattern string, file os.FileInfo, destObj interface{}) err
|
|||||||
decoder := json.NewDecoder(fp)
|
decoder := json.NewDecoder(fp)
|
||||||
if err = decoder.Decode(destObj); err != nil {
|
if err = decoder.Decode(destObj); err != nil {
|
||||||
if err = fp.Close(); 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
|
return nil
|
||||||
}
|
}
|
||||||
@ -103,7 +104,7 @@ func (nc *NodeCache) Create(identifier string, data interface{}) error {
|
|||||||
file := path.Join(nc.BasePath, nc.CacheDir, identifier+".json")
|
file := path.Join(nc.BasePath, nc.CacheDir, identifier+".json")
|
||||||
fp, err := os.Create(file)
|
fp, err := os.Create(file)
|
||||||
if err != nil {
|
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() {
|
defer func() {
|
||||||
@ -114,7 +115,7 @@ func (nc *NodeCache) Create(identifier string, data interface{}) error {
|
|||||||
|
|
||||||
encoder := json.NewEncoder(fp)
|
encoder := json.NewEncoder(fp)
|
||||||
if err = encoder.Encode(data); err != nil {
|
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)
|
klog.V(4).Infof("node-cache: successfully saved metadata into file: %s\n", file)
|
||||||
return nil
|
return nil
|
||||||
@ -126,11 +127,11 @@ func (nc *NodeCache) Get(identifier string, data interface{}) error {
|
|||||||
// #nosec
|
// #nosec
|
||||||
fp, err := os.Open(file)
|
fp, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(errors.Cause(err)) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
return &CacheEntryNotFound{err}
|
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() {
|
defer func() {
|
||||||
@ -141,7 +142,7 @@ func (nc *NodeCache) Get(identifier string, data interface{}) error {
|
|||||||
|
|
||||||
decoder := json.NewDecoder(fp)
|
decoder := json.NewDecoder(fp)
|
||||||
if err = decoder.Decode(data); err != nil {
|
if err = decoder.Decode(data); err != nil {
|
||||||
return errors.Wrap(err, "rbd: decode error")
|
return fmt.Errorf("rbd: decode error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -157,7 +158,7 @@ func (nc *NodeCache) Delete(identifier string) error {
|
|||||||
return nil
|
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)
|
klog.V(4).Infof("node-cache: successfully deleted metadata storage file at: %+v\n", file)
|
||||||
return nil
|
return nil
|
||||||
|
@ -18,6 +18,8 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -25,7 +27,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
@ -145,7 +146,7 @@ func ValidateDriverName(driverName string) error {
|
|||||||
err = errors.New(msg)
|
err = errors.New(msg)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err = errors.Wrap(err, msg)
|
err = fmt.Errorf("%s: %w", msg, err)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user