From 8441b643a8bd87a8f2c30403ce6af93ddcc4d927 Mon Sep 17 00:00:00 2001 From: Derek Wang Date: Sun, 25 Sep 2022 21:32:59 -0700 Subject: [PATCH] feat: Kafka es discontinues processing if eb publishing fails (#2214) Signed-off-by: Derek Wang --- common/common.go | 4 +- common/leaderelection/leaderelection.go | 8 +- common/retry.go | 5 +- common/retry_test.go | 10 +- common/tls/tls.go | 17 +- common/tls/tls_test.go | 8 +- common/util.go | 21 +- controllers/eventbus/installer/exotic_nats.go | 4 +- controllers/eventbus/installer/installer.go | 12 +- controllers/eventbus/installer/nats.go | 5 +- controllers/eventsource/resource.go | 11 +- controllers/eventsource/validate.go | 6 +- controllers/sensor/controller.go | 4 +- controllers/sensor/resource.go | 11 +- controllers/sensor/validate.go | 119 ++++---- eventbus/common/error.go | 15 + eventbus/common/error_test.go | 25 ++ eventbus/driver.go | 26 +- eventbus/jetstream/base/jetstream.go | 11 +- eventbus/jetstream/base/jetstream_conn.go | 4 +- eventbus/jetstream/eventsource/source_conn.go | 3 +- eventbus/jetstream/sensor/sensor_jetstream.go | 36 ++- eventbus/jetstream/sensor/trigger_conn.go | 23 +- eventbus/stan/base/stan.go | 5 +- eventbus/stan/base/stan_conn.go | 4 +- eventbus/stan/eventsource/source_conn.go | 4 +- eventbus/stan/sensor/trigger_conn.go | 7 +- eventsources/common/aws/aws.go | 13 +- eventsources/common/naivewatcher/watcher.go | 8 +- eventsources/eventing.go | 23 +- eventsources/persist/event_persist.go | 5 +- eventsources/sources/amqp/start.go | 39 ++- eventsources/sources/amqp/validate.go | 10 +- eventsources/sources/awssns/start.go | 22 +- eventsources/sources/awssqs/start.go | 6 +- eventsources/sources/azureeventshub/start.go | 17 +- .../sources/azureeventshub/validate.go | 10 +- .../sources/bitbucket/basic_auth_strategy.go | 7 +- .../bitbucket/oauth_token_auth_strategy.go | 5 +- eventsources/sources/bitbucket/start.go | 16 +- eventsources/sources/bitbucketserver/start.go | 38 +-- eventsources/sources/calendar/start.go | 23 +- eventsources/sources/emitter/start.go | 14 +- eventsources/sources/emitter/validate.go | 9 +- eventsources/sources/file/start.go | 26 +- eventsources/sources/gcppubsub/start.go | 37 ++- eventsources/sources/generic/start.go | 5 +- eventsources/sources/github/start.go | 23 +- eventsources/sources/gitlab/start.go | 14 +- eventsources/sources/hdfs/client.go | 5 +- eventsources/sources/hdfs/start.go | 24 +- eventsources/sources/hdfs/validate.go | 16 +- eventsources/sources/kafka/start.go | 42 +-- eventsources/sources/minio/start.go | 12 +- eventsources/sources/mqtt/start.go | 10 +- eventsources/sources/nats/start.go | 14 +- eventsources/sources/nats/validate.go | 7 +- eventsources/sources/nsq/start.go | 10 +- eventsources/sources/nsq/validate.go | 9 +- eventsources/sources/pulsar/start.go | 16 +- eventsources/sources/redis/start.go | 16 +- eventsources/sources/redis/validate.go | 9 +- .../{redisStream => redis_stream}/start.go | 14 +- .../{redisStream => redis_stream}/validate.go | 7 +- .../validate_test.go | 0 eventsources/sources/resource/start.go | 18 +- eventsources/sources/slack/start.go | 32 +-- eventsources/sources/storagegrid/start.go | 5 +- eventsources/sources/stripe/start.go | 4 +- eventsources/sources/stripe/validate.go | 5 +- eventsources/sources/webhook/start.go | 3 +- go.mod | 2 +- pkg/apis/eventsource/v1alpha1/config.go | 12 +- pkg/apis/eventsource/v1alpha1/validate.go | 6 +- sensors/artifacts/file.go | 4 +- sensors/artifacts/git.go | 17 +- sensors/artifacts/inline.go | 4 +- sensors/artifacts/resource.go | 4 +- sensors/artifacts/store.go | 9 +- sensors/artifacts/url.go | 7 +- sensors/listener.go | 17 +- sensors/policy/status.go | 5 +- .../apache-openwhisk/apache-openwhisk.go | 20 +- .../triggers/argo-workflow/argo-workflow.go | 25 +- sensors/triggers/aws-lambda/aws-lambda.go | 14 +- .../azure-event-hubs/azure_event_hubs.go | 14 +- .../triggers/custom-trigger/custom-trigger.go | 22 +- sensors/triggers/fetch.go | 5 +- sensors/triggers/http/http.go | 24 +- sensors/triggers/kafka/kafka.go | 20 +- sensors/triggers/log/log.go | 4 +- sensors/triggers/nats/nats.go | 14 +- sensors/triggers/pulsar/pulsar.go | 20 +- sensors/triggers/pulsar/pulsar_test.go | 4 +- sensors/triggers/slack/slack.go | 24 +- sensors/triggers/standard-k8s/standard-k8s.go | 23 +- test/e2e/functional_test.go | 6 +- test/stress/main.go | 8 +- test/util/util.go | 256 ++++++------------ webhook/validator/validator.go | 3 +- webhook/webhook.go | 21 +- webhook/webhook_test.go | 3 +- 102 files changed, 799 insertions(+), 874 deletions(-) create mode 100644 eventbus/common/error.go create mode 100644 eventbus/common/error_test.go rename eventsources/sources/{redisStream => redis_stream}/start.go (91%) rename eventsources/sources/{redisStream => redis_stream}/validate.go (90%) rename eventsources/sources/{redisStream => redis_stream}/validate_test.go (100%) diff --git a/common/common.go b/common/common.go index aff8d121b2..4f05bea366 100644 --- a/common/common.go +++ b/common/common.go @@ -17,9 +17,9 @@ limitations under the License. package common import ( + "fmt" "reflect" - "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" ) @@ -110,7 +110,7 @@ const ( ) var ( - ErrNilEventSource = errors.New("event source can't be nil") + ErrNilEventSource = fmt.Errorf("event source can't be nil") ) // Miscellaneous Labels diff --git a/common/leaderelection/leaderelection.go b/common/leaderelection/leaderelection.go index 4cf896a519..137fd48ac8 100644 --- a/common/leaderelection/leaderelection.go +++ b/common/leaderelection/leaderelection.go @@ -3,11 +3,11 @@ package leaderelection import ( "context" "crypto/tls" + "fmt" "github.com/fsnotify/fsnotify" "github.com/nats-io/graft" nats "github.com/nats-io/nats.go" - "github.com/pkg/errors" "github.com/spf13/viper" "go.uber.org/zap" @@ -40,7 +40,7 @@ func NewEventBusElector(ctx context.Context, eventBusConfig eventbusv1alpha1.Bus eventBusType = apicommon.EventBusJetStream eventBusAuth = &eventbusv1alpha1.AuthStrategyBasic default: - return nil, errors.New("invalid event bus") + return nil, fmt.Errorf("invalid event bus") } var auth *eventbuscommon.Auth @@ -56,7 +56,7 @@ func NewEventBusElector(ctx context.Context, eventBusConfig eventbusv1alpha1.Bus v.AddConfigPath(common.EventBusAuthFileMountPath) err := v.ReadInConfig() if err != nil { - return nil, errors.Errorf("failed to load auth.yaml. err: %+v", err) + return nil, fmt.Errorf("failed to load auth.yaml. err: %w", err) } err = v.Unmarshal(cred) if err != nil { @@ -91,7 +91,7 @@ func NewEventBusElector(ctx context.Context, eventBusConfig eventbusv1alpha1.Bus auth: auth, } default: - return nil, errors.New("invalid eventbus type") + return nil, fmt.Errorf("invalid eventbus type") } return elector, nil } diff --git a/common/retry.go b/common/retry.go index c67e026765..f7284fa461 100644 --- a/common/retry.go +++ b/common/retry.go @@ -94,7 +94,7 @@ func Convert2WaitBackoff(backoff *apicommon.Backoff) (*wait.Backoff, error) { return &result, nil } -func Connect(backoff *apicommon.Backoff, conn func() error) error { +func DoWithRetry(backoff *apicommon.Backoff, f func() error) error { if backoff == nil { backoff = &DefaultBackoff } @@ -103,8 +103,7 @@ func Connect(backoff *apicommon.Backoff, conn func() error) error { return fmt.Errorf("invalid backoff configuration, %w", err) } _ = wait.ExponentialBackoff(*b, func() (bool, error) { - if err = conn(); err != nil { - // return "false, err" will cover waitErr + if err = f(); err != nil { return false, nil } return true, nil diff --git a/common/retry_test.go b/common/retry_test.go index 7bb67a53b3..708d57569e 100644 --- a/common/retry_test.go +++ b/common/retry_test.go @@ -43,13 +43,13 @@ func TestRetryableKubeAPIError(t *testing.T) { } func TestConnect(t *testing.T) { - err := Connect(nil, func() error { + err := DoWithRetry(nil, func() error { return fmt.Errorf("new error") }) assert.NotNil(t, err) assert.True(t, strings.Contains(err.Error(), "new error")) - err = Connect(nil, func() error { + err = DoWithRetry(nil, func() error { return nil }) assert.Nil(t, err) @@ -58,7 +58,7 @@ func TestConnect(t *testing.T) { func TestConnectDurationString(t *testing.T) { start := time.Now() count := 2 - err := Connect(nil, func() error { + err := DoWithRetry(nil, func() error { if count == 0 { return nil } else { @@ -85,7 +85,7 @@ func TestConnectRetry(t *testing.T) { } count := 2 start := time.Now() - err := Connect(&backoff, func() error { + err := DoWithRetry(&backoff, func() error { if count == 0 { return nil } else { @@ -110,7 +110,7 @@ func TestRetryFailure(t *testing.T) { Jitter: &jitter, Steps: 2, } - err := Connect(&backoff, func() error { + err := DoWithRetry(&backoff, func() error { return fmt.Errorf("this is an error") }) assert.NotNil(t, err) diff --git a/common/tls/tls.go b/common/tls/tls.go index e6a012de74..5312c380d0 100644 --- a/common/tls/tls.go +++ b/common/tls/tls.go @@ -6,17 +6,16 @@ import ( "crypto/x509" "crypto/x509/pkix" "encoding/pem" + "fmt" "math/big" "time" - - "github.com/pkg/errors" ) func certTemplate(org string, hosts []string, notAfter time.Time) (*x509.Certificate, error) { serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) if err != nil { - return nil, errors.Wrap(err, "failed to generate serial number") + return nil, fmt.Errorf("failed to generate serial number, %w", err) } return &x509.Certificate{ SerialNumber: serialNumber, @@ -61,17 +60,17 @@ func createCert(template, parent *x509.Certificate, pub, parentPriv interface{}) func createCA(org string, hosts []string, notAfter time.Time) (*rsa.PrivateKey, *x509.Certificate, []byte, error) { rootKey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { - return nil, nil, nil, errors.Wrap(err, "failed to generate random key") + return nil, nil, nil, fmt.Errorf("failed to generate random key, %w", err) } rootCertTmpl, err := createCACertTemplate(org, hosts, notAfter) if err != nil { - return nil, nil, nil, errors.Wrap(err, "failed to generate CA cert") + return nil, nil, nil, fmt.Errorf("failed to generate CA cert, %w", err) } rootCert, rootCertPEM, err := createCert(rootCertTmpl, rootCertTmpl, &rootKey.PublicKey, rootKey) if err != nil { - return nil, nil, nil, errors.Wrap(err, "failed to sign CA cert") + return nil, nil, nil, fmt.Errorf("failed to sign CA cert, %w", err) } return rootKey, rootCert, rootCertPEM, nil } @@ -82,7 +81,7 @@ func createCA(org string, hosts []string, notAfter time.Time) (*rsa.PrivateKey, // can generate for both server and client but at least one must be specified func CreateCerts(org string, hosts []string, notAfter time.Time, server bool, client bool) (serverKey, serverCert, caCert []byte, err error) { if !server && !client { - return nil, nil, nil, errors.Wrap(err, "CreateCerts() must specify either server or client") + return nil, nil, nil, fmt.Errorf("CreateCerts() must specify either server or client") } // Create a CA certificate and private key @@ -94,7 +93,7 @@ func CreateCerts(org string, hosts []string, notAfter time.Time, server bool, cl // Create the private key privateKey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { - return nil, nil, nil, errors.Wrap(err, "failed to generate random key") + return nil, nil, nil, fmt.Errorf("failed to generate random key, %w", err) } var cert *x509.Certificate @@ -113,7 +112,7 @@ func CreateCerts(org string, hosts []string, notAfter time.Time, server bool, cl // create a certificate wrapping the public key, sign it with the CA private key _, certPEM, err := createCert(cert, caCertificate, &privateKey.PublicKey, caKey) if err != nil { - return nil, nil, nil, errors.Wrap(err, "failed to sign server cert") + return nil, nil, nil, fmt.Errorf("failed to sign server cert, %w", err) } privateKeyPEM := pem.EncodeToMemory(&pem.Block{ Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey), diff --git a/common/tls/tls_test.go b/common/tls/tls_test.go index 1ff14f267a..f2b063c455 100644 --- a/common/tls/tls_test.go +++ b/common/tls/tls_test.go @@ -3,10 +3,10 @@ package tls import ( "crypto/x509" "encoding/pem" + "fmt" "testing" "time" - "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -35,14 +35,14 @@ func validCertificate(cert []byte, t *testing.T) (*x509.Certificate, error) { const certificate = "CERTIFICATE" caCert, _ := pem.Decode(cert) if caCert.Type != certificate { - return nil, errors.Errorf("CERT type mismatch, got %s, want: %s", caCert.Type, certificate) + return nil, fmt.Errorf("CERT type mismatch, got %s, want: %s", caCert.Type, certificate) } parsedCert, err := x509.ParseCertificate(caCert.Bytes) if err != nil { - return nil, errors.Wrap(err, "failed to parse cert") + return nil, fmt.Errorf("failed to parse cert, %w", err) } if parsedCert.SignatureAlgorithm != x509.SHA256WithRSA { - return nil, errors.Errorf("signature not match. Got: %s, want: %s", parsedCert.SignatureAlgorithm, x509.SHA256WithRSA) + return nil, fmt.Errorf("signature not match. Got: %s, want: %s", parsedCert.SignatureAlgorithm, x509.SHA256WithRSA) } return parsedCert, nil } diff --git a/common/util.go b/common/util.go index 94ebf42770..02a5b51b61 100644 --- a/common/util.go +++ b/common/util.go @@ -28,7 +28,6 @@ import ( "reflect" "strings" - "github.com/pkg/errors" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -119,7 +118,7 @@ func GetSecretValue(ctx context.Context, client kubernetes.Interface, namespace } val, ok := secret.Data[selector.Key] if !ok { - return "", errors.Errorf("secret '%s' does not have the key '%s'", selector.Name, selector.Key) + return "", fmt.Errorf("secret '%s' does not have the key '%s'", selector.Name, selector.Key) } return string(val), nil } @@ -151,7 +150,7 @@ func GetSecretFromVolume(selector *v1.SecretKeySelector) (string, error) { } data, err := os.ReadFile(filePath) if err != nil { - return "", errors.Wrapf(err, "failed to get secret value of name: %s, key: %s", selector.Name, selector.Key) + return "", fmt.Errorf("failed to get secret value of name: %s, key: %s, %w", selector.Name, selector.Key, err) } // Secrets edited by tools like "vim" always have an extra invisible "\n" in the end, // and it's often neglected, but it makes differences for some of the applications. @@ -161,7 +160,7 @@ func GetSecretFromVolume(selector *v1.SecretKeySelector) (string, error) { // GetSecretVolumePath returns the path of the mounted secret func GetSecretVolumePath(selector *v1.SecretKeySelector) (string, error) { if selector == nil { - return "", errors.New("secret key selector is nil") + return "", fmt.Errorf("secret key selector is nil") } return fmt.Sprintf("/argo-events/secrets/%s/%s", selector.Name, selector.Key), nil } @@ -175,7 +174,7 @@ func GetConfigMapFromVolume(selector *v1.ConfigMapKeySelector) (string, error) { } data, err := os.ReadFile(filePath) if err != nil { - return "", errors.Wrapf(err, "failed to get configMap value of name: %s, key: %s", selector.Name, selector.Key) + return "", fmt.Errorf("failed to get configMap value of name: %s, key: %s, %w", selector.Name, selector.Key, err) } // Contents edied by tools like "vim" always have an extra invisible "\n" in the end, // and it's often negleted, but it makes differences for some of the applications. @@ -185,7 +184,7 @@ func GetConfigMapFromVolume(selector *v1.ConfigMapKeySelector) (string, error) { // GetConfigMapVolumePath returns the path of the mounted configmap func GetConfigMapVolumePath(selector *v1.ConfigMapKeySelector) (string, error) { if selector == nil { - return "", errors.New("configmap key selector is nil") + return "", fmt.Errorf("configmap key selector is nil") } return fmt.Sprintf("/argo-events/config/%s/%s", selector.Name, selector.Key), nil } @@ -211,7 +210,7 @@ func GenerateEnvFromConfigMapSpec(selector *v1.ConfigMapKeySelector) v1.EnvFromS // GetTLSConfig returns a tls configuration for given cert and key or skips the certs if InsecureSkipVerify is true. func GetTLSConfig(config *apicommon.TLSConfig) (*tls.Config, error) { if config == nil { - return nil, errors.New("TLSConfig is nil") + return nil, fmt.Errorf("TLSConfig is nil") } if config.InsecureSkipVerify { @@ -247,19 +246,19 @@ func GetTLSConfig(config *apicommon.TLSConfig) (*tls.Config, error) { if len(caCertPath)+len(clientCertPath)+len(clientKeyPath) == 0 { // None of 3 is configured - return nil, errors.New("invalid tls config, neither of caCertSecret, clientCertSecret and clientKeySecret is configured") + return nil, fmt.Errorf("invalid tls config, neither of caCertSecret, clientCertSecret and clientKeySecret is configured") } if len(clientCertPath)+len(clientKeyPath) > 0 && len(clientCertPath)*len(clientKeyPath) == 0 { // Only one of clientCertSecret and clientKeySecret is configured - return nil, errors.New("invalid tls config, both of clientCertSecret and clientKeySecret need to be configured") + return nil, fmt.Errorf("invalid tls config, both of clientCertSecret and clientKeySecret need to be configured") } c := &tls.Config{} if len(caCertPath) > 0 { caCert, err := os.ReadFile(caCertPath) if err != nil { - return nil, errors.Wrapf(err, "failed to read ca cert file %s", caCertPath) + return nil, fmt.Errorf("failed to read ca cert file %s, %w", caCertPath, err) } pool := x509.NewCertPool() pool.AppendCertsFromPEM(caCert) @@ -269,7 +268,7 @@ func GetTLSConfig(config *apicommon.TLSConfig) (*tls.Config, error) { if len(clientCertPath) > 0 && len(clientKeyPath) > 0 { clientCert, err := tls.LoadX509KeyPair(clientCertPath, clientKeyPath) if err != nil { - return nil, errors.Wrapf(err, "failed to load client cert key pair %s", caCertPath) + return nil, fmt.Errorf("failed to load client cert key pair %s, %w", caCertPath, err) } c.Certificates = []tls.Certificate{clientCert} } diff --git a/controllers/eventbus/installer/exotic_nats.go b/controllers/eventbus/installer/exotic_nats.go index 15636baadb..ac0b3b2bd7 100644 --- a/controllers/eventbus/installer/exotic_nats.go +++ b/controllers/eventbus/installer/exotic_nats.go @@ -2,7 +2,7 @@ package installer import ( "context" - "errors" + "fmt" "go.uber.org/zap" @@ -27,7 +27,7 @@ func NewExoticNATSInstaller(eventBus *v1alpha1.EventBus, logger *zap.SugaredLogg func (i *exoticNATSInstaller) Install(ctx context.Context) (*v1alpha1.BusConfig, error) { natsObj := i.eventBus.Spec.NATS if natsObj == nil || natsObj.Exotic == nil { - return nil, errors.New("invalid request") + return nil, fmt.Errorf("invalid request") } i.eventBus.Status.MarkDeployed("Skipped", "Skip deployment because of using exotic config.") i.logger.Info("use exotic config") diff --git a/controllers/eventbus/installer/installer.go b/controllers/eventbus/installer/installer.go index dae5dadf7c..29b11b898a 100644 --- a/controllers/eventbus/installer/installer.go +++ b/controllers/eventbus/installer/installer.go @@ -2,8 +2,8 @@ package installer import ( "context" + "fmt" - "github.com/pkg/errors" "go.uber.org/zap" "sigs.k8s.io/controller-runtime/pkg/client" @@ -49,7 +49,7 @@ func getInstaller(eventBus *v1alpha1.EventBus, client client.Client, config *con } else if js := eventBus.Spec.JetStream; js != nil { return NewJetStreamInstaller(client, eventBus, config, getLabels(eventBus), logger), nil } - return nil, errors.New("invalid eventbus spec") + return nil, fmt.Errorf("invalid eventbus spec") } func getLabels(bus *v1alpha1.EventBus) map[string]string { @@ -72,19 +72,19 @@ func Uninstall(ctx context.Context, eventBus *v1alpha1.EventBus, client client.C linkedEventSources, err := linkedEventSources(ctx, eventBus.Namespace, eventBus.Name, client) if err != nil { logger.Errorw("failed to query linked EventSources", zap.Error(err)) - return errors.Wrap(err, "failed to check if there is any EventSource linked") + return fmt.Errorf("failed to check if there is any EventSource linked, %w", err) } if linkedEventSources > 0 { - return errors.Errorf("Can not delete an EventBus with %v EventSources connected", linkedEventSources) + return fmt.Errorf("Can not delete an EventBus with %v EventSources connected", linkedEventSources) } linkedSensors, err := linkedSensors(ctx, eventBus.Namespace, eventBus.Name, client) if err != nil { logger.Errorw("failed to query linked Sensors", zap.Error(err)) - return errors.Wrap(err, "failed to check if there is any Sensor linked") + return fmt.Errorf("failed to check if there is any Sensor linked, %w", err) } if linkedSensors > 0 { - return errors.Errorf("Can not delete an EventBus with %v Sensors connected", linkedSensors) + return fmt.Errorf("Can not delete an EventBus with %v Sensors connected", linkedSensors) } installer, err := getInstaller(eventBus, client, config, logger) diff --git a/controllers/eventbus/installer/nats.go b/controllers/eventbus/installer/nats.go index 90a34c4a88..f2fb13c31b 100644 --- a/controllers/eventbus/installer/nats.go +++ b/controllers/eventbus/installer/nats.go @@ -2,7 +2,6 @@ package installer import ( "context" - "errors" "fmt" "strconv" "strings" @@ -67,7 +66,7 @@ func NewNATSInstaller(client client.Client, eventBus *v1alpha1.EventBus, config func (i *natsInstaller) Install(ctx context.Context) (*v1alpha1.BusConfig, error) { natsObj := i.eventBus.Spec.NATS if natsObj == nil || natsObj.Native == nil { - return nil, errors.New("invalid request") + return nil, fmt.Errorf("invalid request") } svc, err := i.createStanService(ctx) @@ -355,7 +354,7 @@ func (i *natsInstaller) createAuthSecrets(ctx context.Context, strategy v1alpha1 return returnedSSecret, returnedCSecret, nil default: i.eventBus.Status.MarkDeployFailed("UnsupportedAuthStrategy", "Unsupported auth strategy") - return nil, nil, errors.New("unsupported auth strategy") + return nil, nil, fmt.Errorf("unsupported auth strategy") } } diff --git a/controllers/eventsource/resource.go b/controllers/eventsource/resource.go index d6b6853829..53de809f87 100644 --- a/controllers/eventsource/resource.go +++ b/controllers/eventsource/resource.go @@ -7,7 +7,6 @@ import ( "fmt" "github.com/imdario/mergo" - "github.com/pkg/errors" "go.uber.org/zap" appv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -45,7 +44,7 @@ func Reconcile(client client.Client, args *AdaptorArgs, logger *zap.SugaredLogge if apierrors.IsNotFound(err) { eventSource.Status.MarkDeployFailed("EventBusNotFound", "EventBus not found.") logger.Errorw("EventBus not found", "eventBusName", eventBusName, "error", err) - return errors.Errorf("eventbus %s not found", eventBusName) + return fmt.Errorf("eventbus %s not found", eventBusName) } eventSource.Status.MarkDeployFailed("GetEventBusFailed", "Failed to get EventBus.") logger.Errorw("failed to get EventBus", "eventBusName", eventBusName, "error", err) @@ -54,7 +53,7 @@ func Reconcile(client client.Client, args *AdaptorArgs, logger *zap.SugaredLogge if !eventBus.Status.IsReady() { eventSource.Status.MarkDeployFailed("EventBusNotReady", "EventBus not ready.") logger.Errorw("event bus is not in ready status", "eventBusName", eventBusName, "error", err) - return errors.New("eventbus not ready") + return fmt.Errorf("eventbus not ready") } expectedDeploy, err := buildDeployment(args, eventBus) @@ -176,7 +175,7 @@ func buildDeployment(args *AdaptorArgs, eventBus *eventbusv1alpha1.EventBus) (*a } eventSourceBytes, err := json.Marshal(eventSourceCopy) if err != nil { - return nil, errors.New("failed marshal eventsource spec") + return nil, fmt.Errorf("failed marshal eventsource spec") } encodedEventSourceSpec := base64.StdEncoding.EncodeToString(eventSourceBytes) envVars := []corev1.EnvVar{ @@ -196,7 +195,7 @@ func buildDeployment(args *AdaptorArgs, eventBus *eventbusv1alpha1.EventBus) (*a busConfigBytes, err := json.Marshal(eventBus.Status.Config) if err != nil { - return nil, errors.Errorf("failed marshal event bus config: %v", err) + return nil, fmt.Errorf("failed marshal event bus config: %v", err) } encodedBusConfig := base64.StdEncoding.EncodeToString(busConfigBytes) envVars = append(envVars, corev1.EnvVar{Name: common.EnvVarEventBusConfig, Value: encodedBusConfig}) @@ -209,7 +208,7 @@ func buildDeployment(args *AdaptorArgs, eventBus *eventbusv1alpha1.EventBus) (*a jsConf := eventBus.Status.Config.JetStream accessSecret = jsConf.AccessSecret default: - return nil, errors.New("unsupported event bus") + return nil, fmt.Errorf("unsupported event bus") } volumes := deploymentSpec.Template.Spec.Volumes diff --git a/controllers/eventsource/validate.go b/controllers/eventsource/validate.go index 4356347a82..0a6b76491f 100644 --- a/controllers/eventsource/validate.go +++ b/controllers/eventsource/validate.go @@ -4,8 +4,6 @@ import ( "context" "fmt" - "github.com/pkg/errors" - "github.com/argoproj/argo-events/eventsources" apicommon "github.com/argoproj/argo-events/pkg/apis/common" "github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1" @@ -37,7 +35,7 @@ func ValidateEventSource(eventSource *v1alpha1.EventSource) error { } else { // Duplicated event name not allowed in one EventSource, even they are in different EventSourceType. eventSource.Status.MarkSourcesNotProvided("InvalidEventSource", fmt.Sprintf("more than one \"%s\" found", eName)) - return errors.Errorf("more than one \"%s\" found in the spec", eName) + return fmt.Errorf("more than one %q found in the spec", eName) } err := server.ValidateEventSource(ctx) @@ -51,7 +49,7 @@ func ValidateEventSource(eventSource *v1alpha1.EventSource) error { if rollingUpdates > 0 && recreates > 0 { // We don't allow this as if we use recreate strategy for the deployment it will have downtime eventSource.Status.MarkSourcesNotProvided("InvalidEventSource", "Some types of event sources can not be put in one spec") - return errors.New("event sources with rolling update and recreate update strategy can not be put together") + return fmt.Errorf("event sources with rolling update and recreate update strategy can not be put together") } eventSource.Status.MarkSourcesProvided() diff --git a/controllers/sensor/controller.go b/controllers/sensor/controller.go index 55a2bdc989..07ccefca09 100644 --- a/controllers/sensor/controller.go +++ b/controllers/sensor/controller.go @@ -18,6 +18,7 @@ package sensor import ( "context" + "fmt" "go.uber.org/zap" "k8s.io/apimachinery/pkg/api/equality" @@ -33,7 +34,6 @@ import ( "github.com/argoproj/argo-events/common/logging" eventbusv1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1" "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" - "github.com/pkg/errors" ) const ( @@ -109,7 +109,7 @@ func (r *reconciler) reconcile(ctx context.Context, sensor *v1alpha1.Sensor) err if apierrors.IsNotFound(err) { sensor.Status.MarkDeployFailed("EventBusNotFound", "EventBus not found.") log.Errorw("EventBus not found", "eventBusName", eventBusName, "error", err) - return errors.Errorf("eventbus %s not found", eventBusName) + return fmt.Errorf("eventbus %s not found", eventBusName) } sensor.Status.MarkDeployFailed("GetEventBusFailed", "Failed to get EventBus.") log.Errorw("failed to get EventBus", "eventBusName", eventBusName, "error", err) diff --git a/controllers/sensor/resource.go b/controllers/sensor/resource.go index 6a1baed718..0b5dbbdf57 100644 --- a/controllers/sensor/resource.go +++ b/controllers/sensor/resource.go @@ -23,7 +23,6 @@ import ( "fmt" "github.com/imdario/mergo" - "github.com/pkg/errors" "go.uber.org/zap" appv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -54,7 +53,7 @@ func Reconcile(client client.Client, eventBus *eventbusv1alpha1.EventBus, args * if eventBus == nil { sensor.Status.MarkDeployFailed("GetEventBusFailed", "Failed to get EventBus.") logger.Error("failed to get EventBus") - return errors.New("failed to get EventBus") + return fmt.Errorf("failed to get EventBus") } eventBusName := common.DefaultEventBusName @@ -64,7 +63,7 @@ func Reconcile(client client.Client, eventBus *eventbusv1alpha1.EventBus, args * if !eventBus.Status.IsReady() { sensor.Status.MarkDeployFailed("EventBusNotReady", "EventBus not ready.") logger.Errorw("event bus is not in ready status", "eventBusName", eventBusName) - return errors.New("eventbus not ready") + return fmt.Errorf("eventbus not ready") } expectedDeploy, err := buildDeployment(args, eventBus) @@ -136,7 +135,7 @@ func buildDeployment(args *AdaptorArgs, eventBus *eventbusv1alpha1.EventBus) (*a } sensorBytes, err := json.Marshal(sensorCopy) if err != nil { - return nil, errors.New("failed marshal sensor spec") + return nil, fmt.Errorf("failed marshal sensor spec") } encodedSensorSpec := base64.StdEncoding.EncodeToString(sensorBytes) envVars := []corev1.EnvVar{ @@ -156,7 +155,7 @@ func buildDeployment(args *AdaptorArgs, eventBus *eventbusv1alpha1.EventBus) (*a busConfigBytes, err := json.Marshal(eventBus.Status.Config) if err != nil { - return nil, errors.Errorf("failed marshal event bus config: %v", err) + return nil, fmt.Errorf("failed marshal event bus config: %v", err) } encodedBusConfig := base64.StdEncoding.EncodeToString(busConfigBytes) envVars = append(envVars, corev1.EnvVar{Name: common.EnvVarEventBusConfig, Value: encodedBusConfig}) @@ -170,7 +169,7 @@ func buildDeployment(args *AdaptorArgs, eventBus *eventbusv1alpha1.EventBus) (*a jsConf := eventBus.Status.Config.JetStream accessSecret = jsConf.AccessSecret default: - return nil, errors.New("unsupported event bus") + return nil, fmt.Errorf("unsupported event bus") } volumes := deploymentSpec.Template.Spec.Volumes diff --git a/controllers/sensor/validate.go b/controllers/sensor/validate.go index 8945581fa3..9b474c1dd1 100644 --- a/controllers/sensor/validate.go +++ b/controllers/sensor/validate.go @@ -21,7 +21,6 @@ import ( "net/http" "time" - "github.com/pkg/errors" cronlib "github.com/robfig/cron/v3" "github.com/argoproj/argo-events/common" @@ -106,47 +105,47 @@ func validateTriggerTemplate(template *v1alpha1.TriggerTemplate) error { } if template.K8s != nil { if err := validateK8STrigger(template.K8s); err != nil { - return errors.Wrapf(err, "trigger for template %s is invalid", template.Name) + return fmt.Errorf("trigger for template %s is invalid, %w", template.Name, err) } } if template.ArgoWorkflow != nil { if err := validateArgoWorkflowTrigger(template.ArgoWorkflow); err != nil { - return errors.Wrapf(err, "template %s is invalid", template.Name) + return fmt.Errorf("template %s is invalid, %w", template.Name, err) } } if template.HTTP != nil { if err := validateHTTPTrigger(template.HTTP); err != nil { - return errors.Wrapf(err, "template %s is invalid", template.Name) + return fmt.Errorf("template %s is invalid, %w", template.Name, err) } } if template.AWSLambda != nil { if err := validateAWSLambdaTrigger(template.AWSLambda); err != nil { - return errors.Wrapf(err, "template %s is invalid", template.Name) + return fmt.Errorf("template %s is invalid, %w", template.Name, err) } } if template.Kafka != nil { if err := validateKafkaTrigger(template.Kafka); err != nil { - return errors.Wrapf(err, "template %s is invalid", template.Name) + return fmt.Errorf("template %s is invalid, %w", template.Name, err) } } if template.NATS != nil { if err := validateNATSTrigger(template.NATS); err != nil { - return errors.Wrapf(err, "template %s is invalid", template.Name) + return fmt.Errorf("template %s is invalid, %w", template.Name, err) } } if template.Slack != nil { if err := validateSlackTrigger(template.Slack); err != nil { - return errors.Wrapf(err, "template %s is invalid", template.Name) + return fmt.Errorf("template %s is invalid, %w", template.Name, err) } } if template.OpenWhisk != nil { if err := validateOpenWhiskTrigger(template.OpenWhisk); err != nil { - return errors.Wrapf(err, "template %s is invalid", template.Name) + return fmt.Errorf("template %s is invalid, %w", template.Name, err) } } if template.CustomTrigger != nil { if err := validateCustomTrigger(template.CustomTrigger); err != nil { - return errors.Wrapf(err, "template %s is invalid", template.Name) + return fmt.Errorf("template %s is invalid, %w", template.Name, err) } } return nil @@ -155,22 +154,22 @@ func validateTriggerTemplate(template *v1alpha1.TriggerTemplate) error { // validateK8STrigger validates a kubernetes trigger func validateK8STrigger(trigger *v1alpha1.StandardK8STrigger) error { if trigger == nil { - return errors.New("k8s trigger can't be nil") + return fmt.Errorf("k8s trigger can't be nil") } if trigger.Source == nil { - return errors.New("k8s trigger does not contain an absolute action") + return fmt.Errorf("k8s trigger does not contain an absolute action") } switch trigger.Operation { case "", v1alpha1.Create, v1alpha1.Patch, v1alpha1.Update, v1alpha1.Delete: default: - return errors.Errorf("unknown operation type %s", string(trigger.Operation)) + return fmt.Errorf("unknown operation type %s", string(trigger.Operation)) } if trigger.Parameters != nil { for i, parameter := range trigger.Parameters { if err := validateTriggerParameter(¶meter); err != nil { - return errors.Errorf("resource parameter index: %d. err: %+v", i, err) + return fmt.Errorf("resource parameter index: %d. err: %w", i, err) } } } @@ -180,21 +179,21 @@ func validateK8STrigger(trigger *v1alpha1.StandardK8STrigger) error { // validateArgoWorkflowTrigger validates an Argo workflow trigger func validateArgoWorkflowTrigger(trigger *v1alpha1.ArgoWorkflowTrigger) error { if trigger == nil { - return errors.New("argoWorkflow trigger can't be nil") + return fmt.Errorf("argoWorkflow trigger can't be nil") } if trigger.Source == nil { - return errors.New("argoWorkflow trigger does not contain an absolute action") + return fmt.Errorf("argoWorkflow trigger does not contain an absolute action") } switch trigger.Operation { case v1alpha1.Submit, v1alpha1.SubmitFrom, v1alpha1.Suspend, v1alpha1.Retry, v1alpha1.Resume, v1alpha1.Resubmit, v1alpha1.Terminate, v1alpha1.Stop: default: - return errors.Errorf("unknown operation type %s", string(trigger.Operation)) + return fmt.Errorf("unknown operation type %s", string(trigger.Operation)) } if trigger.Parameters != nil { for i, parameter := range trigger.Parameters { if err := validateTriggerParameter(¶meter); err != nil { - return errors.Errorf("resource parameter index: %d. err: %+v", i, err) + return fmt.Errorf("resource parameter index: %d. err: %w", i, err) } } } @@ -204,29 +203,29 @@ func validateArgoWorkflowTrigger(trigger *v1alpha1.ArgoWorkflowTrigger) error { // validateHTTPTrigger validates the HTTP trigger func validateHTTPTrigger(trigger *v1alpha1.HTTPTrigger) error { if trigger == nil { - return errors.New("HTTP trigger for can't be nil") + return fmt.Errorf("HTTP trigger for can't be nil") } if trigger.URL == "" { - return errors.New("server URL is not specified") + return fmt.Errorf("server URL is not specified") } if trigger.Method != "" { switch trigger.Method { case http.MethodGet, http.MethodDelete, http.MethodPatch, http.MethodPost, http.MethodPut: default: - return errors.New("only GET, DELETE, PATCH, POST and PUT methods are supported") + return fmt.Errorf("only GET, DELETE, PATCH, POST and PUT methods are supported") } } if trigger.Parameters != nil { for i, parameter := range trigger.Parameters { if err := validateTriggerParameter(¶meter); err != nil { - return errors.Errorf("resource parameter index: %d. err: %+v", i, err) + return fmt.Errorf("resource parameter index: %d. err: %w", i, err) } } } if trigger.Payload != nil { for i, p := range trigger.Payload { if err := validateTriggerParameter(&p); err != nil { - return errors.Errorf("payload index: %d. err: %+v", i, err) + return fmt.Errorf("payload index: %d. err: %w", i, err) } } } @@ -236,33 +235,33 @@ func validateHTTPTrigger(trigger *v1alpha1.HTTPTrigger) error { // validateOpenWhiskTrigger validates the OpenWhisk trigger func validateOpenWhiskTrigger(trigger *v1alpha1.OpenWhiskTrigger) error { if trigger == nil { - return errors.New("openwhisk trigger for can't be nil") + return fmt.Errorf("openwhisk trigger for can't be nil") } if trigger.ActionName == "" { - return errors.New("action name is not specified") + return fmt.Errorf("action name is not specified") } if trigger.Host == "" { - return errors.New("host URL is not specified") + return fmt.Errorf("host URL is not specified") } if trigger.AuthToken != nil { if trigger.AuthToken.Name == "" || trigger.AuthToken.Key == "" { - return errors.New("auth token key and name must be specified") + return fmt.Errorf("auth token key and name must be specified") } } if trigger.Parameters != nil { for i, parameter := range trigger.Parameters { if err := validateTriggerParameter(¶meter); err != nil { - return errors.Errorf("resource parameter index: %d. err: %+v", i, err) + return fmt.Errorf("resource parameter index: %d. err: %w", i, err) } } } if trigger.Payload == nil { - return errors.New("payload parameters are not specified") + return fmt.Errorf("payload parameters are not specified") } if trigger.Payload != nil { for i, p := range trigger.Payload { if err := validateTriggerParameter(&p); err != nil { - return errors.Errorf("resource parameter index: %d. err: %+v", i, err) + return fmt.Errorf("resource parameter index: %d. err: %w", i, err) } } } @@ -272,28 +271,28 @@ func validateOpenWhiskTrigger(trigger *v1alpha1.OpenWhiskTrigger) error { // validateAWSLambdaTrigger validates the AWS Lambda trigger func validateAWSLambdaTrigger(trigger *v1alpha1.AWSLambdaTrigger) error { if trigger == nil { - return errors.New("openfaas trigger for can't be nil") + return fmt.Errorf("openfaas trigger for can't be nil") } if trigger.FunctionName == "" { - return errors.New("function name is not specified") + return fmt.Errorf("function name is not specified") } if trigger.Region == "" { - return errors.New("region in not specified") + return fmt.Errorf("region in not specified") } if trigger.Payload == nil { - return errors.New("payload parameters are not specified") + return fmt.Errorf("payload parameters are not specified") } if trigger.Parameters != nil { for i, parameter := range trigger.Parameters { if err := validateTriggerParameter(¶meter); err != nil { - return errors.Errorf("resource parameter index: %d. err: %+v", i, err) + return fmt.Errorf("resource parameter index: %d. err: %w", i, err) } } } if trigger.Payload != nil { for i, p := range trigger.Payload { if err := validateTriggerParameter(&p); err != nil { - return errors.Errorf("payload index: %d. err: %+v", i, err) + return fmt.Errorf("payload index: %d. err: %w", i, err) } } } @@ -303,21 +302,21 @@ func validateAWSLambdaTrigger(trigger *v1alpha1.AWSLambdaTrigger) error { // validateKafkaTrigger validates the kafka trigger. func validateKafkaTrigger(trigger *v1alpha1.KafkaTrigger) error { if trigger == nil { - return errors.New("trigger can't be nil") + return fmt.Errorf("trigger can't be nil") } if trigger.URL == "" { - return errors.New("broker url must not be empty") + return fmt.Errorf("broker url must not be empty") } if trigger.Payload == nil { - return errors.New("payload must not be empty") + return fmt.Errorf("payload must not be empty") } if trigger.Topic == "" { - return errors.New("topic must not be empty") + return fmt.Errorf("topic must not be empty") } if trigger.Payload != nil { for i, p := range trigger.Payload { if err := validateTriggerParameter(&p); err != nil { - return errors.Errorf("payload index: %d. err: %+v", i, err) + return fmt.Errorf("payload index: %d. err: %w", i, err) } } } @@ -327,21 +326,21 @@ func validateKafkaTrigger(trigger *v1alpha1.KafkaTrigger) error { // validateNATSTrigger validates the NATS trigger. func validateNATSTrigger(trigger *v1alpha1.NATSTrigger) error { if trigger == nil { - return errors.New("trigger can't be nil") + return fmt.Errorf("trigger can't be nil") } if trigger.URL == "" { - return errors.New("nats server url can't be empty") + return fmt.Errorf("nats server url can't be empty") } if trigger.Subject == "" { - return errors.New("nats subject can't be empty") + return fmt.Errorf("nats subject can't be empty") } if trigger.Payload == nil { - return errors.New("payload can't be nil") + return fmt.Errorf("payload can't be nil") } if trigger.Payload != nil { for i, p := range trigger.Payload { if err := validateTriggerParameter(&p); err != nil { - return errors.Errorf("payload index: %d. err: %+v", i, err) + return fmt.Errorf("payload index: %d. err: %w", i, err) } } } @@ -351,15 +350,15 @@ func validateNATSTrigger(trigger *v1alpha1.NATSTrigger) error { // validateSlackTrigger validates the Slack trigger. func validateSlackTrigger(trigger *v1alpha1.SlackTrigger) error { if trigger == nil { - return errors.New("trigger can't be nil") + return fmt.Errorf("trigger can't be nil") } if trigger.SlackToken == nil { - return errors.New("slack token can't be empty") + return fmt.Errorf("slack token can't be empty") } if trigger.Parameters != nil { for i, parameter := range trigger.Parameters { if err := validateTriggerParameter(¶meter); err != nil { - return errors.Errorf("resource parameter index: %d. err: %+v", i, err) + return fmt.Errorf("resource parameter index: %d. err: %w", i, err) } } } @@ -369,23 +368,23 @@ func validateSlackTrigger(trigger *v1alpha1.SlackTrigger) error { // validateCustomTrigger validates the custom trigger. func validateCustomTrigger(trigger *v1alpha1.CustomTrigger) error { if trigger == nil { - return errors.New("custom trigger for can't be nil") + return fmt.Errorf("custom trigger for can't be nil") } if trigger.ServerURL == "" { - return errors.New("custom trigger gRPC server url is not defined") + return fmt.Errorf("custom trigger gRPC server url is not defined") } if trigger.Spec == nil { - return errors.New("trigger body can't be empty") + return fmt.Errorf("trigger body can't be empty") } if trigger.Secure { if trigger.CertSecret == nil { - return errors.New("certSecret can't be nil when the trigger server connection is secure") + return fmt.Errorf("certSecret can't be nil when the trigger server connection is secure") } } if trigger.Parameters != nil { for i, parameter := range trigger.Parameters { if err := validateTriggerParameter(¶meter); err != nil { - return errors.Errorf("resource parameter index: %d. err: %+v", i, err) + return fmt.Errorf("resource parameter index: %d. err: %w", i, err) } } } @@ -397,7 +396,7 @@ func validateTriggerTemplateParameters(trigger *v1alpha1.Trigger) error { if trigger.Parameters != nil { for i, parameter := range trigger.Parameters { if err := validateTriggerParameter(¶meter); err != nil { - return errors.Errorf("template parameter index: %d. err: %+v", i, err) + return fmt.Errorf("template parameter index: %d. err: %w", i, err) } } } @@ -407,13 +406,13 @@ func validateTriggerTemplateParameters(trigger *v1alpha1.Trigger) error { // validateTriggerParameter validates a trigger parameter func validateTriggerParameter(parameter *v1alpha1.TriggerParameter) error { if parameter.Src == nil { - return errors.Errorf("parameter source can't be empty") + return fmt.Errorf("parameter source can't be empty") } if parameter.Src.DependencyName == "" { - return errors.Errorf("parameter dependency name can't be empty") + return fmt.Errorf("parameter dependency name can't be empty") } if parameter.Dest == "" { - return errors.Errorf("parameter destination can't be empty") + return fmt.Errorf("parameter destination can't be empty") } switch op := parameter.Operation; op { @@ -422,7 +421,7 @@ func validateTriggerParameter(parameter *v1alpha1.TriggerParameter) error { case v1alpha1.TriggerParameterOpPrepend: case v1alpha1.TriggerParameterOpNone: default: - return errors.Errorf("parameter operation %+v is invalid", op) + return fmt.Errorf("parameter operation %+v is invalid", op) } return nil @@ -600,7 +599,7 @@ func validateEventTimeFilter(timeFilter *v1alpha1.TimeFilter) error { } if stopTime.Equal(startTime) { - return errors.Errorf("invalid event time filter: stop '%s' is equal to start '%s", timeFilter.Stop, timeFilter.Start) + return fmt.Errorf("invalid event time filter: stop '%s' is equal to start '%s", timeFilter.Stop, timeFilter.Start) } return nil } diff --git a/eventbus/common/error.go b/eventbus/common/error.go new file mode 100644 index 0000000000..ddbc27d5c6 --- /dev/null +++ b/eventbus/common/error.go @@ -0,0 +1,15 @@ +package common + +// EventBusError is a particular EventBus related error. +type EventBusError struct { + err error +} + +func (e *EventBusError) Error() string { + return e.err.Error() +} + +// NewEventBusError returns an EventBusError. +func NewEventBusError(err error) error { + return &EventBusError{err: err} +} diff --git a/eventbus/common/error_test.go b/eventbus/common/error_test.go new file mode 100644 index 0000000000..5c98e8c1ac --- /dev/null +++ b/eventbus/common/error_test.go @@ -0,0 +1,25 @@ +package common + +import ( + "errors" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_error(t *testing.T) { + err := fmt.Errorf("error") + var ebErr *EventBusError + assert.False(t, errors.As(err, &ebErr)) + err = fmt.Errorf("err1, %w", err) + assert.False(t, errors.As(err, &ebErr)) + err = NewEventBusError(err) + assert.True(t, errors.As(err, &ebErr)) + err = fmt.Errorf("err3, %w", err) + assert.True(t, errors.As(err, &ebErr)) + err = fmt.Errorf("err4, %w", err) + assert.True(t, errors.As(err, &ebErr)) + err = fmt.Errorf("err5, %w", err) + assert.True(t, errors.As(err, &ebErr)) +} diff --git a/eventbus/driver.go b/eventbus/driver.go index 7252a48600..3344607c42 100644 --- a/eventbus/driver.go +++ b/eventbus/driver.go @@ -2,6 +2,7 @@ package eventbus import ( "context" + "fmt" "github.com/argoproj/argo-events/common" apicommon "github.com/argoproj/argo-events/pkg/apis/common" @@ -16,7 +17,6 @@ import ( stansensor "github.com/argoproj/argo-events/eventbus/stan/sensor" eventbusv1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1" "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" - "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -26,7 +26,7 @@ func GetEventSourceDriver(ctx context.Context, eventBusConfig eventbusv1alpha1.B return nil, err } if eventSourceName == "" { - return nil, errors.New("eventSourceName must be specified to create eventbus driver") + return nil, fmt.Errorf("eventSourceName must be specified to create eventbus driver") } logger := logging.FromContext(ctx) @@ -36,20 +36,20 @@ func GetEventSourceDriver(ctx context.Context, eventBusConfig eventbusv1alpha1.B var eventBusType apicommon.EventBusType switch { case eventBusConfig.NATS != nil && eventBusConfig.JetStream != nil: - return nil, errors.New("invalid event bus, NATS and Jetstream shouldn't both be specified") + return nil, fmt.Errorf("invalid event bus, NATS and Jetstream shouldn't both be specified") case eventBusConfig.NATS != nil: eventBusType = apicommon.EventBusNATS case eventBusConfig.JetStream != nil: eventBusType = apicommon.EventBusJetStream default: - return nil, errors.New("invalid event bus") + return nil, fmt.Errorf("invalid event bus") } var dvr eventbuscommon.EventSourceDriver switch eventBusType { case apicommon.EventBusNATS: if defaultSubject == "" { - return nil, errors.New("subject must be specified to create NATS Streaming driver") + return nil, fmt.Errorf("subject must be specified to create NATS Streaming driver") } dvr = stansource.NewSourceSTAN(eventBusConfig.NATS.URL, *eventBusConfig.NATS.ClusterID, eventSourceName, defaultSubject, auth, logger) case apicommon.EventBusJetStream: @@ -58,7 +58,7 @@ func GetEventSourceDriver(ctx context.Context, eventBusConfig eventbusv1alpha1.B return nil, err } default: - return nil, errors.New("invalid eventbus type") + return nil, fmt.Errorf("invalid eventbus type") } return dvr, nil } @@ -70,23 +70,23 @@ func GetSensorDriver(ctx context.Context, eventBusConfig eventbusv1alpha1.BusCon } if sensorSpec == nil { - return nil, errors.New("sensorSpec required for getting eventbus driver") + return nil, fmt.Errorf("sensorSpec required for getting eventbus driver") } if sensorSpec.Name == "" { - return nil, errors.New("sensorSpec name must be set for getting eventbus driver") + return nil, fmt.Errorf("sensorSpec name must be set for getting eventbus driver") } logger := logging.FromContext(ctx) var eventBusType apicommon.EventBusType switch { case eventBusConfig.NATS != nil && eventBusConfig.JetStream != nil: - return nil, errors.New("invalid event bus, NATS and Jetstream shouldn't both be specified") + return nil, fmt.Errorf("invalid event bus, NATS and Jetstream shouldn't both be specified") case eventBusConfig.NATS != nil: eventBusType = apicommon.EventBusNATS case eventBusConfig.JetStream != nil: eventBusType = apicommon.EventBusJetStream default: - return nil, errors.New("invalid event bus") + return nil, fmt.Errorf("invalid event bus") } var dvr eventbuscommon.SensorDriver @@ -98,7 +98,7 @@ func GetSensorDriver(ctx context.Context, eventBusConfig eventbusv1alpha1.BusCon dvr, err = jetstreamsensor.NewSensorJetstream(eventBusConfig.JetStream.URL, sensorSpec, eventBusConfig.JetStream.StreamConfig, auth, logger) // don't need to pass in subject because subjects will be derived from dependencies return dvr, err default: - return nil, errors.New("invalid eventbus type") + return nil, fmt.Errorf("invalid eventbus type") } } @@ -112,7 +112,7 @@ func GetAuth(ctx context.Context, eventBusConfig eventbusv1alpha1.BusConfig) (*e case eventBusConfig.JetStream != nil: eventBusAuth = &eventbusv1alpha1.AuthStrategyBasic default: - return nil, errors.New("invalid event bus") + return nil, fmt.Errorf("invalid event bus") } var auth *eventbuscommon.Auth cred := &eventbuscommon.AuthCredential{} @@ -127,7 +127,7 @@ func GetAuth(ctx context.Context, eventBusConfig eventbusv1alpha1.BusConfig) (*e v.AddConfigPath(common.EventBusAuthFileMountPath) err := v.ReadInConfig() if err != nil { - return nil, errors.Errorf("failed to load auth.yaml. err: %+v", err) + return nil, fmt.Errorf("failed to load auth.yaml. err: %w", err) } err = v.Unmarshal(cred) if err != nil { diff --git a/eventbus/jetstream/base/jetstream.go b/eventbus/jetstream/base/jetstream.go index 41a3311314..3087830fe1 100644 --- a/eventbus/jetstream/base/jetstream.go +++ b/eventbus/jetstream/base/jetstream.go @@ -9,7 +9,6 @@ import ( eventbuscommon "github.com/argoproj/argo-events/eventbus/common" eventbusv1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1" nats "github.com/nats-io/nats.go" - "github.com/pkg/errors" "github.com/spf13/viper" "go.uber.org/zap" ) @@ -41,7 +40,7 @@ func (stream *Jetstream) Init() error { if err != nil { errStr := fmt.Sprintf("error creating Management Connection for Jetstream stream %+v: %v", stream, err) stream.Logger.Error(errStr) - return errors.New(errStr) + return fmt.Errorf(errStr) } err = stream.CreateStream(mgmtConnection) if err != nil { @@ -83,7 +82,7 @@ func (stream *Jetstream) MakeConnection() (*JetstreamConnection, error) { case eventbusv1alpha1.AuthStrategyNone: log.Info("NATS auth strategy: None") default: - return nil, errors.New("unsupported auth strategy") + return nil, fmt.Errorf("unsupported auth strategy") } nc, err := nats.Connect(stream.url, opts...) if err != nil { @@ -106,7 +105,7 @@ func (stream *Jetstream) MakeConnection() (*JetstreamConnection, error) { func (stream *Jetstream) CreateStream(conn *JetstreamConnection) error { if conn == nil { - return errors.New("Can't create Stream on nil connection") + return fmt.Errorf("Can't create Stream on nil connection") } var err error @@ -142,12 +141,12 @@ func (stream *Jetstream) CreateStream(conn *JetstreamConnection) error { } stream.Logger.Infof("Will use this stream config:\n '%v'", streamConfig) - connectErr := common.Connect(nil, func() error { // exponential backoff if it fails the first time + connectErr := common.DoWithRetry(nil, func() error { // exponential backoff if it fails the first time _, err = conn.JSContext.AddStream(&streamConfig) if err != nil { errStr := fmt.Sprintf(`Failed to add Jetstream stream '%s'for connection %+v: err=%v`, common.JetStreamStreamName, conn, err) - return errors.New(errStr) + return fmt.Errorf(errStr) } else { return nil } diff --git a/eventbus/jetstream/base/jetstream_conn.go b/eventbus/jetstream/base/jetstream_conn.go index 01c4566792..5b0d8c7b6c 100644 --- a/eventbus/jetstream/base/jetstream_conn.go +++ b/eventbus/jetstream/base/jetstream_conn.go @@ -1,7 +1,7 @@ package base import ( - "errors" + "fmt" nats "github.com/nats-io/nats.go" "go.uber.org/zap" @@ -18,7 +18,7 @@ type JetstreamConnection struct { func (jsc *JetstreamConnection) Close() error { if jsc == nil { - return errors.New("can't close Jetstream connection, JetstreamConnection is nil") + return fmt.Errorf("can't close Jetstream connection, JetstreamConnection is nil") } if jsc.NATSConn != nil && jsc.NATSConn.IsConnected() { jsc.NATSConn.Close() diff --git a/eventbus/jetstream/eventsource/source_conn.go b/eventbus/jetstream/eventsource/source_conn.go index 341b3e6e97..6b31ab876e 100644 --- a/eventbus/jetstream/eventsource/source_conn.go +++ b/eventbus/jetstream/eventsource/source_conn.go @@ -2,7 +2,6 @@ package eventsource import ( "context" - "errors" "fmt" eventbuscommon "github.com/argoproj/argo-events/eventbus/common" @@ -24,7 +23,7 @@ func CreateJetstreamSourceConn(conn *jetstreambase.JetstreamConnection, eventSou func (jsc *JetstreamSourceConn) Publish(ctx context.Context, msg eventbuscommon.Message) error { if jsc == nil { - return errors.New("Publish() failed; JetstreamSourceConn is nil") + return fmt.Errorf("Publish() failed; JetstreamSourceConn is nil") } // exactly once on the publishing side is done by assigning a "deduplication key" to the message diff --git a/eventbus/jetstream/sensor/sensor_jetstream.go b/eventbus/jetstream/sensor/sensor_jetstream.go index f77bc75a61..73b372c691 100644 --- a/eventbus/jetstream/sensor/sensor_jetstream.go +++ b/eventbus/jetstream/sensor/sensor_jetstream.go @@ -5,8 +5,6 @@ import ( "strings" "time" - "errors" - eventbuscommon "github.com/argoproj/argo-events/eventbus/common" eventbusjetstreambase "github.com/argoproj/argo-events/eventbus/jetstream/base" "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" @@ -36,7 +34,7 @@ func NewSensorJetstream(url string, sensorSpec *v1alpha1.Sensor, streamConfig st if sensorSpec == nil { errStr := SensorNilError logger.Errorf(errStr) - return nil, errors.New(errStr) + return nil, fmt.Errorf(errStr) } baseJetstream, err := eventbusjetstreambase.NewJetstream(url, streamConfig, auth, logger) @@ -89,7 +87,7 @@ func (stream *SensorJetstream) setStateToSpec(sensorSpec *v1alpha1.Sensor) error if sensorSpec == nil { errStr := SensorNilError log.Error(errStr) - return errors.New(errStr) + return fmt.Errorf(errStr) } log.Infof("Comparing previous Spec stored in k/v store for sensor %s to new Spec", sensorSpec.Name) @@ -160,7 +158,7 @@ func (stream *SensorJetstream) saveSpec(sensorSpec *v1alpha1.Sensor, removedTrig if err != nil { errStr := fmt.Sprintf("error deleting key %s: %v", key, err) stream.Logger.Error(errStr) - return errors.New(errStr) + return fmt.Errorf(errStr) } stream.Logger.Debugf("successfully removed Trigger expression at key %s", key) } @@ -172,7 +170,7 @@ func (stream *SensorJetstream) saveSpec(sensorSpec *v1alpha1.Sensor, removedTrig if err != nil { errStr := fmt.Sprintf("failed to hash dependency %+v", dep) stream.Logger.Errorf(errStr) - err = errors.New(errStr) + err = fmt.Errorf(errStr) return err } depMap[dep.Name] = hash @@ -207,7 +205,7 @@ func (stream *SensorJetstream) getChangedTriggers(sensorSpec *v1alpha1.Sensor) ( if sensorSpec == nil { errStr := SensorNilError stream.Logger.Errorf(errStr) - err = errors.New(errStr) + err = fmt.Errorf(errStr) return } @@ -243,7 +241,7 @@ func (stream *SensorJetstream) getChangedDeps(sensorSpec *v1alpha1.Sensor) (chan if sensorSpec == nil { errStr := SensorNilError stream.Logger.Errorf(errStr) - err = errors.New(errStr) + err = fmt.Errorf(errStr) return } @@ -266,7 +264,7 @@ func (stream *SensorJetstream) getChangedDeps(sensorSpec *v1alpha1.Sensor) (chan if err != nil { errStr := fmt.Sprintf("failed to hash dependency %+v", currDep) stream.Logger.Errorf(errStr) - err = errors.New(errStr) + err = fmt.Errorf(errStr) return nil, nil, nil, err } if hash == hashedDep { @@ -288,7 +286,7 @@ func (stream *SensorJetstream) getDependencyDefinitions() (DependencyDefinitionV } errStr := fmt.Sprintf("error getting key %s: %v", DependencyDefsKey, err) stream.Logger.Error(errStr) - return nil, errors.New(errStr) + return nil, fmt.Errorf(errStr) } stream.Logger.Debugf("Value of key %s: %s", DependencyDefsKey, string(depDefs.Value())) @@ -297,7 +295,7 @@ func (stream *SensorJetstream) getDependencyDefinitions() (DependencyDefinitionV if err != nil { errStr := fmt.Sprintf("error unmarshalling value %s of key %s: %v", string(depDefs.Value()), DependencyDefsKey, err) stream.Logger.Error(errStr) - return nil, errors.New(errStr) + return nil, fmt.Errorf(errStr) } return depDefMap, nil @@ -308,13 +306,13 @@ func (stream *SensorJetstream) storeDependencyDefinitions(depDef DependencyDefin if err != nil { errStr := fmt.Sprintf("error marshalling %+v: %v", depDef, err) stream.Logger.Error(errStr) - return errors.New(errStr) + return fmt.Errorf(errStr) } _, err = stream.keyValueStore.Put(DependencyDefsKey, bytes) if err != nil { errStr := fmt.Sprintf("error storing %s under key %s: %v", string(bytes), DependencyDefsKey, err) stream.Logger.Error(errStr) - return errors.New(errStr) + return fmt.Errorf(errStr) } stream.Logger.Debugf("successfully stored dependency definition under key %s: %s", DependencyDefsKey, string(bytes)) return nil @@ -328,7 +326,7 @@ func (stream *SensorJetstream) getTriggerList() (TriggerValue, error) { } errStr := fmt.Sprintf("error getting key %s: %v", TriggersKey, err) stream.Logger.Error(errStr) - return nil, errors.New(errStr) + return nil, fmt.Errorf(errStr) } stream.Logger.Debugf("Value of key %s: %s", TriggersKey, string(triggerListJson.Value())) @@ -337,7 +335,7 @@ func (stream *SensorJetstream) getTriggerList() (TriggerValue, error) { if err != nil { errStr := fmt.Sprintf("error unmarshalling value %s of key %s: %v", string(triggerListJson.Value()), TriggersKey, err) stream.Logger.Error(errStr) - return nil, errors.New(errStr) + return nil, fmt.Errorf(errStr) } return triggerList, nil @@ -348,13 +346,13 @@ func (stream *SensorJetstream) storeTriggerList(triggerList TriggerValue) error if err != nil { errStr := fmt.Sprintf("error marshalling %+v: %v", triggerList, err) stream.Logger.Error(errStr) - return errors.New(errStr) + return fmt.Errorf(errStr) } _, err = stream.keyValueStore.Put(TriggersKey, bytes) if err != nil { errStr := fmt.Sprintf("error storing %s under key %s: %v", string(bytes), TriggersKey, err) stream.Logger.Error(errStr) - return errors.New(errStr) + return fmt.Errorf(errStr) } stream.Logger.Debugf("successfully stored trigger list under key %s: %s", TriggersKey, string(bytes)) return nil @@ -369,7 +367,7 @@ func (stream *SensorJetstream) getTriggerExpression(triggerName string) (string, } errStr := fmt.Sprintf("error getting key %s: %v", key, err) stream.Logger.Error(errStr) - return "", errors.New(errStr) + return "", fmt.Errorf(errStr) } stream.Logger.Debugf("Value of key %s: %s", key, string(expr.Value())) @@ -382,7 +380,7 @@ func (stream *SensorJetstream) storeTriggerExpression(triggerName string, condit if err != nil { errStr := fmt.Sprintf("error storing %s under key %s: %v", conditionExpression, key, err) stream.Logger.Error(errStr) - return errors.New(errStr) + return fmt.Errorf(errStr) } stream.Logger.Debugf("successfully stored trigger expression under key %s: %s", key, conditionExpression) return nil diff --git a/eventbus/jetstream/sensor/trigger_conn.go b/eventbus/jetstream/sensor/trigger_conn.go index 4944554657..80bbcafe59 100644 --- a/eventbus/jetstream/sensor/trigger_conn.go +++ b/eventbus/jetstream/sensor/trigger_conn.go @@ -2,6 +2,8 @@ package sensor import ( "context" + "encoding/json" + "errors" "fmt" "strings" "sync" @@ -9,13 +11,10 @@ import ( "github.com/Knetic/govaluate" cloudevents "github.com/cloudevents/sdk-go/v2" - "github.com/pkg/errors" - - "encoding/json" + nats "github.com/nats-io/nats.go" eventbuscommon "github.com/argoproj/argo-events/eventbus/common" jetstreambase "github.com/argoproj/argo-events/eventbus/jetstream/base" - nats "github.com/nats-io/nats.go" ) type JetstreamTriggerConn struct { @@ -70,14 +69,14 @@ func NewJetstreamTriggerConn(conn *jetstreambase.JetstreamConnection, if err != nil { errStr := fmt.Sprintf("failed to evaluate expression %s: %v", dependencyExpression, err) connection.Logger.Error(errStr) - return nil, errors.New(errStr) + return nil, fmt.Errorf(errStr) } connection.keyValueStore, err = conn.JSContext.KeyValue(sensorName) if err != nil { errStr := fmt.Sprintf("failed to get K/V store for sensor %s: %v", sensorName, err) connection.Logger.Error(errStr) - return nil, errors.New(errStr) + return nil, fmt.Errorf(errStr) } connection.Logger.Infof("Successfully located K/V store for sensor %s", sensorName) @@ -100,7 +99,7 @@ func (conn *JetstreamTriggerConn) Subscribe(ctx context.Context, action func(map[string]cloudevents.Event), defaultSubject *string) error { if conn == nil { - return errors.New("Subscribe() failed; JetstreamTriggerConn is nil") + return fmt.Errorf("Subscribe() failed; JetstreamTriggerConn is nil") } var err error @@ -138,7 +137,7 @@ func (conn *JetstreamTriggerConn) Subscribe(ctx context.Context, if err != nil { errorStr := fmt.Sprintf("Failed to subscribe to subject %s using group %s: %v", subject, durableName, err) log.Error(errorStr) - return errors.New(errorStr) + return fmt.Errorf(errorStr) } else { log.Debugf("successfully subscribed to subject %s with durable name %s", subject, durableName) } @@ -417,7 +416,7 @@ func (conn *JetstreamTriggerConn) getSavedDependency(depName string) (msg MsgInf if err != nil { errStr := fmt.Sprintf("error unmarshalling value %s for key %s: %v", string(entry.Value()), key, err) conn.Logger.Error(errStr) - return MsgInfo{}, true, errors.New(errStr) + return MsgInfo{}, true, fmt.Errorf(errStr) } return msgInfo, true, nil } @@ -434,7 +433,7 @@ func (conn *JetstreamTriggerConn) saveDependency(depName string, msgInfo MsgInfo if err != nil { errorStr := fmt.Sprintf("failed to convert msgInfo struct into JSON: %+v", msgInfo) log.Error(errorStr) - return errors.New(errorStr) + return fmt.Errorf(errorStr) } key := getDependencyKey(conn.triggerName, depName) @@ -442,7 +441,7 @@ func (conn *JetstreamTriggerConn) saveDependency(depName string, msgInfo MsgInfo if err != nil { errorStr := fmt.Sprintf("failed to store dependency under key %s, value:%s: %+v", key, jsonEncodedMsg, err) log.Error(errorStr) - return errors.New(errorStr) + return fmt.Errorf(errorStr) } return nil @@ -506,7 +505,7 @@ func (conn *JetstreamTriggerConn) getDependencyNames(eventSourceName, eventName errStr := fmt.Sprintf("incoming event source and event not associated with any dependencies, event source=%s, event=%s", eventSourceName, eventName) conn.Logger.Error(errStr) - return nil, errors.New(errStr) + return nil, fmt.Errorf(errStr) } return deps, nil diff --git a/eventbus/stan/base/stan.go b/eventbus/stan/base/stan.go index afa4a7fb6e..37cda0676c 100644 --- a/eventbus/stan/base/stan.go +++ b/eventbus/stan/base/stan.go @@ -1,11 +1,12 @@ package base import ( + "fmt" + eventbuscommon "github.com/argoproj/argo-events/eventbus/common" eventbusv1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1" nats "github.com/nats-io/nats.go" "github.com/nats-io/stan.go" - "github.com/pkg/errors" "go.uber.org/zap" ) @@ -49,7 +50,7 @@ func (n *STAN) MakeConnection(clientID string) (*STANConnection, error) { case eventbusv1alpha1.AuthStrategyNone: log.Info("NATS auth strategy: None") default: - return nil, errors.New("unsupported auth strategy") + return nil, fmt.Errorf("unsupported auth strategy") } nc, err := nats.Connect(n.url, opts...) if err != nil { diff --git a/eventbus/stan/base/stan_conn.go b/eventbus/stan/base/stan_conn.go index ba7a0a2b2f..0a434413fb 100644 --- a/eventbus/stan/base/stan_conn.go +++ b/eventbus/stan/base/stan_conn.go @@ -1,7 +1,7 @@ package base import ( - "errors" + "fmt" nats "github.com/nats-io/nats.go" "github.com/nats-io/stan.go" @@ -23,7 +23,7 @@ type STANConnection struct { func (nsc *STANConnection) Close() error { if nsc == nil { - return errors.New("can't close STAN connection, STANConnection is nil") + return fmt.Errorf("can't close STAN connection, STANConnection is nil") } if nsc.STANConn != nil { err := nsc.STANConn.Close() diff --git a/eventbus/stan/eventsource/source_conn.go b/eventbus/stan/eventsource/source_conn.go index e0c7d919bb..930f5f80d9 100644 --- a/eventbus/stan/eventsource/source_conn.go +++ b/eventbus/stan/eventsource/source_conn.go @@ -2,7 +2,7 @@ package eventsource import ( "context" - "errors" + "fmt" eventbuscommon "github.com/argoproj/argo-events/eventbus/common" stanbase "github.com/argoproj/argo-events/eventbus/stan/base" @@ -17,7 +17,7 @@ type STANSourceConn struct { func (n *STANSourceConn) Publish(ctx context.Context, msg eventbuscommon.Message) error { if n == nil { - return errors.New("Publish() failed; JetstreamSourceConn is nil") + return fmt.Errorf("Publish() failed; JetstreamSourceConn is nil") } return n.STANConn.Publish(n.subject, msg.Body) } diff --git a/eventbus/stan/sensor/trigger_conn.go b/eventbus/stan/sensor/trigger_conn.go index fc2f5254ae..58a738fe5e 100644 --- a/eventbus/stan/sensor/trigger_conn.go +++ b/eventbus/stan/sensor/trigger_conn.go @@ -13,7 +13,6 @@ import ( "github.com/gobwas/glob" "github.com/nats-io/stan.go" "github.com/nats-io/stan.go/pb" - "github.com/pkg/errors" "go.uber.org/zap" eventbuscommon "github.com/argoproj/argo-events/eventbus/common" @@ -64,7 +63,7 @@ func (n *STANTriggerConn) Subscribe( action func(map[string]cloudevents.Event), defaultSubject *string) error { if n == nil { - return errors.New("Subscribe() failed; STANTriggerConn is nil") + return fmt.Errorf("Subscribe() failed; STANTriggerConn is nil") } log := n.Logger @@ -282,7 +281,7 @@ func (n *STANTriggerConn) getGroupNameFromClientID(clientID string) (string, err // take off the last part: clientID should have a dash at the end and we can remove that part strs := strings.Split(clientID, "-") if len(strs) < 2 { - err := errors.Errorf("Expected client ID to contain dash: %s", clientID) + err := fmt.Errorf("Expected client ID to contain dash: %s", clientID) log.Error(err) return "", err } @@ -327,7 +326,7 @@ func newEventSourceMessageHolder(logger *zap.SugaredLogger, dependencyExpr strin } deps := unique(expression.Vars()) if len(dependencyExpr) == 0 { - return nil, errors.Errorf("no dependencies found: %s", dependencyExpr) + return nil, fmt.Errorf("no dependencies found: %s", dependencyExpr) } srcDepMap := make(map[string]string) diff --git a/eventsources/common/aws/aws.go b/eventsources/common/aws/aws.go index 148bb3dbfa..6441b5fca9 100644 --- a/eventsources/common/aws/aws.go +++ b/eventsources/common/aws/aws.go @@ -17,11 +17,12 @@ limitations under the License. package aws import ( + "fmt" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials/stscreds" "github.com/aws/aws-sdk-go/aws/session" - "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" "github.com/argoproj/argo-events/common" @@ -31,11 +32,11 @@ import ( func GetAWSCredFromEnvironment(access *corev1.SecretKeySelector, secret *corev1.SecretKeySelector) (*credentials.Credentials, error) { accessKey, ok := common.GetEnvFromSecret(access) if !ok { - return nil, errors.Errorf("can not find envFrom %v", access) + return nil, fmt.Errorf("can not find envFrom %v", access) } secretKey, ok := common.GetEnvFromSecret(secret) if !ok { - return nil, errors.Errorf("can not find envFrom %v", secret) + return nil, fmt.Errorf("can not find envFrom %v", secret) } return credentials.NewStaticCredentialsFromCreds(credentials.Value{ AccessKeyID: accessKey, @@ -47,18 +48,18 @@ func GetAWSCredFromEnvironment(access *corev1.SecretKeySelector, secret *corev1. func GetAWSCredFromVolume(access *corev1.SecretKeySelector, secret *corev1.SecretKeySelector, sessionToken *corev1.SecretKeySelector) (*credentials.Credentials, error) { accessKey, err := common.GetSecretFromVolume(access) if err != nil { - return nil, errors.Wrap(err, "can not find access key") + return nil, fmt.Errorf("can not find access key, %w", err) } secretKey, err := common.GetSecretFromVolume(secret) if err != nil { - return nil, errors.Wrap(err, "can not find secret key") + return nil, fmt.Errorf("can not find secret key, %w", err) } var token string if sessionToken != nil { token, err = common.GetSecretFromVolume(sessionToken) if err != nil { - return nil, errors.Wrap(err, "can not find session token") + return nil, fmt.Errorf("can not find session token, %w", err) } } diff --git a/eventsources/common/naivewatcher/watcher.go b/eventsources/common/naivewatcher/watcher.go index 2ef46bf1dd..dde1554fa3 100644 --- a/eventsources/common/naivewatcher/watcher.go +++ b/eventsources/common/naivewatcher/watcher.go @@ -1,7 +1,7 @@ package naivewatcher import ( - "errors" + "fmt" "os" "path/filepath" "sync" @@ -99,7 +99,7 @@ func (w *Watcher) Close() error { // Start starts the watcher func (w *Watcher) Start(interval time.Duration) error { if !w.mutexRunning.TryLock() { - return errors.New("watcher has already started") + return fmt.Errorf("watcher has already started") } // run initial check err := w.Check() @@ -127,7 +127,7 @@ func (w *Watcher) Start(interval time.Duration) error { // Stop stops the watcher func (w *Watcher) Stop() error { if !w.mutexRunning.IsLocked() { - return errors.New("watcher is not started") + return fmt.Errorf("watcher is not started") } select { case <-w.stop: @@ -141,7 +141,7 @@ func (w *Watcher) Stop() error { // Check checks the state of target directories func (w *Watcher) Check() error { if !w.mCheck.TryLock() { - return errors.New("another check is still running") + return fmt.Errorf("another check is still running") } defer w.mCheck.Unlock() diff --git a/eventsources/eventing.go b/eventsources/eventing.go index 8888c2a10d..f93624d662 100644 --- a/eventsources/eventing.go +++ b/eventsources/eventing.go @@ -12,7 +12,6 @@ import ( cloudevents "github.com/cloudevents/sdk-go/v2" "github.com/google/uuid" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -43,7 +42,7 @@ import ( "github.com/argoproj/argo-events/eventsources/sources/nsq" "github.com/argoproj/argo-events/eventsources/sources/pulsar" "github.com/argoproj/argo-events/eventsources/sources/redis" - redisstream "github.com/argoproj/argo-events/eventsources/sources/redisStream" + redisstream "github.com/argoproj/argo-events/eventsources/sources/redis_stream" "github.com/argoproj/argo-events/eventsources/sources/resource" "github.com/argoproj/argo-events/eventsources/sources/slack" "github.com/argoproj/argo-events/eventsources/sources/storagegrid" @@ -410,7 +409,7 @@ func (e *EventSourceAdaptor) run(ctx context.Context, servers map[apicommon.Even logger.Errorw("failed to get eventbus driver", zap.Error(err)) return err } - if err = common.Connect(&common.DefaultBackoff, func() error { + if err = common.DoWithRetry(&common.DefaultBackoff, func() error { err = driver.Initialize() if err != nil { return err @@ -484,7 +483,7 @@ func (e *EventSourceAdaptor) run(ctx context.Context, servers map[apicommon.Even Factor: &factor, Jitter: &jitter, } - if err = common.Connect(&backoff, func() error { + if err = common.DoWithRetry(&backoff, func() error { return s.StartListening(ctx, func(data []byte, opts ...eventsourcecommon.Option) error { if filter, ok := filters[s.GetEventName()]; ok { proceed, err := filterEvent(data, filter) @@ -493,7 +492,7 @@ func (e *EventSourceAdaptor) run(ctx context.Context, servers map[apicommon.Even return nil } if !proceed { - logger.Debug("Do not publish event, filter condition not met") + logger.Info("Filter condition not met, skip dispatching") return nil } } @@ -520,7 +519,7 @@ func (e *EventSourceAdaptor) run(ctx context.Context, servers map[apicommon.Even } if e.eventBusConn == nil || e.eventBusConn.IsClosed() { - return errors.New("failed to publish event, eventbus connection closed") + return eventbuscommon.NewEventBusError(fmt.Errorf("failed to publish event, eventbus connection closed")) } msg := eventbuscommon.Message{ @@ -532,21 +531,21 @@ func (e *EventSourceAdaptor) run(ctx context.Context, servers map[apicommon.Even Body: eventBody, } - if err = common.Connect(&common.DefaultBackoff, func() error { + if err = common.DoWithRetry(&common.DefaultBackoff, func() error { return e.eventBusConn.Publish(ctx, msg) }); err != nil { - logger.Errorw("failed to publish an event", zap.Error(err), zap.String(logging.LabelEventName, + logger.Errorw("Failed to publish an event", zap.Error(err), zap.String(logging.LabelEventName, s.GetEventName()), zap.Any(logging.LabelEventSourceType, s.GetEventSourceType())) e.metrics.EventSentFailed(s.GetEventSourceName(), s.GetEventName()) - return err + return eventbuscommon.NewEventBusError(err) } - logger.Infow("succeeded to publish an event", zap.String(logging.LabelEventName, + logger.Infow("Succeeded to publish an event", zap.String(logging.LabelEventName, s.GetEventName()), zap.Any(logging.LabelEventSourceType, s.GetEventSourceType()), zap.String("eventID", event.ID())) e.metrics.EventSent(s.GetEventSourceName(), s.GetEventName()) return nil }) }); err != nil { - logger.Errorw("failed to start listening eventsource", zap.Any(logging.LabelEventSourceType, + logger.Errorw("Failed to start listening eventsource", zap.Any(logging.LabelEventSourceType, s.GetEventSourceType()), zap.Any(logging.LabelEventName, s.GetEventName()), zap.Error(err)) } }(server) @@ -572,7 +571,7 @@ func (e *EventSourceAdaptor) run(ctx context.Context, servers map[apicommon.Even logger.Error("Erroring out, no active event server running") cancel() connWG.Wait() - return errors.New("no active event server running") + return fmt.Errorf("no active event server running") } } } diff --git a/eventsources/persist/event_persist.go b/eventsources/persist/event_persist.go index e3d25e06d9..b97eeac1d8 100644 --- a/eventsources/persist/event_persist.go +++ b/eventsources/persist/event_persist.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/pkg/errors" v1 "k8s.io/api/core/v1" apierr "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -73,10 +72,10 @@ func (cmp *ConfigMapPersist) IsEnabled() bool { func (cmp *ConfigMapPersist) Save(event *Event) error { if event == nil { - return errors.Errorf("event object is nil") + return fmt.Errorf("event object is nil") } // Using Connect util func for backoff retry if K8s API returns error - err := common.Connect(&common.DefaultBackoff, func() error { + err := common.DoWithRetry(&common.DefaultBackoff, func() error { cm, err := cmp.kubeClient.CoreV1().ConfigMaps(cmp.namespace).Get(cmp.ctx, cmp.name, metav1.GetOptions{}) if err != nil { if apierr.IsNotFound(err) && cmp.createIfNotExist { diff --git a/eventsources/sources/amqp/start.go b/eventsources/sources/amqp/start.go index a9e17ad446..abfc7f87cc 100644 --- a/eventsources/sources/amqp/start.go +++ b/eventsources/sources/amqp/start.go @@ -19,11 +19,11 @@ package amqp import ( "context" "encoding/json" + "fmt" "time" "sigs.k8s.io/yaml" - "github.com/pkg/errors" amqplib "github.com/rabbitmq/amqp091-go" "go.uber.org/zap" @@ -70,7 +70,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt amqpEventSource := &el.AMQPEventSource var conn *amqplib.Connection - if err := common.Connect(amqpEventSource.ConnectionBackoff, func() error { + if err := common.DoWithRetry(amqpEventSource.ConnectionBackoff, func() error { c := amqplib.Config{ Heartbeat: 10 * time.Second, Locale: "en_US", @@ -78,18 +78,18 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if amqpEventSource.TLS != nil { tlsConfig, err := common.GetTLSConfig(amqpEventSource.TLS) if err != nil { - return errors.Wrap(err, "failed to get the tls configuration") + return fmt.Errorf("failed to get the tls configuration, %w", err) } c.TLSClientConfig = tlsConfig } if amqpEventSource.Auth != nil { username, err := common.GetSecretFromVolume(amqpEventSource.Auth.Username) if err != nil { - return errors.Wrap(err, "username not found") + return fmt.Errorf("username not found, %w", err) } password, err := common.GetSecretFromVolume(amqpEventSource.Auth.Password) if err != nil { - return errors.Wrap(err, "password not found") + return fmt.Errorf("password not found, %w", err) } c.SASL = []amqplib.Authentication{&amqplib.PlainAuth{ Username: username, @@ -101,7 +101,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if amqpEventSource.URLSecret != nil { url, err = common.GetSecretFromVolume(amqpEventSource.URLSecret) if err != nil { - return errors.Wrap(err, "urlSecret not found") + return fmt.Errorf("urlSecret not found, %w", err) } } else { url = amqpEventSource.URL @@ -112,13 +112,13 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt } return nil }); err != nil { - return errors.Wrapf(err, "failed to connect to amqp broker for the event source %s", el.GetEventName()) + return fmt.Errorf("failed to connect to amqp broker for the event source %s, %w", el.GetEventName(), err) } log.Info("opening the server channel...") ch, err := conn.Channel() if err != nil { - return errors.Wrapf(err, "failed to open the channel for the event source %s", el.GetEventName()) + return fmt.Errorf("failed to open the channel for the event source %s, %w", el.GetEventName(), err) } log.Info("checking parameters and set defaults...") @@ -127,7 +127,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt log.Info("setting up the delivery channel...") delivery, err := getDelivery(ch, amqpEventSource) if err != nil { - return errors.Wrapf(err, "failed to get the delivery for the event source %s", el.GetEventName()) + return fmt.Errorf("failed to get the delivery for the event source %s, %w", el.GetEventName(), err) } if amqpEventSource.JSONBody { @@ -140,7 +140,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt case msg, ok := <-delivery: if !ok { log.Error("failed to read a message, channel might have been closed") - return errors.New("channel might have been closed") + return fmt.Errorf("channel might have been closed") } if err := el.handleOne(amqpEventSource, msg, dispatch, log); err != nil { log.Errorw("failed to process an AMQP message", zap.Error(err)) @@ -186,12 +186,12 @@ func (el *EventListener) handleOne(amqpEventSource *v1alpha1.AMQPEventSource, ms bodyBytes, err := json.Marshal(body) if err != nil { - return errors.Wrapf(err, "failed to marshal the message, message-id: %s", msg.MessageId) + return fmt.Errorf("failed to marshal the message, message-id: %s, %w", msg.MessageId, err) } log.Info("dispatching event ...") if err = dispatch(bodyBytes); err != nil { - return errors.Wrap(err, "failed to dispatch AMQP event") + return fmt.Errorf("failed to dispatch AMQP event, %w", err) } return nil } @@ -247,14 +247,11 @@ func getDelivery(ch *amqplib.Channel, eventSource *v1alpha1.AMQPEventSource) (<- nil, ) if err != nil { - return nil, errors.Errorf("failed to declare exchange with name %s and type %s. err: %+v", eventSource.ExchangeName, eventSource.ExchangeType, err) + return nil, fmt.Errorf("failed to declare exchange with name %s and type %s. err: %w", eventSource.ExchangeName, eventSource.ExchangeType, err) } optionalArguments, err := parseYamlTable(eventSource.QueueDeclare.Arguments) if err != nil { - return nil, errors.Errorf( - "failed to parse optional queue declare table arguments from Yaml string: %s", - err, - ) + return nil, fmt.Errorf("failed to parse optional queue declare table arguments from Yaml string: %w", err) } q, err := ch.QueueDeclare( @@ -266,7 +263,7 @@ func getDelivery(ch *amqplib.Channel, eventSource *v1alpha1.AMQPEventSource) (<- optionalArguments, ) if err != nil { - return nil, errors.Errorf("failed to declare queue: %s", err) + return nil, fmt.Errorf("failed to declare queue: %w", err) } err = ch.QueueBind( @@ -277,7 +274,7 @@ func getDelivery(ch *amqplib.Channel, eventSource *v1alpha1.AMQPEventSource) (<- nil, ) if err != nil { - return nil, errors.Errorf("failed to bind %s exchange '%s' to queue with routingKey: %s: %s", eventSource.ExchangeType, eventSource.ExchangeName, eventSource.RoutingKey, err) + return nil, fmt.Errorf("failed to bind %s exchange '%s' to queue with routingKey: %s: %w", eventSource.ExchangeType, eventSource.ExchangeName, eventSource.RoutingKey, err) } delivery, err := ch.Consume( @@ -290,7 +287,7 @@ func getDelivery(ch *amqplib.Channel, eventSource *v1alpha1.AMQPEventSource) (<- nil, ) if err != nil { - return nil, errors.Errorf("failed to begin consuming messages: %s", err) + return nil, fmt.Errorf("failed to begin consuming messages: %w", err) } return delivery, nil } @@ -303,7 +300,7 @@ func parseYamlTable(argString string) (amqplib.Table, error) { args := []byte(argString) err := yaml.Unmarshal(args, &table) if err != nil { - return nil, errors.Errorf("unmarshalling Yaml to Table type. Args: %s. Err: %+v", argString, err) + return nil, fmt.Errorf("unmarshalling Yaml to Table type. Args: %s. Err: %w", argString, err) } return table, nil } diff --git a/eventsources/sources/amqp/validate.go b/eventsources/sources/amqp/validate.go index 0d7052c64c..02a0963ca2 100644 --- a/eventsources/sources/amqp/validate.go +++ b/eventsources/sources/amqp/validate.go @@ -18,11 +18,11 @@ package amqp import ( "context" + "fmt" "github.com/argoproj/argo-events/common" apicommon "github.com/argoproj/argo-events/pkg/apis/common" "github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1" - "github.com/pkg/errors" ) // ValidateEventSource validates gateway event source @@ -35,16 +35,16 @@ func validate(eventSource *v1alpha1.AMQPEventSource) error { return common.ErrNilEventSource } if eventSource.URL == "" && eventSource.URLSecret == nil { - return errors.New("either url or urlSecret must be specified") + return fmt.Errorf("either url or urlSecret must be specified") } if eventSource.URL != "" && eventSource.URLSecret != nil { - return errors.New("only one of url or urlSecret can be specified") + return fmt.Errorf("only one of url or urlSecret can be specified") } if eventSource.RoutingKey == "" { - return errors.New("routing key must be specified") + return fmt.Errorf("routing key must be specified") } if eventSource.ExchangeType == "" { - return errors.New("exchange type must be specified") + return fmt.Errorf("exchange type must be specified") } if eventSource.TLS != nil { return apicommon.ValidateTLSConfig(eventSource.TLS) diff --git a/eventsources/sources/awssns/start.go b/eventsources/sources/awssns/start.go index 66c728de33..bc91749c48 100644 --- a/eventsources/sources/awssns/start.go +++ b/eventsources/sources/awssns/start.go @@ -23,6 +23,7 @@ import ( "encoding/base64" "encoding/json" "encoding/pem" + "fmt" "io" "net/http" "net/url" @@ -33,7 +34,6 @@ import ( "github.com/aws/aws-sdk-go/aws" snslib "github.com/aws/aws-sdk-go/service/sns" "github.com/ghodss/yaml" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -298,13 +298,13 @@ func (m *httpNotification) verifySigningCertUrl() error { regex := regexp.MustCompile(regexSigningCertHost) url, err := url.Parse(m.SigningCertURL) if err != nil { - return errors.Wrap(err, "SigningCertURL is not a valid URL") + return fmt.Errorf("SigningCertURL is not a valid URL, %w", err) } if !regex.MatchString(url.Hostname()) { - return errors.Errorf("SigningCertURL hostname `%s` does not match `%s`", url.Hostname(), regexSigningCertHost) + return fmt.Errorf("SigningCertURL hostname `%s` does not match `%s`", url.Hostname(), regexSigningCertHost) } if url.Scheme != "https" { - return errors.New("SigningCertURL is not using https") + return fmt.Errorf("SigningCertURL is not using https") } return nil } @@ -312,37 +312,37 @@ func (m *httpNotification) verifySigningCertUrl() error { func (m *httpNotification) verify() error { msgSig, err := base64.StdEncoding.DecodeString(m.Signature) if err != nil { - return errors.Wrap(err, "failed to base64 decode signature") + return fmt.Errorf("failed to base64 decode signature, %w", err) } if err := m.verifySigningCertUrl(); err != nil { - return errors.Wrap(err, "failed to verify SigningCertURL") + return fmt.Errorf("failed to verify SigningCertURL, %w", err) } res, err := http.Get(m.SigningCertURL) if err != nil { - return errors.Wrap(err, "failed to fetch signing cert") + return fmt.Errorf("failed to fetch signing cert, %w", err) } defer res.Body.Close() body, err := io.ReadAll(io.LimitReader(res.Body, 65*1024)) if err != nil { - return errors.Wrap(err, "failed to read signing cert body") + return fmt.Errorf("failed to read signing cert body, %w", err) } p, _ := pem.Decode(body) if p == nil { - return errors.New("nothing found in pem encoded bytes") + return fmt.Errorf("nothing found in pem encoded bytes") } cert, err := x509.ParseCertificate(p.Bytes) if err != nil { - return errors.Wrap(err, "failed to parse signing cert") + return fmt.Errorf("failed to parse signing cert, %w", err) } err = cert.CheckSignature(x509.SHA1WithRSA, m.sigSerialized(), msgSig) if err != nil { - return errors.Wrap(err, "message signature check error") + return fmt.Errorf("message signature check error, %w", err) } return nil diff --git a/eventsources/sources/awssqs/start.go b/eventsources/sources/awssqs/start.go index dc1f6d73d0..e83c239ec1 100644 --- a/eventsources/sources/awssqs/start.go +++ b/eventsources/sources/awssqs/start.go @@ -19,13 +19,13 @@ package awssqs import ( "context" "encoding/json" + "fmt" "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/session" sqslib "github.com/aws/aws-sdk-go/service/sqs" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common/logging" @@ -85,7 +85,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt queueURL, err := sqsClient.GetQueueUrl(getQueueURLInput) if err != nil { log.Errorw("Error getting SQS Queue URL", zap.Error(err)) - return errors.Wrapf(err, "failed to get the queue url for %s", el.GetEventName()) + return fmt.Errorf("failed to get the queue url for %s, %w", el.GetEventName(), err) } if sqsEventSource.JSONBody { @@ -202,7 +202,7 @@ func (el *EventListener) createAWSSession() (*session.Session, error) { sqsEventSource := &el.SQSEventSource awsSession, err := awscommon.CreateAWSSessionWithCredsInVolume(sqsEventSource.Region, sqsEventSource.RoleARN, sqsEventSource.AccessKey, sqsEventSource.SecretKey, sqsEventSource.SessionToken) if err != nil { - return nil, errors.Wrapf(err, "failed to create aws session for %s", el.GetEventName()) + return nil, fmt.Errorf("failed to create aws session for %s, %w", el.GetEventName(), err) } return awsSession, nil } diff --git a/eventsources/sources/azureeventshub/start.go b/eventsources/sources/azureeventshub/start.go index db80bc9a12..ac0e1cc407 100644 --- a/eventsources/sources/azureeventshub/start.go +++ b/eventsources/sources/azureeventshub/start.go @@ -23,7 +23,6 @@ import ( "time" eventhub "github.com/Azure/azure-event-hubs-go/v3" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -70,13 +69,13 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt log.Info("retrieving the shared access key name...") sharedAccessKeyName, err := common.GetSecretFromVolume(hubEventSource.SharedAccessKeyName) if err != nil { - return errors.Wrapf(err, "failed to retrieve the shared access key name from secret %s", hubEventSource.SharedAccessKeyName.Name) + return fmt.Errorf("failed to retrieve the shared access key name from secret %s, %w", hubEventSource.SharedAccessKeyName.Name, err) } log.Info("retrieving the shared access key...") sharedAccessKey, err := common.GetSecretFromVolume(hubEventSource.SharedAccessKey) if err != nil { - return errors.Wrapf(err, "failed to retrieve the shared access key from secret %s", hubEventSource.SharedAccessKey.Name) + return fmt.Errorf("failed to retrieve the shared access key from secret %s, %w", hubEventSource.SharedAccessKey.Name, err) } endpoint := fmt.Sprintf("Endpoint=sb://%s/;SharedAccessKeyName=%s;SharedAccessKey=%s;EntityPath=%s", hubEventSource.FQDN, sharedAccessKeyName, sharedAccessKey, hubEventSource.HubName) @@ -84,7 +83,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt log.Info("connecting to the hub...") hub, err := eventhub.NewHubFromConnectionString(endpoint) if err != nil { - return errors.Wrapf(err, "failed to connect to the hub %s", hubEventSource.HubName) + return fmt.Errorf("failed to connect to the hub %s, %w", hubEventSource.HubName, err) } handler := func(c context.Context, event *eventhub.Event) error { @@ -106,7 +105,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt eventBytes, err := json.Marshal(eventData) if err != nil { el.Metrics.EventProcessingFailed(el.GetEventSourceName(), el.GetEventName()) - return errors.Wrapf(err, "failed to marshal the event data for event source %s and message id %s", el.GetEventName(), event.ID) + return fmt.Errorf("failed to marshal the event data for event source %s and message id %s, %w", el.GetEventName(), event.ID, err) } log.Info("dispatching the event to eventbus...") @@ -122,21 +121,21 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt log.Info("gathering the hub runtime information...") runtimeInfo, err := hub.GetRuntimeInformation(ctx) if err != nil { - return errors.Wrapf(err, "failed to get the hub runtime information for %s", el.GetEventName()) + return fmt.Errorf("failed to get the hub runtime information for %s, %w", el.GetEventName(), err) } if runtimeInfo == nil { - return errors.Wrapf(err, "runtime information is not available for %s", el.GetEventName()) + return fmt.Errorf("runtime information is not available for %s, %w", el.GetEventName(), err) } if runtimeInfo.PartitionIDs == nil { - return errors.Wrapf(err, "no partition ids are available for %s", el.GetEventName()) + return fmt.Errorf("no partition ids are available for %s, %w", el.GetEventName(), err) } log.Info("handling the partitions...") for _, partitionID := range runtimeInfo.PartitionIDs { if _, err := hub.Receive(ctx, partitionID, handler, eventhub.ReceiveWithLatestOffset()); err != nil { - return errors.Wrapf(err, "failed to receive events from partition %s", partitionID) + return fmt.Errorf("failed to receive events from partition %s, %w", partitionID, err) } } diff --git a/eventsources/sources/azureeventshub/validate.go b/eventsources/sources/azureeventshub/validate.go index 5f4f79db33..cbfbf2ca98 100644 --- a/eventsources/sources/azureeventshub/validate.go +++ b/eventsources/sources/azureeventshub/validate.go @@ -18,10 +18,10 @@ package azureeventshub import ( "context" + "fmt" "github.com/argoproj/argo-events/common" "github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1" - "github.com/pkg/errors" ) // ValidateEventSource validates azure events hub event source @@ -34,16 +34,16 @@ func validate(eventSource *v1alpha1.AzureEventsHubEventSource) error { return common.ErrNilEventSource } if eventSource.FQDN == "" { - return errors.New("FQDN is not specified") + return fmt.Errorf("FQDN is not specified") } if eventSource.HubName == "" { - return errors.New("hub name/path is not specified") + return fmt.Errorf("hub name/path is not specified") } if eventSource.SharedAccessKey == nil { - return errors.New("SharedAccessKey is not specified") + return fmt.Errorf("SharedAccessKey is not specified") } if eventSource.SharedAccessKeyName == nil { - return errors.New("SharedAccessKeyName is not specified") + return fmt.Errorf("SharedAccessKeyName is not specified") } return nil } diff --git a/eventsources/sources/bitbucket/basic_auth_strategy.go b/eventsources/sources/bitbucket/basic_auth_strategy.go index 8c84203f6b..91a54c4c00 100644 --- a/eventsources/sources/bitbucket/basic_auth_strategy.go +++ b/eventsources/sources/bitbucket/basic_auth_strategy.go @@ -16,8 +16,9 @@ limitations under the License. package bitbucket import ( + "fmt" + bitbucketv2 "github.com/ktrysmt/go-bitbucket" - "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" "github.com/argoproj/argo-events/common" @@ -31,12 +32,12 @@ type BasicAuthStrategy struct { func NewBasicAuthStrategy(usernameSecret, passwordSecret *corev1.SecretKeySelector) (*BasicAuthStrategy, error) { username, err := common.GetSecretFromVolume(usernameSecret) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve bitbucket username from secret") + return nil, fmt.Errorf("failed to retrieve bitbucket username from secret, %w", err) } password, err := common.GetSecretFromVolume(passwordSecret) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve bitbucket password from secret") + return nil, fmt.Errorf("failed to retrieve bitbucket password from secret, %w", err) } return &BasicAuthStrategy{ diff --git a/eventsources/sources/bitbucket/oauth_token_auth_strategy.go b/eventsources/sources/bitbucket/oauth_token_auth_strategy.go index 26a5d571fb..cf5f0881c9 100644 --- a/eventsources/sources/bitbucket/oauth_token_auth_strategy.go +++ b/eventsources/sources/bitbucket/oauth_token_auth_strategy.go @@ -16,8 +16,9 @@ limitations under the License. package bitbucket import ( + "fmt" + bitbucketv2 "github.com/ktrysmt/go-bitbucket" - "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" "github.com/argoproj/argo-events/common" @@ -30,7 +31,7 @@ type OAuthTokenAuthStrategy struct { func NewOAuthTokenAuthStrategy(oauthTokenSecret *corev1.SecretKeySelector) (*OAuthTokenAuthStrategy, error) { token, err := common.GetSecretFromVolume(oauthTokenSecret) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve bitbucket oauth token from secret") + return nil, fmt.Errorf("failed to retrieve bitbucket oauth token from secret, %w", err) } return &OAuthTokenAuthStrategy{ diff --git a/eventsources/sources/bitbucket/start.go b/eventsources/sources/bitbucket/start.go index 5021b4bb78..3a5a2babb7 100644 --- a/eventsources/sources/bitbucket/start.go +++ b/eventsources/sources/bitbucket/start.go @@ -19,6 +19,7 @@ import ( "context" "crypto/rand" "encoding/json" + "errors" "fmt" "io" "math/big" @@ -27,7 +28,6 @@ import ( bitbucketv2 "github.com/ktrysmt/go-bitbucket" "github.com/mitchellh/mapstructure" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -121,13 +121,13 @@ func (router *Router) PostInactivate() error { for _, repo := range bitbucketEventSource.GetBitbucketRepositories() { hookID, ok := router.hookIDs[repo.GetRepositoryID()] if !ok { - return errors.Errorf("can not find hook ID for repo key: %s", repo.GetRepositoryID()) + return fmt.Errorf("can not find hook ID for repo key: %s", repo.GetRepositoryID()) } if err := router.deleteWebhook(repo, hookID); err != nil { logger.Errorw("failed to delete webhook", zap.String("owner", repo.Owner), zap.String("repository-slug", repo.RepositorySlug), zap.Error(err)) - return errors.Wrapf(err, "failed to delete hook for repo %s/%s.", repo.Owner, repo.RepositorySlug) + return fmt.Errorf("failed to delete hook for repo %s/%s, %w", repo.Owner, repo.RepositorySlug, err) } logger.Info("successfully deleted hook for repo", @@ -164,7 +164,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt logger.Info("choosing bitbucket auth strategy...") authStrategy, err := router.chooseAuthStrategy() if err != nil { - return errors.Wrap(err, "failed to get bitbucket auth strategy") + return fmt.Errorf("failed to get bitbucket auth strategy, %w", err) } router.client = authStrategy.BitbucketClient() @@ -199,7 +199,7 @@ func (router *Router) chooseAuthStrategy() (AuthStrategy, error) { case es.HasBitbucketOAuthToken(): return NewOAuthTokenAuthStrategy(es.Auth.OAuthToken) default: - return nil, errors.New("none of the supported auth options were provided") + return nil, fmt.Errorf("none of the supported auth options were provided") } } @@ -221,7 +221,7 @@ func (router *Router) applyBitbucketWebhook(repo v1alpha1.BitbucketRepository) e hooks, err := router.listWebhooks(repo) if err != nil { logger.Errorw("failed to list webhooks", zap.Error(err)) - return errors.Wrap(err, "failed to list webhooks") + return fmt.Errorf("failed to list webhooks, %w", err) } logger.Info("checking if webhook already exists...") @@ -231,7 +231,7 @@ func (router *Router) applyBitbucketWebhook(repo v1alpha1.BitbucketRepository) e if err := router.deleteWebhook(repo, existingHookSubscription.Uuid); err != nil { logger.Errorw("failed to delete old webhook", zap.String("owner", repo.Owner), zap.String("repository-slug", repo.RepositorySlug), zap.Error(err)) - return errors.Wrapf(err, "failed to delete old webhook for repo %s/%s.", repo.Owner, repo.RepositorySlug) + return fmt.Errorf("failed to delete old webhook for repo %s/%s, %w", repo.Owner, repo.RepositorySlug, err) } } @@ -239,7 +239,7 @@ func (router *Router) applyBitbucketWebhook(repo v1alpha1.BitbucketRepository) e newWebhook, err := router.createWebhook(repo, formattedWebhookURL) if err != nil { logger.Errorw("failed to create new webhook", zap.Error(err)) - return errors.Wrap(err, "failed to create new webhook") + return fmt.Errorf("failed to create new webhook, %w", err) } router.hookIDs[repo.GetRepositoryID()] = newWebhook.Uuid diff --git a/eventsources/sources/bitbucketserver/start.go b/eventsources/sources/bitbucketserver/start.go index 0d1db0f6cc..d15d50bfc7 100644 --- a/eventsources/sources/bitbucketserver/start.go +++ b/eventsources/sources/bitbucketserver/start.go @@ -22,6 +22,7 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" + "fmt" "io" "math/big" "net/http" @@ -37,7 +38,6 @@ import ( "github.com/argoproj/argo-events/eventsources/sources" "github.com/argoproj/argo-events/pkg/apis/events" "github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1" - "github.com/pkg/errors" "go.uber.org/zap" ) @@ -130,7 +130,7 @@ func (router *Router) PostInactivate() error { bitbucketToken, err := common.GetSecretFromVolume(bitbucketserverEventSource.AccessToken) if err != nil { - return errors.Errorf("failed to get bitbucketserver token. err: %+v", err) + return fmt.Errorf("failed to get bitbucketserver token. err: %w", err) } bitbucketConfig := bitbucketv1.NewConfiguration(bitbucketserverEventSource.BitbucketServerBaseURL) @@ -140,7 +140,7 @@ func (router *Router) PostInactivate() error { for _, repo := range bitbucketserverEventSource.GetBitbucketServerRepositories() { id, ok := router.hookIDs[repo.ProjectKey+","+repo.RepositorySlug] if !ok { - return errors.Errorf("can not find hook ID for project-key: %s, repository-slug: %s", repo.ProjectKey, repo.RepositorySlug) + return fmt.Errorf("can not find hook ID for project-key: %s, repository-slug: %s", repo.ProjectKey, repo.RepositorySlug) } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) @@ -151,7 +151,7 @@ func (router *Router) PostInactivate() error { _, err = bitbucketClient.DefaultApi.DeleteWebhook(repo.ProjectKey, repo.RepositorySlug, int32(id)) if err != nil { - return errors.Errorf("failed to delete bitbucketserver webhook. err: %+v", err) + return fmt.Errorf("failed to delete bitbucketserver webhook. err: %w", err) } logger.Infow("bitbucket server webhook deleted", @@ -193,14 +193,14 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt logger.Info("retrieving the access token credentials...") bitbucketToken, err := common.GetSecretFromVolume(bitbucketserverEventSource.AccessToken) if err != nil { - return errors.Errorf("failed to get bitbucketserver token. err: %+v", err) + return fmt.Errorf("failed to get bitbucketserver token. err: %w", err) } if bitbucketserverEventSource.WebhookSecret != nil { logger.Info("retrieving the webhook secret...") webhookSecret, err := common.GetSecretFromVolume(bitbucketserverEventSource.WebhookSecret) if err != nil { - return errors.Errorf("failed to get bitbucketserver webhook secret. err: %+v", err) + return fmt.Errorf("failed to get bitbucketserver webhook secret. err: %w", err) } router.hookSecret = webhookSecret @@ -274,7 +274,7 @@ func (router *Router) applyBitbucketServerWebhook(ctx context.Context, bitbucket hooks, err := router.listWebhooks(bitbucketClient, repo) if err != nil { - return errors.Wrapf(err, "failed to list existing hooks to check for duplicates for repository %s/%s", repo.ProjectKey, repo.RepositorySlug) + return fmt.Errorf("failed to list existing hooks to check for duplicates for repository %s/%s, %w", repo.ProjectKey, repo.RepositorySlug, err) } var existingHook bitbucketv1.Webhook @@ -299,7 +299,7 @@ func (router *Router) applyBitbucketServerWebhook(ctx context.Context, bitbucket requestBody, err := router.createRequestBodyFromWebhook(newHook) if err != nil { - return errors.Wrapf(err, "failed to create request body from webhook") + return fmt.Errorf("failed to create request body from webhook, %w", err) } // Update the webhook when it does exist and the events/configuration have changed @@ -309,7 +309,7 @@ func (router *Router) applyBitbucketServerWebhook(ctx context.Context, bitbucket logger.Info("webhook requires an update") err = router.updateWebhook(bitbucketClient, existingHook.ID, requestBody, repo) if err != nil { - return errors.Errorf("failed to update webhook. err: %+v", err) + return fmt.Errorf("failed to update webhook. err: %w", err) } logger.With("hook-id", existingHook.ID).Info("hook successfully updated") @@ -321,7 +321,7 @@ func (router *Router) applyBitbucketServerWebhook(ctx context.Context, bitbucket // Create the webhook when it doesn't exist yet createdHook, err := router.createWebhook(bitbucketClient, requestBody, repo) if err != nil { - return errors.Errorf("failed to create webhook. err: %+v", err) + return fmt.Errorf("failed to create webhook. err: %w", err) } router.hookIDs[repo.ProjectKey+","+repo.RepositorySlug] = createdHook.ID @@ -334,12 +334,12 @@ func (router *Router) applyBitbucketServerWebhook(ctx context.Context, bitbucket func (router *Router) listWebhooks(bitbucketClient *bitbucketv1.APIClient, repo v1alpha1.BitbucketServerRepository) ([]bitbucketv1.Webhook, error) { apiResponse, err := bitbucketClient.DefaultApi.FindWebhooks(repo.ProjectKey, repo.RepositorySlug, nil) if err != nil { - return nil, errors.Wrapf(err, "failed to list existing hooks to check for duplicates for repository %s/%s", repo.ProjectKey, repo.RepositorySlug) + return nil, fmt.Errorf("failed to list existing hooks to check for duplicates for repository %s/%s, %w", repo.ProjectKey, repo.RepositorySlug, err) } hooks, err := bitbucketv1.GetWebhooksResponse(apiResponse) if err != nil { - return nil, errors.Wrapf(err, "failed to convert the list of webhooks for repository %s/%s", repo.ProjectKey, repo.RepositorySlug) + return nil, fmt.Errorf("failed to convert the list of webhooks for repository %s/%s, %w", repo.ProjectKey, repo.RepositorySlug, err) } return hooks, nil @@ -348,13 +348,13 @@ func (router *Router) listWebhooks(bitbucketClient *bitbucketv1.APIClient, repo func (router *Router) createWebhook(bitbucketClient *bitbucketv1.APIClient, requestBody []byte, repo v1alpha1.BitbucketServerRepository) (*bitbucketv1.Webhook, error) { apiResponse, err := bitbucketClient.DefaultApi.CreateWebhook(repo.ProjectKey, repo.RepositorySlug, requestBody, []string{"application/json"}) if err != nil { - return nil, errors.Errorf("failed to add webhook. err: %+v", err) + return nil, fmt.Errorf("failed to add webhook. err: %w", err) } var createdHook *bitbucketv1.Webhook err = mapstructure.Decode(apiResponse.Values, &createdHook) if err != nil { - return nil, errors.Errorf("failed to convert API response to Webhook struct. err: %+v", err) + return nil, fmt.Errorf("failed to convert API response to Webhook struct. err: %w", err) } return createdHook, nil @@ -381,7 +381,7 @@ func (router *Router) createRequestBodyFromWebhook(hook bitbucketv1.Webhook) ([] hookMap := make(map[string]interface{}) err = common.StructToMap(hook, hookMap) if err != nil { - return nil, errors.Wrapf(err, "failed to convert webhook to map") + return nil, fmt.Errorf("failed to convert webhook to map, %w", err) } delete(hookMap, "configuration") @@ -391,7 +391,7 @@ func (router *Router) createRequestBodyFromWebhook(hook bitbucketv1.Webhook) ([] requestBody, err := json.Marshal(finalHook) if err != nil { - return nil, errors.Wrapf(err, "failed to marshal new webhook to JSON") + return nil, fmt.Errorf("failed to marshal new webhook to JSON, %w", err) } return requestBody, nil @@ -400,13 +400,13 @@ func (router *Router) createRequestBodyFromWebhook(hook bitbucketv1.Webhook) ([] func (router *Router) parseAndValidateBitbucketServerRequest(request *http.Request) ([]byte, error) { body, err := io.ReadAll(request.Body) if err != nil { - return nil, errors.Wrap(err, "failed to parse request body") + return nil, fmt.Errorf("failed to parse request body, %w", err) } if len(router.hookSecret) != 0 { signature := request.Header.Get("X-Hub-Signature") if len(signature) == 0 { - return nil, errors.New("missing signature header") + return nil, fmt.Errorf("missing signature header") } mac := hmac.New(sha256.New, []byte(router.hookSecret)) @@ -414,7 +414,7 @@ func (router *Router) parseAndValidateBitbucketServerRequest(request *http.Reque expectedMAC := hex.EncodeToString(mac.Sum(nil)) if !hmac.Equal([]byte(signature[7:]), []byte(expectedMAC)) { - return nil, errors.New("hmac verification failed") + return nil, fmt.Errorf("hmac verification failed") } } diff --git a/eventsources/sources/calendar/start.go b/eventsources/sources/calendar/start.go index 5a025b117e..2b4a0db1bb 100644 --- a/eventsources/sources/calendar/start.go +++ b/eventsources/sources/calendar/start.go @@ -24,7 +24,6 @@ import ( "strings" "time" - "github.com/pkg/errors" cronlib "github.com/robfig/cron/v3" "go.uber.org/zap" "k8s.io/client-go/kubernetes" @@ -75,11 +74,11 @@ func (el *EventListener) initializePersistence(ctx context.Context, persistence restConfig, err := common.GetClientConfig(kubeConfig) if err != nil { - return errors.Wrapf(err, "failed to get a K8s rest config for the event source %s", el.GetEventName()) + return fmt.Errorf("failed to get a K8s rest config for the event source %s, %w", el.GetEventName(), err) } kubeClientset, err := kubernetes.NewForConfig(restConfig) if err != nil { - return errors.Wrapf(err, "failed to set up a K8s client for the event source %s", el.GetEventName()) + return fmt.Errorf("failed to set up a K8s client for the event source %s, %w", el.GetEventName(), err) } el.eventPersistence, err = persist.NewConfigMapPersist(ctx, kubeClientset, persistence.ConfigMap, el.Namespace) @@ -101,20 +100,20 @@ func (el *EventListener) getExecutionTime() (time.Time, error) { lastEvent, err := el.eventPersistence.Get(el.getPersistenceKey()) if err != nil { el.log.Errorw("failed to get last persisted event.", zap.Error(err)) - return lastT, errors.Wrap(err, "failed to get last persisted event.") + return lastT, fmt.Errorf("failed to get last persisted event, , %w", err) } if lastEvent != nil && lastEvent.EventPayload != "" { var eventData events.CalendarEventData err := json.Unmarshal([]byte(lastEvent.EventPayload), &eventData) if err != nil { el.log.Errorw("failed to marshal last persisted event.", zap.Error(err)) - return lastT, errors.Wrap(err, "failed to marshal last persisted event.") + return lastT, fmt.Errorf("failed to marshal last persisted event, , %w", err) } eventTime := strings.Split(eventData.EventTime, " m=") lastT, err = time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", eventTime[0]) if err != nil { el.log.Errorw("failed to parse the persisted last event timestamp", zap.Error(err)) - return lastT, errors.Wrap(err, "failed to parse the persisted last event timestamp.") + return lastT, fmt.Errorf("failed to parse the persisted last event timestamp, %w", err) } } @@ -187,7 +186,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt el.log.Infow("loading location for the schedule...", zap.Any("location", calendarEventSource.Timezone)) location, err = time.LoadLocation(calendarEventSource.Timezone) if err != nil { - return errors.Wrapf(err, "failed to load location for event source %s / %s", el.GetEventSourceName(), el.GetEventName()) + return fmt.Errorf("failed to load location for event source %s / %s, , %w", el.GetEventSourceName(), el.GetEventName(), err) } lastT = lastT.In(location) } @@ -204,13 +203,13 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if err != nil { el.log.Errorw("failed to marshal the event data", zap.Error(err)) // no need to continue as further event payloads will suffer same fate as this one. - return errors.Wrapf(err, "failed to marshal the event data for event source %s / %s", el.GetEventSourceName(), el.GetEventName()) + return fmt.Errorf("failed to marshal the event data for event source %s / %s, %w", el.GetEventSourceName(), el.GetEventName(), err) } el.log.Info("dispatching calendar event...") err = dispatch(payload) if err != nil { el.log.Errorw("failed to dispatch calendar event", zap.Error(err)) - return errors.Wrapf(err, "failed to dispatch calendar event") + return fmt.Errorf("failed to dispatch calendar event, %w", err) } if el.eventPersistence != nil && el.eventPersistence.IsEnabled() { event := persist.Event{EventKey: el.getPersistenceKey(), EventPayload: string(payload)} @@ -278,17 +277,17 @@ func resolveSchedule(cal *v1alpha1.CalendarEventSource) (cronlib.Schedule, error specParser := cronlib.NewParser(cronlib.Minute | cronlib.Hour | cronlib.Dom | cronlib.Month | cronlib.Dow) schedule, err := specParser.Parse(cal.Schedule) if err != nil { - return nil, errors.Errorf("failed to parse schedule %s from calendar event. Cause: %+v", cal.Schedule, err.Error()) + return nil, fmt.Errorf("failed to parse schedule %s from calendar event. Cause: %w", cal.Schedule, err) } return schedule, nil } if cal.Interval != "" { intervalDuration, err := time.ParseDuration(cal.Interval) if err != nil { - return nil, errors.Errorf("failed to parse interval %s from calendar event. Cause: %+v", cal.Interval, err.Error()) + return nil, fmt.Errorf("failed to parse interval %s from calendar event. Cause: %w", cal.Interval, err) } schedule := cronlib.ConstantDelaySchedule{Delay: intervalDuration} return schedule, nil } - return nil, errors.New("calendar event must contain either a schedule or interval") + return nil, fmt.Errorf("calendar event must contain either a schedule or interval") } diff --git a/eventsources/sources/emitter/start.go b/eventsources/sources/emitter/start.go index 46a1fc1fcf..bc6a434693 100644 --- a/eventsources/sources/emitter/start.go +++ b/eventsources/sources/emitter/start.go @@ -19,10 +19,10 @@ package emitter import ( "context" "encoding/json" + "fmt" "time" emitter "github.com/emitter-io/go/v2" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -71,7 +71,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if emitterEventSource.TLS != nil { tlsConfig, err := common.GetTLSConfig(emitterEventSource.TLS) if err != nil { - return errors.Wrap(err, "failed to get the tls configuration") + return fmt.Errorf("failed to get the tls configuration, %w", err) } options = append(options, emitter.WithTLSConfig(tlsConfig)) } @@ -80,7 +80,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if emitterEventSource.Username != nil { username, err := common.GetSecretFromVolume(emitterEventSource.Username) if err != nil { - return errors.Wrapf(err, "failed to retrieve the username from %s", emitterEventSource.Username.Name) + return fmt.Errorf("failed to retrieve the username from %s, %w", emitterEventSource.Username.Name, err) } options = append(options, emitter.WithUsername(username)) } @@ -88,7 +88,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if emitterEventSource.Password != nil { password, err := common.GetSecretFromVolume(emitterEventSource.Password) if err != nil { - return errors.Wrapf(err, "failed to retrieve the password from %s", emitterEventSource.Password.Name) + return fmt.Errorf("failed to retrieve the password from %s, %w", emitterEventSource.Password.Name, err) } options = append(options, emitter.WithPassword(password)) } @@ -100,13 +100,13 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt log.Infow("creating a client", zap.Any("channelName", emitterEventSource.ChannelName)) client := emitter.NewClient(options...) - if err := common.Connect(emitterEventSource.ConnectionBackoff, func() error { + if err := common.DoWithRetry(emitterEventSource.ConnectionBackoff, func() error { if err := client.Connect(); err != nil { return err } return nil }); err != nil { - return errors.Wrapf(err, "failed to connect to %s", emitterEventSource.Broker) + return fmt.Errorf("failed to connect to %s, %w", emitterEventSource.Broker, err) } if err := client.Subscribe(emitterEventSource.ChannelKey, emitterEventSource.ChannelName, func(_ *emitter.Client, message emitter.Message) { @@ -136,7 +136,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt el.Metrics.EventProcessingFailed(el.GetEventSourceName(), el.GetEventName()) } }); err != nil { - return errors.Wrapf(err, "failed to subscribe to channel %s", emitterEventSource.ChannelName) + return fmt.Errorf("failed to subscribe to channel %s, %w", emitterEventSource.ChannelName, err) } <-ctx.Done() diff --git a/eventsources/sources/emitter/validate.go b/eventsources/sources/emitter/validate.go index ec0aa5f7ff..bbf9152149 100644 --- a/eventsources/sources/emitter/validate.go +++ b/eventsources/sources/emitter/validate.go @@ -18,8 +18,7 @@ package emitter import ( "context" - - "github.com/pkg/errors" + "fmt" "github.com/argoproj/argo-events/common" apicommon "github.com/argoproj/argo-events/pkg/apis/common" @@ -36,13 +35,13 @@ func validate(eventSource *v1alpha1.EmitterEventSource) error { return common.ErrNilEventSource } if eventSource.Broker == "" { - return errors.New("broker url must be specified") + return fmt.Errorf("broker url must be specified") } if eventSource.ChannelName == "" { - return errors.New("channel name must be specified") + return fmt.Errorf("channel name must be specified") } if eventSource.ChannelKey == "" { - return errors.New("channel key secret selector must be specified") + return fmt.Errorf("channel key secret selector must be specified") } if eventSource.TLS != nil { return apicommon.ValidateTLSConfig(eventSource.TLS) diff --git a/eventsources/sources/file/start.go b/eventsources/sources/file/start.go index 7e9c88416a..caa4fa02fe 100644 --- a/eventsources/sources/file/start.go +++ b/eventsources/sources/file/start.go @@ -19,12 +19,12 @@ package file import ( "context" "encoding/json" + "fmt" "regexp" "strings" "time" "github.com/fsnotify/fsnotify" - "github.com/pkg/errors" watcherpkg "github.com/radovskyb/watcher" "go.uber.org/zap" @@ -89,7 +89,7 @@ func (el *EventListener) listenEvents(ctx context.Context, dispatch func([]byte, log.Info("setting up a new file watcher...") watcher, err := fsnotify.NewWatcher() if err != nil { - return errors.Wrapf(err, "failed to set up a file watcher for %s", el.GetEventName()) + return fmt.Errorf("failed to set up a file watcher for %s, %w", el.GetEventName(), err) } defer watcher.Close() @@ -97,7 +97,7 @@ func (el *EventListener) listenEvents(ctx context.Context, dispatch func([]byte, log.Info("adding directory to monitor for the watcher...") err = watcher.Add(fileEventSource.WatchPathConfig.Directory) if err != nil { - return errors.Wrapf(err, "failed to add directory %s to the watcher for %s", fileEventSource.WatchPathConfig.Directory, el.GetEventName()) + return fmt.Errorf("failed to add directory %s to the watcher for %s, %w", fileEventSource.WatchPathConfig.Directory, el.GetEventName(), err) } var pathRegexp *regexp.Regexp @@ -105,7 +105,7 @@ func (el *EventListener) listenEvents(ctx context.Context, dispatch func([]byte, log.Infow("matching file path with configured regex...", zap.Any("regex", fileEventSource.WatchPathConfig.PathRegexp)) pathRegexp, err = regexp.Compile(fileEventSource.WatchPathConfig.PathRegexp) if err != nil { - return errors.Wrapf(err, "failed to match file path with configured regex %s for %s", fileEventSource.WatchPathConfig.PathRegexp, el.GetEventName()) + return fmt.Errorf("failed to match file path with configured regex %s for %s, %w", fileEventSource.WatchPathConfig.PathRegexp, el.GetEventName(), err) } } @@ -120,11 +120,11 @@ func (el *EventListener) listenEvents(ctx context.Context, dispatch func([]byte, fileEvent := fsevent.Event{Name: event.Name, Op: fsevent.NewOp(event.Op.String()), Metadata: el.FileEventSource.Metadata} payload, err := json.Marshal(fileEvent) if err != nil { - return errors.Wrap(err, "failed to marshal the event to the fs event") + return fmt.Errorf("failed to marshal the event to the fs event, %w", err) } log.Infow("dispatching file event on data channel...", zap.Any("event-type", event.Op.String()), zap.Any("descriptor-name", event.Name)) if err = dispatch(payload); err != nil { - return errors.Wrap(err, "failed to dispatch a file event") + return fmt.Errorf("failed to dispatch a file event, %w", err) } return nil } @@ -136,7 +136,7 @@ func (el *EventListener) listenEvents(ctx context.Context, dispatch func([]byte, if !ok { log.Info("fs watcher has stopped") // watcher stopped watching file events - return errors.Errorf("fs watcher stopped for %s", el.GetEventName()) + return fmt.Errorf("fs watcher stopped for %s", el.GetEventName()) } // fwc.Path == event.Name is required because we don't want to send event when .swp files are created matched := false @@ -153,7 +153,7 @@ func (el *EventListener) listenEvents(ctx context.Context, dispatch func([]byte, } } case err := <-watcher.Errors: - return errors.Wrapf(err, "failed to process %s", el.GetEventName()) + return fmt.Errorf("failed to process %s, %w", el.GetEventName(), err) case <-ctx.Done(): log.Info("event source has been stopped") return nil @@ -174,7 +174,7 @@ func (el *EventListener) listenEventsPolling(ctx context.Context, dispatch func( log.Info("adding directory to monitor for the watcher...") err := watcher.Add(fileEventSource.WatchPathConfig.Directory) if err != nil { - return errors.Wrapf(err, "failed to add directory %s to the watcher for %s", fileEventSource.WatchPathConfig.Directory, el.GetEventName()) + return fmt.Errorf("failed to add directory %s to the watcher for %s, %w", fileEventSource.WatchPathConfig.Directory, el.GetEventName(), err) } var pathRegexp *regexp.Regexp @@ -182,7 +182,7 @@ func (el *EventListener) listenEventsPolling(ctx context.Context, dispatch func( log.Infow("matching file path with configured regex...", zap.Any("regex", fileEventSource.WatchPathConfig.PathRegexp)) pathRegexp, err = regexp.Compile(fileEventSource.WatchPathConfig.PathRegexp) if err != nil { - return errors.Wrapf(err, "failed to match file path with configured regex %s for %s", fileEventSource.WatchPathConfig.PathRegexp, el.GetEventName()) + return fmt.Errorf("failed to match file path with configured regex %s for %s, %w", fileEventSource.WatchPathConfig.PathRegexp, el.GetEventName(), err) } } @@ -197,11 +197,11 @@ func (el *EventListener) listenEventsPolling(ctx context.Context, dispatch func( fileEvent := fsevent.Event{Name: event.Name(), Op: fsevent.NewOp(event.Op.String()), Metadata: el.FileEventSource.Metadata} payload, err := json.Marshal(fileEvent) if err != nil { - return errors.Wrap(err, "failed to marshal the event to the fs event") + return fmt.Errorf("failed to marshal the event to the fs event, %w", err) } log.Infow("dispatching file event on data channel...", zap.Any("event-type", event.Op.String()), zap.Any("descriptor-name", event.Name)) if err = dispatch(payload); err != nil { - return errors.Wrap(err, "failed to dispatch file event") + return fmt.Errorf("failed to dispatch file event, %w", err) } return nil } @@ -242,7 +242,7 @@ func (el *EventListener) listenEventsPolling(ctx context.Context, dispatch func( }() log.Info("Starting watcher...") if err = watcher.Start(time.Millisecond * 100); err != nil { - return errors.Wrapf(err, "Failed to start watcher for %s", el.GetEventName()) + return fmt.Errorf("Failed to start watcher for %s, %w", el.GetEventName(), err) } return nil } diff --git a/eventsources/sources/gcppubsub/start.go b/eventsources/sources/gcppubsub/start.go index 9703dd15f4..ba452f6f47 100644 --- a/eventsources/sources/gcppubsub/start.go +++ b/eventsources/sources/gcppubsub/start.go @@ -26,7 +26,6 @@ import ( "cloud.google.com/go/compute/metadata" "cloud.google.com/go/pubsub" - "github.com/pkg/errors" "go.uber.org/zap" "google.golang.org/api/option" "google.golang.org/grpc/codes" @@ -82,7 +81,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt err := el.fillDefault(logger) if err != nil { - return errors.Wrapf(err, "failed to fill default values for %s", el.GetEventName()) + return fmt.Errorf("failed to fill default values for %s, %w", el.GetEventName(), err) } pubsubEventSource := &el.PubSubEventSource @@ -100,7 +99,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt log.Info("setting up a client to connect to PubSub...") client, subscription, err := el.prepareSubscription(ctx, log) if err != nil { - return errors.Wrapf(err, "failed to prepare client or subscription for %s", el.GetEventName()) + return fmt.Errorf("failed to prepare client or subscription for %s, %w", el.GetEventName(), err) } log.Info("listening for messages from PubSub...") @@ -138,7 +137,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt m.Ack() }) if err != nil { - return errors.Wrapf(err, "failed to receive the messages for subscription %s for %s", subscription, el.GetEventName()) + return fmt.Errorf("failed to receive the messages for subscription %s for %s, %w", subscription, el.GetEventName(), err) } <-ctx.Done() @@ -171,7 +170,7 @@ func (el *EventListener) fillDefault(logger *zap.SugaredLogger) error { logger.Debug("determine project ID from GCP metadata server") proj, err := metadata.ProjectID() if err != nil { - return errors.Wrap(err, "project ID is not given and couldn't determine from GCP metadata server") + return fmt.Errorf("project ID is not given and couldn't determine from GCP metadata server, %w", err) } el.PubSubEventSource.ProjectID = proj } @@ -184,7 +183,7 @@ func (el *EventListener) fillDefault(logger *zap.SugaredLogger) error { logger.Debug("auto generate subscription ID") hashcode, err := el.hash() if err != nil { - return errors.Wrap(err, "failed get hashcode") + return fmt.Errorf("failed get hashcode, %w", err) } el.PubSubEventSource.SubscriptionID = fmt.Sprintf("%s-%s", el.GetEventName(), hashcode) } @@ -208,7 +207,7 @@ func (el *EventListener) prepareSubscription(ctx context.Context, logger *zap.Su logger.Debug("using credentials from secret") jsonCred, err := common.GetSecretFromVolume(secret) if err != nil { - return nil, nil, errors.Wrap(err, "could not find credentials") + return nil, nil, fmt.Errorf("could not find credentials, %w", err) } opts = append(opts, option.WithCredentialsJSON([]byte(jsonCred))) } else { @@ -216,7 +215,7 @@ func (el *EventListener) prepareSubscription(ctx context.Context, logger *zap.Su } client, err := pubsub.NewClient(ctx, pubsubEventSource.ProjectID, opts...) if err != nil { - return nil, nil, errors.Wrapf(err, "failed to set up client for %s", el.GetEventName()) + return nil, nil, fmt.Errorf("failed to set up client for %s, %w", el.GetEventName(), err) } logger.Debug("set up pubsub client") @@ -238,7 +237,7 @@ func (el *EventListener) prepareSubscription(ctx context.Context, logger *zap.Su subscExists, err = subscription.Exists(ctx) if err != nil { client.Close() - return nil, nil, errors.Errorf("failed to check if subscription %s exists", subscription) + return nil, nil, fmt.Errorf("failed to check if subscription %s exists", subscription) } } else { // trick: you don't need to have get permission to check only whether it exists @@ -248,13 +247,13 @@ func (el *EventListener) prepareSubscription(ctx context.Context, logger *zap.Su switch status.Code(err) { case codes.OK: client.Close() - return nil, nil, errors.Errorf("you lack permission to pull from %s", subscription) + return nil, nil, fmt.Errorf("you lack permission to pull from %s", subscription) case codes.NotFound: // OK, maybe the subscription doesn't exist yet, so create it later // (it possibly means project itself doesn't exist, but it's ok because we'll see an error later in such case) default: client.Close() - return nil, nil, errors.Wrapf(err, "failed to test permission for subscription %s", subscription) + return nil, nil, fmt.Errorf("failed to test permission for subscription %s, %w", subscription, err) } } logger.Debug("checked if subscription exists and you have right permission") @@ -267,7 +266,7 @@ func (el *EventListener) prepareSubscription(ctx context.Context, logger *zap.Su if pubsubEventSource.Topic == "" { if !subscExists { client.Close() - return nil, nil, errors.Errorf("you need to specify topicID to create missing subscription %s", subscription) + return nil, nil, fmt.Errorf("you need to specify topicID to create missing subscription %s", subscription) } logger.Debug("subscription exists and no topic given, fine") return client, subscription, nil @@ -282,18 +281,18 @@ func (el *EventListener) prepareSubscription(ctx context.Context, logger *zap.Su subscConfig, err := subscription.Config(ctx) if err != nil { client.Close() - return nil, nil, errors.Wrapf(err, "failed to get subscription's config for verifying topic") + return nil, nil, fmt.Errorf("failed to get subscription's config for verifying topic, %w", err) } switch actualTopic := subscConfig.Topic.String(); actualTopic { case "_deleted-topic_": client.Close() - return nil, nil, errors.New("the topic for the subscription has been deleted") + return nil, nil, fmt.Errorf("the topic for the subscription has been deleted") case topic.String(): logger.Debug("subscription exists and its topic matches given one, fine") return client, subscription, nil default: client.Close() - return nil, nil, errors.Errorf("this subscription belongs to wrong topic %s", actualTopic) + return nil, nil, fmt.Errorf("this subscription belongs to wrong topic %s", actualTopic) } } @@ -312,7 +311,7 @@ func (el *EventListener) prepareSubscription(ctx context.Context, logger *zap.Su // (it possibly means project itself doesn't exist, but it's ok because we'll see an error later in such case) default: client.Close() - return nil, nil, errors.Wrapf(err, "failed to create %s for %s", subscription, topic) + return nil, nil, fmt.Errorf("failed to create %s for %s, %w", subscription, topic, err) } // subsc. exists | topic given | topic exists | action | required permissions @@ -323,20 +322,20 @@ func (el *EventListener) prepareSubscription(ctx context.Context, logger *zap.Su topicClient, err := pubsub.NewClient(ctx, pubsubEventSource.TopicProjectID, opts...) if err != nil { client.Close() - return nil, nil, errors.Wrapf(err, "failed to create client to create %s", topic) + return nil, nil, fmt.Errorf("failed to create client to create %s, %w", topic, err) } defer topicClient.Close() _, err = topicClient.CreateTopic(ctx, topic.ID()) if err != nil { client.Close() - return nil, nil, errors.Wrapf(err, "failed to create %s", topic) + return nil, nil, fmt.Errorf("failed to create %s, %w", topic, err) } logger.Debug("topic created") _, err = client.CreateSubscription(ctx, subscription.ID(), pubsub.SubscriptionConfig{Topic: topic}) if err != nil { client.Close() - return nil, nil, errors.Wrapf(err, "failed to create %s for %s", subscription, topic) + return nil, nil, fmt.Errorf("failed to create %s for %s, %w", subscription, topic, err) } logger.Debug("subscription created") return client, subscription, nil diff --git a/eventsources/sources/generic/start.go b/eventsources/sources/generic/start.go index 9555f11918..beee0b2f6e 100644 --- a/eventsources/sources/generic/start.go +++ b/eventsources/sources/generic/start.go @@ -6,7 +6,6 @@ import ( fmt "fmt" "time" - "github.com/pkg/errors" "go.uber.org/zap" "google.golang.org/grpc" "google.golang.org/grpc/connectivity" @@ -111,11 +110,11 @@ func (el *EventListener) handleOne(event *Event, dispatch func([]byte, ...events } eventBytes, err := json.Marshal(eventData) if err != nil { - return errors.Wrap(err, "failed to marshal the event data") + return fmt.Errorf("failed to marshal the event data, %w", err) } logger.Info("dispatching event...") if err := dispatch(eventBytes); err != nil { - return errors.Wrap(err, "failed to dispatch a Generic event") + return fmt.Errorf("failed to dispatch a Generic event, %w", err) } return nil } diff --git a/eventsources/sources/github/start.go b/eventsources/sources/github/start.go index 79a1f1817b..452a31f6f1 100644 --- a/eventsources/sources/github/start.go +++ b/eventsources/sources/github/start.go @@ -26,7 +26,6 @@ import ( "time" gh "github.com/google/go-github/v31/github" - "github.com/pkg/errors" "go.uber.org/zap" corev1 "k8s.io/api/core/v1" @@ -57,7 +56,7 @@ func init() { func (router *Router) getCredentials(keySelector *corev1.SecretKeySelector) (*cred, error) { token, err := common.GetSecretFromVolume(keySelector) if err != nil { - return nil, errors.Wrap(err, "secret not found") + return nil, fmt.Errorf("secret not found, %w", err) } return &cred{ @@ -70,7 +69,7 @@ func (router *Router) getCredentials(keySelector *corev1.SecretKeySelector) (*cr func (router *Router) getAPITokenAuthStrategy() (*TokenAuthStrategy, error) { apiTokenCreds, err := router.getCredentials(router.githubEventSource.APIToken) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve api token credentials") + return nil, fmt.Errorf("failed to retrieve api token credentials, %w", err) } return &TokenAuthStrategy{ @@ -84,7 +83,7 @@ func (router *Router) getGithubAppAuthStrategy() (*AppsAuthStrategy, error) { appCreds := router.githubEventSource.GithubApp githubAppPrivateKey, err := router.getCredentials(appCreds.PrivateKey) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve github app credentials") + return nil, fmt.Errorf("failed to retrieve github app credentials, %w", err) } return &AppsAuthStrategy{ @@ -103,7 +102,7 @@ func (router *Router) chooseAuthStrategy() (AuthStrategy, error) { case es.HasGithubAppCreds(): return router.getGithubAppAuthStrategy() default: - return nil, errors.New("none of the supported auth options were provided") + return nil, fmt.Errorf("none of the supported auth options were provided") } } @@ -185,12 +184,12 @@ func (router *Router) PostInactivate() error { for _, org := range githubEventSource.Organizations { id, ok := router.orgHookIDs[org] if !ok { - return errors.Errorf("can not find hook ID for organization %s", org) + return fmt.Errorf("can not find hook ID for organization %s", org) } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if _, err := router.githubClient.Organizations.DeleteHook(ctx, org, id); err != nil { - return errors.Errorf("failed to delete hook for organization %s. err: %+v", org, err) + return fmt.Errorf("failed to delete hook for organization %s. err: %w", org, err) } logger.Infof("GitHub hook deleted for organization %s", org) } @@ -201,12 +200,12 @@ func (router *Router) PostInactivate() error { for _, n := range r.Names { id, ok := router.repoHookIDs[r.Owner+","+n] if !ok { - return errors.Errorf("can not find hook ID for repo %s/%s", r.Owner, n) + return fmt.Errorf("can not find hook ID for repo %s/%s", r.Owner, n) } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if _, err := router.githubClient.Repositories.DeleteHook(ctx, r.Owner, n, id); err != nil { - return errors.Errorf("failed to delete hook for repo %s/%s. err: %+v", r.Owner, n, err) + return fmt.Errorf("failed to delete hook for repo %s/%s. err: %w", r.Owner, n, err) } logger.Infof("GitHub hook deleted for repo %s/%s", r.Owner, n) } @@ -231,7 +230,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if githubEventSource.WebhookSecret != nil { webhookSecretCreds, err := router.getCredentials(githubEventSource.WebhookSecret) if err != nil { - return errors.Errorf("failed to retrieve webhook secret. err: %+v", err) + return fmt.Errorf("failed to retrieve webhook secret. err: %w", err) } router.hookSecret = webhookSecretCreds.secret } @@ -249,13 +248,13 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt logger.Info("choosing github auth strategy...") authStrategy, err := router.chooseAuthStrategy() if err != nil { - return errors.Wrap(err, "failed to get github auth strategy") + return fmt.Errorf("failed to get github auth strategy, %w", err) } logger.Info("setting up auth transport for http client with the chosen strategy...") authTransport, err := authStrategy.AuthTransport() if err != nil { - return errors.Wrap(err, "failed to set up auth transport for http client") + return fmt.Errorf("failed to set up auth transport for http client, %w", err) } logger.Info("configuring GitHub hook...") diff --git a/eventsources/sources/gitlab/start.go b/eventsources/sources/gitlab/start.go index 0a529af25e..97ad5a7252 100644 --- a/eventsources/sources/gitlab/start.go +++ b/eventsources/sources/gitlab/start.go @@ -20,6 +20,7 @@ import ( "context" "crypto/rand" "encoding/json" + "fmt" "io" "math/big" "net/http" @@ -32,7 +33,6 @@ import ( "github.com/argoproj/argo-events/eventsources/common/webhook" "github.com/argoproj/argo-events/eventsources/sources" "github.com/argoproj/argo-events/pkg/apis/events" - "github.com/pkg/errors" "github.com/xanzy/go-gitlab" "go.uber.org/zap" ) @@ -134,10 +134,10 @@ func (router *Router) PostInactivate() error { for _, p := range gitlabEventSource.GetProjects() { id, ok := router.hookIDs[p] if !ok { - return errors.Errorf("can not find hook ID for project %s", p) + return fmt.Errorf("can not find hook ID for project %s", p) } if _, err := router.gitlabClient.Projects.DeleteProjectHook(p, id); err != nil { - return errors.Errorf("failed to delete hook for project %s. err: %+v", p, err) + return fmt.Errorf("failed to delete hook for project %s. err: %w", p, err) } logger.Infof("Gitlab hook deleted for project %s", p) } @@ -189,7 +189,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt for _, event := range gitlabEventSource.Events { elem := reflect.ValueOf(opt).Elem().FieldByName(event) if ok := elem.IsValid(); !ok { - return errors.Errorf("unknown event %s", event) + return fmt.Errorf("unknown event %s", event) } iev := reflect.New(elem.Type().Elem()) reflect.Indirect(iev).SetBool(true) @@ -199,7 +199,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if gitlabEventSource.SecretToken != nil { token, err := common.GetSecretFromVolume(gitlabEventSource.SecretToken) if err != nil { - return errors.Errorf("failed to retrieve secret token. err: %+v", err) + return fmt.Errorf("failed to retrieve secret token. err: %w", err) } opt.Token = &token router.secretToken = token @@ -207,13 +207,13 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt accessToken, err := common.GetSecretFromVolume(gitlabEventSource.AccessToken) if err != nil { - return errors.Errorf("failed to get gitlab credentials. err: %+v", err) + return fmt.Errorf("failed to get gitlab credentials. err: %w", err) } logger.Info("setting up the client to connect to GitLab...") router.gitlabClient, err = gitlab.NewClient(accessToken, gitlab.WithBaseURL(gitlabEventSource.GitlabBaseURL)) if err != nil { - return errors.Wrapf(err, "failed to initialize client") + return fmt.Errorf("failed to initialize client, %w", err) } getHook := func(hooks []*gitlab.ProjectHook, url string) *gitlab.ProjectHook { diff --git a/eventsources/sources/hdfs/client.go b/eventsources/sources/hdfs/client.go index 9de76d58ab..c1bce92fb0 100644 --- a/eventsources/sources/hdfs/client.go +++ b/eventsources/sources/hdfs/client.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/colinmarc/hdfs" - "github.com/pkg/errors" krb "gopkg.in/jcmturner/gokrb5.v5/client" "gopkg.in/jcmturner/gokrb5.v5/config" "gopkg.in/jcmturner/gokrb5.v5/credentials" @@ -45,7 +44,7 @@ type KeytabOptions struct { func getConfigMapKey(selector *corev1.ConfigMapKeySelector) (string, error) { result, err := common.GetConfigMapFromVolume(selector) if err != nil { - return "", errors.Wrap(err, "configmap value not injected") + return "", fmt.Errorf("configmap value not injected, %w", err) } return result, nil } @@ -53,7 +52,7 @@ func getConfigMapKey(selector *corev1.ConfigMapKeySelector) (string, error) { func getSecretKey(selector *corev1.SecretKeySelector) ([]byte, error) { result, err := common.GetSecretFromVolume(selector) if err != nil { - return nil, errors.Wrap(err, "secret value not injected") + return nil, fmt.Errorf("secret value not injected, %w", err) } return []byte(result), nil } diff --git a/eventsources/sources/hdfs/start.go b/eventsources/sources/hdfs/start.go index 3bc7583ec3..0774f7c766 100644 --- a/eventsources/sources/hdfs/start.go +++ b/eventsources/sources/hdfs/start.go @@ -3,6 +3,7 @@ package hdfs import ( "context" "encoding/json" + "fmt" "os" "path/filepath" "regexp" @@ -10,7 +11,6 @@ import ( "time" "github.com/colinmarc/hdfs" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common/logging" @@ -76,20 +76,20 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt log.Info("setting up HDFS configuration...") hdfsConfig, err := createHDFSConfig(hdfsEventSource) if err != nil { - return errors.Wrapf(err, "failed to create HDFS configuration for %s", el.GetEventName()) + return fmt.Errorf("failed to create HDFS configuration for %s, %w", el.GetEventName(), err) } log.Info("setting up HDFS client...") hdfscli, err := createHDFSClient(hdfsConfig.Addresses, hdfsConfig.HDFSUser, hdfsConfig.KrbOptions) if err != nil { - return errors.Wrapf(err, "failed to create the HDFS client for %s", el.GetEventName()) + return fmt.Errorf("failed to create the HDFS client for %s, %w", el.GetEventName(), err) } defer hdfscli.Close() log.Info("setting up a new watcher...") watcher, err := naivewatcher.NewWatcher(&WatchableHDFS{hdfscli: hdfscli}) if err != nil { - return errors.Wrapf(err, "failed to create the HDFS watcher for %s", el.GetEventName()) + return fmt.Errorf("failed to create the HDFS watcher for %s, %w", el.GetEventName(), err) } defer watcher.Close() @@ -97,7 +97,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if hdfsEventSource.CheckInterval != "" { d, err := time.ParseDuration(hdfsEventSource.CheckInterval) if err != nil { - return errors.Wrapf(err, "failed to parse the check in interval for %s", el.GetEventName()) + return fmt.Errorf("failed to parse the check in interval for %s, %w", el.GetEventName(), err) } intervalDuration = d } @@ -105,14 +105,14 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt log.Info("started HDFS watcher") err = watcher.Start(intervalDuration) if err != nil { - return errors.Wrapf(err, "failed to start the watcher for %s", el.GetEventName()) + return fmt.Errorf("failed to start the watcher for %s, %w", el.GetEventName(), err) } // directory to watch must be available in HDFS. You can't watch a directory that is not present. log.Info("adding configured directory to watcher...") err = watcher.Add(hdfsEventSource.Directory) if err != nil { - return errors.Wrapf(err, "failed to add directory %s for %s", hdfsEventSource.Directory, el.GetEventName()) + return fmt.Errorf("failed to add directory %s for %s, %w", hdfsEventSource.Directory, el.GetEventName(), err) } op := fsevent.NewOp(hdfsEventSource.Type) @@ -120,7 +120,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if hdfsEventSource.PathRegexp != "" { pathRegexp, err = regexp.Compile(hdfsEventSource.PathRegexp) if err != nil { - return errors.Wrapf(err, "failed to compile the path regex %s for %s", hdfsEventSource.PathRegexp, el.GetEventName()) + return fmt.Errorf("failed to compile the path regex %s for %s, %w", hdfsEventSource.PathRegexp, el.GetEventName(), err) } } @@ -131,7 +131,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if !ok { log.Info("HDFS watcher has stopped") // watcher stopped watching file events - return errors.Errorf("watcher has been stopped for %s", el.GetEventName()) + return fmt.Errorf("watcher has been stopped for %s", el.GetEventName()) } event.Metadata = hdfsEventSource.Metadata matched := false @@ -150,7 +150,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt } } case err := <-watcher.Errors: - return errors.Wrapf(err, "failed to watch events for %s", el.GetEventName()) + return fmt.Errorf("failed to watch events for %s, %w", el.GetEventName(), err) case <-ctx.Done(): return nil } @@ -170,12 +170,12 @@ func (el *EventListener) handleOne(event fsevent.Event, dispatch func([]byte, .. payload, err := json.Marshal(event) if err != nil { - return errors.Wrap(err, "failed to marshal the event data, rejecting event...") + return fmt.Errorf("failed to marshal the event data, rejecting event, %w", err) } logger.Info("dispatching event on data channel...") if err = dispatch(payload); err != nil { - return errors.Wrap(err, "failed to dispatch an HDFS event") + return fmt.Errorf("failed to dispatch an HDFS event, %w", err) } return nil } diff --git a/eventsources/sources/hdfs/validate.go b/eventsources/sources/hdfs/validate.go index 99f4ddb4f1..659f8f7f79 100644 --- a/eventsources/sources/hdfs/validate.go +++ b/eventsources/sources/hdfs/validate.go @@ -18,7 +18,7 @@ package hdfs import ( "context" - "errors" + "fmt" "time" "github.com/argoproj/argo-events/common" @@ -36,16 +36,16 @@ func validate(eventSource *v1alpha1.HDFSEventSource) error { return common.ErrNilEventSource } if eventSource.Type == "" { - return errors.New("type is required") + return fmt.Errorf("type is required") } op := fsevent.NewOp(eventSource.Type) if op == 0 { - return errors.New("type is invalid") + return fmt.Errorf("type is invalid") } if eventSource.CheckInterval != "" { _, err := time.ParseDuration(eventSource.CheckInterval) if err != nil { - return errors.New("failed to parse interval") + return fmt.Errorf("failed to parse interval") } } err := eventSource.WatchPathConfig.Validate() @@ -53,20 +53,20 @@ func validate(eventSource *v1alpha1.HDFSEventSource) error { return err } if len(eventSource.Addresses) == 0 { - return errors.New("addresses is required") + return fmt.Errorf("addresses is required") } hasKrbCCache := eventSource.KrbCCacheSecret != nil hasKrbKeytab := eventSource.KrbKeytabSecret != nil if eventSource.HDFSUser == "" && !hasKrbCCache && !hasKrbKeytab { - return errors.New("either hdfsUser, krbCCacheSecret or krbKeytabSecret is required") + return fmt.Errorf("either hdfsUser, krbCCacheSecret or krbKeytabSecret is required") } if hasKrbKeytab && (eventSource.KrbServicePrincipalName == "" || eventSource.KrbConfigConfigMap == nil || eventSource.KrbUsername == "" || eventSource.KrbRealm == "") { - return errors.New("krbServicePrincipalName, krbConfigConfigMap, krbUsername and krbRealm are required with krbKeytabSecret") + return fmt.Errorf("krbServicePrincipalName, krbConfigConfigMap, krbUsername and krbRealm are required with krbKeytabSecret") } if hasKrbCCache && (eventSource.KrbServicePrincipalName == "" || eventSource.KrbConfigConfigMap == nil) { - return errors.New("krbServicePrincipalName and krbConfigConfigMap are required with krbCCacheSecret") + return fmt.Errorf("krbServicePrincipalName and krbConfigConfigMap are required with krbCCacheSecret") } return err } diff --git a/eventsources/sources/kafka/start.go b/eventsources/sources/kafka/start.go index e5c84c93d0..bb7e240955 100644 --- a/eventsources/sources/kafka/start.go +++ b/eventsources/sources/kafka/start.go @@ -19,6 +19,7 @@ package kafka import ( "context" "encoding/json" + "errors" "fmt" "strconv" "strings" @@ -26,11 +27,11 @@ import ( "time" "github.com/Shopify/sarama" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" "github.com/argoproj/argo-events/common/logging" + eventbuscommon "github.com/argoproj/argo-events/eventbus/common" eventsourcecommon "github.com/argoproj/argo-events/eventsources/common" "github.com/argoproj/argo-events/eventsources/sources" metrics "github.com/argoproj/argo-events/metrics" @@ -165,7 +166,7 @@ func (el *EventListener) partitionConsumer(ctx context.Context, log *zap.Sugared var consumer sarama.Consumer log.Info("connecting to Kafka cluster...") - if err := common.Connect(kafkaEventSource.ConnectionBackoff, func() error { + if err := common.DoWithRetry(kafkaEventSource.ConnectionBackoff, func() error { var err error config, err := getSaramaConfig(kafkaEventSource, log) @@ -180,7 +181,7 @@ func (el *EventListener) partitionConsumer(ctx context.Context, log *zap.Sugared } return nil }); err != nil { - return errors.Wrapf(err, "failed to connect to Kafka broker for event source %s", el.GetEventName()) + return fmt.Errorf("failed to connect to Kafka broker for event source %s, %w", el.GetEventName(), err) } log = log.With("partition-id", kafkaEventSource.Partition) @@ -188,25 +189,25 @@ func (el *EventListener) partitionConsumer(ctx context.Context, log *zap.Sugared log.Info("parsing the partition value...") pInt, err := strconv.ParseInt(kafkaEventSource.Partition, 10, 32) if err != nil { - return errors.Wrapf(err, "failed to parse Kafka partition %s for event source %s", kafkaEventSource.Partition, el.GetEventName()) + return fmt.Errorf("failed to parse Kafka partition %s for event source %s, %w", kafkaEventSource.Partition, el.GetEventName(), err) } partition := int32(pInt) log.Info("getting available partitions...") availablePartitions, err := consumer.Partitions(kafkaEventSource.Topic) if err != nil { - return errors.Wrapf(err, "failed to get the available partitions for topic %s and event source %s", kafkaEventSource.Topic, el.GetEventName()) + return fmt.Errorf("failed to get the available partitions for topic %s and event source %s, %w", kafkaEventSource.Topic, el.GetEventName(), err) } log.Info("verifying the partition exists within available partitions...") if ok := verifyPartitionAvailable(partition, availablePartitions); !ok { - return errors.Wrapf(err, "partition %d is not available. event source %s", partition, el.GetEventName()) + return fmt.Errorf("partition %d is not available. event source %s, %w", partition, el.GetEventName(), err) } log.Info("getting partition consumer...") partitionConsumer, err := consumer.ConsumePartition(kafkaEventSource.Topic, partition, sarama.OffsetNewest) if err != nil { - return errors.Wrapf(err, "failed to create consumer partition for event source %s", el.GetEventName()) + return fmt.Errorf("failed to create consumer partition for event source %s, %w", el.GetEventName(), err) } processOne := func(msg *sarama.ConsumerMessage) error { @@ -228,13 +229,13 @@ func (el *EventListener) partitionConsumer(ctx context.Context, log *zap.Sugared } eventBody, err := json.Marshal(eventData) if err != nil { - return errors.Wrap(err, "failed to marshal the event data, rejecting the event...") + return fmt.Errorf("failed to marshal the event data, rejecting the event, %w", err) } kafkaID := genUniqueID(el.GetEventSourceName(), el.GetEventName(), kafkaEventSource.URL, msg.Topic, msg.Partition, msg.Offset) if err = dispatch(eventBody, eventsourcecommon.WithID(kafkaID)); err != nil { - return errors.Wrap(err, "failed to dispatch a Kafka event...") + return fmt.Errorf("failed to dispatch a Kafka event, %w", err) } return nil } @@ -248,7 +249,7 @@ func (el *EventListener) partitionConsumer(ctx context.Context, log *zap.Sugared el.Metrics.EventProcessingFailed(el.GetEventSourceName(), el.GetEventName()) } case err := <-partitionConsumer.Errors(): - return errors.Wrapf(err, "failed to consume messages for event source %s", el.GetEventName()) + return fmt.Errorf("failed to consume messages for event source %s, %w", el.GetEventName(), err) case <-ctx.Done(): log.Info("event source is stopped, closing partition consumer") @@ -306,7 +307,7 @@ func getSaramaConfig(kafkaEventSource *v1alpha1.KafkaEventSource, log *zap.Sugar if kafkaEventSource.TLS != nil { tlsConfig, err := common.GetTLSConfig(kafkaEventSource.TLS) if err != nil { - return nil, errors.Wrap(err, "failed to get the tls configuration") + return nil, fmt.Errorf("failed to get the tls configuration, %w", err) } config.Net.TLS.Config = tlsConfig config.Net.TLS.Enable = true @@ -343,6 +344,10 @@ func (consumer *Consumer) Cleanup(sarama.ConsumerGroupSession) error { return nil } +var ( + eventBusErr *eventbuscommon.EventBusError +) + // ConsumeClaim must start a consumer loop of ConsumerGroupClaim's Messages(). func (consumer *Consumer) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error { // NOTE: @@ -351,14 +356,19 @@ func (consumer *Consumer) ConsumeClaim(session sarama.ConsumerGroupSession, clai // https://github.com/Shopify/sarama/blob/master/consumer_group.go#L27-L29 for message := range claim.Messages() { if err := consumer.processOne(session, message); err != nil { - consumer.logger.Errorw("failed to process a Kafka message", zap.Error(err)) consumer.metrics.EventProcessingFailed(consumer.eventSourceName, consumer.eventName) - continue + if errors.As(err, &eventBusErr) { // EventBus error, do not continue. + consumer.logger.Errorw("failed to process a Kafka message due to event bus issue", zap.Error(err)) + break + } else { + consumer.logger.Errorw("failed to process a Kafka message, skip it", zap.Error(err)) + continue + } } if consumer.kafkaEventSource.LimitEventsPerSecond > 0 { // 1000000000 is 1 second in nanoseconds d := (1000000000 / time.Duration(consumer.kafkaEventSource.LimitEventsPerSecond) * time.Nanosecond) * time.Nanosecond - consumer.logger.Infof("Sleeping for: %v.", d) + consumer.logger.Debugf("Sleeping for: %v.", d) time.Sleep(d) } } @@ -385,13 +395,13 @@ func (consumer *Consumer) processOne(session sarama.ConsumerGroupSession, messag } eventBody, err := json.Marshal(eventData) if err != nil { - return errors.Wrap(err, "failed to marshal the event data, rejecting the event...") + return fmt.Errorf("failed to marshal the event data, rejecting the event, %w", err) } messageID := genUniqueID(consumer.eventSourceName, consumer.eventName, consumer.kafkaEventSource.URL, message.Topic, message.Partition, message.Offset) if err = consumer.dispatch(eventBody, eventsourcecommon.WithID(messageID)); err != nil { - return errors.Wrap(err, "failed to dispatch a kafka event...") + return fmt.Errorf("failed to dispatch a kafka event, %w", err) } session.MarkMessage(message, "") return nil diff --git a/eventsources/sources/minio/start.go b/eventsources/sources/minio/start.go index 7d254d553c..78b93d6b63 100644 --- a/eventsources/sources/minio/start.go +++ b/eventsources/sources/minio/start.go @@ -19,12 +19,12 @@ package minio import ( "context" "encoding/json" + "fmt" "time" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/notification" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -73,18 +73,18 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt log.Info("retrieving access and secret key...") accessKey, err := common.GetSecretFromVolume(minioEventSource.AccessKey) if err != nil { - return errors.Wrapf(err, "failed to get the access key for event source %s", el.GetEventName()) + return fmt.Errorf("failed to get the access key for event source %s, %w", el.GetEventName(), err) } secretKey, err := common.GetSecretFromVolume(minioEventSource.SecretKey) if err != nil { - return errors.Wrapf(err, "failed to retrieve the secret key for event source %s", el.GetEventName()) + return fmt.Errorf("failed to retrieve the secret key for event source %s, %w", el.GetEventName(), err) } log.Info("setting up a minio client...") minioClient, err := minio.New(minioEventSource.Endpoint, &minio.Options{ Creds: credentials.NewStaticV4(accessKey, secretKey, ""), Secure: !minioEventSource.Insecure}) if err != nil { - return errors.Wrapf(err, "failed to create a client for event source %s", el.GetEventName()) + return fmt.Errorf("failed to create a client for event source %s, %w", el.GetEventName(), err) } prefix, suffix := getFilters(minioEventSource) @@ -114,12 +114,12 @@ func (el *EventListener) handleOne(notification notification.Info, dispatch func eventData := &events.MinioEventData{Notification: notification.Records, Metadata: el.MinioEventSource.Metadata} eventBytes, err := json.Marshal(eventData) if err != nil { - return errors.Wrap(err, "failed to marshal the event data, rejecting the event...") + return fmt.Errorf("failed to marshal the event data, rejecting the event, %w", err) } log.Info("dispatching the event on data channel...") if err = dispatch(eventBytes); err != nil { - return errors.Wrap(err, "failed to dispatch minio event") + return fmt.Errorf("failed to dispatch minio event, %w", err) } return nil } diff --git a/eventsources/sources/mqtt/start.go b/eventsources/sources/mqtt/start.go index 6a2c03acab..bdbe32a420 100644 --- a/eventsources/sources/mqtt/start.go +++ b/eventsources/sources/mqtt/start.go @@ -19,10 +19,10 @@ package mqtt import ( "context" "encoding/json" + "fmt" "time" mqttlib "github.com/eclipse/paho.mqtt.golang" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -108,7 +108,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if mqttEventSource.TLS != nil { tlsConfig, err := common.GetTLSConfig(mqttEventSource.TLS) if err != nil { - return errors.Wrap(err, "failed to get the tls configuration") + return fmt.Errorf("failed to get the tls configuration, %w", err) } opts.TLSConfig = tlsConfig } @@ -116,19 +116,19 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt var client mqttlib.Client log.Info("connecting to mqtt broker...") - if err := common.Connect(mqttEventSource.ConnectionBackoff, func() error { + if err := common.DoWithRetry(mqttEventSource.ConnectionBackoff, func() error { client = mqttlib.NewClient(opts) if token := client.Connect(); token.Wait() && token.Error() != nil { return token.Error() } return nil }); err != nil { - return errors.Wrapf(err, "failed to connect to the mqtt broker for event source %s", el.GetEventName()) + return fmt.Errorf("failed to connect to the mqtt broker for event source %s, %w", el.GetEventName(), err) } log.Info("subscribing to the topic...") if token := client.Subscribe(mqttEventSource.Topic, 0, handler); token.Wait() && token.Error() != nil { - return errors.Wrapf(token.Error(), "failed to subscribe to the topic %s for event source %s", mqttEventSource.Topic, el.GetEventName()) + return fmt.Errorf("failed to subscribe to the topic %s for event source %s, %w", mqttEventSource.Topic, el.GetEventName(), token.Error()) } <-ctx.Done() diff --git a/eventsources/sources/nats/start.go b/eventsources/sources/nats/start.go index d83f67ace2..8f73407eeb 100644 --- a/eventsources/sources/nats/start.go +++ b/eventsources/sources/nats/start.go @@ -19,10 +19,10 @@ package nats import ( "context" "encoding/json" + "fmt" "time" natslib "github.com/nats-io/nats.go" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -70,7 +70,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if natsEventSource.TLS != nil { tlsConfig, err := common.GetTLSConfig(natsEventSource.TLS) if err != nil { - return errors.Wrap(err, "failed to get the tls configuration") + return fmt.Errorf("failed to get the tls configuration, %w", err) } opt = append(opt, natslib.Secure(tlsConfig)) } @@ -100,7 +100,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt } o, err := natslib.NkeyOptionFromSeed(nkeyFile) if err != nil { - return errors.Wrap(err, "failed to get NKey") + return fmt.Errorf("failed to get NKey, %w", err) } opt = append(opt, o) case natsEventSource.Auth.Credential != nil: @@ -114,14 +114,14 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt var conn *natslib.Conn log.Info("connecting to nats cluster...") - if err := common.Connect(natsEventSource.ConnectionBackoff, func() error { + if err := common.DoWithRetry(natsEventSource.ConnectionBackoff, func() error { var err error if conn, err = natslib.Connect(natsEventSource.URL, opt...); err != nil { return err } return nil }); err != nil { - return errors.Wrapf(err, "failed to connect to the nats server for event source %s", el.GetEventName()) + return fmt.Errorf("failed to connect to the nats server for event source %s, %w", el.GetEventName(), err) } defer conn.Close() @@ -158,12 +158,12 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt } }) if err != nil { - return errors.Wrapf(err, "failed to subscribe to the subject %s for event source %s", natsEventSource.Subject, el.GetEventName()) + return fmt.Errorf("failed to subscribe to the subject %s for event source %s, %w", natsEventSource.Subject, el.GetEventName(), err) } conn.Flush() if err := conn.LastError(); err != nil { - return errors.Wrapf(err, "connection failure for event source %s", el.GetEventName()) + return fmt.Errorf("connection failure for event source %s, %w", el.GetEventName(), err) } <-ctx.Done() diff --git a/eventsources/sources/nats/validate.go b/eventsources/sources/nats/validate.go index cbccf2a868..38a86e54b9 100644 --- a/eventsources/sources/nats/validate.go +++ b/eventsources/sources/nats/validate.go @@ -18,8 +18,7 @@ package nats import ( "context" - - "github.com/pkg/errors" + "fmt" "github.com/argoproj/argo-events/common" apicommon "github.com/argoproj/argo-events/pkg/apis/common" @@ -36,10 +35,10 @@ func validate(eventSource *v1alpha1.NATSEventsSource) error { return common.ErrNilEventSource } if eventSource.URL == "" { - return errors.New("url must be specified") + return fmt.Errorf("url must be specified") } if eventSource.Subject == "" { - return errors.New("subject must be specified") + return fmt.Errorf("subject must be specified") } if eventSource.TLS != nil { return apicommon.ValidateTLSConfig(eventSource.TLS) diff --git a/eventsources/sources/nsq/start.go b/eventsources/sources/nsq/start.go index 9007e22ecb..7eabc85d38 100644 --- a/eventsources/sources/nsq/start.go +++ b/eventsources/sources/nsq/start.go @@ -19,11 +19,11 @@ package nsq import ( "context" "encoding/json" + "fmt" "strconv" "time" "github.com/nsqio/go-nsq" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -86,20 +86,20 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if nsqEventSource.TLS != nil { tlsConfig, err := common.GetTLSConfig(nsqEventSource.TLS) if err != nil { - return errors.Wrap(err, "failed to get the tls configuration") + return fmt.Errorf("failed to get the tls configuration, %w", err) } config.TlsConfig = tlsConfig config.TlsV1 = true } - if err := common.Connect(nsqEventSource.ConnectionBackoff, func() error { + if err := common.DoWithRetry(nsqEventSource.ConnectionBackoff, func() error { var err error if consumer, err = nsq.NewConsumer(nsqEventSource.Topic, nsqEventSource.Channel, config); err != nil { return err } return nil }); err != nil { - return errors.Wrapf(err, "failed to create a new consumer for topic %s and channel %s for event source %s", nsqEventSource.Topic, nsqEventSource.Channel, el.GetEventName()) + return fmt.Errorf("failed to create a new consumer for topic %s and channel %s for event source %s, %w", nsqEventSource.Topic, nsqEventSource.Channel, el.GetEventName(), err) } if nsqEventSource.JSONBody { @@ -109,7 +109,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt consumer.AddHandler(&messageHandler{eventSourceName: el.EventSourceName, eventName: el.EventName, dispatch: dispatch, logger: log, isJSON: nsqEventSource.JSONBody, metadata: nsqEventSource.Metadata, metrics: el.Metrics}) if err := consumer.ConnectToNSQLookupd(nsqEventSource.HostAddress); err != nil { - return errors.Wrapf(err, "lookup failed for host %s for event source %s", nsqEventSource.HostAddress, el.GetEventName()) + return fmt.Errorf("lookup failed for host %s for event source %s, %w", nsqEventSource.HostAddress, el.GetEventName(), err) } <-ctx.Done() diff --git a/eventsources/sources/nsq/validate.go b/eventsources/sources/nsq/validate.go index a9585d7c38..df46caf06d 100644 --- a/eventsources/sources/nsq/validate.go +++ b/eventsources/sources/nsq/validate.go @@ -18,8 +18,7 @@ package nsq import ( "context" - - "github.com/pkg/errors" + "fmt" "github.com/argoproj/argo-events/common" apicommon "github.com/argoproj/argo-events/pkg/apis/common" @@ -36,13 +35,13 @@ func validate(eventSource *v1alpha1.NSQEventSource) error { return common.ErrNilEventSource } if eventSource.HostAddress == "" { - return errors.New("host address must be specified") + return fmt.Errorf("host address must be specified") } if eventSource.Topic == "" { - return errors.New("topic must be specified") + return fmt.Errorf("topic must be specified") } if eventSource.Channel == "" { - return errors.New("channel must be specified") + return fmt.Errorf("channel must be specified") } if eventSource.TLS != nil { return apicommon.ValidateTLSConfig(eventSource.TLS) diff --git a/eventsources/sources/pulsar/start.go b/eventsources/sources/pulsar/start.go index e41d5afe17..35c9bec085 100644 --- a/eventsources/sources/pulsar/start.go +++ b/eventsources/sources/pulsar/start.go @@ -18,10 +18,10 @@ package pulsar import ( "context" "encoding/json" + "fmt" "time" "github.com/apache/pulsar-client-go/pulsar" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -123,27 +123,27 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt return err } default: - return errors.New("invalid TLS config") + return fmt.Errorf("invalid TLS config") } clientOpt.Authentication = pulsar.NewAuthenticationTLS(clientCertPath, clientKeyPath) } var client pulsar.Client - if err := common.Connect(pulsarEventSource.ConnectionBackoff, func() error { + if err := common.DoWithRetry(pulsarEventSource.ConnectionBackoff, func() error { var err error if client, err = pulsar.NewClient(clientOpt); err != nil { return err } return nil }); err != nil { - return errors.Wrapf(err, "failed to connect to %s for event source %s", pulsarEventSource.URL, el.GetEventName()) + return fmt.Errorf("failed to connect to %s for event source %s, %w", pulsarEventSource.URL, el.GetEventName(), err) } log.Info("subscribing to messages on the topic...") consumer, err := client.Subscribe(consumerOpt) if err != nil { - return errors.Wrapf(err, "failed to connect to topic %+v for event source %s", pulsarEventSource.Topics, el.GetEventName()) + return fmt.Errorf("failed to connect to topic %+v for event source %s, %w", pulsarEventSource.Topics, el.GetEventName(), err) } consumeMessages: @@ -152,7 +152,7 @@ consumeMessages: case msg, ok := <-msgChannel: if !ok { log.Error("failed to read a message, channel might have been closed") - return errors.New("channel might have been closed") + return fmt.Errorf("channel might have been closed") } if err := el.handleOne(msg, dispatch, log); err != nil { @@ -191,12 +191,12 @@ func (el *EventListener) handleOne(msg pulsar.Message, dispatch func([]byte, ... eventBody, err := json.Marshal(eventData) if err != nil { - return errors.Wrap(err, "failed to marshal the event data. rejecting the event...") + return fmt.Errorf("failed to marshal the event data. rejecting the event, %w", err) } log.Infof("dispatching the message received on the topic %s to eventbus", msg.Topic()) if err = dispatch(eventBody); err != nil { - return errors.Wrap(err, "failed to dispatch a Pulsar event") + return fmt.Errorf("failed to dispatch a Pulsar event, %w", err) } return nil } diff --git a/eventsources/sources/redis/start.go b/eventsources/sources/redis/start.go index ae3d8fb435..6f0ddbbca5 100644 --- a/eventsources/sources/redis/start.go +++ b/eventsources/sources/redis/start.go @@ -19,10 +19,10 @@ package redis import ( "context" "encoding/json" + "fmt" "time" "github.com/go-redis/redis/v8" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -76,7 +76,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if redisEventSource.Password != nil { password, err := common.GetSecretFromVolume(redisEventSource.Password) if err != nil { - return errors.Wrapf(err, "failed to find the secret password %s", redisEventSource.Password.Name) + return fmt.Errorf("failed to find the secret password %s, %w", redisEventSource.Password.Name, err) } opt.Password = password } @@ -88,7 +88,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if redisEventSource.TLS != nil { tlsConfig, err := common.GetTLSConfig(redisEventSource.TLS) if err != nil { - return errors.Wrap(err, "failed to get the tls configuration") + return fmt.Errorf("failed to get the tls configuration, %w", err) } opt.TLSConfig = tlsConfig } @@ -97,13 +97,13 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt client := redis.NewClient(opt) if status := client.Ping(ctx); status.Err() != nil { - return errors.Wrapf(status.Err(), "failed to connect to host %s and db %d for event source %s", redisEventSource.HostAddress, redisEventSource.DB, el.GetEventName()) + return fmt.Errorf("failed to connect to host %s and db %d for event source %s, %w", redisEventSource.HostAddress, redisEventSource.DB, el.GetEventName(), status.Err()) } pubsub := client.Subscribe(ctx, redisEventSource.Channels...) // Wait for confirmation that subscription is created before publishing anything. if _, err := pubsub.Receive(ctx); err != nil { - return errors.Wrapf(err, "failed to receive the subscription confirmation for event source %s", el.GetEventName()) + return fmt.Errorf("failed to receive the subscription confirmation for event source %s, %w", el.GetEventName(), err) } // Go channel which receives messages. @@ -113,7 +113,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt case message, ok := <-ch: if !ok { log.Error("failed to read a message, channel might have been closed") - return errors.New("channel might have been closed") + return fmt.Errorf("channel might have been closed") } if err := el.handleOne(message, dispatch, log); err != nil { @@ -149,11 +149,11 @@ func (el *EventListener) handleOne(message *redis.Message, dispatch func([]byte, eventBody, err := json.Marshal(&eventData) if err != nil { - return errors.Wrap(err, "failed to marshal the event data, rejecting the event...") + return fmt.Errorf("failed to marshal the event data, rejecting the event, %w", err) } log.With("channel", message.Channel).Info("dispatching the event on the data channel...") if err = dispatch(eventBody); err != nil { - return errors.Wrap(err, "failed dispatch a Redis event") + return fmt.Errorf("failed dispatch a Redis event, %w", err) } return nil } diff --git a/eventsources/sources/redis/validate.go b/eventsources/sources/redis/validate.go index 840ac821ec..53f2f1cb26 100644 --- a/eventsources/sources/redis/validate.go +++ b/eventsources/sources/redis/validate.go @@ -17,8 +17,7 @@ package redis import ( "context" - - "github.com/pkg/errors" + "fmt" "github.com/argoproj/argo-events/common" apicommon "github.com/argoproj/argo-events/pkg/apis/common" @@ -35,13 +34,13 @@ func validate(eventSource *v1alpha1.RedisEventSource) error { return common.ErrNilEventSource } if eventSource.HostAddress == "" { - return errors.New("host address must be specified") + return fmt.Errorf("host address must be specified") } if eventSource.Channels == nil { - return errors.New("channel/s must be specified") + return fmt.Errorf("channel/s must be specified") } if eventSource.Password != nil && eventSource.Namespace == "" { - return errors.New("namespace must be defined in order to retrieve the password from the secret") + return fmt.Errorf("namespace must be defined in order to retrieve the password from the secret") } if eventSource.TLS != nil { return apicommon.ValidateTLSConfig(eventSource.TLS) diff --git a/eventsources/sources/redisStream/start.go b/eventsources/sources/redis_stream/start.go similarity index 91% rename from eventsources/sources/redisStream/start.go rename to eventsources/sources/redis_stream/start.go index 5b56c046c8..df35f3437b 100644 --- a/eventsources/sources/redisStream/start.go +++ b/eventsources/sources/redis_stream/start.go @@ -19,10 +19,10 @@ package redisstream import ( "context" "encoding/json" + "fmt" "time" "github.com/go-redis/redis/v8" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -76,7 +76,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if redisEventSource.Password != nil { password, err := common.GetSecretFromVolume(redisEventSource.Password) if err != nil { - return errors.Wrapf(err, "failed to find the secret password %s", redisEventSource.Password.Name) + return fmt.Errorf("failed to find the secret password %s, %w", redisEventSource.Password.Name, err) } opt.Password = password } @@ -88,7 +88,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if redisEventSource.TLS != nil { tlsConfig, err := common.GetTLSConfig(redisEventSource.TLS) if err != nil { - return errors.Wrap(err, "failed to get the tls configuration") + return fmt.Errorf("failed to get the tls configuration, %w", err) } opt.TLSConfig = tlsConfig } @@ -97,7 +97,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt client := redis.NewClient(opt) if status := client.Ping(ctx); status.Err() != nil { - return errors.Wrapf(status.Err(), "failed to connect to host %s and db %d for event source %s", redisEventSource.HostAddress, redisEventSource.DB, el.GetEventName()) + return fmt.Errorf("failed to connect to host %s and db %d for event source %s, %w", redisEventSource.HostAddress, redisEventSource.DB, el.GetEventName(), status.Err()) } log.Infof("connected to redis server %s", redisEventSource.HostAddress) @@ -112,7 +112,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if err := client.XGroupCreate(ctx, stream, consumersGroup, "$").Err(); err != nil { // redis package doesn't seem to expose concrete error types if err.Error() != "BUSYGROUP Consumer Group name already exists" { - return errors.Wrapf(err, "creating consumer group %s for stream %s on host %s for event source %s", consumersGroup, stream, redisEventSource.HostAddress, el.GetEventName()) + return fmt.Errorf("creating consumer group %s for stream %s on host %s for event source %s, %w", consumersGroup, stream, redisEventSource.HostAddress, el.GetEventName(), err) } log.Infof("Consumer group %q already exists in stream %q", consumersGroup, stream) } @@ -208,11 +208,11 @@ func (el *EventListener) handleOne(stream string, message redis.XMessage, dispat } eventBody, err := json.Marshal(&eventData) if err != nil { - return errors.Wrap(err, "failed to marshal the event data, rejecting the event...") + return fmt.Errorf("failed to marshal the event data, rejecting the event, %w", err) } log.With("stream", stream).Info("dispatching the event on the data channel...") if err = dispatch(eventBody); err != nil { - return errors.Wrap(err, "failed dispatch a Redis stream event") + return fmt.Errorf("failed dispatch a Redis stream event, %w", err) } return nil } diff --git a/eventsources/sources/redisStream/validate.go b/eventsources/sources/redis_stream/validate.go similarity index 90% rename from eventsources/sources/redisStream/validate.go rename to eventsources/sources/redis_stream/validate.go index 3d6806443d..d8cab86b6a 100644 --- a/eventsources/sources/redisStream/validate.go +++ b/eventsources/sources/redis_stream/validate.go @@ -17,8 +17,7 @@ package redisstream import ( "context" - - "github.com/pkg/errors" + "fmt" "github.com/argoproj/argo-events/common" apicommon "github.com/argoproj/argo-events/pkg/apis/common" @@ -35,10 +34,10 @@ func validate(eventSource *v1alpha1.RedisStreamEventSource) error { return common.ErrNilEventSource } if eventSource.HostAddress == "" { - return errors.New("host address must be specified") + return fmt.Errorf("host address must be specified") } if eventSource.Streams == nil { - return errors.New("stream/streams must be specified") + return fmt.Errorf("stream/streams must be specified") } if eventSource.TLS != nil { return apicommon.ValidateTLSConfig(eventSource.TLS) diff --git a/eventsources/sources/redisStream/validate_test.go b/eventsources/sources/redis_stream/validate_test.go similarity index 100% rename from eventsources/sources/redisStream/validate_test.go rename to eventsources/sources/redis_stream/validate_test.go diff --git a/eventsources/sources/resource/start.go b/eventsources/sources/resource/start.go index 12937a46e5..181735c036 100644 --- a/eventsources/sources/resource/start.go +++ b/eventsources/sources/resource/start.go @@ -19,12 +19,12 @@ package resource import ( "context" "encoding/json" + "fmt" "os" "regexp" "strings" "time" - "github.com/pkg/errors" "github.com/tidwall/gjson" "go.uber.org/zap" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -86,11 +86,11 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt kubeConfig, _ := os.LookupEnv(common.EnvVarKubeConfig) restConfig, err := common.GetClientConfig(kubeConfig) if err != nil { - return errors.Wrapf(err, "failed to get a K8s rest config for the event source %s", el.GetEventName()) + return fmt.Errorf("failed to get a K8s rest config for the event source %s, %w", el.GetEventName(), err) } client, err := dynamic.NewForConfig(restConfig) if err != nil { - return errors.Wrapf(err, "failed to set up a dynamic K8s client for the event source %s", el.GetEventName()) + return fmt.Errorf("failed to set up a dynamic K8s client for the event source %s, %w", el.GetEventName(), err) } resourceEventSource := &el.ResourceEventSource @@ -109,7 +109,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt if resourceEventSource.Filter != nil && resourceEventSource.Filter.Labels != nil { sel, err := LabelSelector(resourceEventSource.Filter.Labels) if err != nil { - return errors.Wrapf(err, "failed to create the label selector for the event source %s", el.GetEventName()) + return fmt.Errorf("failed to create the label selector for the event source %s, %w", el.GetEventName(), err) } options.LabelSelector = sel.String() } @@ -138,14 +138,14 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt objBody, err := json.Marshal(event.Obj) if err != nil { - return errors.Wrap(err, "failed to marshal the resource, rejecting the event...") + return fmt.Errorf("failed to marshal the resource, rejecting the event, %w", err) } var oldObjBody []byte if event.OldObj != nil { oldObjBody, err = json.Marshal(event.OldObj) if err != nil { - return errors.Wrap(err, "failed to marshal the resource, rejecting the event...") + return fmt.Errorf("failed to marshal the resource, rejecting the event, %w", err) } } @@ -161,11 +161,11 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt eventBody, err := json.Marshal(eventData) if err != nil { - return errors.Wrap(err, "failed to marshal the event. rejecting the event...") + return fmt.Errorf("failed to marshal the event. rejecting the event, %w", err) } if err = dispatch(eventBody); err != nil { - return errors.Wrap(err, "failed to dispatch a resource event") + return fmt.Errorf("failed to dispatch a resource event, %w", err) } return nil } @@ -222,7 +222,7 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt } default: stopCh <- struct{}{} - return errors.Errorf("unknown event type: %s", string(eventType)) + return fmt.Errorf("unknown event type: %s", string(eventType)) } } diff --git a/eventsources/sources/slack/start.go b/eventsources/sources/slack/start.go index 61378a5a8d..c5393c342c 100644 --- a/eventsources/sources/slack/start.go +++ b/eventsources/sources/slack/start.go @@ -20,11 +20,11 @@ import ( "bytes" "context" "encoding/json" + "fmt" "io" "net/http" "time" - "github.com/pkg/errors" "github.com/slack-go/slack" "github.com/slack-go/slack/slackevents" "go.uber.org/zap" @@ -159,7 +159,7 @@ func (rc *Router) HandleRoute(writer http.ResponseWriter, request *http.Request) } default: - err = errors.New("could not determine slack type from form parameters") + err = fmt.Errorf("could not determine slack type from form parameters") logger.Errorw("failed to determine type of slack post", zap.Error(err)) common.SendInternalErrorResponse(writer, err.Error()) route.Metrics.EventProcessingFailed(route.EventSourceName, route.EventName) @@ -212,19 +212,19 @@ func (rc *Router) handleEvent(request *http.Request) ([]byte, []byte, error) { var data []byte body, err := rc.getRequestBody(request) if err != nil { - return data, response, errors.Wrap(err, "failed to fetch request body") + return data, response, fmt.Errorf("failed to fetch request body, %w", err) } eventsAPIEvent, err := slackevents.ParseEvent(json.RawMessage(body), slackevents.OptionVerifyToken(&slackevents.TokenComparator{VerificationToken: rc.token})) if err != nil { - return data, response, errors.Wrap(err, "failed to extract event") + return data, response, fmt.Errorf("failed to extract event, %w", err) } if eventsAPIEvent.Type == slackevents.URLVerification { var r *slackevents.ChallengeResponse err = json.Unmarshal(body, &r) if err != nil { - return data, response, errors.Wrap(err, "failed to verify the challenge") + return data, response, fmt.Errorf("failed to verify the challenge, %w", err) } response = []byte(r.Challenge) } @@ -232,7 +232,7 @@ func (rc *Router) handleEvent(request *http.Request) ([]byte, []byte, error) { if eventsAPIEvent.Type == slackevents.CallbackEvent { data, err = json.Marshal(&eventsAPIEvent.InnerEvent) if err != nil { - return data, response, errors.Wrap(err, "failed to marshal event data, rejecting the event...") + return data, response, fmt.Errorf("failed to marshal event data, rejecting the event, %w", err) } } @@ -244,12 +244,12 @@ func (rc *Router) handleInteraction(request *http.Request) ([]byte, error) { ie := &slack.InteractionCallback{} err := json.Unmarshal([]byte(payload), ie) if err != nil { - return nil, errors.Wrap(err, "failed to parse interaction event") + return nil, fmt.Errorf("failed to parse interaction event, %w", err) } data, err := json.Marshal(ie) if err != nil { - return nil, errors.Wrap(err, "failed to marshal action data") + return nil, fmt.Errorf("failed to marshal action data, %w", err) } return data, nil @@ -258,12 +258,12 @@ func (rc *Router) handleInteraction(request *http.Request) ([]byte, error) { func (rc *Router) handleSlashCommand(request *http.Request) ([]byte, error) { command, err := slack.SlashCommandParse(request) if err != nil { - return nil, errors.Wrap(err, "failed to parse command") + return nil, fmt.Errorf("failed to parse command, %w", err) } data, err := json.Marshal(command) if err != nil { - return nil, errors.Wrap(err, "failed to marshal command data") + return nil, fmt.Errorf("failed to marshal command data, %w", err) } return data, nil @@ -275,7 +275,7 @@ func (rc *Router) getRequestBody(request *http.Request) ([]byte, error) { // Reset request.Body ReadCloser to prevent side-effect if re-read request.Body = io.NopCloser(bytes.NewBuffer(body)) if err != nil { - return nil, errors.Wrap(err, "failed to parse request body") + return nil, fmt.Errorf("failed to parse request body, %w", err) } return body, nil } @@ -289,7 +289,7 @@ func (rc *Router) verifyRequest(request *http.Request) error { if len(signingSecret) > 0 { sv, err := slack.NewSecretsVerifier(request.Header, signingSecret) if err != nil { - return errors.Wrap(err, "cannot create secrets verifier") + return fmt.Errorf("cannot create secrets verifier, %w", err) } // Read the request body @@ -300,12 +300,12 @@ func (rc *Router) verifyRequest(request *http.Request) error { _, err = sv.Write(body) if err != nil { - return errors.Wrap(err, "error writing body: cannot verify signature") + return fmt.Errorf("error writing body: cannot verify signature, %w", err) } err = sv.Ensure() if err != nil { - return errors.Wrap(err, "signature validation failed") + return fmt.Errorf("signature validation failed, %w", err) } } return nil @@ -323,13 +323,13 @@ func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byt log.Info("retrieving the slack token...") token, err := common.GetSecretFromVolume(slackEventSource.Token) if err != nil { - return errors.Wrap(err, "failed to retrieve the token") + return fmt.Errorf("failed to retrieve the token, %w", err) } log.Info("retrieving the signing secret...") signingSecret, err := common.GetSecretFromVolume(slackEventSource.SigningSecret) if err != nil { - return errors.Wrap(err, "failed to retrieve the signing secret") + return fmt.Errorf("failed to retrieve the signing secret, %w", err) } route := webhook.NewRoute(slackEventSource.Webhook, log, el.GetEventSourceName(), el.GetEventName(), el.Metrics) diff --git a/eventsources/sources/storagegrid/start.go b/eventsources/sources/storagegrid/start.go index 0a399c6967..db6aa02c24 100644 --- a/eventsources/sources/storagegrid/start.go +++ b/eventsources/sources/storagegrid/start.go @@ -30,7 +30,6 @@ import ( "github.com/go-resty/resty/v2" "github.com/google/uuid" "github.com/joncalhoun/qson" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -215,7 +214,7 @@ func (router *Router) PostActivate() error { authToken, err := common.GetSecretFromVolume(eventSource.AuthToken) if err != nil { - return errors.Wrap(err, "AuthToken not found") + return fmt.Errorf("AuthToken not found, %w", err) } registrationURL := common.FormattedURL(eventSource.Webhook.URL, eventSource.Webhook.Endpoint) @@ -325,7 +324,7 @@ func (router *Router) PostActivate() error { if !response.IsSuccess() { errObj := response.Error().(*genericResponse) - return errors.Errorf("failed to configure notification. reason %s", errObj.Message.Text) + return fmt.Errorf("failed to configure notification. reason %s", errObj.Message.Text) } logger.Info("successfully registered notification configuration on storagegrid") diff --git a/eventsources/sources/stripe/start.go b/eventsources/sources/stripe/start.go index e65b7e931e..92648e969a 100644 --- a/eventsources/sources/stripe/start.go +++ b/eventsources/sources/stripe/start.go @@ -19,11 +19,11 @@ package stripe import ( "context" "encoding/json" + "fmt" "io" "net/http" "time" - "github.com/pkg/errors" "github.com/stripe/stripe-go" "github.com/stripe/stripe-go/webhookendpoint" "go.uber.org/zap" @@ -151,7 +151,7 @@ func (rc *Router) PostActivate() error { apiKey, err := common.GetSecretFromVolume(stripeEventSource.APIKey) if err != nil { - return errors.Wrap(err, "APIKey not found") + return fmt.Errorf("APIKey not found, %w", err) } stripe.Key = apiKey diff --git a/eventsources/sources/stripe/validate.go b/eventsources/sources/stripe/validate.go index d5d5bb1c3b..827166ef97 100644 --- a/eventsources/sources/stripe/validate.go +++ b/eventsources/sources/stripe/validate.go @@ -18,8 +18,7 @@ package stripe import ( "context" - - "github.com/pkg/errors" + "fmt" "github.com/argoproj/argo-events/common" "github.com/argoproj/argo-events/eventsources/common/webhook" @@ -37,7 +36,7 @@ func validate(eventSource *v1alpha1.StripeEventSource) error { } if eventSource.CreateWebhook { if eventSource.APIKey == nil { - return errors.New("api key K8s secret selector not provided") + return fmt.Errorf("api key K8s secret selector not provided") } } return webhook.ValidateWebhookContext(eventSource.Webhook) diff --git a/eventsources/sources/webhook/start.go b/eventsources/sources/webhook/start.go index e4e259f8bd..2be2ad90dc 100644 --- a/eventsources/sources/webhook/start.go +++ b/eventsources/sources/webhook/start.go @@ -33,7 +33,6 @@ import ( apicommon "github.com/argoproj/argo-events/pkg/apis/common" "github.com/argoproj/argo-events/pkg/apis/events" "github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1" - "github.com/pkg/errors" "go.uber.org/zap" ) @@ -210,7 +209,7 @@ func getRequestBody(request *http.Request) ([]byte, error) { // Reset request.Body ReadCloser to prevent side-effect if re-read request.Body = io.NopCloser(bytes.NewBuffer(body)) if err != nil { - return nil, errors.Wrap(err, "failed to parse request body") + return nil, fmt.Errorf("failed to parse request body, %w", err) } return body, nil } diff --git a/go.mod b/go.mod index 8311a59d3b..84990f3c36 100644 --- a/go.mod +++ b/go.mod @@ -53,7 +53,6 @@ require ( github.com/nats-io/nats.go v1.17.0 github.com/nats-io/stan.go v0.10.3 github.com/nsqio/go-nsq v1.1.0 - github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.13.0 github.com/rabbitmq/amqp091-go v1.4.0 github.com/radovskyb/watcher v1.0.7 @@ -114,6 +113,7 @@ require ( github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852 // indirect github.com/pelletier/go-toml/v2 v2.0.1 // indirect github.com/pierrec/lz4/v4 v4.1.15 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/stringprep v1.0.3 // indirect sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect diff --git a/pkg/apis/eventsource/v1alpha1/config.go b/pkg/apis/eventsource/v1alpha1/config.go index 07fb8101ff..e955742a8f 100644 --- a/pkg/apis/eventsource/v1alpha1/config.go +++ b/pkg/apis/eventsource/v1alpha1/config.go @@ -1,7 +1,7 @@ package v1alpha1 import ( - "errors" + fmt "fmt" "path" "regexp" ) @@ -18,19 +18,19 @@ type WatchPathConfig struct { // Validate validates WatchPathConfig func (c *WatchPathConfig) Validate() error { if c.Directory == "" { - return errors.New("directory is required") + return fmt.Errorf("directory is required") } if !path.IsAbs(c.Directory) { - return errors.New("directory must be an absolute file path") + return fmt.Errorf("directory must be an absolute file path") } if c.Path == "" && c.PathRegexp == "" { - return errors.New("either path or pathRegexp must be specified") + return fmt.Errorf("either path or pathRegexp must be specified") } if c.Path != "" && c.PathRegexp != "" { - return errors.New("path and pathRegexp cannot be specified together") + return fmt.Errorf("path and pathRegexp cannot be specified together") } if c.Path != "" && path.IsAbs(c.Path) { - return errors.New("path must be a relative file path") + return fmt.Errorf("path must be a relative file path") } if c.PathRegexp != "" { _, err := regexp.Compile(c.PathRegexp) diff --git a/pkg/apis/eventsource/v1alpha1/validate.go b/pkg/apis/eventsource/v1alpha1/validate.go index c912ba24b5..511f5e1cc8 100644 --- a/pkg/apis/eventsource/v1alpha1/validate.go +++ b/pkg/apis/eventsource/v1alpha1/validate.go @@ -16,14 +16,12 @@ limitations under the License. package v1alpha1 -import ( - "github.com/pkg/errors" -) +import fmt "fmt" // ValidateEventSource validates a generic event source func ValidateEventSource(eventSource *EventSource) error { if eventSource == nil { - return errors.New("event source can't be nil") + return fmt.Errorf("event source can't be nil") } return nil } diff --git a/sensors/artifacts/file.go b/sensors/artifacts/file.go index c7e8f1b159..db1831759e 100644 --- a/sensors/artifacts/file.go +++ b/sensors/artifacts/file.go @@ -17,7 +17,7 @@ limitations under the License. package artifacts import ( - "errors" + "fmt" "os" "github.com/argoproj/argo-events/common/logging" @@ -33,7 +33,7 @@ type FileReader struct { func NewFileReader(fileArtifact *v1alpha1.FileArtifact) (ArtifactReader, error) { // This should never happen! if fileArtifact == nil { - return nil, errors.New("FileArtifact cannot be empty") + return nil, fmt.Errorf("FileArtifact cannot be empty") } return &FileReader{fileArtifact}, nil } diff --git a/sensors/artifacts/git.go b/sensors/artifacts/git.go index baa88b5543..38fc96bdd4 100644 --- a/sensors/artifacts/git.go +++ b/sensors/artifacts/git.go @@ -28,7 +28,6 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport/http" go_git_ssh "github.com/go-git/go-git/v5/plumbing/transport/ssh" - "github.com/pkg/errors" "golang.org/x/crypto/ssh" "github.com/argoproj/argo-events/common" @@ -96,11 +95,11 @@ func (g *GitArtifactReader) getGitAuth() (transport.AuthMethod, error) { if g.artifact.Creds != nil { username, err := common.GetSecretFromVolume(g.artifact.Creds.Username) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve username") + return nil, fmt.Errorf("failed to retrieve username, %w", err) } password, err := common.GetSecretFromVolume(g.artifact.Creds.Password) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve password") + return nil, fmt.Errorf("failed to retrieve password, %w", err) } return &http.BasicAuth{ Username: username, @@ -110,7 +109,7 @@ func (g *GitArtifactReader) getGitAuth() (transport.AuthMethod, error) { if g.artifact.SSHKeySecret != nil { sshKeyPath, err := common.GetSecretVolumePath(g.artifact.SSHKeySecret) if err != nil { - return nil, errors.Wrap(err, "failed to get SSH key from mounted volume") + return nil, fmt.Errorf("failed to get SSH key from mounted volume, %w", err) } return getSSHKeyAuth(sshKeyPath, g.artifact.InsecureIgnoreHostKey) } @@ -129,7 +128,7 @@ func (g *GitArtifactReader) readFromRepository(r *git.Repository, dir string) ([ URLs: g.artifact.Remote.URLS, }) if err != nil { - return nil, fmt.Errorf("failed to create remote. err: %+v", err) + return nil, fmt.Errorf("failed to create remote. err: %w", err) } fetchOptions := &git.FetchOptions{ @@ -142,13 +141,13 @@ func (g *GitArtifactReader) readFromRepository(r *git.Repository, dir string) ([ } if err := r.Fetch(fetchOptions); err != nil { - return nil, fmt.Errorf("failed to fetch remote %s. err: %+v", g.artifact.Remote.Name, err) + return nil, fmt.Errorf("failed to fetch remote %s. err: %w", g.artifact.Remote.Name, err) } } w, err := r.Worktree() if err != nil { - return nil, fmt.Errorf("failed to get working tree. err: %+v", err) + return nil, fmt.Errorf("failed to get working tree. err: %w", err) } fetchOptions := &git.FetchOptions{ @@ -225,7 +224,7 @@ func (g *GitArtifactReader) Read() ([]byte, error) { if cloneDir == "" { tempDir, err := os.MkdirTemp("", "git-tmp") if err != nil { - return nil, errors.Wrap(err, "failed to create a temp file to clone the repository") + return nil, fmt.Errorf("failed to create a temp file to clone the repository, %w", err) } defer os.Remove(tempDir) cloneDir = tempDir @@ -234,7 +233,7 @@ func (g *GitArtifactReader) Read() ([]byte, error) { r, err := git.PlainOpen(cloneDir) if err != nil { if err != git.ErrRepositoryNotExists { - return nil, fmt.Errorf("failed to open repository. err: %+v", err) + return nil, fmt.Errorf("failed to open repository. err: %w", err) } cloneOpt := &git.CloneOptions{ diff --git a/sensors/artifacts/inline.go b/sensors/artifacts/inline.go index 1c6e426203..92353c450e 100644 --- a/sensors/artifacts/inline.go +++ b/sensors/artifacts/inline.go @@ -17,7 +17,7 @@ limitations under the License. package artifacts import ( - "errors" + "fmt" "github.com/argoproj/argo-events/common/logging" ) @@ -31,7 +31,7 @@ type InlineReader struct { func NewInlineReader(inlineArtifact *string) (ArtifactReader, error) { // This should never happen! if inlineArtifact == nil { - return nil, errors.New("InlineArtifact does not exist") + return nil, fmt.Errorf("InlineArtifact does not exist") } return &InlineReader{inlineArtifact}, nil } diff --git a/sensors/artifacts/resource.go b/sensors/artifacts/resource.go index 3619bcd477..3ca6aaafb5 100644 --- a/sensors/artifacts/resource.go +++ b/sensors/artifacts/resource.go @@ -18,7 +18,7 @@ package artifacts import ( "encoding/json" - "errors" + "fmt" "github.com/ghodss/yaml" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -35,7 +35,7 @@ type ResourceReader struct { // NewResourceReader creates a new ArtifactReader for resource func NewResourceReader(resourceArtifact *common.Resource) (ArtifactReader, error) { if resourceArtifact == nil { - return nil, errors.New("ResourceArtifact does not exist") + return nil, fmt.Errorf("ResourceArtifact does not exist") } data, err := json.Marshal(resourceArtifact) if err != nil { diff --git a/sensors/artifacts/store.go b/sensors/artifacts/store.go index d41aa0a027..7fb80bb453 100644 --- a/sensors/artifacts/store.go +++ b/sensors/artifacts/store.go @@ -21,7 +21,6 @@ import ( "strings" "github.com/ghodss/yaml" - "github.com/pkg/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "github.com/argoproj/argo-events/common" @@ -37,12 +36,12 @@ type ArtifactReader interface { func FetchArtifact(reader ArtifactReader) (*unstructured.Unstructured, error) { var obj []byte - if err := common.Connect(&common.DefaultBackoff, func() error { + if err := common.DoWithRetry(&common.DefaultBackoff, func() error { var e error obj, e = reader.Read() return e }); err != nil { - return nil, errors.Wrap(err, "failed to fetch artifact") + return nil, fmt.Errorf("failed to fetch artifact, %w", err) } return decodeAndUnstructure(obj) } @@ -57,11 +56,11 @@ func GetCredentials(art *v1alpha1.ArtifactLocation) (*Credentials, error) { if art.S3 != nil { accessKey, err := common.GetSecretFromVolume(art.S3.AccessKey) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve accessKey") + return nil, fmt.Errorf("failed to retrieve accessKey, %w", err) } secretKey, err := common.GetSecretFromVolume(art.S3.SecretKey) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve secretKey") + return nil, fmt.Errorf("failed to retrieve secretKey, %w", err) } return &Credentials{ accessKey: strings.TrimSpace(accessKey), diff --git a/sensors/artifacts/url.go b/sensors/artifacts/url.go index fd2b6c77ca..3764b14209 100644 --- a/sensors/artifacts/url.go +++ b/sensors/artifacts/url.go @@ -2,11 +2,10 @@ package artifacts import ( "crypto/tls" + "fmt" "io" "net/http" - "github.com/pkg/errors" - "github.com/argoproj/argo-events/common/logging" "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" ) @@ -19,7 +18,7 @@ type URLReader struct { // NewURLReader creates a new ArtifactReader for workflows at URL endpoints. func NewURLReader(urlArtifact *v1alpha1.URLArtifact) (ArtifactReader, error) { if urlArtifact == nil { - return nil, errors.New("URLArtifact cannot be empty") + return nil, fmt.Errorf("URLArtifact cannot be empty") } return &URLReader{urlArtifact}, nil } @@ -42,7 +41,7 @@ func (reader *URLReader) Read() ([]byte, error) { if resp.StatusCode != http.StatusOK { log.Warnf("failed to read %s. status code: %d", reader.urlArtifact.Path, resp.StatusCode) - return nil, errors.Errorf("status code %v", resp.StatusCode) + return nil, fmt.Errorf("status code %v", resp.StatusCode) } content, err := io.ReadAll(io.LimitReader(resp.Body, 512*1024)) diff --git a/sensors/listener.go b/sensors/listener.go index 9b62c15113..9af149d936 100644 --- a/sensors/listener.go +++ b/sensors/listener.go @@ -36,7 +36,6 @@ import ( sensordependencies "github.com/argoproj/argo-events/sensors/dependencies" sensortriggers "github.com/argoproj/argo-events/sensors/triggers" cloudevents "github.com/cloudevents/sdk-go/v2" - "github.com/pkg/errors" cronlib "github.com/robfig/cron/v3" "go.uber.org/ratelimit" "go.uber.org/zap" @@ -106,7 +105,7 @@ func (sensorCtx *SensorContext) listenEvents(ctx context.Context) error { if err != nil { return err } - err = common.Connect(&common.DefaultBackoff, func() error { + err = common.DoWithRetry(&common.DefaultBackoff, func() error { return ebDriver.Initialize() }) if err != nil { @@ -150,7 +149,7 @@ func (sensorCtx *SensorContext) listenEvents(ctx context.Context) error { } var conn eventbuscommon.TriggerConnection - err = common.Connect(&common.DefaultBackoff, func() error { + err = common.DoWithRetry(&common.DefaultBackoff, func() error { var err error conn, err = ebDriver.Connect(trigger.Template.Name, depExpression, deps) triggerLogger.Debugf("just created connection %v, %+v", &conn, conn) @@ -343,7 +342,7 @@ func (sensorCtx *SensorContext) triggerWithRateLimit(ctx context.Context, sensor log := logging.FromContext(ctx) if err := sensorCtx.triggerOne(ctx, sensor, trigger, eventsMapping, depNames, eventIDs, log); err != nil { // Log the error, and let it continue - log.Errorw("failed to execute a trigger", zap.Error(err), zap.String(logging.LabelTriggerName, trigger.Template.Name), + log.Errorw("Failed to execute a trigger", zap.Error(err), zap.String(logging.LabelTriggerName, trigger.Template.Name), zap.Any("triggeredBy", depNames), zap.Any("triggeredByEvents", eventIDs)) sensorCtx.metrics.ActionFailed(sensor.Name, trigger.Template.Name) } else { @@ -366,7 +365,7 @@ func (sensorCtx *SensorContext) triggerOne(ctx context.Context, sensor *v1alpha1 logger.Debugw("resolving the trigger implementation") triggerImpl := sensorCtx.GetTrigger(ctx, &trigger) if triggerImpl == nil { - return errors.Errorf("invalid trigger %s, could not find an implementation", trigger.Template.Name) + return fmt.Errorf("invalid trigger %s, could not find an implementation", trigger.Template.Name) } logger = logger.With(logging.LabelTriggerType, triggerImpl.GetTriggerType()) @@ -376,7 +375,7 @@ func (sensorCtx *SensorContext) triggerOne(ctx context.Context, sensor *v1alpha1 return err } if obj == nil { - return errors.Errorf("invalid trigger %s, could not fetch the trigger resource", trigger.Template.Name) + return fmt.Errorf("invalid trigger %s, could not fetch the trigger resource", trigger.Template.Name) } logger.Debug("applying resource parameters if any") @@ -391,12 +390,12 @@ func (sensorCtx *SensorContext) triggerOne(ctx context.Context, sensor *v1alpha1 retryStrategy = &apicommon.Backoff{Steps: 1} } var newObj interface{} - if err := common.Connect(retryStrategy, func() error { + if err := common.DoWithRetry(retryStrategy, func() error { var e error newObj, e = triggerImpl.Execute(ctx, eventsMapping, updatedObj) return e }); err != nil { - return errors.Wrap(err, "failed to execute trigger") + return fmt.Errorf("failed to execute trigger, %w", err) } logger.Debug("trigger resource successfully executed") @@ -404,7 +403,7 @@ func (sensorCtx *SensorContext) triggerOne(ctx context.Context, sensor *v1alpha1 if err := triggerImpl.ApplyPolicy(ctx, newObj); err != nil { return err } - logger.Infow(fmt.Sprintf("successfully processed trigger '%s'", trigger.Template.Name), + logger.Infow(fmt.Sprintf("Successfully processed trigger '%s'", trigger.Template.Name), zap.Any("triggeredBy", depNames), zap.Any("triggeredByEvents", eventIDs)) return nil } diff --git a/sensors/policy/status.go b/sensors/policy/status.go index 6347481f4d..94656cc379 100644 --- a/sensors/policy/status.go +++ b/sensors/policy/status.go @@ -18,8 +18,7 @@ package policy import ( "context" - - "github.com/pkg/errors" + "fmt" ) // StatusPolicy implements the policy for a HTTP trigger @@ -44,5 +43,5 @@ func (hp *StatusPolicy) ApplyPolicy(ctx context.Context) error { return nil } } - return errors.Errorf("policy application resulted in failure. http response status %d is not allowed", hp.Status) + return fmt.Errorf("policy application resulted in failure. http response status %d is not allowed", hp.Status) } diff --git a/sensors/triggers/apache-openwhisk/apache-openwhisk.go b/sensors/triggers/apache-openwhisk/apache-openwhisk.go index 25f2d010a4..bdd4810d35 100644 --- a/sensors/triggers/apache-openwhisk/apache-openwhisk.go +++ b/sensors/triggers/apache-openwhisk/apache-openwhisk.go @@ -18,10 +18,10 @@ package apache_openwhisk import ( "context" "encoding/json" + "fmt" "net/http" "github.com/apache/openwhisk-client-go/whisk" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -55,7 +55,7 @@ func NewTriggerImpl(sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, openWhis config, err := whisk.GetDefaultConfig() if err != nil { - return nil, errors.Wrap(err, "failed to get default configuration") + return nil, fmt.Errorf("failed to get default configuration, %w", err) } config.Host = openwhisktrigger.Host @@ -63,7 +63,7 @@ func NewTriggerImpl(sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, openWhis if openwhisktrigger.AuthToken != nil { token, err := common.GetSecretFromVolume(openwhisktrigger.AuthToken) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve auth token") + return nil, fmt.Errorf("failed to retrieve auth token, %w", err) } config.AuthToken = token } @@ -79,7 +79,7 @@ func NewTriggerImpl(sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, openWhis client, err = whisk.NewClient(http.DefaultClient, config) if err != nil { - return nil, errors.Wrap(err, "failed to instantiate OpenWhisk client") + return nil, fmt.Errorf("failed to instantiate OpenWhisk client, %w", err) } openWhiskClients[trigger.Template.Name] = client @@ -108,12 +108,12 @@ func (t *TriggerImpl) FetchResource(ctx context.Context) (interface{}, error) { func (t *TriggerImpl) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { fetchedResource, ok := resource.(*v1alpha1.OpenWhiskTrigger) if !ok { - return nil, errors.New("failed to interpret the fetched trigger resource") + return nil, fmt.Errorf("failed to interpret the fetched trigger resource") } resourceBytes, err := json.Marshal(fetchedResource) if err != nil { - return nil, errors.Wrap(err, "failed to marshal the OpenWhisk trigger resource") + return nil, fmt.Errorf("failed to marshal the OpenWhisk trigger resource, %w", err) } parameters := fetchedResource.Parameters if parameters != nil { @@ -123,7 +123,7 @@ func (t *TriggerImpl) ApplyResourceParameters(events map[string]*v1alpha1.Event, } var openwhisktrigger *v1alpha1.OpenWhiskTrigger if err := json.Unmarshal(updatedResourceBytes, &openwhisktrigger); err != nil { - return nil, errors.Wrap(err, "failed to unmarshal the updated OpenWhisk trigger resource after applying resource parameters") + return nil, fmt.Errorf("failed to unmarshal the updated OpenWhisk trigger resource after applying resource parameters, %w", err) } t.Logger.Debugw("applied parameters to the OpenWhisk trigger", zap.Any("name", t.Trigger.Template.Name), zap.Any("trigger", *openwhisktrigger)) @@ -141,7 +141,7 @@ func (t *TriggerImpl) Execute(ctx context.Context, events map[string]*v1alpha1.E openwhisktrigger, ok := resource.(*v1alpha1.OpenWhiskTrigger) if !ok { - return nil, errors.New("failed to interpret the OpenWhisk trigger resource") + return nil, fmt.Errorf("failed to interpret the OpenWhisk trigger resource") } if openwhisktrigger.Payload != nil { @@ -155,7 +155,7 @@ func (t *TriggerImpl) Execute(ctx context.Context, events map[string]*v1alpha1.E response, status, err := t.OpenWhiskClient.Actions.Invoke(openwhisktrigger.ActionName, payload, true, true) if err != nil { - return nil, errors.Wrapf(err, "failed to invoke action %s", openwhisktrigger.ActionName) + return nil, fmt.Errorf("failed to invoke action %s, %w", openwhisktrigger.ActionName, err) } t.Logger.Debugw("response for the OpenWhisk action invocation", zap.Any("name", t.Trigger.Template.Name), zap.Any("response", response)) @@ -170,7 +170,7 @@ func (t *TriggerImpl) ApplyPolicy(ctx context.Context, resource interface{}) err } response, ok := resource.(*http.Response) if !ok { - return errors.New("failed to interpret the trigger execution response") + return fmt.Errorf("failed to interpret the trigger execution response") } p := policy.NewStatusPolicy(response.StatusCode, t.Trigger.Policy.Status.GetAllow()) diff --git a/sensors/triggers/argo-workflow/argo-workflow.go b/sensors/triggers/argo-workflow/argo-workflow.go index 32b710efe3..d36fe8e6e9 100644 --- a/sensors/triggers/argo-workflow/argo-workflow.go +++ b/sensors/triggers/argo-workflow/argo-workflow.go @@ -24,7 +24,6 @@ import ( "strings" "time" - "github.com/pkg/errors" "go.uber.org/zap" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -87,7 +86,7 @@ func (t *ArgoWorkflowTrigger) FetchResource(ctx context.Context) (interface{}, e func (t *ArgoWorkflowTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { obj, ok := resource.(*unstructured.Unstructured) if !ok { - return nil, errors.New("failed to interpret the trigger resource") + return nil, fmt.Errorf("failed to interpret the trigger resource") } if err := triggers.ApplyResourceParameters(events, t.Trigger.Template.ArgoWorkflow.Parameters, obj); err != nil { return nil, err @@ -106,16 +105,16 @@ func (t *ArgoWorkflowTrigger) Execute(ctx context.Context, events map[string]*v1 obj, ok := resource.(*unstructured.Unstructured) if !ok { - return nil, errors.New("failed to interpret the trigger resource") + return nil, fmt.Errorf("failed to interpret the trigger resource") } name := obj.GetName() if name == "" { if op != v1alpha1.Submit { - return nil, errors.Errorf("failed to execute the workflow %v operation, no name is given", op) + return nil, fmt.Errorf("failed to execute the workflow %v operation, no name is given", op) } if obj.GetGenerateName() == "" { - return nil, errors.New("failed to trigger the workflow, neither name nor generateName is given") + return nil, fmt.Errorf("failed to trigger the workflow, neither name nor generateName is given") } } @@ -137,7 +136,7 @@ func (t *ArgoWorkflowTrigger) Execute(ctx context.Context, events map[string]*v1 case v1alpha1.Submit: file, err := os.CreateTemp("", fmt.Sprintf("%s%s", name, obj.GetGenerateName())) if err != nil { - return nil, errors.Wrapf(err, "failed to create a temp file for the workflow %s", obj.GetName()) + return nil, fmt.Errorf("failed to create a temp file for the workflow %s, %w", obj.GetName(), err) } defer os.Remove(file.Name()) @@ -157,7 +156,7 @@ func (t *ArgoWorkflowTrigger) Execute(ctx context.Context, events map[string]*v1 } if _, err := file.Write(jObj); err != nil { - return nil, errors.Wrapf(err, "failed to write workflow json %s to the temp file %s", name, file.Name()) + return nil, fmt.Errorf("failed to write workflow json %s to the temp file %s, %w", name, file.Name(), err) } cmd = exec.Command("argo", "-n", namespace, "submit", file.Name()) case v1alpha1.SubmitFrom: @@ -168,7 +167,7 @@ func (t *ArgoWorkflowTrigger) Execute(ctx context.Context, events map[string]*v1 case "workflowtemplate": kind = "workflowtemplate" default: - return nil, errors.Errorf("invalid kind %s", kind) + return nil, fmt.Errorf("invalid kind %s", kind) } fromArg := fmt.Sprintf("%s/%s", kind, name) cmd = exec.Command("argo", "-n", namespace, "submit", "--from", fromArg) @@ -185,14 +184,14 @@ func (t *ArgoWorkflowTrigger) Execute(ctx context.Context, events map[string]*v1 case v1alpha1.Stop: cmd = exec.Command("argo", "-n", namespace, "stop", name) default: - return nil, errors.Errorf("unknown operation type %s", string(op)) + return nil, fmt.Errorf("unknown operation type %s", string(op)) } cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Args = append(cmd.Args, trigger.Template.ArgoWorkflow.Args...) if err := t.cmdRunner(cmd); err != nil { - return nil, errors.Wrapf(err, "failed to execute %s command for workflow %s", string(op), name) + return nil, fmt.Errorf("failed to execute %s command for workflow %s, %w", string(op), name, err) } t.namespableDynamicClient = t.DynamicClient.Resource(schema.GroupVersionResource{ @@ -209,7 +208,7 @@ func (t *ArgoWorkflowTrigger) Execute(ctx context.Context, events map[string]*v1 return nil, err } if len(l.Items) == 0 { - return nil, errors.New("failed to list created workflows for unknown reason") + return nil, fmt.Errorf("failed to list created workflows for unknown reason") } return l.Items[0], nil } @@ -224,7 +223,7 @@ func (t *ArgoWorkflowTrigger) ApplyPolicy(ctx context.Context, resource interfac obj, ok := resource.(*unstructured.Unstructured) if !ok { - return errors.New("failed to interpret the trigger resource") + return fmt.Errorf("failed to interpret the trigger resource") } p := policy.NewResourceLabels(trigger, t.namespableDynamicClient, obj) @@ -237,7 +236,7 @@ func (t *ArgoWorkflowTrigger) ApplyPolicy(ctx context.Context, resource interfac switch err { case wait.ErrWaitTimeout: if trigger.Policy.K8s.ErrorOnBackoffTimeout { - return errors.Errorf("failed to determine status of the triggered resource. setting trigger state as failed") + return fmt.Errorf("failed to determine status of the triggered resource. setting trigger state as failed") } return nil default: diff --git a/sensors/triggers/aws-lambda/aws-lambda.go b/sensors/triggers/aws-lambda/aws-lambda.go index 41ed536e4e..079453bcc9 100644 --- a/sensors/triggers/aws-lambda/aws-lambda.go +++ b/sensors/triggers/aws-lambda/aws-lambda.go @@ -18,10 +18,10 @@ package aws_lambda import ( "context" "encoding/json" + "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/lambda" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common/logging" @@ -52,7 +52,7 @@ func NewAWSLambdaTrigger(lambdaClients map[string]*lambda.Lambda, sensor *v1alph if !ok { awsSession, err := commonaws.CreateAWSSessionWithCredsInVolume(lambdatrigger.Region, lambdatrigger.RoleARN, lambdatrigger.AccessKey, lambdatrigger.SecretKey, nil) if err != nil { - return nil, errors.Wrap(err, "failed to create a AWS session") + return nil, fmt.Errorf("failed to create a AWS session, %w", err) } lambdaClient = lambda.New(awsSession, &aws.Config{Region: &lambdatrigger.Region}) lambdaClients[trigger.Template.Name] = lambdaClient @@ -80,7 +80,7 @@ func (t *AWSLambdaTrigger) FetchResource(ctx context.Context) (interface{}, erro func (t *AWSLambdaTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { resourceBytes, err := json.Marshal(resource) if err != nil { - return nil, errors.Wrap(err, "failed to marshal the aws lamda trigger resource") + return nil, fmt.Errorf("failed to marshal the aws lamda trigger resource, %w", err) } parameters := t.Trigger.Template.AWSLambda.Parameters if parameters != nil { @@ -90,7 +90,7 @@ func (t *AWSLambdaTrigger) ApplyResourceParameters(events map[string]*v1alpha1.E } var ht *v1alpha1.AWSLambdaTrigger if err := json.Unmarshal(updatedResourceBytes, &ht); err != nil { - return nil, errors.Wrap(err, "failed to unmarshal the updated aws lambda trigger resource after applying resource parameters") + return nil, fmt.Errorf("failed to unmarshal the updated aws lambda trigger resource after applying resource parameters, %w", err) } return ht, nil } @@ -101,11 +101,11 @@ func (t *AWSLambdaTrigger) ApplyResourceParameters(events map[string]*v1alpha1.E func (t *AWSLambdaTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { trigger, ok := resource.(*v1alpha1.AWSLambdaTrigger) if !ok { - return nil, errors.New("failed to interpret the trigger resource") + return nil, fmt.Errorf("failed to interpret the trigger resource") } if trigger.Payload == nil { - return nil, errors.New("payload parameters are not specified") + return nil, fmt.Errorf("payload parameters are not specified") } payload, err := triggers.ConstructPayload(events, trigger.Payload) @@ -133,7 +133,7 @@ func (t *AWSLambdaTrigger) ApplyPolicy(ctx context.Context, resource interface{} obj, ok := resource.(*lambda.InvokeOutput) if !ok { - return errors.New("failed to interpret the trigger resource") + return fmt.Errorf("failed to interpret the trigger resource") } p := policy.NewStatusPolicy(int(*obj.StatusCode), t.Trigger.Policy.Status.GetAllow()) diff --git a/sensors/triggers/azure-event-hubs/azure_event_hubs.go b/sensors/triggers/azure-event-hubs/azure_event_hubs.go index edbfc0b444..c8170bbd47 100644 --- a/sensors/triggers/azure-event-hubs/azure_event_hubs.go +++ b/sensors/triggers/azure-event-hubs/azure_event_hubs.go @@ -20,10 +20,8 @@ import ( "encoding/json" "fmt" - "github.com/pkg/errors" - "go.uber.org/zap" - eventhub "github.com/Azure/azure-event-hubs-go/v3" + "go.uber.org/zap" "github.com/argoproj/argo-events/common" "github.com/argoproj/argo-events/common/logging" @@ -100,12 +98,12 @@ func (t *AzureEventHubsTrigger) FetchResource(ctx context.Context) (interface{}, func (t *AzureEventHubsTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { fetchedResource, ok := resource.(*v1alpha1.AzureEventHubsTrigger) if !ok { - return nil, errors.New("failed to interpret the fetched trigger resource") + return nil, fmt.Errorf("failed to interpret the fetched trigger resource") } resourceBytes, err := json.Marshal(fetchedResource) if err != nil { - return nil, errors.Wrap(err, "failed to marshal the azure event hubs trigger resource") + return nil, fmt.Errorf("failed to marshal the azure event hubs trigger resource, %w", err) } parameters := fetchedResource.Parameters if parameters != nil { @@ -115,7 +113,7 @@ func (t *AzureEventHubsTrigger) ApplyResourceParameters(events map[string]*v1alp } var ht *v1alpha1.AzureEventHubsTrigger if err := json.Unmarshal(updatedResourceBytes, &ht); err != nil { - return nil, errors.Wrap(err, "failed to unmarshal the updated azure event hubs trigger resource after applying resource parameters") + return nil, fmt.Errorf("failed to unmarshal the updated azure event hubs trigger resource after applying resource parameters, %w", err) } return ht, nil } @@ -126,11 +124,11 @@ func (t *AzureEventHubsTrigger) ApplyResourceParameters(events map[string]*v1alp func (t *AzureEventHubsTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { trigger, ok := resource.(*v1alpha1.AzureEventHubsTrigger) if !ok { - return nil, errors.New("failed to interpret the trigger resource") + return nil, fmt.Errorf("failed to interpret the trigger resource") } if trigger.Payload == nil { - return nil, errors.New("payload parameters are not specified") + return nil, fmt.Errorf("payload parameters are not specified") } payload, err := triggers.ConstructPayload(events, trigger.Payload) diff --git a/sensors/triggers/custom-trigger/custom-trigger.go b/sensors/triggers/custom-trigger/custom-trigger.go index 35caa7215d..d222e7f88e 100644 --- a/sensors/triggers/custom-trigger/custom-trigger.go +++ b/sensors/triggers/custom-trigger/custom-trigger.go @@ -18,9 +18,9 @@ package customtrigger import ( "context" "encoding/json" + "fmt" "github.com/ghodss/yaml" - "github.com/pkg/errors" "go.uber.org/zap" "google.golang.org/grpc" "google.golang.org/grpc/connectivity" @@ -85,7 +85,7 @@ func NewCustomTrigger(sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, logger return nil, err } default: - return nil, errors.New("invalid config, CERT secret not defined") + return nil, fmt.Errorf("invalid config, CERT secret not defined") } creds, err := credentials.NewClientTLSFromFile(certFilePath, ct.ServerNameOverride) if err != nil { @@ -132,7 +132,7 @@ func (ct *CustomTrigger) GetTriggerType() apicommon.TriggerType { func (ct *CustomTrigger) FetchResource(ctx context.Context) (interface{}, error) { specBody, err := yaml.Marshal(ct.Trigger.Template.CustomTrigger.Spec) if err != nil { - return nil, errors.Wrap(err, "failed to parse the custom trigger spec body") + return nil, fmt.Errorf("failed to parse the custom trigger spec body, %w", err) } ct.Logger.Debugw("trigger spec body", zap.Any("spec", string(specBody))) @@ -141,7 +141,7 @@ func (ct *CustomTrigger) FetchResource(ctx context.Context) (interface{}, error) Resource: specBody, }) if err != nil { - return nil, errors.Wrapf(err, "failed to fetch the custom trigger resource for %s", ct.Trigger.Template.Name) + return nil, fmt.Errorf("failed to fetch the custom trigger resource for %s, %w", ct.Trigger.Template.Name, err) } ct.Logger.Debugw("fetched resource", zap.Any("resource", string(resource.Resource))) @@ -152,7 +152,7 @@ func (ct *CustomTrigger) FetchResource(ctx context.Context) (interface{}, error) func (ct *CustomTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { obj, ok := resource.([]byte) if !ok { - return nil, errors.New("failed to interpret the trigger resource for resource parameters application") + return nil, fmt.Errorf("failed to interpret the trigger resource for resource parameters application") } parameters := ct.Trigger.Template.CustomTrigger.Parameters @@ -160,12 +160,12 @@ func (ct *CustomTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Eve // only JSON formatted resource body is eligible for parameters var temp map[string]interface{} if err := json.Unmarshal(obj, &temp); err != nil { - return nil, errors.Wrapf(err, "fetched resource body is not valid JSON for trigger %s", ct.Trigger.Template.Name) + return nil, fmt.Errorf("fetched resource body is not valid JSON for trigger %s, %w", ct.Trigger.Template.Name, err) } result, err := triggers.ApplyParams(obj, ct.Trigger.Template.CustomTrigger.Parameters, events) if err != nil { - return nil, errors.Wrapf(err, "failed to apply the parameters to the custom trigger resource for %s", ct.Trigger.Template.Name) + return nil, fmt.Errorf("failed to apply the parameters to the custom trigger resource for %s, %w", ct.Trigger.Template.Name, err) } ct.Logger.Debugw("resource after parameterization", zap.Any("resource", string(result))) @@ -179,7 +179,7 @@ func (ct *CustomTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Eve func (ct *CustomTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { obj, ok := resource.([]byte) if !ok { - return nil, errors.New("failed to interpret the trigger resource for the execution") + return nil, fmt.Errorf("failed to interpret the trigger resource for the execution") } ct.Logger.Debugw("resource to execute", zap.Any("resource", string(obj))) @@ -203,7 +203,7 @@ func (ct *CustomTrigger) Execute(ctx context.Context, events map[string]*v1alpha Payload: payload, }) if err != nil { - return nil, errors.Wrapf(err, "failed to execute the custom trigger resource for %s", ct.Trigger.Template.Name) + return nil, fmt.Errorf("failed to execute the custom trigger resource for %s, %w", ct.Trigger.Template.Name, err) } ct.Logger.Debugw("trigger execution response", zap.Any("response", string(result.Response))) @@ -214,7 +214,7 @@ func (ct *CustomTrigger) Execute(ctx context.Context, events map[string]*v1alpha func (ct *CustomTrigger) ApplyPolicy(ctx context.Context, resource interface{}) error { obj, ok := resource.([]byte) if !ok { - return errors.New("failed to interpret the trigger resource for the policy application") + return fmt.Errorf("failed to interpret the trigger resource for the policy application") } ct.Logger.Debugw("resource to apply policy on", zap.Any("resource", string(obj))) @@ -223,7 +223,7 @@ func (ct *CustomTrigger) ApplyPolicy(ctx context.Context, resource interface{}) Request: obj, }) if err != nil { - return errors.Wrapf(err, "failed to apply the policy for the custom trigger resource for %s", ct.Trigger.Template.Name) + return fmt.Errorf("failed to apply the policy for the custom trigger resource for %s, %w", ct.Trigger.Template.Name, err) } ct.Logger.Infow("policy application result", zap.Any("success", result.Success), zap.Any("message", result.Message)) return err diff --git a/sensors/triggers/fetch.go b/sensors/triggers/fetch.go index a19cbfbcad..3b003c38fb 100644 --- a/sensors/triggers/fetch.go +++ b/sensors/triggers/fetch.go @@ -17,7 +17,8 @@ limitations under the License. package triggers import ( - "github.com/pkg/errors" + "fmt" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" @@ -26,7 +27,7 @@ import ( func FetchKubernetesResource(source *v1alpha1.ArtifactLocation) (*unstructured.Unstructured, error) { if source == nil { - return nil, errors.Errorf("trigger source for k8s is empty") + return nil, fmt.Errorf("trigger source for k8s is empty") } creds, err := artifacts.GetCredentials(source) if err != nil { diff --git a/sensors/triggers/http/http.go b/sensors/triggers/http/http.go index ebc3f8543b..e8b27f59a6 100644 --- a/sensors/triggers/http/http.go +++ b/sensors/triggers/http/http.go @@ -19,10 +19,10 @@ import ( "bytes" "context" "encoding/json" + "fmt" "net/http" "time" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -56,7 +56,7 @@ func NewHTTPTrigger(httpClients map[string]*http.Client, sensor *v1alpha1.Sensor if httptrigger.TLS != nil { tlsConfig, err := common.GetTLSConfig(httptrigger.TLS) if err != nil { - return nil, errors.Wrap(err, "failed to get the tls configuration") + return nil, fmt.Errorf("failed to get the tls configuration, %w", err) } client.Transport = &http.Transport{ TLSClientConfig: tlsConfig, @@ -98,12 +98,12 @@ func (t *HTTPTrigger) FetchResource(ctx context.Context) (interface{}, error) { func (t *HTTPTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { fetchedResource, ok := resource.(*v1alpha1.HTTPTrigger) if !ok { - return nil, errors.New("failed to interpret the fetched trigger resource") + return nil, fmt.Errorf("failed to interpret the fetched trigger resource") } resourceBytes, err := json.Marshal(fetchedResource) if err != nil { - return nil, errors.Wrap(err, "failed to marshal the http trigger resource") + return nil, fmt.Errorf("failed to marshal the http trigger resource, %w", err) } parameters := fetchedResource.Parameters if parameters != nil { @@ -113,7 +113,7 @@ func (t *HTTPTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, } var ht *v1alpha1.HTTPTrigger if err := json.Unmarshal(updatedResourceBytes, &ht); err != nil { - return nil, errors.Wrap(err, "failed to unmarshal the updated http trigger resource after applying resource parameters") + return nil, fmt.Errorf("failed to unmarshal the updated http trigger resource after applying resource parameters, %w", err) } return ht, nil } @@ -127,7 +127,7 @@ func (t *HTTPTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.E trigger, ok := resource.(*v1alpha1.HTTPTrigger) if !ok { - return nil, errors.New("failed to interpret the trigger resource") + return nil, fmt.Errorf("failed to interpret the trigger resource") } if (trigger.Method == http.MethodPost || trigger.Method == http.MethodPatch || trigger.Method == http.MethodPut) && trigger.Payload == nil { @@ -143,7 +143,7 @@ func (t *HTTPTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.E request, err := http.NewRequest(trigger.Method, trigger.URL, bytes.NewReader(payload)) if err != nil { - return nil, errors.Wrapf(err, "failed to construct request for %s", trigger.URL) + return nil, fmt.Errorf("failed to construct request for %s, %w", trigger.URL, err) } if trigger.Headers != nil { @@ -162,7 +162,7 @@ func (t *HTTPTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.E value, err = common.GetConfigMapFromVolume(secure.ValueFrom.ConfigMapKeyRef) } if err != nil { - return nil, errors.Wrap(err, "failed to retrieve the value for secureHeader") + return nil, fmt.Errorf("failed to retrieve the value for secureHeader, %w", err) } request.Header[secure.Name] = []string{value} } @@ -177,21 +177,21 @@ func (t *HTTPTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.E if basicAuth.Username != nil { username, err = common.GetSecretFromVolume(basicAuth.Username) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve the username") + return nil, fmt.Errorf("failed to retrieve the username, %w", err) } } if basicAuth.Password != nil { password, err = common.GetSecretFromVolume(basicAuth.Password) if !ok { - return nil, errors.Wrap(err, "failed to retrieve the password") + return nil, fmt.Errorf("failed to retrieve the password, %w", err) } } request.SetBasicAuth(username, password) } - t.Logger.Infow("making a http request...", zap.Any("url", trigger.URL)) + t.Logger.Infow("Making a http request...", zap.Any("url", trigger.URL)) return t.Client.Do(request) } @@ -203,7 +203,7 @@ func (t *HTTPTrigger) ApplyPolicy(ctx context.Context, resource interface{}) err } response, ok := resource.(*http.Response) if !ok { - return errors.New("failed to interpret the trigger execution response") + return fmt.Errorf("failed to interpret the trigger execution response") } p := policy.NewStatusPolicy(response.StatusCode, t.Trigger.Policy.Status.GetAllow()) diff --git a/sensors/triggers/kafka/kafka.go b/sensors/triggers/kafka/kafka.go index c826063334..5dcaac9cbd 100644 --- a/sensors/triggers/kafka/kafka.go +++ b/sensors/triggers/kafka/kafka.go @@ -18,11 +18,11 @@ package kafka import ( "context" "encoding/json" + "fmt" "strings" "time" "github.com/Shopify/sarama" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -59,7 +59,7 @@ func NewKafkaTrigger(sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, kafkaPr } else { version, err := sarama.ParseKafkaVersion(kafkatrigger.Version) if err != nil { - return nil, errors.Wrap(err, "failed to parse Kafka version") + return nil, fmt.Errorf("failed to parse Kafka version, %w", err) } config.Version = version } @@ -75,13 +75,13 @@ func NewKafkaTrigger(sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, kafkaPr user, err := common.GetSecretFromVolume(kafkatrigger.SASL.UserSecret) if err != nil { - return nil, errors.Wrap(err, "Error getting user value from secret") + return nil, fmt.Errorf("Error getting user value from secret, %w", err) } config.Net.SASL.User = user password, err := common.GetSecretFromVolume(kafkatrigger.SASL.PasswordSecret) if err != nil { - return nil, errors.Wrap(err, "Error getting password value from secret") + return nil, fmt.Errorf("Error getting password value from secret, %w", err) } config.Net.SASL.Password = password } @@ -89,7 +89,7 @@ func NewKafkaTrigger(sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, kafkaPr if kafkatrigger.TLS != nil { tlsConfig, err := common.GetTLSConfig(kafkatrigger.TLS) if err != nil { - return nil, errors.Wrap(err, "failed to get the tls configuration") + return nil, fmt.Errorf("failed to get the tls configuration, %w", err) } tlsConfig.InsecureSkipVerify = true config.Net.TLS.Config = tlsConfig @@ -151,12 +151,12 @@ func (t *KafkaTrigger) FetchResource(ctx context.Context) (interface{}, error) { func (t *KafkaTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { fetchedResource, ok := resource.(*v1alpha1.KafkaTrigger) if !ok { - return nil, errors.New("failed to interpret the fetched trigger resource") + return nil, fmt.Errorf("failed to interpret the fetched trigger resource") } resourceBytes, err := json.Marshal(fetchedResource) if err != nil { - return nil, errors.Wrap(err, "failed to marshal the kafka trigger resource") + return nil, fmt.Errorf("failed to marshal the kafka trigger resource, %w", err) } parameters := fetchedResource.Parameters if parameters != nil { @@ -166,7 +166,7 @@ func (t *KafkaTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event } var ht *v1alpha1.KafkaTrigger if err := json.Unmarshal(updatedResourceBytes, &ht); err != nil { - return nil, errors.Wrap(err, "failed to unmarshal the updated kafka trigger resource after applying resource parameters") + return nil, fmt.Errorf("failed to unmarshal the updated kafka trigger resource after applying resource parameters. %w", err) } return ht, nil } @@ -177,11 +177,11 @@ func (t *KafkaTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event func (t *KafkaTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { trigger, ok := resource.(*v1alpha1.KafkaTrigger) if !ok { - return nil, errors.New("failed to interpret the trigger resource") + return nil, fmt.Errorf("failed to interpret the trigger resource") } if trigger.Payload == nil { - return nil, errors.New("payload parameters are not specified") + return nil, fmt.Errorf("payload parameters are not specified") } payload, err := triggers.ConstructPayload(events, trigger.Payload) diff --git a/sensors/triggers/log/log.go b/sensors/triggers/log/log.go index 570d58a060..2278a61dd8 100644 --- a/sensors/triggers/log/log.go +++ b/sensors/triggers/log/log.go @@ -2,7 +2,7 @@ package log import ( "context" - "errors" + "fmt" "time" "go.uber.org/zap" @@ -39,7 +39,7 @@ func (t *LogTrigger) ApplyResourceParameters(_ map[string]*v1alpha1.Event, resou func (t *LogTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { log, ok := resource.(*v1alpha1.LogTrigger) if !ok { - return nil, errors.New("failed to interpret the fetched trigger resource") + return nil, fmt.Errorf("failed to interpret the fetched trigger resource") } if t.shouldLog(log) { for dependencyName, event := range events { diff --git a/sensors/triggers/nats/nats.go b/sensors/triggers/nats/nats.go index 49b523363c..994fd3dfcf 100644 --- a/sensors/triggers/nats/nats.go +++ b/sensors/triggers/nats/nats.go @@ -18,9 +18,9 @@ package nats import ( "context" "encoding/json" + "fmt" natslib "github.com/nats-io/nats.go" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -55,7 +55,7 @@ func NewNATSTrigger(sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, natsConn if natstrigger.TLS != nil { tlsConfig, err := common.GetTLSConfig(natstrigger.TLS) if err != nil { - return nil, errors.Wrap(err, "failed to get the tls configuration") + return nil, fmt.Errorf("failed to get the tls configuration, %w", err) } tlsConfig.InsecureSkipVerify = true opts.Secure = true @@ -93,12 +93,12 @@ func (t *NATSTrigger) FetchResource(ctx context.Context) (interface{}, error) { func (t *NATSTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { fetchedResource, ok := resource.(*v1alpha1.NATSTrigger) if !ok { - return nil, errors.New("failed to interpret the fetched trigger resource") + return nil, fmt.Errorf("failed to interpret the fetched trigger resource") } resourceBytes, err := json.Marshal(fetchedResource) if err != nil { - return nil, errors.Wrap(err, "failed to marshal the nats trigger resource") + return nil, fmt.Errorf("failed to marshal the nats trigger resource, %w", err) } parameters := fetchedResource.Parameters if parameters != nil { @@ -108,7 +108,7 @@ func (t *NATSTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, } var ht *v1alpha1.NATSTrigger if err := json.Unmarshal(updatedResourceBytes, &ht); err != nil { - return nil, errors.Wrap(err, "failed to unmarshal the updated nats trigger resource after applying resource parameters") + return nil, fmt.Errorf("failed to unmarshal the updated nats trigger resource after applying resource parameters, %w", err) } return ht, nil } @@ -119,11 +119,11 @@ func (t *NATSTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, func (t *NATSTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { trigger, ok := resource.(*v1alpha1.NATSTrigger) if !ok { - return nil, errors.New("failed to interpret the trigger resource") + return nil, fmt.Errorf("failed to interpret the trigger resource") } if trigger.Payload == nil { - return nil, errors.New("payload parameters are not specified") + return nil, fmt.Errorf("payload parameters are not specified") } payload, err := triggers.ConstructPayload(events, trigger.Payload) diff --git a/sensors/triggers/pulsar/pulsar.go b/sensors/triggers/pulsar/pulsar.go index 1d7046b6ae..a159c9d7e2 100644 --- a/sensors/triggers/pulsar/pulsar.go +++ b/sensors/triggers/pulsar/pulsar.go @@ -18,9 +18,9 @@ package pulsar import ( "context" "encoding/json" + "fmt" "github.com/apache/pulsar-client-go/pulsar" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/argoproj/argo-events/common" @@ -89,21 +89,21 @@ func NewPulsarTrigger(sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, pulsar return nil, err } default: - return nil, errors.New("invalid TLS config") + return nil, fmt.Errorf("invalid TLS config") } clientOpt.Authentication = pulsar.NewAuthenticationTLS(clientCertPath, clientKeyPath) } var client pulsar.Client - if err := common.Connect(pulsarTrigger.ConnectionBackoff, func() error { + if err := common.DoWithRetry(pulsarTrigger.ConnectionBackoff, func() error { var err error if client, err = pulsar.NewClient(clientOpt); err != nil { return err } return nil }); err != nil { - return nil, errors.Wrapf(err, "failed to connect to %s for sensor %s", pulsarTrigger.URL, trigger.Template.Name) + return nil, fmt.Errorf("failed to connect to %s for sensor %s, %w", pulsarTrigger.URL, trigger.Template.Name, err) } producer, err = client.CreateProducer(pulsar.ProducerOptions{ @@ -139,12 +139,12 @@ func (t *PulsarTrigger) FetchResource(ctx context.Context) (interface{}, error) func (t *PulsarTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { fetchedResource, ok := resource.(*v1alpha1.PulsarTrigger) if !ok { - return nil, errors.New("failed to interpret the fetched trigger resource") + return nil, fmt.Errorf("failed to interpret the fetched trigger resource") } resourceBytes, err := json.Marshal(fetchedResource) if err != nil { - return nil, errors.Wrap(err, "failed to marshal the pulsar trigger resource") + return nil, fmt.Errorf("failed to marshal the pulsar trigger resource, %w", err) } parameters := fetchedResource.Parameters @@ -155,7 +155,7 @@ func (t *PulsarTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Even } var ht *v1alpha1.PulsarTrigger if err := json.Unmarshal(updatedResourceBytes, &ht); err != nil { - return nil, errors.Wrap(err, "failed to unmarshal the updated pulsar trigger resource after applying resource parameters") + return nil, fmt.Errorf("failed to unmarshal the updated pulsar trigger resource after applying resource parameters, %w", err) } return ht, nil } @@ -166,11 +166,11 @@ func (t *PulsarTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Even func (t *PulsarTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { trigger, ok := resource.(*v1alpha1.PulsarTrigger) if !ok { - return nil, errors.New("failed to interpret the trigger resource") + return nil, fmt.Errorf("failed to interpret the trigger resource") } if trigger.Payload == nil { - return nil, errors.New("payload parameters are not specified") + return nil, fmt.Errorf("payload parameters are not specified") } payload, err := triggers.ConstructPayload(events, trigger.Payload) @@ -182,7 +182,7 @@ func (t *PulsarTrigger) Execute(ctx context.Context, events map[string]*v1alpha1 Payload: payload, }) if err != nil { - return nil, errors.Wrap(err, "failed to send message to pulsar") + return nil, fmt.Errorf("failed to send message to pulsar, %w", err) } t.Logger.Infow("successfully produced a message", zap.Any("topic", trigger.Topic)) diff --git a/sensors/triggers/pulsar/pulsar_test.go b/sensors/triggers/pulsar/pulsar_test.go index 810b9a57b2..53a6a4f385 100644 --- a/sensors/triggers/pulsar/pulsar_test.go +++ b/sensors/triggers/pulsar/pulsar_test.go @@ -17,11 +17,11 @@ package pulsar import ( "context" + "fmt" "testing" "github.com/apache/pulsar-client-go/pulsar" cloudevents "github.com/cloudevents/sdk-go/v2" - "github.com/pkg/errors" "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -49,7 +49,7 @@ func (m *mockPulsarProducer) Send(context.Context, *pulsar.ProducerMessage) (pul m.expected = false return nil, nil } - return nil, errors.New("input not expected") + return nil, fmt.Errorf("input not expected") } func (m *mockPulsarProducer) SendAsync(context.Context, *pulsar.ProducerMessage, func(pulsar.MessageID, *pulsar.ProducerMessage, error)) { diff --git a/sensors/triggers/slack/slack.go b/sensors/triggers/slack/slack.go index 56f4920022..adaee5da81 100644 --- a/sensors/triggers/slack/slack.go +++ b/sensors/triggers/slack/slack.go @@ -18,11 +18,11 @@ package slack import ( "context" "encoding/json" + "fmt" "net/http" "strings" "time" - "github.com/pkg/errors" "github.com/slack-go/slack" "go.uber.org/zap" @@ -66,7 +66,7 @@ func (t *SlackTrigger) FetchResource(ctx context.Context) (interface{}, error) { func (t *SlackTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { resourceBytes, err := json.Marshal(resource) if err != nil { - return nil, errors.Wrap(err, "failed to marshal the Slack trigger resource") + return nil, fmt.Errorf("failed to marshal the Slack trigger resource, %w", err) } parameters := t.Trigger.Template.Slack.Parameters @@ -78,7 +78,7 @@ func (t *SlackTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event var st *v1alpha1.SlackTrigger if err := json.Unmarshal(updatedResourceBytes, &st); err != nil { - return nil, errors.Wrap(err, "failed to unmarshal the updated Slack trigger resource after applying resource parameters") + return nil, fmt.Errorf("failed to unmarshal the updated Slack trigger resource after applying resource parameters, %w", err) } return st, nil @@ -92,25 +92,25 @@ func (t *SlackTrigger) Execute(ctx context.Context, events map[string]*v1alpha1. t.Logger.Info("executing SlackTrigger") _, ok := resource.(*v1alpha1.SlackTrigger) if !ok { - return nil, errors.New("failed to marshal the Slack trigger resource") + return nil, fmt.Errorf("failed to marshal the Slack trigger resource") } slacktrigger := t.Trigger.Template.Slack channel := slacktrigger.Channel if channel == "" { - return nil, errors.New("no slack channel provided") + return nil, fmt.Errorf("no slack channel provided") } channel = strings.TrimPrefix(channel, "#") message := slacktrigger.Message if message == "" { - return nil, errors.New("no slack message to post") + return nil, fmt.Errorf("no slack message to post") } slackToken, err := common.GetSecretFromVolume(slacktrigger.SlackToken) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve the slack token") + return nil, fmt.Errorf("failed to retrieve the slack token, %w", err) } api := slack.New(slackToken, slack.OptionDebug(false)) @@ -136,7 +136,7 @@ func (t *SlackTrigger) Execute(ctx context.Context, events map[string]*v1alpha1. continue default: t.Logger.Errorw("unable to list channels", zap.Error(err)) - return nil, errors.Wrapf(err, "failed to list channels") + return nil, fmt.Errorf("failed to list channels, %w", err) } } for _, c := range channels { @@ -152,22 +152,22 @@ func (t *SlackTrigger) Execute(ctx context.Context, events map[string]*v1alpha1. params.Cursor = nextCursor } if channelID == "" { - return nil, errors.Errorf("failed to get channelID of %s", channel) + return nil, fmt.Errorf("failed to get channelID of %s", channel) } if isPrivateChannel { - return nil, errors.Errorf("cannot join private channel %s", channel) + return nil, fmt.Errorf("cannot join private channel %s", channel) } c, _, _, err := api.JoinConversation(channelID) if err != nil { t.Logger.Errorw("unable to join channel...", zap.Any("channelName", channel), zap.Any("channelID", channelID), zap.Error(err)) - return nil, errors.Wrapf(err, "failed to join channel %s", channel) + return nil, fmt.Errorf("failed to join channel %s, %w", channel, err) } t.Logger.Debugw("successfully joined channel", zap.Any("channel", c)) continue } else { t.Logger.Errorw("unable to post to channel...", zap.Any("channelName", channel), zap.Error(err)) - return nil, errors.Wrapf(err, "failed to post to channel %s", channel) + return nil, fmt.Errorf("failed to post to channel %s, %w", channel, err) } } t.Logger.Infow("message successfully sent to channelID with timestamp", zap.Any("message", message), zap.Any("channelID", channelID), zap.Any("timestamp", timestamp)) diff --git a/sensors/triggers/standard-k8s/standard-k8s.go b/sensors/triggers/standard-k8s/standard-k8s.go index c15de218df..b8bed70d0e 100644 --- a/sensors/triggers/standard-k8s/standard-k8s.go +++ b/sensors/triggers/standard-k8s/standard-k8s.go @@ -23,7 +23,6 @@ import ( "time" "github.com/imdario/mergo" - "github.com/pkg/errors" "go.uber.org/zap" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -116,7 +115,7 @@ func (k8sTrigger *StandardK8sTrigger) FetchResource(ctx context.Context) (interf func (k8sTrigger *StandardK8sTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) { obj, ok := resource.(*unstructured.Unstructured) if !ok { - return nil, errors.New("failed to interpret the trigger resource") + return nil, fmt.Errorf("failed to interpret the trigger resource") } if err := triggers.ApplyResourceParameters(events, k8sTrigger.Trigger.Template.K8s.Parameters, obj); err != nil { return nil, err @@ -130,7 +129,7 @@ func (k8sTrigger *StandardK8sTrigger) Execute(ctx context.Context, events map[st obj, ok := resource.(*unstructured.Unstructured) if !ok { - return nil, errors.New("failed to interpret the trigger resource") + return nil, fmt.Errorf("failed to interpret the trigger resource") } gvr := triggers.GetGroupVersionResource(obj) @@ -176,11 +175,11 @@ func (k8sTrigger *StandardK8sTrigger) Execute(ctx context.Context, events map[st k8sTrigger.Logger.Info("object not found, creating the object...") return k8sTrigger.namespableDynamicClient.Namespace(namespace).Create(ctx, obj, metav1.CreateOptions{}) } else if err != nil { - return nil, errors.Errorf("failed to retrieve existing object. err: %+v\n", err) + return nil, fmt.Errorf("failed to retrieve existing object. err: %w", err) } if err := mergo.Merge(oldObj, obj, mergo.WithOverride); err != nil { - return nil, errors.Errorf("failed to update the object. err: %+v\n", err) + return nil, fmt.Errorf("failed to update the object. err: %w", err) } return k8sTrigger.namespableDynamicClient.Namespace(namespace).Update(ctx, oldObj, metav1.UpdateOptions{}) @@ -193,7 +192,7 @@ func (k8sTrigger *StandardK8sTrigger) Execute(ctx context.Context, events map[st k8sTrigger.Logger.Info("object not found, creating the object...") return k8sTrigger.namespableDynamicClient.Namespace(namespace).Create(ctx, obj, metav1.CreateOptions{}) } else if err != nil { - return nil, errors.Errorf("failed to retrieve existing object. err: %+v\n", err) + return nil, fmt.Errorf("failed to retrieve existing object. err: %w", err) } if k8sTrigger.Trigger.Template.K8s.PatchStrategy == "" { @@ -202,7 +201,7 @@ func (k8sTrigger *StandardK8sTrigger) Execute(ctx context.Context, events map[st body, err := obj.MarshalJSON() if err != nil { - return nil, errors.Errorf("failed to marshal object into JSON schema. err: %+v\n", err) + return nil, fmt.Errorf("failed to marshal object into JSON schema. err: %w", err) } return k8sTrigger.namespableDynamicClient.Namespace(namespace).Patch(ctx, obj.GetName(), k8sTrigger.Trigger.Template.K8s.PatchStrategy, body, metav1.PatchOptions{}) @@ -215,17 +214,17 @@ func (k8sTrigger *StandardK8sTrigger) Execute(ctx context.Context, events map[st k8sTrigger.Logger.Info("object not found, nothing to delete...") return nil, nil } else if err != nil { - return nil, errors.Errorf("failed to retrieve existing object. err: %+v\n", err) + return nil, fmt.Errorf("failed to retrieve existing object. err: %w", err) } err = k8sTrigger.namespableDynamicClient.Namespace(namespace).Delete(ctx, obj.GetName(), metav1.DeleteOptions{}) if err != nil { - return nil, errors.Errorf("failed to delete object. err: %+v\n", err) + return nil, fmt.Errorf("failed to delete object. err: %w", err) } return nil, nil default: - return nil, errors.Errorf("unknown operation type %s", string(op)) + return nil, fmt.Errorf("unknown operation type %s", string(op)) } } @@ -239,7 +238,7 @@ func (k8sTrigger *StandardK8sTrigger) ApplyPolicy(ctx context.Context, resource obj, ok := resource.(*unstructured.Unstructured) if !ok { - return errors.New("failed to interpret the trigger resource") + return fmt.Errorf("failed to interpret the trigger resource") } p := policy.NewResourceLabels(trigger, k8sTrigger.namespableDynamicClient, obj) @@ -252,7 +251,7 @@ func (k8sTrigger *StandardK8sTrigger) ApplyPolicy(ctx context.Context, resource switch err { case wait.ErrWaitTimeout: if trigger.Policy.K8s.ErrorOnBackoffTimeout { - return errors.Errorf("failed to determine status of the triggered resource. setting trigger state as failed") + return fmt.Errorf("failed to determine status of the triggered resource. setting trigger state as failed") } return nil default: diff --git a/test/e2e/functional_test.go b/test/e2e/functional_test.go index 249aa139d4..8e451a8842 100644 --- a/test/e2e/functional_test.go +++ b/test/e2e/functional_test.go @@ -22,12 +22,12 @@ type FunctionalSuite struct { const ( LogEventSourceStarted = "Eventing server started." LogSensorStarted = "Sensor started." - LogPublishEventSuccessful = "succeeded to publish an event" - LogTriggerActionFailed = "failed to execute a trigger" + LogPublishEventSuccessful = "Succeeded to publish an event" + LogTriggerActionFailed = "Failed to execute a trigger" ) func LogTriggerActionSuccessful(triggerName string) string { - return fmt.Sprintf("successfully processed trigger '%s'", triggerName) + return fmt.Sprintf("Successfully processed trigger '%s'", triggerName) } func (s *FunctionalSuite) e(baseURL string) *httpexpect.Expect { diff --git a/test/stress/main.go b/test/stress/main.go index cf7d107c8a..4a98910a13 100644 --- a/test/stress/main.go +++ b/test/stress/main.go @@ -58,10 +58,10 @@ const ( logEventSourceStarted = "Eventing server started." logSensorStarted = "Sensor started." - logTriggerActionSuccessful = "successfully processed trigger" - logTriggerActionFailed = "failed to execute a trigger" - logEventSuccessful = "succeeded to publish an event" - logEventFailed = "failed to publish an event" + logTriggerActionSuccessful = "Successfully processed trigger" + logTriggerActionFailed = "Failed to execute a trigger" + logEventSuccessful = "Succeeded to publish an event" + logEventFailed = "Failed to publish an event" ) type TestingEventSource string diff --git a/test/util/util.go b/test/util/util.go index 17f13d121a..0b990f9d3c 100644 --- a/test/util/util.go +++ b/test/util/util.go @@ -5,16 +5,13 @@ import ( "context" "fmt" "regexp" + "sync" "time" - appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" - eventbusv1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1" - eventsourcev1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1" - sensorv1alpha1 "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" eventbuspkg "github.com/argoproj/argo-events/pkg/client/eventbus/clientset/versioned/typed/eventbus/v1alpha1" eventsourcepkg "github.com/argoproj/argo-events/pkg/client/eventsource/clientset/versioned/typed/eventsource/v1alpha1" sensorpkg "github.com/argoproj/argo-events/pkg/client/sensor/clientset/versioned/typed/sensor/v1alpha1" @@ -23,127 +20,66 @@ import ( func WaitForEventBusReady(ctx context.Context, eventBusClient eventbuspkg.EventBusInterface, eventBusName string, timeout time.Duration) error { fieldSelector := "metadata.name=" + eventBusName opts := metav1.ListOptions{FieldSelector: fieldSelector} - watch, err := eventBusClient.Watch(ctx, opts) - if err != nil { - return err - } - defer watch.Stop() - timeoutCh := make(chan bool, 1) - go func() { - time.Sleep(timeout) - timeoutCh <- true - }() + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() for { select { - case event := <-watch.ResultChan(): - eb, ok := event.Object.(*eventbusv1alpha1.EventBus) - if ok { - if eb.Status.IsReady() { - return nil - } - } else { - return fmt.Errorf("not eventbus") - } - case <-timeoutCh: + case <-ctx.Done(): return fmt.Errorf("timeout after %v waiting for EventBus ready", timeout) + default: } + ebList, err := eventBusClient.List(ctx, opts) + if err != nil { + return fmt.Errorf("error getting EventBus list: %w", err) + } + if len(ebList.Items) > 0 && ebList.Items[0].Status.IsReady() { + return nil + } + time.Sleep(1 * time.Second) } } func WaitForEventBusStatefulSetReady(ctx context.Context, kubeClient kubernetes.Interface, namespace, eventBusName string, timeout time.Duration) error { labelSelector := fmt.Sprintf("controller=eventbus-controller,eventbus-name=%s", eventBusName) opts := metav1.ListOptions{LabelSelector: labelSelector} - watch, err := kubeClient.AppsV1().StatefulSets(namespace).Watch(ctx, opts) - if err != nil { - return err - } - defer watch.Stop() - timeoutCh := make(chan bool, 1) - go func() { - time.Sleep(timeout) - timeoutCh <- true - }() - -statefulSetWatch: + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() for { select { - case event := <-watch.ResultChan(): - ss, ok := event.Object.(*appsv1.StatefulSet) - if ok { - if ss.Status.Replicas == ss.Status.ReadyReplicas { - break statefulSetWatch - } - } else { - return fmt.Errorf("not statefulset") - } - case <-timeoutCh: + case <-ctx.Done(): return fmt.Errorf("timeout after %v waiting for EventBus StatefulSet ready", timeout) + default: } - } - - // POD - podWatch, err := kubeClient.CoreV1().Pods(namespace).Watch(ctx, opts) - if err != nil { - return err - } - defer podWatch.Stop() - podTimeoutCh := make(chan bool, 1) - go func() { - time.Sleep(timeout) - podTimeoutCh <- true - }() - - podNames := make(map[string]bool) - for { - if len(podNames) == 3 { - // defaults to 3 Pods - return nil + stsList, err := kubeClient.AppsV1().StatefulSets(namespace).List(ctx, opts) + if err != nil { + return fmt.Errorf("error getting EventBus StatefulSet list: %w", err) } - select { - case event := <-podWatch.ResultChan(): - p, ok := event.Object.(*corev1.Pod) - if ok { - if p.Status.Phase == corev1.PodRunning { - if _, existing := podNames[p.GetName()]; !existing { - podNames[p.GetName()] = true - } - } - } else { - return fmt.Errorf("not pod") - } - case <-podTimeoutCh: - return fmt.Errorf("timeout after %v waiting for event bus Pod ready", timeout) + if len(stsList.Items) > 0 && stsList.Items[0].Status.Replicas == stsList.Items[0].Status.ReadyReplicas { + return nil } + time.Sleep(1 * time.Second) } } func WaitForEventSourceReady(ctx context.Context, eventSourceClient eventsourcepkg.EventSourceInterface, eventSourceName string, timeout time.Duration) error { fieldSelector := "metadata.name=" + eventSourceName opts := metav1.ListOptions{FieldSelector: fieldSelector} - watch, err := eventSourceClient.Watch(ctx, opts) - if err != nil { - return err - } - defer watch.Stop() - timeoutCh := make(chan bool, 1) - go func() { - time.Sleep(timeout) - timeoutCh <- true - }() + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() for { select { - case event := <-watch.ResultChan(): - es, ok := event.Object.(*eventsourcev1alpha1.EventSource) - if ok { - if es.Status.IsReady() { - return nil - } - } else { - return fmt.Errorf("not eventsource") - } - case <-timeoutCh: + case <-ctx.Done(): return fmt.Errorf("timeout after %v waiting for EventSource ready", timeout) + default: } + esList, err := eventSourceClient.List(ctx, opts) + if err != nil { + return fmt.Errorf("error getting EventSource list: %w", err) + } + if len(esList.Items) > 0 && esList.Items[0].Status.IsReady() { + return nil + } + time.Sleep(1 * time.Second) } } @@ -155,30 +91,22 @@ func WaitForEventSourceDeploymentReady(ctx context.Context, kubeClient kubernete func WaitForSensorReady(ctx context.Context, sensorClient sensorpkg.SensorInterface, sensorName string, timeout time.Duration) error { fieldSelector := "metadata.name=" + sensorName opts := metav1.ListOptions{FieldSelector: fieldSelector} - watch, err := sensorClient.Watch(ctx, opts) - if err != nil { - return err - } - defer watch.Stop() - timeoutCh := make(chan bool, 1) - go func() { - time.Sleep(timeout) - timeoutCh <- true - }() + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() for { select { - case event := <-watch.ResultChan(): - s, ok := event.Object.(*sensorv1alpha1.Sensor) - if ok { - if s.Status.IsReady() { - return nil - } - } else { - return fmt.Errorf("not sensor") - } - case <-timeoutCh: + case <-ctx.Done(): return fmt.Errorf("timeout after %v waiting for Sensor ready", timeout) + default: } + sensorList, err := sensorClient.List(ctx, opts) + if err != nil { + return fmt.Errorf("error getting Sensor list: %w", err) + } + if len(sensorList.Items) > 0 && sensorList.Items[0].Status.IsReady() { + return nil + } + time.Sleep(1 * time.Second) } } @@ -189,59 +117,35 @@ func WaitForSensorDeploymentReady(ctx context.Context, kubeClient kubernetes.Int func waitForDeploymentAndPodReady(ctx context.Context, kubeClient kubernetes.Interface, namespace, objectType, labelSelector string, timeout time.Duration) error { opts := metav1.ListOptions{LabelSelector: labelSelector} - deployWatch, err := kubeClient.AppsV1().Deployments(namespace).Watch(ctx, opts) - if err != nil { - return err - } - defer deployWatch.Stop() - deployTimeoutCh := make(chan bool, 1) - go func() { - time.Sleep(timeout) - deployTimeoutCh <- true - }() - -deployWatch: + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() for { select { - case event := <-deployWatch.ResultChan(): - ss, ok := event.Object.(*appsv1.Deployment) - if ok { - if ss.Status.Replicas == ss.Status.AvailableReplicas { - break deployWatch - } - } else { - return fmt.Errorf("not deployment") - } - case <-deployTimeoutCh: - return fmt.Errorf("timeout after %v waiting for %s Deployment ready", timeout, objectType) + case <-ctx.Done(): + return fmt.Errorf("timeout after %v waiting for deployment ready", timeout) + default: } - } - - // POD - podWatch, err := kubeClient.CoreV1().Pods(namespace).Watch(ctx, opts) - if err != nil { - return err - } - defer podWatch.Stop() - podTimeoutCh := make(chan bool, 1) - go func() { - time.Sleep(timeout) - podTimeoutCh <- true - }() - for { - select { - case event := <-podWatch.ResultChan(): - p, ok := event.Object.(*corev1.Pod) - if ok { - if p.Status.Phase == corev1.PodRunning { - return nil - } - } else { - return fmt.Errorf("not Pod") - } - case <-podTimeoutCh: - return fmt.Errorf("timeout after %v waiting for %s Pod ready", timeout, objectType) + deployList, err := kubeClient.AppsV1().Deployments(namespace).List(ctx, opts) + if err != nil { + return fmt.Errorf("error getting deployment list: %w", err) } + ok := len(deployList.Items) == 1 + if !ok { + continue + } + ok = ok && deployList.Items[0].Status.Replicas == deployList.Items[0].Status.ReadyReplicas + podList, err := kubeClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{LabelSelector: labelSelector, FieldSelector: "status.phase=Running"}) + if err != nil { + return fmt.Errorf("error getting deployment pod list: %w", err) + } + ok = ok && len(podList.Items) > 0 && len(podList.Items) == int(*deployList.Items[0].Spec.Replicas) + for _, p := range podList.Items { + ok = ok && p.Status.Phase == corev1.PodRunning + } + if ok { + return nil + } + time.Sleep(1 * time.Second) } } @@ -304,8 +208,11 @@ func PodsLogContains(ctx context.Context, kubeClient kubernetes.Interface, names defer cancel() errChan := make(chan error) resultChan := make(chan bool) + wg := &sync.WaitGroup{} for _, p := range podList.Items { + wg.Add(1) go func(podName string) { + defer wg.Done() fmt.Printf("Watching POD: %s\n", podName) var contains bool var err error @@ -323,7 +230,11 @@ func PodsLogContains(ctx context.Context, kubeClient kubernetes.Interface, names } }(p.Name) } - + allDone := make(chan bool) + go func() { + wg.Wait() + close(allDone) + }() for { select { case result := <-resultChan: @@ -334,6 +245,13 @@ func PodsLogContains(ctx context.Context, kubeClient kubernetes.Interface, names } case err := <-errChan: fmt.Printf("error: %v", err) + case <-allDone: + for len(resultChan) > 0 { + if x := <-resultChan; x { + return true + } + } + return false } } } diff --git a/webhook/validator/validator.go b/webhook/validator/validator.go index f9363b27af..003bccc1d5 100644 --- a/webhook/validator/validator.go +++ b/webhook/validator/validator.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" - "github.com/pkg/errors" admissionv1 "k8s.io/api/admission/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -90,7 +89,7 @@ func GetValidator(ctx context.Context, client kubernetes.Interface, ebClient eve } return NewSensorValidator(client, ebClient, esClient, sensorClient, old, new), nil default: - return nil, errors.Errorf("unrecognized GVK %v", kind) + return nil, fmt.Errorf("unrecognized GVK %v", kind) } } diff --git a/webhook/webhook.go b/webhook/webhook.go index 6b5b6412de..950979ec18 100644 --- a/webhook/webhook.go +++ b/webhook/webhook.go @@ -13,7 +13,6 @@ import ( "time" "github.com/go-openapi/inflect" - "github.com/pkg/errors" "go.uber.org/zap" admissionv1 "k8s.io/api/admission/v1" admissionregistrationv1 "k8s.io/api/admissionregistration/v1" @@ -122,7 +121,7 @@ func (ac *AdmissionController) Run(ctx context.Context) error { case <-ctx.Done(): return server.Close() case <-serverStartErrCh: - return errors.New("webhook server failed to start") + return fmt.Errorf("webhook server failed to start") } } @@ -186,7 +185,7 @@ func (ac *AdmissionController) register( } clusterRole, err := ac.Client.RbacV1().ClusterRoles().Get(ctx, ac.Options.ClusterRoleName, metav1.GetOptions{}) if err != nil { - return errors.Wrapf(err, "failed to fetch webhook cluster role") + return fmt.Errorf("failed to fetch webhook cluster role, %w", err) } clusterRoleRef := metav1.NewControllerRef(clusterRole, rbacv1.SchemeGroupVersion.WithKind("ClusterRole")) webhook.OwnerReferences = append(webhook.OwnerReferences, *clusterRoleRef) @@ -194,19 +193,19 @@ func (ac *AdmissionController) register( _, err = client.Create(ctx, webhook, metav1.CreateOptions{}) if err != nil { if !apierrors.IsAlreadyExists(err) { - return errors.Wrap(err, "failed to create a webhook") + return fmt.Errorf("failed to create a webhook, %w", err) } ac.Logger.Info("Webhook already exists") configuredWebhook, err := client.Get(ctx, ac.Options.WebhookName, metav1.GetOptions{}) if err != nil { - return errors.Wrap(err, "failed to retrieve webhook") + return fmt.Errorf("failed to retrieve webhook, %w", err) } if !reflect.DeepEqual(configuredWebhook.Webhooks, webhook.Webhooks) { ac.Logger.Info("Updating webhook") // Set the ResourceVersion as required by update. webhook.ObjectMeta.ResourceVersion = configuredWebhook.ObjectMeta.ResourceVersion if _, err := client.Update(ctx, webhook, metav1.UpdateOptions{}); err != nil { - return errors.Wrap(err, "failed to update webhook") + return fmt.Errorf("failed to update webhook, %w", err) } } else { ac.Logger.Info("Webhook is valid") @@ -298,7 +297,7 @@ func (ac *AdmissionController) generateSecret(ctx context.Context) (*corev1.Secr } deployment, err := ac.Client.AppsV1().Deployments(ac.Options.Namespace).Get(ctx, ac.Options.DeploymentName, metav1.GetOptions{}) if err != nil { - return nil, errors.Wrapf(err, "Failed to fetch webhook deployment") + return nil, fmt.Errorf("Failed to fetch webhook deployment, %w", err) } deploymentRef := metav1.NewControllerRef(deployment, appsv1.SchemeGroupVersion.WithKind("Deployment")) secret := &corev1.Secret{ @@ -341,13 +340,13 @@ func (ac *AdmissionController) getOrGenerateKeyCertsFromSecret(ctx context.Conte var ok bool if serverKey, ok = secret.Data[secretServerKey]; !ok { - return nil, nil, nil, errors.New("server key missing") + return nil, nil, nil, fmt.Errorf("server key missing") } if serverCert, ok = secret.Data[secretServerCert]; !ok { - return nil, nil, nil, errors.New("server cert missing") + return nil, nil, nil, fmt.Errorf("server cert missing") } if caCert, ok = secret.Data[secretCACert]; !ok { - return nil, nil, nil, errors.New("ca cert missing") + return nil, nil, nil, fmt.Errorf("ca cert missing") } return serverKey, serverCert, caCert, nil } @@ -364,7 +363,7 @@ func (ac *AdmissionController) getAPIServerExtensionCACert(ctx context.Context) const caFileName = "requestheader-client-ca-file" pem, ok := c.Data[caFileName] if !ok { - return nil, errors.Errorf("cannot find %s in ConfigMap %s", caFileName, name) + return nil, fmt.Errorf("cannot find %s in ConfigMap %s", caFileName, name) } return []byte(pem), nil } diff --git a/webhook/webhook_test.go b/webhook/webhook_test.go index 67237b4d3d..5b66780dc2 100644 --- a/webhook/webhook_test.go +++ b/webhook/webhook_test.go @@ -7,7 +7,6 @@ import ( "net" "testing" - "github.com/pkg/errors" "github.com/stretchr/testify/assert" admissionv1 "k8s.io/api/admission/v1" admissionregistrationv1 "k8s.io/api/admissionregistration/v1" @@ -139,7 +138,7 @@ func createWebhook(ac *AdmissionController, wh *admissionregistrationv1.Validati client := ac.Client.AdmissionregistrationV1().ValidatingWebhookConfigurations() _, err := client.Create(context.TODO(), wh, metav1.CreateOptions{}) if err != nil { - panic(errors.Wrap(err, "failed to create test webhook: %s")) + panic(fmt.Errorf("failed to create test webhook, %w", err)) } }