Skip to content

Commit

Permalink
Replace github.com/pkg/errors with standard errors
Browse files Browse the repository at this point in the history
The non standard:

    errors.Wrap(err, "msg")

was replaced with:

    fmt.Errorf("msg: %w", err)

Fixes: #1678
Signed-off-by: Nir Soffer <[email protected]>
  • Loading branch information
nirs committed Dec 5, 2024
1 parent 41789cc commit ba5f2dc
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 48 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/onsi/ginkgo/v2 v2.20.2
github.com/onsi/gomega v1.34.2
github.com/operator-framework/api v0.17.6
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.20.4
github.com/ramendr/ramen/api v0.0.0-20240924121439-b7cba82de417
github.com/ramendr/recipe v0.0.0-20240918115450-667b9d79599f
Expand Down Expand Up @@ -76,6 +75,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.59.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/drplacementcontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package controllers

import (
"context"
"errors"
"fmt"
"reflect"
"time"

"github.com/go-logr/logr"
errorswrapper "github.com/pkg/errors"
"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -40,11 +40,11 @@ const (
)

var (
WaitForAppResourceRestoreToComplete error = errorswrapper.New("Waiting for App resources to be restored...")
WaitForVolSyncDestRepToComplete error = errorswrapper.New("Waiting for VolSync RD to complete...")
WaitForSourceCluster error = errorswrapper.New("Waiting for primary to provide Protected PVCs...")
WaitForVolSyncManifestWorkCreation error = errorswrapper.New("Waiting for VolSync ManifestWork to be created...")
WaitForVolSyncRDInfoAvailability error = errorswrapper.New("Waiting for VolSync RDInfo...")
WaitForAppResourceRestoreToComplete error = errors.New("Waiting for App resources to be restored...")
WaitForVolSyncDestRepToComplete error = errors.New("Waiting for VolSync RD to complete...")
WaitForSourceCluster error = errors.New("Waiting for primary to provide Protected PVCs...")
WaitForVolSyncManifestWorkCreation error = errors.New("Waiting for VolSync ManifestWork to be created...")
WaitForVolSyncRDInfoAvailability error = errors.New("Waiting for VolSync RDInfo...")
)

type DRType string
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/kubeobjects/velero/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ package velero
import (
"context"
"errors"
"fmt"

"github.com/go-logr/logr"
pkgerrors "github.com/pkg/errors"
"github.com/ramendr/ramen/internal/controller/kubeobjects"
velero "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -109,7 +109,7 @@ func (RequestsManager) ProtectRequestsDelete(
}

if err := writer.DeleteAllOf(ctx, &velero.Backup{}, options...); err != nil {
return pkgerrors.Wrap(err, "backup requests delete")
return fmt.Errorf("backup requests delete: %w", err)
}

return writer.DeleteAllOf(ctx, &velero.BackupStorageLocation{}, options...)
Expand All @@ -125,7 +125,7 @@ func (r RequestsManager) RecoverRequestsDelete(
client.InNamespace(requestNamespaceName),
client.MatchingLabels(labels),
); err != nil {
return pkgerrors.Wrap(err, "restore requests delete")
return fmt.Errorf("restore requests delete: %w", err)
}

return r.ProtectRequestsDelete(ctx, writer, requestNamespaceName, labels)
Expand Down Expand Up @@ -210,7 +210,7 @@ func restoreRealCreate(
annotations,
)
if err != nil {
return nil, pkgerrors.Wrap(err, "backup dummy request create")
return nil, fmt.Errorf("backup dummy request create: %w", err)
}

return nil, err
Expand Down Expand Up @@ -496,7 +496,7 @@ func (w objectWriter) restoreObjectsDelete(
func (w objectWriter) objectCreate(o client.Object) error {
if err := w.Create(w.ctx, o); err != nil {
if !k8serrors.IsAlreadyExists(err) {
return pkgerrors.Wrap(err, "object create")
return fmt.Errorf("object create: %w", err)
}

w.log.Info("Object created previously", "type", o.GetObjectKind(), "name", o.GetName())
Expand All @@ -510,7 +510,7 @@ func (w objectWriter) objectCreate(o client.Object) error {
func (w objectWriter) objectDelete(o client.Object) error {
if err := w.Delete(w.ctx, o); err != nil {
if !k8serrors.IsNotFound(err) {
return pkgerrors.Wrap(err, "object delete")
return fmt.Errorf("object delete: %w", err)
}

w.log.Info("Object deleted previously", "type", o.GetObjectKind(), "name", o.GetName())
Expand Down
7 changes: 3 additions & 4 deletions internal/controller/s3utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/go-logr/logr"
errorswrapper "github.com/pkg/errors"
ramen "github.com/ramendr/ramen/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -233,7 +232,7 @@ func (s *s3ObjectStore) CreateBucket(bucket string) (err error) {
_, err = s.client.CreateBucket(cbInput)
if err != nil {
var aerr awserr.Error
if errorswrapper.As(err, &aerr) {
if errors.As(err, &aerr) {
switch aerr.Code() {
case s3.ErrCodeBucketAlreadyExists:
case s3.ErrCodeBucketAlreadyOwnedByYou:
Expand Down Expand Up @@ -575,7 +574,7 @@ func (s *s3ObjectStore) DownloadObject(key string,
}

gzReader, err := gzip.NewReader(bytes.NewReader(writerAt.Bytes()))
if err != nil && !errorswrapper.Is(err, io.EOF) {
if err != nil && !errors.Is(err, io.EOF) {
return fmt.Errorf("failed to unzip data of %s:%s, %w",
bucket, key, err)
}
Expand Down Expand Up @@ -665,7 +664,7 @@ func (s *s3ObjectStore) DeleteObjects(keys ...string) error {
// the awserr.ErrCodeNoSuchBucket anywhere in its chain of errors.
func isAwsErrCodeNoSuchBucket(err error) bool {
var aerr awserr.Error
if errorswrapper.As(err, &aerr) {
if errors.As(err, &aerr) {
if aerr.Code() == s3.ErrCodeNoSuchBucket {
return true
}
Expand Down
13 changes: 6 additions & 7 deletions internal/controller/util/mcv_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
volrep "github.com/csi-addons/kubernetes-csi-addons/api/replication.storage/v1alpha1"
"github.com/go-logr/logr"
snapv1 "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1"
errorswrapper "github.com/pkg/errors"
storagev1 "k8s.io/api/storage/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -303,7 +302,7 @@ func (m ManagedClusterViewGetterImpl) getManagedClusterResource(
// create MCV first
mcv, err := m.getOrCreateManagedClusterView(meta, viewscope, logger)
if err != nil {
return errorswrapper.Wrap(err, "getManagedClusterResource failed")
return fmt.Errorf("getManagedClusterResource failed: %w", err)
}

logger.Info(fmt.Sprintf("Get managedClusterResource Returned the following MCV Conditions: %v",
Expand Down Expand Up @@ -362,13 +361,13 @@ func (m ManagedClusterViewGetterImpl) GetResource(mcv *viewv1beta1.ManagedCluste
}

if err != nil {
return errorswrapper.Wrap(err, "getManagedClusterResource results")
return fmt.Errorf("getManagedClusterResource results: %w", err)
}

// good path: convert raw data to usable object
err = json.Unmarshal(mcv.Status.Result.Raw, resource)
if err != nil {
return errorswrapper.Wrap(err, "failed to Unmarshal data from ManagedClusterView to resource")
return fmt.Errorf("failed to Unmarshal data from ManagedClusterView to resource: %w", err)
}

return nil // success
Expand Down Expand Up @@ -397,14 +396,14 @@ func (m ManagedClusterViewGetterImpl) getOrCreateManagedClusterView(
err := m.Get(context.TODO(), key, mcv)
if err != nil {
if !k8serrors.IsNotFound(err) {
return nil, errorswrapper.Wrap(err, "failed to get ManagedClusterView")
return nil, fmt.Errorf("failed to get ManagedClusterView: %w", err)
}

logger.Info(fmt.Sprintf("Creating ManagedClusterView %s with scope %s",
key, viewscope.Name))

if err := m.Create(context.TODO(), mcv); err != nil {
return nil, errorswrapper.Wrap(err, "failed to create ManagedClusterView")
return nil, fmt.Errorf("failed to create ManagedClusterView: %w", err)
}
}

Expand All @@ -415,7 +414,7 @@ func (m ManagedClusterViewGetterImpl) getOrCreateManagedClusterView(

mcv.Spec.Scope = viewscope
if err := m.Update(context.TODO(), mcv); err != nil {
return nil, errorswrapper.Wrap(err, "failed to update ManagedClusterView")
return nil, fmt.Errorf("failed to update ManagedClusterView: %w", err)
}
}

Expand Down
3 changes: 1 addition & 2 deletions internal/controller/util/mw_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"reflect"

"github.com/go-logr/logr"
errorswrapper "github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -612,7 +611,7 @@ func (mwu *MWUtil) createOrUpdateManifestWork(
err := mwu.Client.Get(mwu.Ctx, key, foundMW)
if err != nil {
if !k8serrors.IsNotFound(err) {
return ctrlutil.OperationResultNone, errorswrapper.Wrap(err, fmt.Sprintf("failed to fetch ManifestWork %s", key))
return ctrlutil.OperationResultNone, fmt.Errorf("failed to fetch ManifestWork %s: %w", key, err)
}

mwu.Log.Info("Creating ManifestWork", "cluster", managedClusternamespace, "MW", mw)
Expand Down
37 changes: 15 additions & 22 deletions internal/controller/util/secrets_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"fmt"

"github.com/go-logr/logr"
errorswrapper "github.com/pkg/errors"
plrv1 "github.com/stolostron/multicloud-operators-placementrule/pkg/apis/apps/v1"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -388,8 +387,7 @@ func (sutil *SecretsUtil) createPolicyResources(
if err := sutil.Client.Update(sutil.Ctx, secret); err != nil {
sutil.Log.Error(err, "unable to add finalizer to secret", "secret", secret.Name, "cluster", cluster)

return errorswrapper.Wrap(err, fmt.Sprintf("unable to add finalizer to secret (secret: %s, cluster: %s)",
secret.Name, cluster))
return fmt.Errorf("unable to add finalizer to secret (secret: %s, cluster: %s): %w", secret.Name, cluster, err)
}
}

Expand All @@ -406,8 +404,7 @@ func (sutil *SecretsUtil) createPolicyResources(
if err := sutil.Client.Create(sutil.Ctx, plRuleBindingObject); err != nil && !k8serrors.IsAlreadyExists(err) {
sutil.Log.Error(err, "unable to create placement binding", "secret", secret.Name, "cluster", cluster)

return errorswrapper.Wrap(err, fmt.Sprintf("unable to create placement binding (secret: %s, cluster: %s)",
secret.Name, cluster))
return fmt.Errorf("unable to create placement binding (secret: %s, cluster: %s): %w", secret.Name, cluster, err)
}

// Create a Policy object for the secret
Expand All @@ -421,17 +418,15 @@ func (sutil *SecretsUtil) createPolicyResources(
if err := sutil.Client.Create(sutil.Ctx, policyObject); err != nil && !k8serrors.IsAlreadyExists(err) {
sutil.Log.Error(err, "unable to create policy", "secret", secret.Name, "cluster", cluster)

return errorswrapper.Wrap(err, fmt.Sprintf("unable to create policy (secret: %s, cluster: %s)",
secret.Name, cluster))
return fmt.Errorf("unable to create policy (secret: %s, cluster: %s): %w", secret.Name, cluster, err)
}

// Create a PlacementRule, including cluster
plRuleObject := newPlacementRule(plRuleName, namespace, []string{cluster})
if err := sutil.Client.Create(sutil.Ctx, plRuleObject); err != nil && !k8serrors.IsAlreadyExists(err) {
sutil.Log.Error(err, "unable to create placement rule", "secret", secret.Name, "cluster", cluster)

return errorswrapper.Wrap(err, fmt.Sprintf("unable to create placement rule (secret: %s, cluster: %s)",
secret.Name, cluster))
return fmt.Errorf("unable to create placement rule (secret: %s, cluster: %s): %w", secret.Name, cluster, err)
}

return nil
Expand Down Expand Up @@ -478,7 +473,7 @@ func (sutil *SecretsUtil) deletePolicyResources(
if err := sutil.Client.Delete(sutil.Ctx, plRuleBindingObject); err != nil && !k8serrors.IsNotFound(err) {
sutil.Log.Error(err, "unable to delete placement binding", "secret", secret.Name)

return errorswrapper.Wrap(err, fmt.Sprintf("unable to delete placement binding (secret: %s)", secret.Name))
return fmt.Errorf("unable to delete placement binding (secret: %s): %w", secret.Name, err)
}

policyObject := &gppv1.Policy{
Expand All @@ -490,7 +485,7 @@ func (sutil *SecretsUtil) deletePolicyResources(
if err := sutil.Client.Delete(sutil.Ctx, policyObject); err != nil && !k8serrors.IsNotFound(err) {
sutil.Log.Error(err, "unable to delete policy", "secret", secret.Name)

return errorswrapper.Wrap(err, fmt.Sprintf("unable to delete policy (secret: %s)", secret.Name))
return fmt.Errorf("unable to delete policy (secret: %s): %w", secret.Name, err)
}

plRuleObject := &plrv1.PlacementRule{
Expand All @@ -502,8 +497,7 @@ func (sutil *SecretsUtil) deletePolicyResources(
if err := sutil.Client.Delete(sutil.Ctx, plRuleObject); err != nil && !k8serrors.IsNotFound(err) {
sutil.Log.Error(err, "unable to delete placement rule", "secret", secret.Name)

return errorswrapper.Wrap(err, fmt.Sprintf("unable to delete placement rule (secret: %s)",
secret.Name))
return fmt.Errorf("unable to delete placement rule (secret: %s): %w", secret.Name, err)
}

// Remove finalizer from secret. Allow secret deletion and recreation ordering for policy tickle
Expand All @@ -513,8 +507,7 @@ func (sutil *SecretsUtil) deletePolicyResources(
if err := sutil.Client.Update(sutil.Ctx, secret); err != nil {
sutil.Log.Error(err, "unable to remove finalizer from secret", "secret", secret.Name)

return errorswrapper.Wrap(err, fmt.Sprintf("unable to remove finalizer from secret (secret: %s)",
secret.Name))
return fmt.Errorf("unable to remove finalizer from secret (secret: %s): %w", secret.Name, err)
}
}

Expand Down Expand Up @@ -584,8 +577,8 @@ func (sutil *SecretsUtil) updatePlacementRule(
if err != nil {
sutil.Log.Error(err, "unable to update placement rule", "placementRule", plRule.Name, "cluster", cluster)

return !deleted, errorswrapper.Wrap(err,
fmt.Sprintf("unable to update placement rule (placementRule: %s, cluster: %s)", plRule.Name, cluster))
return !deleted, fmt.Errorf(
"unable to update placement rule (placementRule: %s, cluster: %s): %w", plRule.Name, cluster, err)
}

return !deleted, nil
Expand All @@ -606,7 +599,7 @@ func (sutil *SecretsUtil) ticklePolicy(secret *corev1.Secret, namespace string)
&policyObject); err != nil {
sutil.Log.Error(err, "unable to get policy", "secret", secret.Name)

return errorswrapper.Wrap(err, fmt.Sprintf("unable to get policy (secret: %s)", secret.Name))
return fmt.Errorf("unable to get policy (secret: %s): %w", secret.Name, err)
}

for annotation, value := range policyObject.GetAnnotations() {
Expand All @@ -621,7 +614,7 @@ func (sutil *SecretsUtil) ticklePolicy(secret *corev1.Secret, namespace string)
if err := sutil.Client.Update(sutil.Ctx, &policyObject); err != nil {
sutil.Log.Error(err, "unable to trigger policy update", "secret", secret.Name)

return errorswrapper.Wrap(err, fmt.Sprintf("unable to trigger policy update (secret: %s)", secret.Name))
return fmt.Errorf("unable to trigger policy update (secret: %s): %w", secret.Name, err)
}

return nil
Expand Down Expand Up @@ -655,7 +648,7 @@ func (sutil *SecretsUtil) ensureS3SecretResources(
types.NamespacedName{Namespace: namespace, Name: secretName},
&secret); err != nil {
if !k8serrors.IsNotFound(err) {
return nil, errorswrapper.Wrap(err, "failed to get secret object")
return nil, fmt.Errorf("failed to get secret object: %w", err)
}

// Cleanup policy for missing secret
Expand Down Expand Up @@ -717,7 +710,7 @@ func (sutil *SecretsUtil) AddSecretToCluster(
err = sutil.APIReader.Get(sutil.Ctx, plRuleName, plRule)
if err != nil {
if !k8serrors.IsNotFound(err) {
return errorswrapper.Wrap(err, "failed to get placementRule object")
return fmt.Errorf("failed to get placementRule object: %w", err)
}

return sutil.createPolicyResources(secret, clusterName, namespace, targetNS, format, veleroNS)
Expand Down Expand Up @@ -754,7 +747,7 @@ func (sutil *SecretsUtil) RemoveSecretFromCluster(
err = sutil.APIReader.Get(sutil.Ctx, plRuleName, plRule)
if err != nil {
if !k8serrors.IsNotFound(err) {
return errorswrapper.Wrap(err, "failed to get placementRule object")
return fmt.Errorf("failed to get placementRule object: %w", err)
}

// Ensure all related resources and finalizers are deleted
Expand Down

0 comments on commit ba5f2dc

Please sign in to comment.