From 2e8d85ef65e28aad19edd3fcabbcdc1a9119ae7b Mon Sep 17 00:00:00 2001 From: Michael Crenshaw Date: Fri, 25 Feb 2022 01:28:55 -0500 Subject: [PATCH] chore: remove dependency on unmaintained 'errors' package (#490) (#509) Signed-off-by: Michael Crenshaw --- go.mod | 1 - .../applicationset_controller_test.go | 11 +++++------ pkg/generators/cluster_test.go | 6 +++--- pkg/generators/duck_type.go | 7 +++---- pkg/generators/duck_type_test.go | 8 ++++---- pkg/generators/interface.go | 4 ++-- pkg/generators/matrix.go | 7 +++---- pkg/generators/merge.go | 7 +++---- pkg/generators/pull_request_test.go | 6 +++--- pkg/services/repo_service.go | 16 ++++++++-------- pkg/services/repo_service_test.go | 8 ++++---- pkg/utils/clusterUtils_test.go | 4 ++-- pkg/utils/map_test.go | 4 ++-- pkg/utils/util.go | 3 +-- .../e2e/fixture/applicationsets/utils/fixture.go | 14 ++++---------- 15 files changed, 47 insertions(+), 59 deletions(-) diff --git a/go.mod b/go.mod index 12c99714..81f7868a 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/google/go-github/v35 v35.0.0 github.com/imdario/mergo v0.3.12 github.com/jeremywohl/flatten v1.0.1 - github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.7.0 github.com/valyala/fasttemplate v1.2.1 diff --git a/pkg/controllers/applicationset_controller_test.go b/pkg/controllers/applicationset_controller_test.go index 7d83db41..69c45637 100644 --- a/pkg/controllers/applicationset_controller_test.go +++ b/pkg/controllers/applicationset_controller_test.go @@ -3,7 +3,6 @@ package controllers import ( "context" "encoding/json" - "errors" "fmt" "reflect" "strings" @@ -103,7 +102,7 @@ func TestExtractApplications(t *testing.T) { }, { name: "Handles error from the generator", - generateParamsError: errors.New("error"), + generateParamsError: fmt.Errorf("error"), expectErr: true, expectedReason: v1alpha1.ApplicationSetReasonApplicationParamsGenerationError, }, @@ -118,7 +117,7 @@ func TestExtractApplications(t *testing.T) { }, Spec: argov1alpha1.ApplicationSpec{}, }, - rendererError: errors.New("error"), + rendererError: fmt.Errorf("error"), expectErr: true, expectedReason: v1alpha1.ApplicationSetReasonRenderTemplateParamsError, }, @@ -1620,7 +1619,7 @@ func TestValidateGeneratedApplications(t *testing.T) { }, }, expectedErrors: []string{"application destination can't have both name and server defined"}, - validationErrors: map[int]error{0: errors.New("application destination spec is invalid: application destination can't have both name and server defined: my-cluster my-server")}, + validationErrors: map[int]error{0: fmt.Errorf("application destination spec is invalid: application destination can't have both name and server defined: my-cluster my-server")}, }, { name: "project mismatch should return error", @@ -1643,7 +1642,7 @@ func TestValidateGeneratedApplications(t *testing.T) { }, }, expectedErrors: []string{"application references project DOES-NOT-EXIST which does not exist"}, - validationErrors: map[int]error{0: errors.New("application references project DOES-NOT-EXIST which does not exist")}, + validationErrors: map[int]error{0: fmt.Errorf("application references project DOES-NOT-EXIST which does not exist")}, }, { name: "valid app should return true", @@ -1689,7 +1688,7 @@ func TestValidateGeneratedApplications(t *testing.T) { }, }, expectedErrors: []string{"there are no clusters with this name: nonexistent-cluster"}, - validationErrors: map[int]error{0: errors.New("application destination spec is invalid: unable to find destination server: there are no clusters with this name: nonexistent-cluster")}, + validationErrors: map[int]error{0: fmt.Errorf("application destination spec is invalid: unable to find destination server: there are no clusters with this name: nonexistent-cluster")}, }, } { diff --git a/pkg/generators/cluster_test.go b/pkg/generators/cluster_test.go index 857eaed5..2160d892 100644 --- a/pkg/generators/cluster_test.go +++ b/pkg/generators/cluster_test.go @@ -2,7 +2,7 @@ package generators import ( "context" - "errors" + "fmt" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -25,7 +25,7 @@ type possiblyErroringFakeCtrlRuntimeClient struct { func (p *possiblyErroringFakeCtrlRuntimeClient) List(ctx context.Context, secretList client.ObjectList, opts ...client.ListOption) error { if p.shouldError { - return errors.New("could not list Secrets") + return fmt.Errorf("could not list Secrets") } return p.Client.List(ctx, secretList, opts...) } @@ -200,7 +200,7 @@ func TestGenerateParams(t *testing.T) { values: nil, expected: nil, clientError: true, - expectedError: errors.New("could not list Secrets"), + expectedError: fmt.Errorf("could not list Secrets"), }, } diff --git a/pkg/generators/duck_type.go b/pkg/generators/duck_type.go index eb6047ff..f59e45da 100644 --- a/pkg/generators/duck_type.go +++ b/pkg/generators/duck_type.go @@ -2,7 +2,6 @@ package generators import ( "context" - "errors" "fmt" "strings" "time" @@ -98,14 +97,14 @@ func (g *DuckTypeGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.A // Validate the fields if kind == "" || versionIdx < 1 { log.Warningf("kind=%v, resourceName=%v, versionIdx=%v", kind, resourceName, versionIdx) - return nil, errors.New("There is a problem with the apiVersion, kind or resourceName provided") + return nil, fmt.Errorf("There is a problem with the apiVersion, kind or resourceName provided") } if (resourceName == "" && labelSelector.MatchLabels == nil && labelSelector.MatchExpressions == nil) || (resourceName != "" && (labelSelector.MatchExpressions != nil || labelSelector.MatchLabels != nil)) { log.Warningf("You must choose either resourceName=%v, labelSelector.matchLabels=%v or labelSelect.matchExpressions=%v", resourceName, labelSelector.MatchLabels, labelSelector.MatchExpressions) - return nil, errors.New("There is a problem with the definition of the ClusterDecisionResource generator") + return nil, fmt.Errorf("There is a problem with the definition of the ClusterDecisionResource generator") } // Split up the apiVersion @@ -134,7 +133,7 @@ func (g *DuckTypeGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.A if len(duckResources.Items) == 0 { log.Warning("no resource found, make sure you clusterDecisionResource is defined correctly") - return nil, errors.New("no clusterDecisionResources found") + return nil, fmt.Errorf("no clusterDecisionResources found") } // Override the duck type in the status of the resource diff --git a/pkg/generators/duck_type_test.go b/pkg/generators/duck_type_test.go index 669f7623..583d9920 100644 --- a/pkg/generators/duck_type_test.go +++ b/pkg/generators/duck_type_test.go @@ -2,7 +2,7 @@ package generators import ( "context" - "errors" + "fmt" argoprojiov1alpha1 "github.com/argoproj/applicationset/api/v1alpha1" "github.com/stretchr/testify/assert" @@ -157,7 +157,7 @@ func TestGenerateParamsForDuckType(t *testing.T) { resource: duckType, values: nil, expected: []map[string]string{}, - expectedError: errors.New("There is a problem with the definition of the ClusterDecisionResource generator"), + expectedError: fmt.Errorf("There is a problem with the definition of the ClusterDecisionResource generator"), }, /*** This does not work with the FAKE runtime client, fieldSelectors are broken. { @@ -166,7 +166,7 @@ func TestGenerateParamsForDuckType(t *testing.T) { resource: duckType, values: nil, expected: []map[string]string{}, - expectedError: errors.New("duck.mallard.io \"quak\" not found"), + expectedError: fmt.Errorf("duck.mallard.io \"quak\" not found"), }, ***/ { @@ -268,7 +268,7 @@ func TestGenerateParamsForDuckType(t *testing.T) { resource: duckType, values: nil, expected: nil, - expectedError: errors.New("There is a problem with the definition of the ClusterDecisionResource generator"), + expectedError: fmt.Errorf("There is a problem with the definition of the ClusterDecisionResource generator"), }, } diff --git a/pkg/generators/interface.go b/pkg/generators/interface.go index 7a7457d1..1d354cb4 100644 --- a/pkg/generators/interface.go +++ b/pkg/generators/interface.go @@ -1,7 +1,7 @@ package generators import ( - "errors" + "fmt" "time" argoprojiov1alpha1 "github.com/argoproj/applicationset/api/v1alpha1" @@ -23,7 +23,7 @@ type Generator interface { GetTemplate(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator) *argoprojiov1alpha1.ApplicationSetTemplate } -var EmptyAppSetGeneratorError = errors.New("ApplicationSet is empty") +var EmptyAppSetGeneratorError = fmt.Errorf("ApplicationSet is empty") var NoRequeueAfter time.Duration // DefaultRequeueAfterSeconds is used when GetRequeueAfter is not specified, it is the default time to wait before the next reconcile loop diff --git a/pkg/generators/matrix.go b/pkg/generators/matrix.go index 089d917a..b6deedc6 100644 --- a/pkg/generators/matrix.go +++ b/pkg/generators/matrix.go @@ -1,7 +1,6 @@ package generators import ( - "errors" "fmt" "time" @@ -12,9 +11,9 @@ import ( var _ Generator = (*MatrixGenerator)(nil) var ( - ErrMoreThanTwoGenerators = errors.New("found more than two generators, Matrix support only two") - ErrLessThanTwoGenerators = errors.New("found less than two generators, Matrix support only two") - ErrMoreThenOneInnerGenerators = errors.New("found more than one generator in matrix.Generators") + ErrMoreThanTwoGenerators = fmt.Errorf("found more than two generators, Matrix support only two") + ErrLessThanTwoGenerators = fmt.Errorf("found less than two generators, Matrix support only two") + ErrMoreThenOneInnerGenerators = fmt.Errorf("found more than one generator in matrix.Generators") ) type MatrixGenerator struct { diff --git a/pkg/generators/merge.go b/pkg/generators/merge.go index ea78bf57..73fe1412 100644 --- a/pkg/generators/merge.go +++ b/pkg/generators/merge.go @@ -2,7 +2,6 @@ package generators import ( "encoding/json" - "errors" "fmt" "time" @@ -13,9 +12,9 @@ import ( var _ Generator = (*MergeGenerator)(nil) var ( - ErrLessThanTwoGeneratorsInMerge = errors.New("found less than two generators, Merge requires two or more") - ErrNoMergeKeys = errors.New("no merge keys were specified, Merge requires at least one") - ErrNonUniqueParamSets = errors.New("the parameters from a generator were not unique by the given mergeKeys, Merge requires all param sets to be unique") + ErrLessThanTwoGeneratorsInMerge = fmt.Errorf("found less than two generators, Merge requires two or more") + ErrNoMergeKeys = fmt.Errorf("no merge keys were specified, Merge requires at least one") + ErrNonUniqueParamSets = fmt.Errorf("the parameters from a generator were not unique by the given mergeKeys, Merge requires all param sets to be unique") ) type MergeGenerator struct { diff --git a/pkg/generators/pull_request_test.go b/pkg/generators/pull_request_test.go index 6ae72472..6cfe1c3b 100644 --- a/pkg/generators/pull_request_test.go +++ b/pkg/generators/pull_request_test.go @@ -2,7 +2,7 @@ package generators import ( "context" - "errors" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -49,11 +49,11 @@ func TestPullRequestGithubGenerateParams(t *testing.T) { return pullrequest.NewFakeService( ctx, nil, - errors.New("fake error"), + fmt.Errorf("fake error"), ) }, expected: nil, - expectedErr: errors.New("error listing repos: fake error"), + expectedErr: fmt.Errorf("error listing repos: fake error"), }, } diff --git a/pkg/services/repo_service.go b/pkg/services/repo_service.go index 19226310..958836fe 100644 --- a/pkg/services/repo_service.go +++ b/pkg/services/repo_service.go @@ -2,6 +2,7 @@ package services import ( "context" + "fmt" "os" "path/filepath" "strings" @@ -9,7 +10,6 @@ import ( "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" "github.com/argoproj/argo-cd/v2/util/db" "github.com/argoproj/argo-cd/v2/util/git" - "github.com/pkg/errors" ) // RepositoryDB Is a lean facade for ArgoDB, @@ -41,7 +41,7 @@ func NewArgoCDService(db db.ArgoDB, repoServerAddress string) Repos { func (a *argoCDService) GetFiles(ctx context.Context, repoURL string, revision string, pattern string) (map[string][]byte, error) { repo, err := a.repositoriesDB.GetRepository(ctx, repoURL) if err != nil { - return nil, errors.Wrap(err, "Error in GetRepository") + return nil, fmt.Errorf("Error in GetRepository: %w", err) } gitRepoClient, err := git.NewClient(repo.Repo, repo.GetGitCreds(), repo.IsInsecure(), repo.IsLFSEnabled(), repo.Proxy) @@ -57,7 +57,7 @@ func (a *argoCDService) GetFiles(ctx context.Context, repoURL string, revision s paths, err := gitRepoClient.LsFiles(pattern) if err != nil { - return nil, errors.Wrap(err, "Error during listing files of local repo") + return nil, fmt.Errorf("Error during listing files of local repo: %w", err) } res := map[string][]byte{} @@ -76,7 +76,7 @@ func (a *argoCDService) GetDirectories(ctx context.Context, repoURL string, revi repo, err := a.repositoriesDB.GetRepository(ctx, repoURL) if err != nil { - return nil, errors.Wrap(err, "Error in GetRepository") + return nil, fmt.Errorf("Error in GetRepository: %w", err) } gitRepoClient, err := git.NewClient(repo.Repo, repo.GetGitCreds(), repo.IsInsecure(), repo.IsLFSEnabled(), repo.Proxy) @@ -129,21 +129,21 @@ func (a *argoCDService) GetDirectories(ctx context.Context, repoURL string, revi func checkoutRepo(gitRepoClient git.Client, revision string) error { err := gitRepoClient.Init() if err != nil { - return errors.Wrap(err, "Error during initializing repo") + return fmt.Errorf("Error during initializing repo: %w", err) } err = gitRepoClient.Fetch(revision) if err != nil { - return errors.Wrap(err, "Error during fetching repo") + return fmt.Errorf("Error during fetching repo: %w", err) } commitSHA, err := gitRepoClient.LsRemote(revision) if err != nil { - return errors.Wrap(err, "Error during fetching commitSHA") + return fmt.Errorf("Error during fetching commitSHA: %w", err) } err = gitRepoClient.Checkout(commitSHA) if err != nil { - return errors.Wrap(err, "Error during repo checkout") + return fmt.Errorf("Error during repo checkout: %w", err) } return nil } diff --git a/pkg/services/repo_service_test.go b/pkg/services/repo_service_test.go index 077036e5..873efdcc 100644 --- a/pkg/services/repo_service_test.go +++ b/pkg/services/repo_service_test.go @@ -2,7 +2,7 @@ package services import ( "context" - "errors" + "fmt" "sort" "testing" @@ -60,9 +60,9 @@ func TestGetDirectories(t *testing.T) { repoRes: &v1alpha1.Repository{ Repo: "https://github.com/argoproj/argocd-example-apps/", }, - repoErr: errors.New("Simulated error from GetRepository"), + repoErr: fmt.Errorf("Simulated error from GetRepository"), expected: nil, - expectedError: errors.New("Error in GetRepository: Simulated error from GetRepository"), + expectedError: fmt.Errorf("Error in GetRepository: Simulated error from GetRepository"), }, { name: "Test against repository containing no directories", @@ -162,7 +162,7 @@ func TestGetFiles(t *testing.T) { revision: "this-tag-does-not-exist", pattern: "*", expectSubsetOfPaths: []string{}, - expectedError: errors.New("Error during fetching repo: `git fetch origin this-tag-does-not-exist --tags --force` failed exit status 128: fatal: couldn't find remote ref this-tag-does-not-exist"), + expectedError: fmt.Errorf("Error during fetching repo: `git fetch origin this-tag-does-not-exist --tags --force` failed exit status 128: fatal: couldn't find remote ref this-tag-does-not-exist"), }, { name: "pull a specific revision of example apps, and use a ** pattern", diff --git a/pkg/utils/clusterUtils_test.go b/pkg/utils/clusterUtils_test.go index 15d009d0..3984a39c 100644 --- a/pkg/utils/clusterUtils_test.go +++ b/pkg/utils/clusterUtils_test.go @@ -2,7 +2,7 @@ package utils import ( "context" - "errors" + "fmt" "testing" "github.com/argoproj/applicationset/test/e2e/fixture/applicationsets/utils" @@ -135,7 +135,7 @@ func TestValidateDestination(t *testing.T) { kubeclientset := fake.NewSimpleClientset() kubeclientset.PrependReactor("list", "*", func(action kubetesting.Action) (handled bool, ret runtime.Object, err error) { - return true, nil, errors.New("an error occurred") + return true, nil, fmt.Errorf("an error occurred") }) err := ValidateDestination(context.Background(), &dest, kubeclientset, utils.ArgoCDNamespace) diff --git a/pkg/utils/map_test.go b/pkg/utils/map_test.go index 4f82a83c..adbba890 100644 --- a/pkg/utils/map_test.go +++ b/pkg/utils/map_test.go @@ -1,7 +1,7 @@ package utils import ( - "errors" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -27,7 +27,7 @@ func TestCombineStringMaps(t *testing.T) { left: map[string]string{"foo": "bar", "a": "fail"}, right: map[string]string{"a": "b", "c": "d"}, expected: map[string]string{"a": "b", "foo": "bar"}, - expectedErr: errors.New("found duplicate key a with different value, a: fail ,b: b"), + expectedErr: fmt.Errorf("found duplicate key a with different value, a: fail ,b: b"), }, { name: "pass if keys & values are the same", diff --git a/pkg/utils/util.go b/pkg/utils/util.go index 151171ee..1436cb23 100644 --- a/pkg/utils/util.go +++ b/pkg/utils/util.go @@ -11,7 +11,6 @@ import ( argoprojiov1alpha1 "github.com/argoproj/applicationset/api/v1alpha1" argov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/valyala/fasttemplate" ) @@ -78,7 +77,7 @@ func (r *Render) replace(fstTmpl *fasttemplate.Template, replaceMap map[string]s // just write the same string back return w.Write([]byte(fmt.Sprintf("{{%s}}", tag))) } - unresolvedErr = errors.Errorf("failed to resolve {{%s}}", tag) + unresolvedErr = fmt.Errorf("failed to resolve {{%s}}", tag) return 0, nil } // The following escapes any special characters (e.g. newlines, tabs, etc...) diff --git a/test/e2e/fixture/applicationsets/utils/fixture.go b/test/e2e/fixture/applicationsets/utils/fixture.go index e4a3dc73..d4c02d70 100644 --- a/test/e2e/fixture/applicationsets/utils/fixture.go +++ b/test/e2e/fixture/applicationsets/utils/fixture.go @@ -3,7 +3,6 @@ package utils import ( "context" "encoding/json" - "errors" "fmt" "os" "regexp" @@ -112,9 +111,7 @@ func EnsureCleanState(t *testing.T) { } if list != nil && len(list.Items) > 0 { // Fail - msg := fmt.Sprintf("Waiting for list of ApplicationSets to be size zero: %d", len(list.Items)) - // Intentionally not making this an Errorf, so it can be printf-ed for debugging purposes. - return errors.New(msg) + return fmt.Errorf("Waiting for list of ApplicationSets to be size zero: %d", len(list.Items)) } return nil // Pass @@ -160,9 +157,7 @@ func waitForExpectedClusterState() error { } if list != nil && len(list.Items) > 0 { // Fail - msg := fmt.Sprintf("Waiting for list of ApplicationSets to be size zero: %d", len(list.Items)) - // Intentionally not making this an Errorf, so it can be printf-ed for debugging purposes. - return errors.New(msg) + return fmt.Errorf("Waiting for list of ApplicationSets to be size zero: %d", len(list.Items)) } return nil // Pass @@ -178,8 +173,7 @@ func waitForExpectedClusterState() error { } if appList != nil && len(appList.Items) > 0 { // Fail - msg := fmt.Sprintf("Waiting for list of Applications to be size zero: %d", len(appList.Items)) - return errors.New(msg) + return fmt.Errorf("Waiting for list of Applications to be size zero: %d", len(appList.Items)) } return nil // Pass @@ -206,7 +200,7 @@ func waitForExpectedClusterState() error { msg = err.Error() } - return errors.New(msg) + return fmt.Errorf(msg) }, time.Now().Add(120*time.Second)); err != nil { return err