Skip to content

Commit

Permalink
Drop github.com/pkg/errors
Browse files Browse the repository at this point in the history
The package is unmaintained. Apart from the wrapper functions, its
features are provided by built-in errors and the "errors" package.

Most of the changes are straightforward; it might be worth discussing
whether to leave the conditional wrappers as-is (or even add a utility
function for them).

Signed-off-by: Stephen Kitt <skitt@redhat.com>
  • Loading branch information
skitt committed Oct 16, 2023
1 parent 147741f commit 390f8d7
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 54 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/kelseyhightower/envconfig v1.4.0
github.com/onsi/ginkgo/v2 v2.12.0
github.com/onsi/gomega v1.27.10
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.17.0
github.com/rs/zerolog v1.31.0
github.com/submariner-io/shipyard v0.16.0-m4
Expand Down Expand Up @@ -53,6 +52,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion pkg/fake/conflict_reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ limitations under the License.
package fake

import (
"errors"
"sync"

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
2 changes: 1 addition & 1 deletion pkg/fake/delete_colection_reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ limitations under the License.
package fake

import (
"errors"
"fmt"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/syncer/test"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
2 changes: 1 addition & 1 deletion pkg/fake/dynamic_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ package fake

import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"sync/atomic"
"time"

. "github.com/onsi/gomega"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down
2 changes: 1 addition & 1 deletion pkg/fake/fail_on_action_reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ limitations under the License.
package fake

import (
"errors"
"sync/atomic"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/testing"
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/fake/filtering_watch_reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ limitations under the License.
package fake

import (
"github.com/pkg/errors"
"errors"

"github.com/submariner-io/admiral/pkg/resource"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/watch"
Expand Down
12 changes: 9 additions & 3 deletions pkg/finalizer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ package finalizer

import (
"context"
"fmt"

"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/resource"
"github.com/submariner-io/admiral/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -44,8 +44,11 @@ func Add[T runtime.Object](ctx context.Context, client resource.Interface[T], ob

return existing, nil
})
if err != nil {
return false, fmt.Errorf("error adding finalizer %q to %q: %w", finalizerName, objMeta.GetName(), err)
}

return err == nil, errors.Wrapf(err, "error adding finalizer %q to %q", finalizerName, objMeta.GetName())
return true, nil
}

func Remove[T runtime.Object](ctx context.Context, client resource.Interface[T], obj T, finalizerName string) error {
Expand All @@ -70,8 +73,11 @@ func Remove[T runtime.Object](ctx context.Context, client resource.Interface[T],

return existing, nil
})
if err != nil {
return fmt.Errorf("error removing finalizer %q from %q: %w", finalizerName, objMeta.GetName(), err)
}

return errors.Wrapf(err, "error removing finalizer %q from %q", finalizerName, objMeta.GetName())
return nil
}

func IsPresent(objMeta metav1.Object, finalizerName string) bool {
Expand Down
5 changes: 2 additions & 3 deletions pkg/reporter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ limitations under the License.
package reporter

import (
"fmt"
"unicode"

"github.com/pkg/errors"
)

type Adapter struct {
Expand All @@ -34,7 +33,7 @@ func (a *Adapter) Error(err error, message string, args ...interface{}) error {
}

if message != "" {
err = errors.Wrapf(err, message, args...)
err = fmt.Errorf("%s: %w", fmt.Sprintf(message, args...), err)
}

capitalizeFirst := func(str string) string {
Expand Down
8 changes: 4 additions & 4 deletions pkg/resource/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"context"
"crypto/x509"
"encoding/base64"
"errors"
"fmt"

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -84,7 +84,7 @@ func BuildRestConfigFromData(apiServer, apiServerToken, caData string, tls *rest
if !tls.Insecure && caData != "" {
caDecoded, err := base64.StdEncoding.DecodeString(caData)
if err != nil {
return nil, errors.Wrap(err, "error decoding CA data")
return nil, fmt.Errorf("error decoding CA data: %w", err)
}

tls.CAData = caDecoded
Expand Down Expand Up @@ -116,12 +116,12 @@ func BuildRestConfigFromFiles(apiServer, apiServerTokenFile, caFile string, tls
func IsAuthorizedFor(restConfig *rest.Config, gvr schema.GroupVersionResource, namespace string) (bool, error) {
client, err := NewDynamicClient(restConfig)
if err != nil {
return false, errors.Wrap(err, "error creating dynamic client")
return false, fmt.Errorf("error creating dynamic client: %w", err)
}

_, err = client.Resource(gvr).Namespace(namespace).Get(context.TODO(), "any", metav1.GetOptions{})
if IsUnknownAuthorityError(err) {
return false, errors.Wrapf(err, "cannot access the API server %q", restConfig.Host)
return false, fmt.Errorf("cannot access the API server %q: %w", restConfig.Host, err)
}

if apierrors.IsNotFound(err) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/resource/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ package resource_test
import (
"crypto/x509"
"encoding/base64"
"errors"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/fake"
"github.com/submariner-io/admiral/pkg/resource"
corev1 "k8s.io/api/core/v1"
Expand Down
4 changes: 2 additions & 2 deletions pkg/resource/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ package resource

import (
"encoding/json"
"fmt"
"strings"
"unicode"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -43,7 +43,7 @@ func ToUnstructuredUsingScheme(from runtime.Object, usingScheme *runtime.Scheme)
to := &unstructured.Unstructured{}
err := usingScheme.Convert(from, to, nil)
if err != nil {
return nil, errors.Wrapf(err, "error converting %#v to unstructured.Unstructured", from)
return nil, fmt.Errorf("error converting %#v to unstructured.Unstructured: %w", from, err)
}

return to, nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/syncer/broker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"strings"

"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
)

type brokerSpecification struct {
Expand All @@ -43,7 +42,7 @@ func getBrokerSpecification() (*brokerSpecification, error) {

err := envconfig.Process(brokerConfigPrefix, &brokerSpec)
if err != nil {
return nil, errors.Wrap(err, "error processing env configuration")
return nil, fmt.Errorf("error processing env configuration: %w", err)
}

return &brokerSpec, nil
Expand Down
21 changes: 14 additions & 7 deletions pkg/syncer/broker/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"reflect"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/submariner-io/admiral/pkg/federate"
"github.com/submariner-io/admiral/pkg/log"
Expand Down Expand Up @@ -159,14 +158,14 @@ func NewSyncer(config SyncerConfig) (*Syncer, error) { //nolint:gocritic // Mini
if config.RestMapper == nil {
config.RestMapper, err = util.BuildRestMapper(config.LocalRestConfig)
if err != nil {
return nil, errors.Wrap(err, "error building the REST mapper")
return nil, fmt.Errorf("error building the REST mapper: %w", err)
}
}

if config.LocalClient == nil {
config.LocalClient, err = resource.NewDynamicClient(config.LocalRestConfig)
if err != nil {
return nil, errors.Wrap(err, "error creating dynamic client")
return nil, fmt.Errorf("error creating dynamic client: %w", err)
}
}

Expand Down Expand Up @@ -228,7 +227,7 @@ func NewSyncer(config SyncerConfig) (*Syncer, error) { //nolint:gocritic // Mini
SyncCounter: syncCounter,
})
if err != nil {
return nil, errors.Wrap(err, "error creating local resource syncer")
return nil, fmt.Errorf("error creating local resource syncer: %w", err)
}

brokerSyncer.syncers = append(brokerSyncer.syncers, localSyncer)
Expand Down Expand Up @@ -256,7 +255,7 @@ func NewSyncer(config SyncerConfig) (*Syncer, error) { //nolint:gocritic // Mini
SyncCounter: syncCounter,
})
if err != nil {
return nil, errors.Wrap(err, "error creating remote resource syncer")
return nil, fmt.Errorf("error creating remote resource syncer: %w", err)
}

brokerSyncer.syncers = append(brokerSyncer.syncers, remoteSyncer)
Expand Down Expand Up @@ -303,7 +302,11 @@ func createBrokerClient(config *SyncerConfig) error {
}

if !authorized {
return errors.Wrap(err, "error authorizing access to the broker API server")
if err != nil {
return fmt.Errorf("error authorizing access to the broker API server: %w", err)
}

return nil
}

if err != nil {
Expand All @@ -312,7 +315,11 @@ func createBrokerClient(config *SyncerConfig) error {

config.BrokerClient, err = resource.NewDynamicClient(config.BrokerRestConfig)

return errors.Wrap(err, "error creating dynamic client")
if err != nil {
return fmt.Errorf("error creating dynamic client: %w", err)
}

return nil
}

func (s *Syncer) Start(stopCh <-chan struct{}) error {
Expand Down
20 changes: 14 additions & 6 deletions pkg/syncer/resource_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"sync"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/submariner-io/admiral/pkg/federate"
"github.com/submariner-io/admiral/pkg/log"
Expand Down Expand Up @@ -303,7 +302,7 @@ func (r *resourceSyncer) AwaitStopped() {
func (r *resourceSyncer) GetResource(name, namespace string) (runtime.Object, bool, error) {
obj, exists, err := r.store.GetByKey(namespace + "/" + name)
if err != nil {
return nil, false, errors.Wrap(err, "error retrieving resource")
return nil, false, fmt.Errorf("error retrieving resource: %w", err)
}

if !exists {
Expand Down Expand Up @@ -428,7 +427,7 @@ func (r *resourceSyncer) runIfCacheSynced(defaultReturn any, run func() any) any
func (r *resourceSyncer) processNextWorkItem(key, _, _ string) (bool, error) {
obj, exists, err := r.store.GetByKey(key)
if err != nil {
return true, errors.Wrapf(err, "error retrieving resource %q", key)
return true, fmt.Errorf("error retrieving resource %q: %w", key, err)
}

if !exists {
Expand Down Expand Up @@ -461,8 +460,12 @@ func (r *resourceSyncer) processNextWorkItem(key, _, _ string) (bool, error) {
r.log.V(log.LIBDEBUG).Infof("Syncer %q syncing resource %q", r.config.Name, resource.GetName())

err = r.config.Federator.Distribute(resource)
if err != nil || r.onSuccessfulSync(resource, transformed, op) {
return true, errors.Wrapf(err, "error distributing resource %q", key)
if err != nil {
return true, fmt.Errorf("error distributing resource %q: %w", key, err)
}

if r.onSuccessfulSync(resource, transformed, op) {
return true, nil
}

if r.syncCounter != nil {
Expand Down Expand Up @@ -515,7 +518,12 @@ func (r *resourceSyncer) handleDeleted(key string) (bool, error) {

if err != nil || r.onSuccessfulSync(resource, transformed, Delete) {
r.deleted.Store(key, deletedResource)
return true, errors.Wrapf(err, "error deleting resource %q", key)

if err != nil {
return true, fmt.Errorf("error deleting resource %q: %w", key, err)
}

return true, nil
}

if deleted {
Expand Down
Loading

0 comments on commit 390f8d7

Please sign in to comment.