From 9919e932328f7487f91068d624fd87f58196ba85 Mon Sep 17 00:00:00 2001 From: Eugene Sumin <95425330+e-sumin@users.noreply.github.com> Date: Wed, 26 Jun 2024 10:58:49 +0200 Subject: [PATCH] Errkit migration 3 (pkg/app) (#2892) Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- pkg/app/bp.go | 3 +- pkg/app/cassandra.go | 24 ++++++------ pkg/app/cockroachdb.go | 51 ++++++++++++------------ pkg/app/couchbase.go | 30 +++++++------- pkg/app/csi-snapshot.go | 8 ++-- pkg/app/elasticsearch.go | 22 +++++------ pkg/app/foundationdb.go | 26 ++++++------- pkg/app/kafka.go | 54 +++++++++++++------------- pkg/app/mariadb.go | 26 ++++++------- pkg/app/mongodb-deploymentconfig.go | 14 +++---- pkg/app/mongodb.go | 22 +++++------ pkg/app/mssql.go | 18 ++++----- pkg/app/mysql-deploymentconfig.go | 18 ++++----- pkg/app/mysql.go | 26 ++++++------- pkg/app/postgresql-deploymentconfig.go | 24 ++++++------ pkg/app/postgresql.go | 24 ++++++------ pkg/app/rds_aurora_mysql.go | 52 ++++++++++++------------- pkg/app/rds_postgres.go | 44 ++++++++++----------- pkg/app/utils.go | 13 ++++--- 19 files changed, 251 insertions(+), 248 deletions(-) diff --git a/pkg/app/bp.go b/pkg/app/bp.go index 2dfbabf87a..b9f5a96d4a 100644 --- a/pkg/app/bp.go +++ b/pkg/app/bp.go @@ -19,11 +19,12 @@ import ( "strings" "time" + "k8s.io/apimachinery/pkg/util/rand" + crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" bp "github.com/kanisterio/kanister/pkg/blueprint" "github.com/kanisterio/kanister/pkg/field" "github.com/kanisterio/kanister/pkg/log" - "k8s.io/apimachinery/pkg/util/rand" ) const ( diff --git a/pkg/app/cassandra.go b/pkg/app/cassandra.go index d14e034f28..644da64f42 100644 --- a/pkg/app/cassandra.go +++ b/pkg/app/cassandra.go @@ -21,7 +21,7 @@ import ( "strings" "time" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "k8s.io/client-go/kubernetes" crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" @@ -86,7 +86,7 @@ func (cas *CassandraInstance) Install(ctx context.Context, namespace string) err log.Print("Installing application.", field.M{"app": cas.name}) cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } err = cli.AddRepo(ctx, cas.chart.RepoName, cas.chart.RepoURL) if err != nil { @@ -129,11 +129,11 @@ func (cas *CassandraInstance) Uninstall(ctx context.Context) error { log.Print("Uninstalling application.", field.M{"app": cas.name}) cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } err = cli.Uninstall(ctx, cas.chart.Release, cas.namespace) if err != nil { - return errors.Wrapf(err, "Error uninstalling cassandra app.") + return errkit.Wrap(err, "Error uninstalling cassandra app.") } log.Print("Application was uninstalled successfully.", field.M{"app": cas.name}) return nil @@ -150,7 +150,7 @@ func (cas *CassandraInstance) Ping(ctx context.Context) error { pingCMD := []string{"sh", "-c", "cqlsh -u cassandra -p $CASSANDRA_PASSWORD -e 'describe cluster'"} _, stderr, err := cas.execCommand(ctx, pingCMD) if err != nil { - return errors.Wrapf(err, "Error %s while pinging the database.", stderr) + return errkit.Wrap(err, "Error while pinging the database.", "stderr", stderr) } log.Print("Ping to the application was successful.", field.M{"app": cas.name}) return nil @@ -164,7 +164,7 @@ func (cas *CassandraInstance) Insert(ctx context.Context) error { "'2015-02-18');\" --request-timeout=%s", cqlTimeout)} _, stderr, err := cas.execCommand(ctx, insertCMD) if err != nil { - return errors.Wrapf(err, "Error %s inserting records into the database.", stderr) + return errkit.Wrap(err, "Error inserting records into the database.", "stderr", stderr) } return nil } @@ -174,7 +174,7 @@ func (cas *CassandraInstance) Count(ctx context.Context) (int, error) { countCMD := []string{"sh", "-c", "cqlsh -u cassandra -p $CASSANDRA_PASSWORD -e \"select count(*) from restaurants.guests;\" "} stdout, stderr, err := cas.execCommand(ctx, countCMD) if err != nil { - return 0, errors.Wrapf(err, "Error %s counting the number of records in the database.", stderr) + return 0, errkit.Wrap(err, "Error counting the number of records in the database.", "stderr", stderr) } // parse stdout to get the number of rows in the table // the count output from cqlsh is in below format @@ -185,7 +185,7 @@ func (cas *CassandraInstance) Count(ctx context.Context) (int, error) { count, err := strconv.Atoi(strings.Trim(strings.Split(stdout, "\n")[2], " ")) if err != nil { - return 0, errors.Wrapf(err, "Error, converting count value into int.") + return 0, errkit.Wrap(err, "Error, converting count value into int.") } log.Print("Counted number of records in the database.", field.M{"app": cas.name, "count": count}) return count, nil @@ -198,7 +198,7 @@ func (cas *CassandraInstance) Reset(ctx context.Context) error { "'drop table if exists restaurants.guests; drop keyspace if exists restaurants;' --request-timeout=%s", cqlTimeout)} _, stderr, err := cas.execCommand(ctx, delRes) if err != nil { - return errors.Wrapf(err, "Error %s, deleting resources while reseting application.", stderr) + return errkit.Wrap(err, "Error deleting resources while reseting application.", "stderr", stderr) } return nil } @@ -210,7 +210,7 @@ func (cas *CassandraInstance) Initialize(ctx context.Context) error { "restaurants with replication = {'class':'SimpleStrategy', 'replication_factor': 1};\" --request-timeout=%s", cqlTimeout)} _, stderr, err := cas.execCommand(ctx, createKS) if err != nil { - return errors.Wrapf(err, "Error %s while creating the keyspace for application.", stderr) + return errkit.Wrap(err, "Error while creating the keyspace for application.", "stderr", stderr) } // create the table @@ -218,7 +218,7 @@ func (cas *CassandraInstance) Initialize(ctx context.Context) error { "restaurants.guests (id UUID primary key, firstname text, lastname text, birthday timestamp);\" --request-timeout=%s", cqlTimeout)} _, stderr, err = cas.execCommand(ctx, createTab) if err != nil { - return errors.Wrapf(err, "Error %s creating table.", stderr) + return errkit.Wrap(err, "Error creating table.", "stderr", stderr) } return nil } @@ -226,7 +226,7 @@ func (cas *CassandraInstance) Initialize(ctx context.Context) error { func (cas *CassandraInstance) execCommand(ctx context.Context, command []string) (string, string, error) { podname, containername, err := kube.GetPodContainerFromStatefulSet(ctx, cas.cli, cas.namespace, cas.chart.Release) if err != nil || podname == "" { - return "", "", errors.Wrapf(err, "Error getting the pod and container name %s.", cas.name) + return "", "", errkit.Wrap(err, "Error getting the pod and container name.", "app", cas.name) } return kube.Exec(ctx, cas.cli, cas.namespace, podname, containername, command, nil) } diff --git a/pkg/app/cockroachdb.go b/pkg/app/cockroachdb.go index ea7027f93c..384dc7925a 100644 --- a/pkg/app/cockroachdb.go +++ b/pkg/app/cockroachdb.go @@ -7,7 +7,7 @@ import ( "strings" "time" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -63,15 +63,15 @@ func (c *CockroachDB) Install(ctx context.Context, namespace string) error { //n cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } if err = cli.AddRepo(ctx, c.chart.RepoName, c.chart.RepoURL); err != nil { - return errors.Wrapf(err, "Failed to install helm repo. app=%s repo=%s", c.name, c.chart.RepoName) + return errkit.Wrap(err, "Failed to install helm repo.", "app", c.name, "repo", c.chart.RepoName) } _, err = cli.Install(ctx, fmt.Sprintf("%s/%s", c.chart.RepoName, c.chart.Chart), c.chart.Version, c.chart.Release, c.namespace, c.chart.Values, false, false) - return errors.Wrapf(err, "Failed to install helm chart. app=%s chart=%s release=%s", c.name, c.chart.Chart, c.chart.Release) + return errkit.Wrap(err, "Failed to install helm chart.", "app", c.name, "chart", c.chart.Chart, "release", c.chart.Release) } func (c *CockroachDB) IsReady(ctx context.Context) (bool, error) { @@ -92,23 +92,24 @@ func (c *CockroachDB) IsReady(ctx context.Context) (bool, error) { // cluster, and executing queries like Creating Database and Table, // Inserting Data, Setting up Garbage Collection Time, // Delete Database etc - secret, err := c.cli.CoreV1().Secrets(c.namespace).Get(ctx, fmt.Sprintf("%s-client-secret", c.chart.Release), metav1.GetOptions{}) + secretName := fmt.Sprintf("%s-client-secret", c.chart.Release) + secret, err := c.cli.CoreV1().Secrets(c.namespace).Get(ctx, secretName, metav1.GetOptions{}) if err != nil { return false, err } if _, exist := secret.Data["ca.crt"]; !exist { - return false, errors.Errorf("Error: ca.crt not found in the cluster credential %s-client-secret", c.chart.Release) + return false, errkit.New("Error: ca.crt not found in the cluster credential", "secret", secretName) } c.cacrt = string(secret.Data["ca.crt"]) if _, exist := secret.Data["tls.crt"]; !exist { - return false, errors.Errorf("Error: tls.crt not found in the cluster credential %s-client-secret", c.chart.Release) + return false, errkit.New("Error: tls.crt not found in the cluster credential", "secret", secretName) } c.tlscrt = string(secret.Data["tls.crt"]) if _, exist := secret.Data["tls.key"]; !exist { - return false, errors.Errorf("Error: tls.key not found in the cluster credential %s-client-secret", c.chart.Release) + return false, errkit.New("Error: tls.key not found in the cluster credential", "secret", secretName) } c.tlskey = string(secret.Data["tls.key"]) @@ -116,42 +117,42 @@ func (c *CockroachDB) IsReady(ctx context.Context) (bool, error) { createCrtDir := []string{"sh", "-c", createCrtDirCmd} _, stderr, err := c.execCommand(ctx, createCrtDir) if err != nil { - return false, errors.Wrapf(err, "Error while Creating Cert Directory %s", stderr) + return false, errkit.Wrap(err, "Error while Creating Cert Directory", "stderr", stderr) } createCaCrtCmd := fmt.Sprintf("echo '%s' >> /cockroach/cockroach-client-certs/ca.crt", c.cacrt) createCaCrt := []string{"sh", "-c", createCaCrtCmd} _, stderr, err = c.execCommand(ctx, createCaCrt) if err != nil { - return false, errors.Wrapf(err, "Error while Creating ca.crt %s", stderr) + return false, errkit.Wrap(err, "Error while Creating ca.crt", "stderr", stderr) } createTlsCrtCmd := fmt.Sprintf("echo '%s'>> /cockroach/cockroach-client-certs/client.root.crt", c.tlscrt) createTlsCrt := []string{"sh", "-c", createTlsCrtCmd} _, stderr, err = c.execCommand(ctx, createTlsCrt) if err != nil { - return false, errors.Wrapf(err, "Error while Creating tls.crt %s", stderr) + return false, errkit.Wrap(err, "Error while Creating tls.crt", "stderr", stderr) } createTlsKeyCmd := fmt.Sprintf("echo '%s' >> /cockroach/cockroach-client-certs/client.root.key", c.tlskey) createTlsKey := []string{"sh", "-c", createTlsKeyCmd} _, stderr, err = c.execCommand(ctx, createTlsKey) if err != nil { - return false, errors.Wrapf(err, "Error while Creating tls.key %s", stderr) + return false, errkit.Wrap(err, "Error while Creating tls.key", "stderr", stderr) } changeFilePermCmd := "cd /cockroach/cockroach-client-certs/ && chmod 0600 *" changeFilePerm := []string{"sh", "-c", changeFilePermCmd} _, stderr, err = c.execCommand(ctx, changeFilePerm) if err != nil { - return false, errors.Wrapf(err, "Error while changing certificate file permissions %s", stderr) + return false, errkit.Wrap(err, "Error while changing certificate file permissions", "stderr", stderr) } changeDefaultGCTimeCmd := "./cockroach sql --certs-dir=/cockroach/cockroach-client-certs -e 'ALTER RANGE default CONFIGURE ZONE USING gc.ttlseconds = 10;'" changeDefaultGCTime := []string{"sh", "-c", changeDefaultGCTimeCmd} _, stderr, err = c.execCommand(ctx, changeDefaultGCTime) if err != nil { - return false, errors.Wrapf(err, "Error while setting up Garbage Collection time %s", stderr) + return false, errkit.Wrap(err, "Error while setting up Garbage Collection time", "stderr", stderr) } return err == nil, err @@ -168,7 +169,7 @@ func (c *CockroachDB) Object() crv1alpha1.ObjectReference { func (c *CockroachDB) Uninstall(ctx context.Context) error { cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } err = cli.Uninstall(ctx, c.chart.Release, c.namespace) if err != nil { @@ -191,7 +192,7 @@ func (c *CockroachDB) Ping(ctx context.Context) error { login := []string{"sh", "-c", loginCmd} _, stderr, err := c.execCommand(ctx, login) if err != nil { - return errors.Wrapf(err, "Error while pinging database %s", stderr) + return errkit.Wrap(err, "Error while pinging database", "stderr", stderr) } log.Print("Ping to the application was success.", field.M{"app": c.name}) @@ -204,7 +205,7 @@ func (c *CockroachDB) Initialize(ctx context.Context) error { createDatabase := []string{"sh", "-c", createDatabaseCMD} _, stderr, err := c.execCommand(ctx, createDatabase) if err != nil { - return errors.Wrapf(err, "Error while initializing: %s", stderr) + return errkit.Wrap(err, "Error while initializing", "stderr", stderr) } return nil } @@ -216,7 +217,7 @@ func (c *CockroachDB) Insert(ctx context.Context) error { insertRecord := []string{"sh", "-c", insertRecordCMD} _, stderr, err := c.execCommand(ctx, insertRecord) if err != nil { - return errors.Wrapf(err, "Error while inserting the data into database: %s", stderr) + return errkit.Wrap(err, "Error while inserting the data into database", "stderr", stderr) } log.Print("Successfully inserted records in the application.", field.M{"app": c.name}) @@ -230,13 +231,13 @@ func (c *CockroachDB) Count(ctx context.Context) (int, error) { selectRows := []string{"sh", "-c", selectRowsCMD} stdout, stderr, err := c.execCommand(ctx, selectRows) if err != nil { - return 0, errors.Wrapf(err, "Error while counting the data of the database: %s", stderr) + return 0, errkit.Wrap(err, "Error while counting the data of the database", "stderr", stderr) } // output returned from above query is "count\n3" // get the returned count and convert it to int, to return rowsReturned, err := strconv.Atoi(strings.Split(stdout, "\n")[1]) if err != nil { - return 0, errors.Wrapf(err, "Error while converting row count to int.") + return 0, errkit.Wrap(err, "Error while converting row count to int.") } log.Print("Count that we received from application is.", field.M{"app": c.name, "count": rowsReturned}) return rowsReturned, nil @@ -251,7 +252,7 @@ func (c *CockroachDB) Reset(ctx context.Context) error { }) if err != nil { - return errors.Wrapf(err, "Error waiting for application %s to be ready to reset it", c.name) + return errkit.Wrap(err, "Error waiting for application to be ready to reset it", "app", c.name) } log.Print("Resetting the cockroachdb instance.", field.M{"app": "cockroachdb"}) @@ -261,7 +262,7 @@ func (c *CockroachDB) Reset(ctx context.Context) error { deleteFromTable := []string{"sh", "-c", deleteFromTableCMD} _, stderr, err := c.execCommand(ctx, deleteFromTable) if err != nil { - return errors.Wrapf(err, "Error while dropping the table: %s", stderr) + return errkit.Wrap(err, "Error while dropping the table", "stderr", stderr) } // Even though the table is deleted from the database, it's present in the // descriptor table. We will have to wait for it to be deleted from there as @@ -293,7 +294,7 @@ func (c *CockroachDB) Secrets() map[string]crv1alpha1.ObjectReference { func (c *CockroachDB) execCommand(ctx context.Context, command []string) (string, string, error) { podName, containerName, err := kube.GetPodContainerFromStatefulSet(ctx, c.cli, c.namespace, c.chart.Release) if err != nil || podName == "" { - return "", "", errors.Wrapf(err, "Error getting pod and container name %s.", c.name) + return "", "", errkit.Wrap(err, "Error getting pod and container name.", "app", c.name) } return kube.Exec(ctx, c.cli, c.namespace, podName, containerName, command, nil) } @@ -304,12 +305,12 @@ func (c *CockroachDB) waitForGC(ctx context.Context) error { getDescriptor := []string{"sh", "-c", getDescriptorCMD} stdout, stderr, err := c.execCommand(ctx, getDescriptor) if err != nil { - return errors.Wrapf(err, "Error while getiing descriptor table data: %s", stderr) + return errkit.Wrap(err, "Error while getiing descriptor table data", "stderr", stderr) } bankInDescriptor := strings.Contains(stdout, "bank") || strings.Contains(stdout, "account") log.Info().Print("bankInDescriptor: ", field.M{"value": bankInDescriptor}) if bankInDescriptor { - return errors.New("Bank Database exists. Waiting for garbage collector to run and remove the database") + return errkit.New("Bank Database exists. Waiting for garbage collector to run and remove the database") } return nil } diff --git a/pkg/app/couchbase.go b/pkg/app/couchbase.go index 515768d94a..29fafbf623 100644 --- a/pkg/app/couchbase.go +++ b/pkg/app/couchbase.go @@ -22,7 +22,7 @@ import ( "time" "github.com/google/uuid" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -87,17 +87,17 @@ func (cb *CouchbaseDB) Install(ctx context.Context, ns string) error { //nolint: // Create helm client cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } // Add helm repo and fetch charts if err = cli.AddRepo(ctx, cb.chart.RepoName, cb.chart.RepoURL); err != nil { - return errors.Wrapf(err, "Failed to install helm repo. app=%s repo=%s", cb.name, cb.chart.RepoName) + return errkit.Wrap(err, "Failed to install helm repo.", "app", cb.name, "repo", cb.chart.RepoName) } // Install cb operator, admission controller and cluster _, err = cli.Install(ctx, fmt.Sprintf("%s/%s", cb.chart.RepoName, cb.chart.Chart), cb.chart.Version, cb.chart.Release, cb.namespace, cb.chart.Values, true, false) - return errors.Wrapf(err, "Failed to install helm chart. app=%s chart=%s release=%s", cb.name, cb.chart.Chart, cb.chart.Release) + return errkit.Wrap(err, "Failed to install helm chart.", "app", cb.name, "chart", cb.chart.Chart, "release", cb.chart.Release) } func (cb *CouchbaseDB) IsReady(ctx context.Context) (bool, error) { @@ -144,7 +144,7 @@ func (cb *CouchbaseDB) Ping(ctx context.Context) error { cmd := fmt.Sprintf("cbc-ping -u %s -P %s -c %d", cb.username, cb.password, numPings) _, stderr, err := cb.execCommand(ctx, []string{"sh", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to ping couchbase DB. %s", stderr) + return errkit.Wrap(err, "Failed to ping couchbase DB", "stderr", stderr) } log.Info().Print("Connected to database.", field.M{"app": cb.name}) return nil @@ -160,7 +160,7 @@ func (cb CouchbaseDB) Insert(ctx context.Context) error { cmd := fmt.Sprintf("cbc-create -u %s -P %s %s -V '{\"name\":\"test\", \"age\": 25}'", cb.username, cb.password, uuid.New().String()) _, stderr, err := cb.execCommand(ctx, []string{"sh", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to add document in couchbase default bucket. %s", stderr) + return errkit.Wrap(err, "Failed to add document in couchbase default bucket", "stderr", stderr) } // We'll wait till count correct result @@ -177,7 +177,7 @@ func (cb CouchbaseDB) Count(ctx context.Context) (int, error) { cmd := fmt.Sprintf("cbc-n1ql -u %s -P %s 'select count(*) from default'", cb.username, cb.password) stdout, stderr, err := cb.execCommand(ctx, []string{"sh", "-c", cmd}) if err != nil { - return 0, errors.Wrapf(err, "Failed to count db entries in couchbase. %s ", stderr) + return 0, errkit.Wrap(err, "Failed to count db entries in couchbase", "stderr", stderr) } // Parse output @@ -201,7 +201,7 @@ func (cb CouchbaseDB) Count(ctx context.Context) (int, error) { count, err := strconv.Atoi(matched[0][1]) if err != nil { - return 0, errors.Wrapf(err, "Failed to count db entries in couchbase. %s ", stderr) + return 0, errkit.Wrap(err, "Failed to count db entries in couchbase", "stderr", stderr) } log.Info().Print("Counting rows in test db.", field.M{"app": cb.name, "count": count}) return count, nil @@ -215,14 +215,14 @@ func (cb CouchbaseDB) Reset(ctx context.Context) error { cmd := fmt.Sprintf("cbc-n1ql -u %s -P %s 'create primary index on default'", cb.username, cb.password) _, stderr, err := cb.execCommand(ctx, []string{"sh", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to create index on default. %s app=%s", stderr, cb.name) + return errkit.Wrap(err, "Failed to create index on default", "stderr", stderr, "app", cb.name) } // Delete all documents cmd = fmt.Sprintf("cbc-n1ql -u %s -P %s 'delete from default'", cb.username, cb.password) _, stderr, err = cb.execCommand(ctx, []string{"sh", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to delete documents from default bucket. %s app=%s", stderr, cb.name) + return errkit.Wrap(err, "Failed to delete documents from default bucket", "stderr", stderr, "app", cb.name) } // We'll wait till count returns zero @@ -238,13 +238,13 @@ func (cb CouchbaseDB) Uninstall(ctx context.Context) error { // Create helm client cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } // Uninstall couchbase-operator helm chart log.Info().Print("Uninstalling helm charts.", field.M{"app": cb.name, "release": cb.chart.Release, "namespace": cb.namespace}) err = cli.Uninstall(ctx, cb.chart.Release, cb.namespace) - return errors.Wrapf(err, "Failed to uninstall %s helm release", cb.chart.Release) + return errkit.Wrap(err, "Failed to uninstall helm release", "release", cb.chart.Release) } func (cb CouchbaseDB) GetClusterScopedResources(ctx context.Context) []crv1alpha1.ObjectReference { @@ -274,10 +274,10 @@ func (cb CouchbaseDB) getRunningCBPod() (string, error) { } if len(pod.Status.ContainerStatuses) == 0 { - return "", errors.New(fmt.Sprintf("Could not find ready pod. name=%s namespace=%s", podName, cb.namespace)) + return "", errkit.New("Could not find ready pod.", "name", podName, "namespace", cb.namespace) } if !pod.Status.ContainerStatuses[0].Ready { - return "", errors.New(fmt.Sprintf("Could not find ready pod. name=%s namespace=%s", podName, cb.namespace)) + return "", errkit.New("Could not find ready pod.", podName, cb.namespace) } return pod.GetName(), nil @@ -292,5 +292,5 @@ func (cb CouchbaseDB) waitForCount(ctx context.Context, result int) error { count, err := cb.Count(ctx) return count == result, err }) - return errors.Wrapf(err, "Timed out while waiting for Couchbase cluster to be in sync") + return errkit.Wrap(err, "Timed out while waiting for Couchbase cluster to be in sync") } diff --git a/pkg/app/csi-snapshot.go b/pkg/app/csi-snapshot.go index d2bd00c779..447422c658 100644 --- a/pkg/app/csi-snapshot.go +++ b/pkg/app/csi-snapshot.go @@ -17,7 +17,7 @@ package app import ( "context" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -86,7 +86,7 @@ func (tlc *TimeLogCSI) Reset(ctx context.Context) error { removeLogFileCmd := []string{"sh", "-c", "rm /var/log/time.log"} stderr, err := tlc.execCommand(ctx, removeLogFileCmd) if err != nil { - return errors.Wrapf(err, "Error while deleting log file: %s", stderr) + return errkit.Wrap(err, "Error while deleting log file", "stderr", stderr) } log.Print("Reset of the application was successful.", field.M{"app": tlc.name}) @@ -133,7 +133,7 @@ func (tlc *TimeLogCSI) Ping(ctx context.Context) error { listDirectories := []string{"sh", "-c", "ls /var/log"} stderr, err := tlc.execCommand(ctx, listDirectories) if err != nil { - return errors.Wrapf(err, "Error while Pinging the application %s", stderr) + return errkit.Wrap(err, "Error while Pinging the application", "stderr", stderr) } log.Print("Ping to the application was success.", field.M{"app": tlc.name}) @@ -167,7 +167,7 @@ func (tlc *TimeLogCSI) GetClusterScopedResources(ctx context.Context) []crv1alph func (tlc *TimeLogCSI) execCommand(ctx context.Context, command []string) (string, error) { podname, containername, err := kube.GetPodContainerFromDeployment(ctx, tlc.cli, tlc.namespace, tlc.name) if err != nil || podname == "" { - return "", errors.Wrapf(err, "Error getting pod and containername %s.", tlc.name) + return "", errkit.Wrap(err, "Error getting pod and containername.", "deployment", tlc.name) } _, stderr, err := kube.Exec(ctx, tlc.cli, tlc.namespace, podname, containername, command, nil) return stderr, err diff --git a/pkg/app/elasticsearch.go b/pkg/app/elasticsearch.go index ce0168cdf8..5e3c4d7964 100644 --- a/pkg/app/elasticsearch.go +++ b/pkg/app/elasticsearch.go @@ -20,7 +20,7 @@ import ( "fmt" "time" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "k8s.io/client-go/kubernetes" crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" @@ -90,7 +90,7 @@ func (esi *ElasticsearchInstance) Install(ctx context.Context, namespace string) // Get the HELM cli cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } log.Print("Installing the application using helm.", field.M{"app": esi.name}) @@ -132,13 +132,13 @@ func (esi *ElasticsearchInstance) Object() crv1alpha1.ObjectReference { func (esi *ElasticsearchInstance) Uninstall(ctx context.Context) error { cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } log.Print("UnInstalling the application using helm.", field.M{"app": esi.name}) err = cli.Uninstall(ctx, esi.chart.Release, esi.namespace) if err != nil { - return errors.Wrapf(err, "Error uninstalling the application %s", esi.name) + return errkit.Wrap(err, "Error uninstalling the application", "app", esi.name) } return nil @@ -164,7 +164,7 @@ func (esi *ElasticsearchInstance) Ping(ctx context.Context) error { pingCMD := []string{"sh", "-c", esi.curlCommand("GET", "")} _, stderr, err := esi.execCommand(ctx, pingCMD) if err != nil { - return errors.Wrapf(err, "Failed to ping the application. Error:%s", stderr) + return errkit.Wrap(err, "Failed to ping the application", "stderr", stderr) } log.Print("Ping to the application was successful.", field.M{"app": esi.name}) @@ -175,8 +175,8 @@ func (esi *ElasticsearchInstance) Insert(ctx context.Context) error { _, stderr, err := esi.execCommand(ctx, addDocumentToIndexCMD) if err != nil { // even one insert failed we will have to return because - // the count wont match anyway and the test will fail - return errors.Wrapf(err, "Error %s inserting document to an index %s.", stderr, esi.indexname) + // the count won't match anyway and the test will fail + return errkit.Wrap(err, "Error inserting document to an index.", "stderr", stderr, "index", esi.indexname) } log.Print("A document was inserted into the elastics search index.", field.M{"app": esi.name}) @@ -187,7 +187,7 @@ func (esi *ElasticsearchInstance) Count(ctx context.Context) (int, error) { documentCountCMD := []string{"sh", "-c", esi.curlCommand("GET", esi.indexname+"/_search?pretty")} stdout, stderr, err := esi.execCommand(ctx, documentCountCMD) if err != nil { - return 0, errors.Wrapf(err, "Error %s Counting the documents in an index.", stderr) + return 0, errkit.Wrap(err, "Error Counting the documents in an index.", "stderr", stderr) } // convert the output to ElasticsearchPingOutput object so that we can get the document count @@ -208,7 +208,7 @@ func (esi *ElasticsearchInstance) Reset(ctx context.Context) error { deleteIndexCMD := []string{"sh", "-c", esi.curlCommand("DELETE", esi.indexname+"/?pretty")} _, stderr, err := esi.execCommand(ctx, deleteIndexCMD) if err != nil { - return errors.Wrapf(err, "Error %s while deleting the index %s to reset the application.", stderr, esi.indexname) + return errkit.Wrap(err, "Error while deleting the index to reset the application.", "stderr", stderr, "index", esi.indexname) } return nil @@ -220,7 +220,7 @@ func (esi *ElasticsearchInstance) Initialize(ctx context.Context) error { createIndexCMD := []string{"sh", "-c", esi.curlCommand("PUT", esi.indexname+"/?pretty")} _, stderr, err := esi.execCommand(ctx, createIndexCMD) if err != nil { - return errors.Wrapf(err, "Error %s: Resetting the application.", stderr) + return errkit.Wrap(err, "Error Resetting the application.", "stderr", stderr) } return nil } @@ -228,7 +228,7 @@ func (esi *ElasticsearchInstance) Initialize(ctx context.Context) error { func (esi *ElasticsearchInstance) execCommand(ctx context.Context, command []string) (string, string, error) { podname, containername, err := kube.GetPodContainerFromStatefulSet(ctx, esi.cli, esi.namespace, fmt.Sprintf("%s-master", esi.name)) if err != nil || podname == "" { - return "", "", errors.Wrapf(err, "Error getting the pod and container name %s.", esi.name) + return "", "", errkit.Wrap(err, "Error getting the pod and container name.", "app", esi.name) } return kube.Exec(ctx, esi.cli, esi.namespace, podname, containername, command, nil) } diff --git a/pkg/app/foundationdb.go b/pkg/app/foundationdb.go index 7d7fbba660..f8088d4832 100644 --- a/pkg/app/foundationdb.go +++ b/pkg/app/foundationdb.go @@ -21,7 +21,7 @@ import ( "time" "github.com/google/uuid" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -78,7 +78,7 @@ func (fdb *FoundationDB) Install(ctx context.Context, namespace string) error { helmVersion, err := helm.FindVersion() if err != nil { - return errors.Wrapf(err, "Couldn't find the helm version.") + return errkit.Wrap(err, "Couldn't find the helm version.") } var oprARG, instARG []string @@ -93,12 +93,12 @@ func (fdb *FoundationDB) Install(ctx context.Context, namespace string) error { out, err := helm.RunCmdWithTimeout(ctx, helm.GetHelmBinName(), oprARG) if err != nil { - return errors.Wrapf(err, "Error installing the operator for %s, error is %s.", fdb.name, out) + return errkit.Wrap(err, "Error installing the operator.", "app", fdb.name, "out", out) } out, err = helm.RunCmdWithTimeout(ctx, helm.GetHelmBinName(), instARG) if err != nil { - return errors.Wrapf(err, "Error installing the application %s, error is %s", fdb.name, out) + return errkit.Wrap(err, "Error installing the application", "app", fdb.name, "out", out) } log.Print("Application was installed successfully.", field.M{"app": fdb.name}) @@ -140,13 +140,13 @@ func (fdb *FoundationDB) Uninstall(ctx context.Context) error { unintFDB := []string{"delete", "-n", fdb.namespace, fdb.fdbReleaseName} out, err := helm.RunCmdWithTimeout(ctx, helm.GetHelmBinName(), unintFDB) if err != nil { - return errors.Wrapf(err, "Error uninstalling the fdb instance. Error=%s", out) + return errkit.Wrap(err, "Error uninstalling the fdb instance.", "out", out) } uninstOpr := []string{"delete", "-n", fdb.namespace, fdb.oprReleaseName} out, err = helm.RunCmdWithTimeout(ctx, helm.GetHelmBinName(), uninstOpr) - return errors.Wrapf(err, "Error uninstalling the operator. Error=%s", out) + return errkit.Wrap(err, "Error uninstalling the operator.", "out", out) } func (fdb *FoundationDB) GetClusterScopedResources(ctx context.Context) []crv1alpha1.ObjectReference { @@ -163,10 +163,10 @@ func (fdb *FoundationDB) getRunningFDBPod() (string, error) { } if len(pod.Status.ContainerStatuses) == 0 { - return "", errors.New(fmt.Sprintf("Couldn't find ready pod. name=%s namespace=%s", fdb.name, fdb.namespace)) + return "", errkit.New("Couldn't find ready pod.", "name", fdb.name, "namespace", fdb.namespace) } if !pod.Status.ContainerStatuses[0].Ready { - return "", errors.New(fmt.Sprintf("Couldn't find ready pod. name=%s namespace=%s", fdb.name, fdb.namespace)) + return "", errkit.New("Couldn't find ready pod.", "name", fdb.name, "namespace", fdb.namespace) } return pod.GetName(), nil @@ -179,7 +179,7 @@ func (fdb *FoundationDB) Ping(ctx context.Context) error { pingCMD := []string{"sh", "-c", "fdbcli"} stdout, stderr, err := fdb.execCommand(ctx, pingCMD) if err != nil { - return errors.Wrapf(err, "Error while pinging the database. Application %s, Err %s", fdb.name, stderr) + return errkit.Wrap(err, "Error while pinging the database.", "app", fdb.name, "stderr", stderr) } // This is how we get the output of fdbcli @@ -188,7 +188,7 @@ func (fdb *FoundationDB) Ping(ctx context.Context) error { // Welcome to the fdbcli. For help, type `help'. if !strings.Contains(stdout, "The database is available") { - return errors.New(fmt.Sprintf("Database %s is still not ready.", fdb.name)) + return errkit.New("Database is still not ready.", "name", fdb.name) } return nil @@ -200,7 +200,7 @@ func (fdb *FoundationDB) Insert(ctx context.Context) error { insertCMD := []string{"sh", "-c", fmt.Sprintf("fdbcli --exec 'writemode on; set %s vivek; '", uuid.New())} _, stderr, err := fdb.execCommand(ctx, insertCMD) - return errors.Wrapf(err, "Error %s inserting data into the database.", stderr) + return errkit.Wrap(err, "Error inserting data into the database.", "stderr", stderr) } // Count is used to count the number of records @@ -208,7 +208,7 @@ func (fdb *FoundationDB) Count(ctx context.Context) (int, error) { countCMD := []string{"sh", "-c", "fdbcli --exec \"getrangekeys '' \xFF \""} stdout, stderr, err := fdb.execCommand(ctx, countCMD) if err != nil { - return 0, errors.Wrapf(err, "Error %s counting the records in the %s database.", stderr, fdb.name) + return 0, errkit.Wrap(err, "Error counting the records in the database.", "app", fdb.name, "stderr", stderr) } // Below is how we get the output of getrangekeys @@ -226,7 +226,7 @@ func (fdb *FoundationDB) Reset(ctx context.Context) error { resetCMD := []string{"sh", "-c", "fdbcli --exec \"writemode on; clearrange '' \xFF\" "} stdout, stderr, err := fdb.execCommand(ctx, resetCMD) - return errors.Wrapf(err, "Error %s resetting the database %s. stdout=%s", stderr, fdb.name, stdout) + return errkit.Wrap(err, "Error resetting the database.", "stderr", stderr, "app", fdb.name, "stdout", stdout) } // Initialize is used to initialize the database or create schema diff --git a/pkg/app/kafka.go b/pkg/app/kafka.go index 0a4cdb4661..512647ca39 100644 --- a/pkg/app/kafka.go +++ b/pkg/app/kafka.go @@ -24,7 +24,7 @@ import ( "net/url" "time" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" @@ -111,11 +111,11 @@ func (kc *KafkaCluster) Init(context.Context) error { } kc.cli, err = kubernetes.NewForConfig(cfg) if err != nil { - return errors.Wrap(err, "failed to get a k8s client") + return errkit.Wrap(err, "failed to get a k8s client") } kc.dynClient, err = dynamic.NewForConfig(cfg) if err != nil { - return errors.Wrap(err, "failed to get a k8s dynamic client") + return errkit.Wrap(err, "failed to get a k8s dynamic client") } return nil } @@ -124,17 +124,17 @@ func (kc *KafkaCluster) Install(ctx context.Context, namespace string) error { kc.namespace = namespace cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } log.Print("Adding repo.", field.M{"app": kc.name}) err = cli.AddRepo(ctx, kc.chart.RepoName, kc.chart.RepoURL) if err != nil { - return errors.Wrapf(err, "Error adding helm repo for app %s.", kc.name) + return errkit.Wrap(err, "Error adding helm repo for app.", "app", kc.name) } log.Print("Installing kafka operator using helm.", field.M{"app": kc.name}) _, err = cli.Install(ctx, kc.chart.RepoName+"/"+kc.chart.Chart, kc.chart.Version, kc.chart.Release, kc.namespace, kc.chart.Values, true, false) if err != nil { - return errors.Wrapf(err, "Error installing operator %s through helm.", kc.name) + return errkit.Wrap(err, "Error installing operator through helm.", "app", kc.name) } createKafka := []string{ "create", @@ -143,7 +143,7 @@ func (kc *KafkaCluster) Install(ctx context.Context, namespace string) error { } out, err := helm.RunCmdWithTimeout(ctx, "kubectl", createKafka) if err != nil { - return errors.Wrapf(err, "Error installing the application %s, %s", kc.name, out) + return errkit.Wrap(err, "Error installing the application", "app", kc.name, "out", out) } createConfig := []string{ "create", @@ -156,7 +156,7 @@ func (kc *KafkaCluster) Install(ctx context.Context, namespace string) error { } out, err = helm.RunCmdWithTimeout(ctx, "kubectl", createConfig) if err != nil { - return errors.Wrapf(err, "Error creating ConfigMap %s, %s", kc.name, out) + return errkit.Wrap(err, "Error creating ConfigMap", "app", kc.name, "out", out) } createKafkaBridge := []string{ "create", @@ -166,7 +166,7 @@ func (kc *KafkaCluster) Install(ctx context.Context, namespace string) error { out, err = helm.RunCmdWithTimeout(ctx, "kubectl", createKafkaBridge) if err != nil { - return errors.Wrapf(err, "Error installing the application %s, %s", kc.name, out) + return errkit.Wrap(err, "Error installing the application", "app", kc.name, "out", out) } log.Print("Application was installed successfully.", field.M{"app": kc.name}) return nil @@ -310,12 +310,12 @@ func (kc *KafkaCluster) Secrets() map[string]crv1alpha1.ObjectReference { func (kc *KafkaCluster) Uninstall(ctx context.Context) error { cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } err = kc.cli.CoreV1().ConfigMaps(kc.namespace).Delete(ctx, configMapName, metav1.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { - return errors.Wrapf(err, "Error deleting ConfigMap %s", configMapName) + return errkit.Wrap(err, "Error deleting ConfigMap", "configMap", configMapName) } err = cli.Uninstall(ctx, kc.chart.Release, kc.namespace) @@ -357,7 +357,7 @@ func (kc *KafkaCluster) Ping(ctx context.Context) error { } out, err := helm.RunCmdWithTimeout(ctx, "kubectl", pingKafka) if err != nil { - return errors.Wrapf(err, "Error Pinging the app for %s, %s.", kc.name, out) + return errkit.Wrap(err, "Error Pinging the app", "app", kc.name, "out", out) } log.Print("Ping to the application was successful.") return nil @@ -368,7 +368,7 @@ func (kc *KafkaCluster) Insert(ctx context.Context) error { err := kc.InsertRecord(ctx, kc.namespace) if err != nil { - return errors.Wrapf(err, "Error inserting the record for %s", kc.name) + return errkit.Wrap(err, "Error inserting the record for", "app", kc.name) } log.Print("Successfully inserted record in the application.", field.M{"app": kc.name}) @@ -408,7 +408,7 @@ func (kc *KafkaCluster) Count(ctx context.Context) (int, error) { count, err := consumeTopic(ctx, kc.namespace) if err != nil { - return 0, errors.Wrapf(err, "Error counting the records for %s, %s.", kc.name, err) + return 0, errkit.Wrap(err, "Error counting the records.", "app", kc.name) } log.Print("Count that we received from application is.", field.M{"app": kc.name, "count": count}) @@ -498,7 +498,7 @@ func (kc *KafkaCluster) InsertRecord(ctx context.Context, namespace string) erro return err } if resp.StatusCode != 200 { - return errors.New("Error inserting records in topic") + return errkit.New("Error inserting records in topic") } defer resp.Body.Close() bytes, err := io.ReadAll(resp.Body) @@ -516,37 +516,37 @@ func K8SServicePortForward(ctx context.Context, svcName string, ns string, pPort cfg, err := kube.LoadConfig() if err != nil { - return nil, errors.Wrapf(err, "Failed to Load config") + return nil, errkit.Wrap(err, "Failed to Load config") } clientset, err := kubernetes.NewForConfig(cfg) if err != nil { - return nil, errors.Wrapf(err, "Failed to create Clienset for k8s config") + return nil, errkit.Wrap(err, "Failed to create Clienset for k8s config") } roundTripper, upgrader, err := spdy.RoundTripperFor(cfg) if err != nil { - return nil, errors.Wrapf(err, "Failed to create RoundTripper for k8s config") + return nil, errkit.Wrap(err, "Failed to create RoundTripper for k8s config") } svc, err := clientset.CoreV1().Services(ns).Get(ctx, svcName, metav1.GetOptions{}) if err != nil { - return nil, errors.Wrapf(err, "Failed to get service for component") + return nil, errkit.Wrap(err, "Failed to get service for component") } pods, err := clientset.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{ LabelSelector: metav1.FormatLabelSelector(metav1.SetAsLabelSelector(svc.Spec.Selector)), }) if err != nil { - return nil, errors.Wrapf(err, "Failed to list pods for component") + return nil, errkit.Wrap(err, "Failed to list pods for component") } if len(pods.Items) == 0 { - return nil, errors.Wrapf(err, "Empty pods list for component") + return nil, errkit.Wrap(err, "Empty pods list for component") } path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", ns, pods.Items[0].Name) u, err := url.Parse(cfg.Host) if err != nil { - return nil, errors.Wrapf(err, "Failed to parse url struct from k8s config") + return nil, errkit.Wrap(err, "Failed to parse url struct from k8s config") } hostIP := fmt.Sprintf("%s:%s", u.Hostname(), u.Port()) serverURL := url.URL{Scheme: "https", Path: path, Host: hostIP} @@ -570,7 +570,7 @@ func K8SServicePortForward(ctx context.Context, svcName string, ns string, pPort f, err := portforward.New(dialer, []string{fmt.Sprintf(":%s", pPort)}, ctx.Done(), readyChan, pwo, pwe) if err != nil { - return nil, errors.Wrapf(err, "Failed to create port forward") + return nil, errkit.Wrap(err, "Failed to create port forward") } go func() { @@ -581,7 +581,7 @@ func K8SServicePortForward(ctx context.Context, svcName string, ns string, pPort case <-readyChan: log.Print("PortForward is Ready") case err = <-errCh: - return nil, errors.Wrapf(err, "Failed to get ports from forwarded ports") + return nil, errkit.Wrap(err, "Failed to get ports from forwarded ports") } return f, nil @@ -615,7 +615,7 @@ func createConsumerGroup(uri string) error { if resp.StatusCode != 200 && resp.StatusCode != 409 { // we are checking if consumer is already present and if not present it should be created - return errors.New("Error creating consumer") + return errkit.New("Error creating consumer") } defer resp.Body.Close() bytes, err := io.ReadAll(resp.Body) @@ -648,7 +648,7 @@ func subscribe(uri string) error { return err } if resp.StatusCode != 204 { - return errors.New("Error subscribing to the topic") + return errkit.New("Error subscribing to the topic") } defer resp.Body.Close() bytes, err := io.ReadAll(resp.Body) @@ -669,7 +669,7 @@ func consumeMessage(uri string) (int, error) { resp, err := http.DefaultClient.Do(req) if resp.StatusCode != 200 { - return 0, errors.New("Error consuming records from topic") + return 0, errkit.New("Error consuming records from topic") } if err != nil { return 0, err diff --git a/pkg/app/mariadb.go b/pkg/app/mariadb.go index 086fd37493..cf12d2bb51 100644 --- a/pkg/app/mariadb.go +++ b/pkg/app/mariadb.go @@ -21,7 +21,7 @@ import ( "strings" "time" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "k8s.io/client-go/kubernetes" crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" @@ -78,18 +78,18 @@ func (m *MariaDB) Install(ctx context.Context, namespace string) error { //nolin m.namespace = namespace cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } log.Print("Adding repo.", field.M{"app": m.name}) err = cli.AddRepo(ctx, m.chart.RepoName, m.chart.RepoURL) if err != nil { - return errors.Wrapf(err, "Error adding helm repo for app %s.", m.name) + return errkit.Wrap(err, "Error adding helm repo for app.", "app", m.name) } log.Print("Installing maria instance using helm.", field.M{"app": m.name}) _, err = cli.Install(ctx, m.chart.RepoName+"/"+m.chart.Chart, m.chart.Version, m.chart.Release, m.namespace, m.chart.Values, true, false) if err != nil { - return errors.Wrapf(err, "Error intalling application %s through helm.", m.name) + return errkit.Wrap(err, "Error intalling application through helm.", "app", m.name) } return nil @@ -118,7 +118,7 @@ func (m *MariaDB) Object() crv1alpha1.ObjectReference { func (m *MariaDB) Uninstall(ctx context.Context) error { cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } err = cli.Uninstall(ctx, m.chart.Release, m.namespace) if err != nil { @@ -140,7 +140,7 @@ func (m *MariaDB) Ping(ctx context.Context) error { loginMaria := []string{"sh", "-c", "mysql -u root --password=$MARIADB_ROOT_PASSWORD"} _, stderr, err := m.execCommand(ctx, loginMaria) if err != nil { - return errors.Wrapf(err, "Error while Pinging the database %s", stderr) + return errkit.Wrap(err, "Error while Pinging the database", "stderr", stderr) } log.Print("Ping to the application was success.", field.M{"app": m.name}) @@ -153,7 +153,7 @@ func (m *MariaDB) Insert(ctx context.Context) error { insertRecordCMD := []string{"sh", "-c", "mysql -u root --password=$MARIADB_ROOT_PASSWORD -e 'use testdb; INSERT INTO pets VALUES (\"Puffball\",\"Diane\",\"hamster\",\"f\",\"1999-03-30\",NULL); '"} _, stderr, err := m.execCommand(ctx, insertRecordCMD) if err != nil { - return errors.Wrapf(err, "Error while inserting the data into msyql database: %s", stderr) + return errkit.Wrap(err, "Error while inserting the data into msyql database", "stderr", stderr) } log.Print("Successfully inserted records in the application.", field.M{"app": m.name}) @@ -166,12 +166,12 @@ func (m *MariaDB) Count(ctx context.Context) (int, error) { selectRowsCMD := []string{"sh", "-c", "mysql -u root --password=$MARIADB_ROOT_PASSWORD -e 'use testdb; select count(*) from pets; '"} stdout, stderr, err := m.execCommand(ctx, selectRowsCMD) if err != nil { - return 0, errors.Wrapf(err, "Error while counting the data of the database: %s", stderr) + return 0, errkit.Wrap(err, "Error while counting the data of the database", "stderr", stderr) } // get the returned count and convert it to int, to return rowsReturned, err := strconv.Atoi((strings.Split(stdout, "\n")[1])) if err != nil { - return 0, errors.Wrapf(err, "Error while converting row count to int.") + return 0, errkit.Wrap(err, "Error while converting row count to int.") } log.Print("Count that we received from application is.", field.M{"app": m.name, "count": rowsReturned}) return rowsReturned, nil @@ -186,7 +186,7 @@ func (m *MariaDB) Reset(ctx context.Context) error { }) if err != nil { - return errors.Wrapf(err, "Error waiting for application %s to be ready to reset it", m.name) + return errkit.Wrap(err, "Error waiting for application to be ready to reset it", "app", m.name) } log.Print("Resetting the maria instance.", field.M{"app": m.name}) @@ -195,7 +195,7 @@ func (m *MariaDB) Reset(ctx context.Context) error { deleteFromTableCMD := []string{"sh", "-c", "mysql -u root --password=$MARIADB_ROOT_PASSWORD -e 'DROP DATABASE IF EXISTS testdb'"} _, stderr, err := m.execCommand(ctx, deleteFromTableCMD) if err != nil { - return errors.Wrapf(err, "Error while dropping the maria table: %s", stderr) + return errkit.Wrap(err, "Error while dropping the maria table", "stderr", stderr) } log.Print("Reset of the application was successful.", field.M{"app": m.name}) @@ -210,7 +210,7 @@ func (m *MariaDB) Initialize(ctx context.Context) error { "birth DATE, death DATE);'"} _, stderr, err := m.execCommand(ctx, createTableCMD) if err != nil { - return errors.Wrapf(err, "Error while creating the maria table: %s", stderr) + return errkit.Wrap(err, "Error while creating the maria table", "stderr", stderr) } return nil } @@ -218,7 +218,7 @@ func (m *MariaDB) Initialize(ctx context.Context) error { func (m *MariaDB) execCommand(ctx context.Context, command []string) (string, string, error) { podname, containername, err := kube.GetPodContainerFromStatefulSet(ctx, m.cli, m.namespace, mariaDBSTSName(m.chart.Release)) if err != nil || podname == "" { - return "", "", errors.Wrapf(err, "Error getting pod and containername %s.", m.name) + return "", "", errkit.Wrap(err, "Error getting pod and containername.", "app", m.name) } return kube.Exec(ctx, m.cli, m.namespace, podname, containername, command, nil) } diff --git a/pkg/app/mongodb-deploymentconfig.go b/pkg/app/mongodb-deploymentconfig.go index 0343c74e93..af7f42b435 100644 --- a/pkg/app/mongodb-deploymentconfig.go +++ b/pkg/app/mongodb-deploymentconfig.go @@ -21,8 +21,8 @@ import ( "time" "github.com/google/uuid" + "github.com/kanisterio/errkit" osversioned "github.com/openshift/client-go/apps/clientset/versioned" - "github.com/pkg/errors" "k8s.io/client-go/kubernetes" crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" @@ -86,7 +86,7 @@ func (mongo *MongoDBDepConfig) Install(ctx context.Context, namespace string) er _, err := mongo.osClient.NewApp(ctx, mongo.namespace, dbTemplate, nil, mongo.params) - return errors.Wrapf(err, "Error installing app %s on openshift cluster.", mongo.name) + return errkit.Wrap(err, "Error installing app on openshift cluster.", "app", mongo.name) } func (mongo *MongoDBDepConfig) IsReady(ctx context.Context) (bool, error) { @@ -126,7 +126,7 @@ func (mongo *MongoDBDepConfig) Ping(ctx context.Context) error { pingCMD := []string{"bash", "-c", fmt.Sprintf("mongo admin --authenticationDatabase admin -u %s -p $MONGODB_ADMIN_PASSWORD --quiet --eval \"rs.slaveOk(); db\"", mongo.user)} _, stderr, err := mongo.execCommand(ctx, pingCMD) if err != nil { - return errors.Wrapf(err, "Error while Pinging the database %s, %s", stderr, err) + return errkit.Wrap(err, "Error while Pinging the database", "stderr", stderr) } log.Print("Ping to the application was successful.") return nil @@ -139,7 +139,7 @@ func (mongo *MongoDBDepConfig) Insert(ctx context.Context) error { "'cuisine' : 'Hawaiian', 'id' : '8675309'})\"", mongo.user, uuid.New())} _, stderr, err := mongo.execCommand(ctx, insertCMD) if err != nil { - return errors.Wrapf(err, "Error %s while inserting data data into mongodb collection.", stderr) + return errkit.Wrap(err, "Error while inserting data data into mongodb collection.", "stderr", stderr) } log.Print("Insertion of documents into collection was successful.", field.M{"app": mongo.name}) @@ -151,7 +151,7 @@ func (mongo *MongoDBDepConfig) Count(ctx context.Context) (int, error) { countCMD := []string{"bash", "-c", fmt.Sprintf("mongo admin --authenticationDatabase admin -u %s -p $MONGODB_ADMIN_PASSWORD --quiet --eval \"rs.slaveOk(); db.restaurants.count()\"", mongo.user)} stdout, stderr, err := mongo.execCommand(ctx, countCMD) if err != nil { - return 0, errors.Wrapf(err, "Error %s while counting the data in mongodb collection.", stderr) + return 0, errkit.Wrap(err, "Error while counting the data in mongodb collection.", "stderr", stderr) } count, err := strconv.Atoi(stdout) @@ -170,7 +170,7 @@ func (mongo *MongoDBDepConfig) Reset(ctx context.Context) error { // and deletion admin database is prohibited deleteDBCMD := []string{"bash", "-c", fmt.Sprintf("mongo admin --authenticationDatabase admin -u %s -p $MONGODB_ADMIN_PASSWORD --quiet --eval \"db.restaurants.drop()\"", mongo.user)} stdout, stderr, err := mongo.execCommand(ctx, deleteDBCMD) - return errors.Wrapf(err, "Error %s, resetting the mongodb application. stdout is %s", stderr, stdout) + return errkit.Wrap(err, "Error resetting the mongodb application.", "stdout", stdout, "stderr", stderr) } // Initialize is used to initialize the database or create schema @@ -186,5 +186,5 @@ func (mongo *MongoDBDepConfig) execCommand(ctx context.Context, command []string stdout, stderr, err := kube.Exec(ctx, mongo.cli, mongo.namespace, podName, containerName, command, nil) log.Print("Executing the command in pod and container", field.M{"pod": podName, "container": containerName, "cmd": command}) - return stdout, stderr, errors.Wrapf(err, "Error executing command in the pod") + return stdout, stderr, errkit.Wrap(err, "Error executing command in the pod") } diff --git a/pkg/app/mongodb.go b/pkg/app/mongodb.go index ef030ddf86..41b0bc360b 100644 --- a/pkg/app/mongodb.go +++ b/pkg/app/mongodb.go @@ -22,7 +22,7 @@ import ( "time" "github.com/google/uuid" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "k8s.io/client-go/kubernetes" crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" @@ -92,7 +92,7 @@ func (mongo *MongoDB) Install(ctx context.Context, namespace string) error { mongo.namespace = namespace cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } log.Print("Adding repo for the application.", field.M{"app": mongo.name}) @@ -138,11 +138,11 @@ func (mongo *MongoDB) Object() crv1alpha1.ObjectReference { func (mongo *MongoDB) Uninstall(ctx context.Context) error { cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } log.Print("Uninstalling application.", field.M{"app": mongo.name}) err = cli.Uninstall(ctx, mongo.chart.Release, mongo.namespace) - return errors.Wrapf(err, "Error while uninstalling the application.") + return errkit.Wrap(err, "Error while uninstalling the application.") } func (mongo *MongoDB) GetClusterScopedResources(ctx context.Context) []crv1alpha1.ObjectReference { @@ -154,7 +154,7 @@ func (mongo *MongoDB) Ping(ctx context.Context) error { pingCMD := []string{"sh", "-c", fmt.Sprintf("mongosh admin --authenticationDatabase admin -u %s -p $MONGODB_ROOT_PASSWORD --quiet --eval \"db\"", mongo.username)} _, stderr, err := mongo.execCommand(ctx, pingCMD) if err != nil { - return errors.Wrapf(err, "Error while pinging the mongodb application %s", stderr) + return errkit.Wrap(err, "Error while pinging the mongodb application", "stderr", stderr) } // even after ping is successful, it takes some time for primary pod to becomd the master @@ -162,17 +162,17 @@ func (mongo *MongoDB) Ping(ctx context.Context) error { isMasterCMD := []string{"sh", "-c", fmt.Sprintf("mongosh admin --authenticationDatabase admin -u %s -p $MONGODB_ROOT_PASSWORD --quiet --eval \"JSON.stringify(db.isMaster())\"", mongo.username)} stdout, stderr, err := mongo.execCommand(ctx, isMasterCMD) if err != nil { - return errors.Wrapf(err, "Error %s checking if the pod is master.", stderr) + return errkit.Wrap(err, "Error checking if the pod is master.", "stderr", stderr) } // convert the mongo's output to go struct so that we can check if the pod has become master or not. op := IsMasterOutput{} err = json.Unmarshal([]byte(stdout), &op) if err != nil { - return errors.Wrapf(err, "Error unmarshalling the ismaster ouptut.") + return errkit.Wrap(err, "Error unmarshalling the ismaster ouptut.") } if !op.Ismaster { - return errors.New("the pod is not master yet") + return errkit.New("the pod is not master yet") } log.Print("Ping was successful to application.", field.M{"app": mongo.name}) @@ -186,7 +186,7 @@ func (mongo *MongoDB) Insert(ctx context.Context) error { "'cuisine' : 'Hawaiian', 'id' : '8675309'})\"", mongo.username, uuid.New())} _, stderr, err := mongo.execCommand(ctx, insertCMD) if err != nil { - return errors.Wrapf(err, "Error %s while inserting data data into mongodb collection.", stderr) + return errkit.Wrap(err, "Error while inserting data data into mongodb collection.", "stderr", stderr) } log.Print("Insertion of documents into collection was successful.", field.M{"app": mongo.name}) @@ -198,7 +198,7 @@ func (mongo *MongoDB) Count(ctx context.Context) (int, error) { countCMD := []string{"sh", "-c", fmt.Sprintf("mongosh admin --authenticationDatabase admin -u %s -p $MONGODB_ROOT_PASSWORD --quiet --eval \"db.restaurants.countDocuments()\"", mongo.username)} stdout, stderr, err := mongo.execCommand(ctx, countCMD) if err != nil { - return 0, errors.Wrapf(err, "Error %s while counting the data in mongodb collection.", stderr) + return 0, errkit.Wrap(err, "Error while counting the data in mongodb collection.", "stderr", stderr) } count, err := strconv.Atoi(stdout) @@ -216,7 +216,7 @@ func (mongo *MongoDB) Reset(ctx context.Context) error { // and deletion admin database is prohibited deleteDBCMD := []string{"sh", "-c", fmt.Sprintf("mongosh admin --authenticationDatabase admin -u %s -p $MONGODB_ROOT_PASSWORD --quiet --eval \"db.restaurants.drop()\"", mongo.username)} stdout, stderr, err := mongo.execCommand(ctx, deleteDBCMD) - return errors.Wrapf(err, "Error %s, resetting the mongodb application. stdout is %s", stderr, stdout) + return errkit.Wrap(err, "Error resetting the mongodb application.", "stdout", stdout, "stderr", stderr) } // Initialize is used to initialize the database or create schema diff --git a/pkg/app/mssql.go b/pkg/app/mssql.go index f38354891d..5dffc24403 100644 --- a/pkg/app/mssql.go +++ b/pkg/app/mssql.go @@ -7,7 +7,7 @@ import ( "strings" "time" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -168,7 +168,7 @@ func (m *MssqlDB) Ping(ctx context.Context) error { loginMssql := []string{"sh", "-c", count} _, stderr, err := m.execCommand(ctx, loginMssql) if err != nil { - return errors.Wrapf(err, "Error while Pinging the database: %s", stderr) + return errkit.Wrap(err, "Error while Pinging the database", "stderr", stderr) } log.Print("Ping to the application was success.", field.M{"app": m.name}) return nil @@ -182,7 +182,7 @@ func (m *MssqlDB) Insert(ctx context.Context) error { insertQuery := []string{"sh", "-c", insert} _, stderr, err := m.execCommand(ctx, insertQuery) if err != nil { - return errors.Wrapf(err, "Error while inserting data into table: %s", stderr) + return errkit.Wrap(err, "Error while inserting data into table", "stderr", stderr) } return nil } @@ -195,11 +195,11 @@ func (m *MssqlDB) Count(ctx context.Context) (int, error) { insertQuery := []string{"sh", "-c", insert} stdout, stderr, err := m.execCommand(ctx, insertQuery) if err != nil { - return 0, errors.Wrapf(err, "Error while inserting data into table: %s", stderr) + return 0, errkit.Wrap(err, "Error while inserting data into table", "stderr", stderr) } rowsReturned, err := strconv.Atoi(strings.TrimSpace(strings.Split(stdout, "\n")[1])) if err != nil { - return 0, errors.Wrapf(err, "Error while converting response of count query: %s", stderr) + return 0, errkit.Wrap(err, "Error while converting response of count query", "stderr", stderr) } return rowsReturned, nil } @@ -210,7 +210,7 @@ func (m *MssqlDB) Reset(ctx context.Context) error { deleteQuery := []string{"sh", "-c", delete} _, stderr, err := m.execCommand(ctx, deleteQuery) if err != nil { - return errors.Wrapf(err, "Error while inserting data into table: %s", stderr) + return errkit.Wrap(err, "Error while inserting data into table", "stderr", stderr) } return nil } @@ -225,13 +225,13 @@ func (m *MssqlDB) Initialize(ctx context.Context) error { execQuery := []string{"sh", "-c", createDB} _, stderr, err := m.execCommand(ctx, execQuery) if err != nil { - return errors.Wrapf(err, "Error while creating the database: %s", stderr) + return errkit.Wrap(err, "Error while creating the database", "stderr", stderr) } execQuery = []string{"sh", "-c", createTable} _, stderr, err = m.execCommand(ctx, execQuery) if err != nil { - return errors.Wrapf(err, "Error while creating table: %s", stderr) + return errkit.Wrap(err, "Error while creating table", "stderr", stderr) } return nil } @@ -243,7 +243,7 @@ func (m *MssqlDB) GetClusterScopedResources(ctx context.Context) []crv1alpha1.Ob func (m MssqlDB) execCommand(ctx context.Context, command []string) (string, string, error) { podName, containerName, err := kube.GetPodContainerFromDeployment(ctx, m.cli, m.namespace, m.deployment.Name) if err != nil || podName == "" { - return "", "", errors.Wrapf(err, "Error getting pod and container name for app %s.", m.name) + return "", "", errkit.Wrap(err, "Error getting pod and container name for app.", "app", m.name) } return kube.Exec(ctx, m.cli, m.namespace, podName, containerName, command, nil) } diff --git a/pkg/app/mysql-deploymentconfig.go b/pkg/app/mysql-deploymentconfig.go index aef558f582..7a3f522e8c 100644 --- a/pkg/app/mysql-deploymentconfig.go +++ b/pkg/app/mysql-deploymentconfig.go @@ -20,8 +20,8 @@ import ( "strings" "time" + "github.com/kanisterio/errkit" osversioned "github.com/openshift/client-go/apps/clientset/versioned" - "github.com/pkg/errors" "k8s.io/client-go/kubernetes" crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" @@ -85,7 +85,7 @@ func (mdep *MysqlDepConfig) Install(ctx context.Context, namespace string) error oc := openshift.NewOpenShiftClient() _, err := oc.NewApp(ctx, mdep.namespace, dbTemplate, nil, mdep.params) - return errors.Wrapf(err, "Error installing app %s on openshift cluster.", mdep.name) + return errkit.Wrap(err, "Error installing app on openshift cluster.", "app", mdep.name) } func (mdep *MysqlDepConfig) IsReady(ctx context.Context) (bool, error) { @@ -125,7 +125,7 @@ func (mdep *MysqlDepConfig) Ping(ctx context.Context) error { pingCMD := []string{"bash", "-c", "mysql -u root -e 'show databases;'"} _, stderr, err := mdep.execCommand(ctx, pingCMD) if err != nil { - return errors.Wrapf(err, "Error while Pinging the database %s, %s", stderr, err) + return errkit.Wrap(err, "Error while Pinging the database", "stderr", stderr) } log.Print("Ping to the application was successful.") return nil @@ -137,7 +137,7 @@ func (mdep *MysqlDepConfig) Insert(ctx context.Context) error { insertRecordCMD := []string{"bash", "-c", "mysql -u root -e 'use testdb; INSERT INTO pets VALUES (\"Puffball\",\"Diane\",\"hamster\",\"f\",\"1999-03-30\",NULL); '"} _, stderr, err := mdep.execCommand(ctx, insertRecordCMD) if err != nil { - return errors.Wrapf(err, "Error while inserting the data into msyql deployment config database: %s", stderr) + return errkit.Wrap(err, "Error while inserting the data into msyql deployment config database", "stderr", stderr) } log.Print("Successfully inserted record in the application.", field.M{"app": mdep.name}) @@ -150,13 +150,13 @@ func (mdep *MysqlDepConfig) Count(ctx context.Context) (int, error) { selectRowsCMD := []string{"bash", "-c", "mysql -u root -e 'use testdb; select count(*) from pets; '"} stdout, stderr, err := mdep.execCommand(ctx, selectRowsCMD) if err != nil { - return 0, errors.Wrapf(err, "Error while counting the data of the database: %s", stderr) + return 0, errkit.Wrap(err, "Error while counting the data of the database", "stderr", stderr) } // get the returned count and convert it to int, to return rowsReturned, err := strconv.Atoi((strings.Split(stdout, "\n")[1])) if err != nil { - return 0, errors.Wrapf(err, "Error while converting row count to int.") + return 0, errkit.Wrap(err, "Error while converting row count to int.") } log.Print("Count that we received from application is.", field.M{"app": mdep.name, "count": rowsReturned}) @@ -170,14 +170,14 @@ func (mdep *MysqlDepConfig) Reset(ctx context.Context) error { deleteCMD := []string{"bash", "-c", "mysql -u root -e 'DROP DATABASE IF EXISTS testdb'"} _, stderr, err := mdep.execCommand(ctx, deleteCMD) if err != nil { - return errors.Wrapf(err, "Error while dropping the mysql table: %s", stderr) + return errkit.Wrap(err, "Error while dropping the mysql table", "stderr", stderr) } // create the database and a pets table createCMD := []string{"bash", "-c", "mysql -u root -e 'create database testdb; use testdb; CREATE TABLE pets (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);'"} _, stderr, err = mdep.execCommand(ctx, createCMD) if err != nil { - return errors.Wrapf(err, "Error while creating the mysql table: %s", stderr) + return errkit.Wrap(err, "Error while creating the mysql table", "stderr", stderr) } log.Print("Reset of the application was successful.", field.M{"app": mdep.name}) @@ -196,7 +196,7 @@ func (mdep *MysqlDepConfig) execCommand(ctx context.Context, command []string) ( } stdout, stderr, err := kube.Exec(ctx, mdep.cli, mdep.namespace, podName, containerName, command, nil) if err != nil { - return stdout, stderr, errors.Wrapf(err, "Error executing command in the pod.") + return stdout, stderr, errkit.Wrap(err, "Error executing command in the pod.") } return stdout, stderr, err } diff --git a/pkg/app/mysql.go b/pkg/app/mysql.go index 638ae42ac3..6b35c2d419 100644 --- a/pkg/app/mysql.go +++ b/pkg/app/mysql.go @@ -20,7 +20,7 @@ import ( "strings" "time" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "k8s.io/client-go/kubernetes" crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" @@ -87,18 +87,18 @@ func (mdb *MysqlDB) Install(ctx context.Context, namespace string) error { //nol mdb.namespace = namespace cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } log.Print("Adding repo.", field.M{"app": mdb.name}) err = cli.AddRepo(ctx, mdb.chart.RepoName, mdb.chart.RepoURL) if err != nil { - return errors.Wrapf(err, "Error adding helm repo for app %s.", mdb.name) + return errkit.Wrap(err, "Error adding helm repo for app.", "app", mdb.name) } log.Print("Installing mysql instance using helm.", field.M{"app": mdb.name}) _, err = cli.Install(ctx, mdb.chart.RepoName+"/"+mdb.chart.Chart, mdb.chart.Version, mdb.chart.Release, mdb.namespace, mdb.chart.Values, true, false) if err != nil { - return errors.Wrapf(err, "Error intalling application %s through helm.", mdb.name) + return errkit.Wrap(err, "Error installing application through helm.", "app", mdb.name) } return nil @@ -127,7 +127,7 @@ func (mdb *MysqlDB) Object() crv1alpha1.ObjectReference { func (mdb *MysqlDB) Uninstall(ctx context.Context) error { cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } err = cli.Uninstall(ctx, mdb.chart.Release, mdb.namespace) if err != nil { @@ -150,7 +150,7 @@ func (mdb *MysqlDB) Ping(ctx context.Context) error { loginMysql := []string{"sh", "-c", "mysql -u root --password=$MYSQL_ROOT_PASSWORD"} _, stderr, err := mdb.execCommand(ctx, loginMysql) if err != nil { - return errors.Wrapf(err, "Error while Pinging the database %s", stderr) + return errkit.Wrap(err, "Error while Pinging the database", "stderr", stderr) } log.Print("Ping to the application was success.", field.M{"app": mdb.name}) @@ -163,7 +163,7 @@ func (mdb *MysqlDB) Insert(ctx context.Context) error { insertRecordCMD := []string{"sh", "-c", "mysql -u root --password=$MYSQL_ROOT_PASSWORD -e 'use testdb; INSERT INTO pets VALUES (\"Puffball\",\"Diane\",\"hamster\",\"f\",\"1999-03-30\",NULL); '"} _, stderr, err := mdb.execCommand(ctx, insertRecordCMD) if err != nil { - return errors.Wrapf(err, "Error while inserting the data into msyql database: %s", stderr) + return errkit.Wrap(err, "Error while inserting the data into msyql database", "stderr", stderr) } log.Print("Successfully inserted records in the application.", field.M{"app": mdb.name}) @@ -176,12 +176,12 @@ func (mdb *MysqlDB) Count(ctx context.Context) (int, error) { selectRowsCMD := []string{"sh", "-c", "mysql -u root --password=$MYSQL_ROOT_PASSWORD -e 'use testdb; select count(*) from pets; '"} stdout, stderr, err := mdb.execCommand(ctx, selectRowsCMD) if err != nil { - return 0, errors.Wrapf(err, "Error while counting the data of the database: %s", stderr) + return 0, errkit.Wrap(err, "Error while counting the data of the database", "stderr", stderr) } // get the returned count and convert it to int, to return rowsReturned, err := strconv.Atoi((strings.Split(stdout, "\n")[1])) if err != nil { - return 0, errors.Wrapf(err, "Error while converting row count to int.") + return 0, errkit.Wrap(err, "Error while converting row count to int.") } log.Print("Count that we received from application is.", field.M{"app": mdb.name, "count": rowsReturned}) return rowsReturned, nil @@ -196,7 +196,7 @@ func (mdb *MysqlDB) Reset(ctx context.Context) error { }) if err != nil { - return errors.Wrapf(err, "Error waiting for application %s to be ready to reset it", mdb.name) + return errkit.Wrap(err, "Error waiting for application to be ready to reset it", "app", mdb.name) } log.Print("Resetting the mysql instance.", field.M{"app": "mysql"}) @@ -205,7 +205,7 @@ func (mdb *MysqlDB) Reset(ctx context.Context) error { deleteFromTableCMD := []string{"sh", "-c", "mysql -u root --password=$MYSQL_ROOT_PASSWORD -e 'DROP DATABASE IF EXISTS testdb'"} _, stderr, err := mdb.execCommand(ctx, deleteFromTableCMD) if err != nil { - return errors.Wrapf(err, "Error while dropping the mysql table: %s", stderr) + return errkit.Wrap(err, "Error while dropping the mysql table", "stderr", stderr) } log.Print("Reset of the application was successful.", field.M{"app": mdb.name}) @@ -220,7 +220,7 @@ func (mdb *MysqlDB) Initialize(ctx context.Context) error { "species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);'"} _, stderr, err := mdb.execCommand(ctx, createTableCMD) if err != nil { - return errors.Wrapf(err, "Error while creating the mysql table: %s", stderr) + return errkit.Wrap(err, "Error while creating the mysql table", "stderr", stderr) } return nil } @@ -242,7 +242,7 @@ func (mdb *MysqlDB) Secrets() map[string]crv1alpha1.ObjectReference { func (mdb *MysqlDB) execCommand(ctx context.Context, command []string) (string, string, error) { podname, containername, err := kube.GetPodContainerFromStatefulSet(ctx, mdb.cli, mdb.namespace, mdb.chart.Release) if err != nil || podname == "" { - return "", "", errors.Wrapf(err, "Error getting pod and containername %s.", mdb.name) + return "", "", errkit.Wrap(err, "Error getting pod and containername.", "app", mdb.name) } return kube.Exec(ctx, mdb.cli, mdb.namespace, podname, containername, command, nil) } diff --git a/pkg/app/postgresql-deploymentconfig.go b/pkg/app/postgresql-deploymentconfig.go index ee0025371f..eac33dba00 100644 --- a/pkg/app/postgresql-deploymentconfig.go +++ b/pkg/app/postgresql-deploymentconfig.go @@ -21,8 +21,8 @@ import ( "strings" "time" + "github.com/kanisterio/errkit" osversioned "github.com/openshift/client-go/apps/clientset/versioned" - "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -90,7 +90,7 @@ func (pgres *PostgreSQLDepConfig) Install(ctx context.Context, namespace string) _, err := pgres.opeshiftClient.NewApp(ctx, pgres.namespace, dbTemplate, pgres.envVar, pgres.params) if err != nil { - return errors.Wrapf(err, "Error installing application %s on openshift cluster", pgres.name) + return errkit.Wrap(err, "Error installing application on openshift cluster", "app", pgres.name) } // The secret that get created after installation doesnt have the creds that are mentioned in the // POSTGRESQL_ADMIN_PASSWORD above, we are creating another secret that will have this detail @@ -115,7 +115,7 @@ func (pgres *PostgreSQLDepConfig) createPostgreSQLSecret(ctx context.Context) er _, err := pgres.cli.CoreV1().Secrets(pgres.namespace).Create(ctx, postgreSQLSecret, metav1.CreateOptions{}) - return errors.Wrapf(err, "Error creating secret for mysqldepconf app.") + return errkit.Wrap(err, "Error creating secret for mysqldepconf app.") } func (pgres *PostgreSQLDepConfig) IsReady(ctx context.Context) (bool, error) { @@ -125,7 +125,7 @@ func (pgres *PostgreSQLDepConfig) IsReady(ctx context.Context) (bool, error) { err := kube.WaitOnDeploymentConfigReady(ctx, pgres.osCli, pgres.cli, pgres.namespace, postgresDepConfigName) if err != nil { - return false, errors.Wrapf(err, "Error %s waiting for application to be ready.", pgres.name) + return false, errkit.Wrap(err, "Error waiting for application to be ready.", "app", pgres.name) } log.Print("Application is ready", field.M{"app": pgres.name}) @@ -153,7 +153,7 @@ func (pgres *PostgreSQLDepConfig) Ping(ctx context.Context) error { cmd := "pg_isready -U 'postgres' -h 127.0.0.1 -p 5432" _, stderr, err := pgres.execCommand(ctx, []string{"bash", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to ping postgresql deployment config DB. %s", stderr) + return errkit.Wrap(err, "Failed to ping postgresql deployment config DB", "stderr", stderr) } log.Info().Print("Connected to database.", field.M{"app": pgres.name}) return nil @@ -163,7 +163,7 @@ func (pgres *PostgreSQLDepConfig) Insert(ctx context.Context) error { cmd := "psql -d test -c \"INSERT INTO COMPANY (NAME,AGE,CREATED_AT) VALUES ('foo', 32, now());\"" _, stderr, err := pgres.execCommand(ctx, []string{"bash", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to create db in postgresql deployment config. %s", stderr) + return errkit.Wrap(err, "Failed to create db in postgresql deployment config", "stderr", stderr) } log.Info().Print("Inserted a row in test db.", field.M{"app": pgres.name}) return nil @@ -173,16 +173,16 @@ func (pgres *PostgreSQLDepConfig) Count(ctx context.Context) (int, error) { cmd := "psql -d test -c 'SELECT COUNT(*) FROM company;'" stdout, stderr, err := pgres.execCommand(ctx, []string{"bash", "-c", cmd}) if err != nil { - return 0, errors.Wrapf(err, "Failed to count db entries in postgresql deployment config. %s ", stderr) + return 0, errkit.Wrap(err, "Failed to count db entries in postgresql deployment config", "stderr", stderr) } out := strings.Fields(stdout) if len(out) < 4 { - return 0, fmt.Errorf("unknown response for count query") + return 0, errkit.New("unknown response for count query") } count, err := strconv.Atoi(out[2]) if err != nil { - return 0, errors.Wrapf(err, "Failed to count db entries in postgresql deployment config. %s ", stderr) + return 0, errkit.Wrap(err, "Failed to count db entries in postgresql deployment config", "stderr", stderr) } log.Info().Print("Counting rows in test db.", field.M{"app": pgres.name, "count": count}) return count, nil @@ -192,7 +192,7 @@ func (pgres *PostgreSQLDepConfig) Reset(ctx context.Context) error { cmd := "psql -c 'DROP DATABASE IF EXISTS test;'" _, stderr, err := pgres.execCommand(ctx, []string{"bash", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to drop db from postgresql deployment config. %s ", stderr) + return errkit.Wrap(err, "Failed to drop db from postgresql deployment config", "stderr", stderr) } log.Info().Print("Database reset successful!", field.M{"app": pgres.name}) @@ -205,14 +205,14 @@ func (pgres *PostgreSQLDepConfig) Initialize(ctx context.Context) error { cmd := "psql -c 'CREATE DATABASE test;'" _, stderr, err := pgres.execCommand(ctx, []string{"bash", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to create db in postgresql deployment config %s ", stderr) + return errkit.Wrap(err, "Failed to create db in postgresql deployment config", "stderr", stderr) } // Create table cmd = "psql -d test -c 'CREATE TABLE COMPANY(ID SERIAL PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, CREATED_AT TIMESTAMP);'" _, stderr, err = pgres.execCommand(ctx, []string{"bash", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to create table in postgresql deployment config %s ", stderr) + return errkit.Wrap(err, "Failed to create table in postgresql deployment config", "stderr", stderr) } return nil } diff --git a/pkg/app/postgresql.go b/pkg/app/postgresql.go index debe50fbf3..9c8cd72223 100644 --- a/pkg/app/postgresql.go +++ b/pkg/app/postgresql.go @@ -21,7 +21,7 @@ import ( "strings" "time" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "k8s.io/client-go/kubernetes" crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" @@ -87,7 +87,7 @@ func (pdb *PostgresDB) Install(ctx context.Context, ns string) error { // Create helm client cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } // Add helm repo and fetch charts @@ -137,7 +137,7 @@ func (pdb *PostgresDB) Ping(ctx context.Context) error { cmd := "pg_isready -U 'postgres' -h 127.0.0.1 -p 5432" _, stderr, err := pdb.execCommand(ctx, []string{"sh", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to ping postgresql DB. %s", stderr) + return errkit.Wrap(err, "Failed to ping postgresql DB", "stderr", stderr) } log.Info().Print("Connected to database.", field.M{"app": pdb.name}) return nil @@ -147,7 +147,7 @@ func (pdb PostgresDB) Insert(ctx context.Context) error { cmd := "PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres -d test -c \"INSERT INTO COMPANY (NAME,AGE,CREATED_AT) VALUES ('foo', 32, now());\"" _, stderr, err := pdb.execCommand(ctx, []string{"sh", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to create db in postgresql. %s", stderr) + return errkit.Wrap(err, "Failed to create db in postgresql", "stderr", stderr) } log.Info().Print("Inserted a row in test db.", field.M{"app": pdb.name}) return nil @@ -157,16 +157,16 @@ func (pdb PostgresDB) Count(ctx context.Context) (int, error) { cmd := "PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres -d test -c 'SELECT COUNT(*) FROM company;'" stdout, stderr, err := pdb.execCommand(ctx, []string{"sh", "-c", cmd}) if err != nil { - return 0, errors.Wrapf(err, "Failed to count db entries in postgresql. %s ", stderr) + return 0, errkit.Wrap(err, "Failed to count db entries in postgresql", "stderr", stderr) } out := strings.Fields(stdout) if len(out) < 4 { - return 0, fmt.Errorf("unknown response for count query") + return 0, errkit.New("unknown response for count query") } count, err := strconv.Atoi(out[2]) if err != nil { - return 0, errors.Wrapf(err, "Failed to count db entries in postgresql. %s ", stderr) + return 0, errkit.Wrap(err, "Failed to count db entries in postgresql", "stderr", stderr) } log.Info().Print("Counting rows in test db.", field.M{"app": pdb.name, "count": count}) return count, nil @@ -177,7 +177,7 @@ func (pdb PostgresDB) Reset(ctx context.Context) error { cmd := "PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres -c 'DROP DATABASE IF EXISTS test;'" _, stderr, err := pdb.execCommand(ctx, []string{"sh", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to drop db from postgresql. %s ", stderr) + return errkit.Wrap(err, "Failed to drop db from postgresql", "stderr", stderr) } log.Info().Print("Database reset successful!", field.M{"app": pdb.name}) @@ -190,14 +190,14 @@ func (pdb PostgresDB) Initialize(ctx context.Context) error { cmd := "PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres -c 'CREATE DATABASE test;'" _, stderr, err := pdb.execCommand(ctx, []string{"sh", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to create db in postgresql. %s ", stderr) + return errkit.Wrap(err, "Failed to create db in postgresql", "stderr", stderr) } // Create table cmd = "PGPASSWORD=${POSTGRES_PASSWORD} psql -U postgres -d test -c 'CREATE TABLE COMPANY(ID SERIAL PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, CREATED_AT TIMESTAMP);'" _, stderr, err = pdb.execCommand(ctx, []string{"sh", "-c", cmd}) if err != nil { - return errors.Wrapf(err, "Failed to create table in postgresql. %s ", stderr) + return errkit.Wrap(err, "Failed to create table in postgresql", "stderr", stderr) } return nil } @@ -208,11 +208,11 @@ func (pdb PostgresDB) Uninstall(ctx context.Context) error { // Create helm client cli, err := helm.NewCliClient() if err != nil { - return errors.Wrap(err, "failed to create helm client") + return errkit.Wrap(err, "failed to create helm client") } // Uninstall helm chart - return errors.Wrapf(cli.Uninstall(ctx, pdb.chart.Release, pdb.namespace), "Failed to uninstall %s helm release", pdb.chart.Release) + return errkit.Wrap(cli.Uninstall(ctx, pdb.chart.Release, pdb.namespace), "Failed to uninstall helm release", "release", pdb.chart.Release) } func (pdp *PostgresDB) GetClusterScopedResources(ctx context.Context) []crv1alpha1.ObjectReference { diff --git a/pkg/app/rds_aurora_mysql.go b/pkg/app/rds_aurora_mysql.go index d122350eef..ce1d786173 100644 --- a/pkg/app/rds_aurora_mysql.go +++ b/pkg/app/rds_aurora_mysql.go @@ -23,7 +23,7 @@ import ( awssdk "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" awsrds "github.com/aws/aws-sdk-go/service/rds" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -94,7 +94,7 @@ func (a *RDSAuroraMySQLDB) Init(context.Context) error { if a.region == "" { a.region, ok = os.LookupEnv(aws.Region) if !ok { - return errors.New(fmt.Sprintf("Env var %s is not set", aws.Region)) + return errkit.New("Env var is not set", "name", aws.Region) } } @@ -106,11 +106,11 @@ func (a *RDSAuroraMySQLDB) Init(context.Context) error { a.accessID, ok = os.LookupEnv(aws.AccessKeyID) if !ok { - return errors.New(fmt.Sprintf("Env var %s is not set", aws.AccessKeyID)) + return errkit.New("Env var is not set", "name", aws.AccessKeyID) } a.secretKey, ok = os.LookupEnv(aws.SecretAccessKey) if !ok { - return errors.New(fmt.Sprintf("Env var %s is not set", aws.SecretAccessKey)) + return errkit.New("Env var is not set", "name", aws.SecretAccessKey) } return nil @@ -122,7 +122,7 @@ func (a *RDSAuroraMySQLDB) Install(ctx context.Context, namespace string) error // Get aws config awsConfig, region, err := a.getAWSConfig(ctx) if err != nil { - return errors.Wrapf(err, "Error getting aws config app=%s", a.name) + return errkit.Wrap(err, "Error getting aws config", "app", a.name) } // Create ec2 client @@ -136,11 +136,11 @@ func (a *RDSAuroraMySQLDB) Install(ctx context.Context, namespace string) error deploymentSpec := bastionDebugWorkloadSpec(ctx, a.bastionDebugWorkloadName, "mysql", a.namespace) _, err = a.cli.AppsV1().Deployments(a.namespace).Create(ctx, deploymentSpec, metav1.CreateOptions{}) if err != nil { - return errors.Wrapf(err, "Failed to create deployment %s, app=%s", a.bastionDebugWorkloadName, a.name) + return errkit.Wrap(err, "Failed to create deployment", "deployment", a.bastionDebugWorkloadName, "app", a.name) } if err := kube.WaitOnDeploymentReady(ctx, a.cli, a.namespace, a.bastionDebugWorkloadName); err != nil { - return errors.Wrapf(err, "Failed while waiting for deployment %s to be ready, app=%s", a.bastionDebugWorkloadName, a.name) + return errkit.Wrap(err, "Failed while waiting for deployment to be ready", "deployment", a.bastionDebugWorkloadName, "app", a.name) } rdsCli, err := rds.NewClient(ctx, awsConfig, region) @@ -163,36 +163,36 @@ func (a *RDSAuroraMySQLDB) Install(ctx context.Context, namespace string) error log.Info().Print("Creating security group.", field.M{"app": a.name, "name": a.securityGroupName}) sg, err := ec2Cli.CreateSecurityGroup(ctx, a.securityGroupName, "To allow ingress to Aurora DB cluster", a.vpcID) if err != nil { - return errors.Wrap(err, "Error creating security group") + return errkit.Wrap(err, "Error creating security group") } a.securityGroupID = *sg.GroupId // Add ingress rule _, err = ec2Cli.AuthorizeSecurityGroupIngress(ctx, a.securityGroupID, "0.0.0.0/0", "tcp", 3306) if err != nil { - return errors.Wrap(err, "Error authorizing security group") + return errkit.Wrap(err, "Error authorizing security group") } // Create RDS instance log.Info().Print("Creating RDS Aurora DB cluster.", field.M{"app": a.name, "id": a.id}) _, err = rdsCli.CreateDBCluster(ctx, AuroraDBStorage, AuroraDBInstanceClass, a.id, a.dbSubnetGroup, string(function.DBEngineAuroraMySQL), a.dbName, a.username, a.password, []string{a.securityGroupID}) if err != nil { - return errors.Wrap(err, "Error creating DB cluster") + return errkit.Wrap(err, "Error creating DB cluster") } err = rdsCli.WaitUntilDBClusterAvailable(ctx, a.id) if err != nil { - return errors.Wrap(err, "Error waiting for DB cluster to be available") + return errkit.Wrap(err, "Error waiting for DB cluster to be available") } _, err = rdsCli.CreateDBInstance(ctx, nil, AuroraDBInstanceClass, fmt.Sprintf("%s-instance-1", a.id), string(function.DBEngineAuroraMySQL), "", "", nil, awssdk.Bool(a.publicAccess), awssdk.String(a.id), a.dbSubnetGroup) if err != nil { - return errors.Wrap(err, "Error creating an instance in Aurora DB cluster") + return errkit.Wrap(err, "Error creating an instance in Aurora DB cluster") } err = rdsCli.WaitUntilDBInstanceAvailable(ctx, fmt.Sprintf("%s-instance-1", a.id)) if err != nil { - return errors.Wrap(err, "Error waiting for DB instance to be available") + return errkit.Wrap(err, "Error waiting for DB instance to be available") } dbCluster, err := rdsCli.DescribeDBClusters(ctx, a.id) @@ -200,7 +200,7 @@ func (a *RDSAuroraMySQLDB) Install(ctx context.Context, namespace string) error return err } if len(dbCluster.DBClusters) == 0 { - return errors.New(fmt.Sprintf("Error installing application %s, DBCluster not available", a.name)) + return errkit.New("Error installing application %s, DBCluster not available", "name", a.name) } a.host = *dbCluster.DBClusters[0].Endpoint @@ -235,7 +235,7 @@ func (a *RDSAuroraMySQLDB) Ping(ctx context.Context) error { _, stderr, err := a.execCommand(ctx, pingCommand) if err != nil { - return errors.Wrapf(err, "Error while Pinging the database: %s, app: %s", stderr, a.name) + return errkit.Wrap(err, "Error while Pinging the database", "stderr", stderr, "app", a.name) } log.Print("Ping to the application was success.", field.M{"app": a.name}) @@ -250,7 +250,7 @@ func (a *RDSAuroraMySQLDB) Insert(ctx context.Context) error { insertCommand := []string{"sh", "-c", insertQuery} _, stderr, err := a.execCommand(ctx, insertCommand) if err != nil { - return errors.Wrapf(err, "Error while inserting data into table: %s, app: %s", stderr, a.name) + return errkit.Wrap(err, "Error while inserting data into table", "stderr", stderr, "app", a.name) } log.Info().Print("Inserted a row in test db.", field.M{"app": a.name}) return nil @@ -264,12 +264,12 @@ func (a *RDSAuroraMySQLDB) Count(ctx context.Context) (int, error) { countCommand := []string{"sh", "-c", countQuery} stdout, stderr, err := a.execCommand(ctx, countCommand) if err != nil { - return 0, errors.Wrapf(err, "Error while counting data of table: %s, app: %s", stderr, a.name) + return 0, errkit.Wrap(err, "Error while counting data of table", "stderr", stderr, "app", a.name) } rowsReturned, err := strconv.Atoi(stdout) if err != nil { - return 0, errors.Wrapf(err, "Error while converting response of count query to int: %s, app: %s", stderr, a.name) + return 0, errkit.Wrap(err, "Error while converting response of count query to int", "stderr", stderr, "app", a.name) } log.Info().Print("Number of rows in test DB.", field.M{"app": a.name, "count": rowsReturned}) @@ -283,7 +283,7 @@ func (a *RDSAuroraMySQLDB) Reset(ctx context.Context) error { deleteCommand := []string{"sh", "-c", deleteQuery} _, stderr, err := a.execCommand(ctx, deleteCommand) if err != nil { - return errors.Wrapf(err, "Error while deleting data from table: %s, app: %s", stderr, a.name) + return errkit.Wrap(err, "Error while deleting data from table", "stderr", stderr, "app", a.name) } log.Info().Print("Database reset was successful!", field.M{"app": a.name}) @@ -296,7 +296,7 @@ func (a *RDSAuroraMySQLDB) Initialize(ctx context.Context) error { createCommand := []string{"sh", "-c", createQuery} _, stderr, err := a.execCommand(ctx, createCommand) if err != nil { - return errors.Wrapf(err, "Error while creating the database: %s, app: %s", stderr, a.name) + return errkit.Wrap(err, "Error while creating the database", "stderr", stderr, "app", a.name) } return nil } @@ -313,12 +313,12 @@ func (a *RDSAuroraMySQLDB) Object() crv1alpha1.ObjectReference { func (a *RDSAuroraMySQLDB) Uninstall(ctx context.Context) error { awsConfig, region, err := a.getAWSConfig(ctx) if err != nil { - return errors.Wrapf(err, "app=%s", a.name) + return errkit.Wrap(err, "Error getting aws config", "app", a.name) } // Create rds client rdsCli, err := rds.NewClient(ctx, awsConfig, region) if err != nil { - return errors.Wrap(err, "Failed to create rds client. You may need to delete RDS resources manually. app=rds-postgresql") + return errkit.Wrap(err, "Failed to create rds client. You may need to delete RDS resources manually. app=rds-postgresql") } descOp, err := rdsCli.DescribeDBClusters(ctx, a.id) @@ -339,7 +339,7 @@ func (a *RDSAuroraMySQLDB) Uninstall(ctx context.Context) error { // Create ec2 client ec2Cli, err := ec2.NewClient(ctx, awsConfig, region) if err != nil { - return errors.Wrap(err, "Failed to create ec2 client.") + return errkit.Wrap(err, "Failed to create ec2 client.") } log.Info().Print("Deleting db subnet group.", field.M{"app": a.name}) @@ -351,7 +351,7 @@ func (a *RDSAuroraMySQLDB) Uninstall(ctx context.Context) error { case awsrds.ErrCodeDBSubnetGroupNotFoundFault: log.Info().Print("Subnet Group Does not exist: ErrCodeDBSubnetGroupNotFoundFault.", field.M{"app": a.name, "name": a.dbSubnetGroup}) default: - return errors.Wrapf(err, "Failed to delete subnet group. You may need to delete it manually. app=%s name=%s", a.name, a.dbSubnetGroup) + return errkit.Wrap(err, "Failed to delete subnet group. You may need to delete it manually.", "app", a.name, "name", a.dbSubnetGroup) } } } @@ -365,7 +365,7 @@ func (a *RDSAuroraMySQLDB) Uninstall(ctx context.Context) error { case "InvalidGroup.NotFound": log.Error().Print("Security group already deleted: InvalidGroup.NotFound.", field.M{"app": a.name, "name": a.securityGroupName}) default: - return errors.Wrapf(err, "Failed to delete security group. You may need to delete it manually. app=%s name=%s", a.name, a.securityGroupName) + return errkit.Wrap(err, "Failed to delete security group. You may need to delete it manually.", "app", a.name, "name", a.securityGroupName) } } } @@ -373,7 +373,7 @@ func (a *RDSAuroraMySQLDB) Uninstall(ctx context.Context) error { // Remove workload object created for executing commands err = a.cli.AppsV1().Deployments(a.namespace).Delete(ctx, a.bastionDebugWorkloadName, metav1.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { - return errors.Wrapf(err, "Error deleting Workload %s, app=%s", a.bastionDebugWorkloadName, a.name) + return errkit.Wrap(err, "Error deleting Workload", "deployment", a.bastionDebugWorkloadName, "app", a.name) } return nil } diff --git a/pkg/app/rds_postgres.go b/pkg/app/rds_postgres.go index d533aac731..839da42134 100644 --- a/pkg/app/rds_postgres.go +++ b/pkg/app/rds_postgres.go @@ -24,7 +24,7 @@ import ( awssdk "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" awsrds "github.com/aws/aws-sdk-go/service/rds" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -99,7 +99,7 @@ func (pdb *RDSPostgresDB) Init(ctx context.Context) error { if pdb.region == "" { pdb.region, ok = os.LookupEnv(aws.Region) if !ok { - return fmt.Errorf("env var %s is not set", aws.Region) + return errkit.New("env var is not set", "name", aws.Region) } } @@ -111,11 +111,11 @@ func (pdb *RDSPostgresDB) Init(ctx context.Context) error { pdb.accessID, ok = os.LookupEnv(aws.AccessKeyID) if !ok { - return fmt.Errorf("env var %s is not set", aws.AccessKeyID) + return errkit.New("env var is not set", "name", aws.AccessKeyID) } pdb.secretKey, ok = os.LookupEnv(aws.SecretAccessKey) if !ok { - return fmt.Errorf("env var %s is not set", aws.SecretAccessKey) + return errkit.New("env var is not set", "name", aws.SecretAccessKey) } return nil } @@ -127,7 +127,7 @@ func (pdb *RDSPostgresDB) Install(ctx context.Context, ns string) error { // Create AWS config awsConfig, region, err := pdb.getAWSConfig(ctx) if err != nil { - return errors.Wrapf(err, "app=%s", pdb.name) + return errkit.Wrap(err, "Error getting aws config", "app", pdb.name) } ec2Cli, err := ec2.NewClient(ctx, awsConfig, region) @@ -145,11 +145,11 @@ func (pdb *RDSPostgresDB) Install(ctx context.Context, ns string) error { deploymentSpec := bastionDebugWorkloadSpec(ctx, pdb.bastionDebugWorkloadName, "postgres", pdb.namespace) _, err = pdb.cli.AppsV1().Deployments(pdb.namespace).Create(ctx, deploymentSpec, metav1.CreateOptions{}) if err != nil { - return errors.Wrapf(err, "Failed to create deployment %s, app: %s", pdb.bastionDebugWorkloadName, pdb.name) + return errkit.Wrap(err, "Failed to create deployment", "deployment", pdb.bastionDebugWorkloadName, "app", pdb.name) } if err := kube.WaitOnDeploymentReady(ctx, pdb.cli, pdb.namespace, pdb.bastionDebugWorkloadName); err != nil { - return errors.Wrapf(err, "Failed while waiting for deployment %s to be ready, app: %s", pdb.bastionDebugWorkloadName, pdb.name) + return errkit.Wrap(err, "Failed while waiting for deployment to be ready", "deployment", pdb.bastionDebugWorkloadName, "app", pdb.name) } pdb.vpcID, err = vpcIDForRDSInstance(ctx, ec2Cli) @@ -279,7 +279,7 @@ func (pdb *RDSPostgresDB) Ping(ctx context.Context) error { return err } if databases == nil { - return errors.New("Databases are missing from configmap") + return errkit.New("Databases are missing from configmap") } isReadyQuery := fmt.Sprintf(postgresConnectionString+"'SELECT version();'", dbsecret.Data["password"], dbconfig.Data["postgres.host"], dbconfig.Data["postgres.user"], databases[0]) @@ -288,7 +288,7 @@ func (pdb *RDSPostgresDB) Ping(ctx context.Context) error { _, stderr, err := pdb.execCommand(ctx, pingCommand) if err != nil { - return errors.Wrapf(err, "Error while Pinging the database: %s, app: %s", stderr, pdb.name) + return errkit.Wrap(err, "Error while Pinging the database", "stderr", stderr, "app", pdb.name) } log.Print("Ping to the application was successful.", field.M{"app": pdb.name}) return nil @@ -303,7 +303,7 @@ func (pdb RDSPostgresDB) Insert(ctx context.Context) error { insertCommand := []string{"sh", "-c", insertQuery} _, stderr, err := pdb.execCommand(ctx, insertCommand) if err != nil { - return errors.Wrapf(err, "Error while inserting data into table: %s, app: %s", stderr, pdb.name) + return errkit.Wrap(err, "Error while inserting data into table", "stderr", stderr, "app", pdb.name) } log.Info().Print("Inserted a row in test db.", field.M{"app": pdb.name}) return nil @@ -317,12 +317,12 @@ func (pdb RDSPostgresDB) Count(ctx context.Context) (int, error) { countCommand := []string{"sh", "-c", countQuery} stdout, stderr, err := pdb.execCommand(ctx, countCommand) if err != nil { - return 0, errors.Wrapf(err, "Error while counting data of table: %s, app: %s", stderr, pdb.name) + return 0, errkit.Wrap(err, "Error while counting data of table", "stderr", stderr, "app", pdb.name) } rowsReturned, err := strconv.Atoi(stdout) if err != nil { - return 0, errors.Wrapf(err, "Error while converting response of count query: %s, app: %s", stderr, pdb.name) + return 0, errkit.Wrap(err, "Error while converting response of count query", "stderr", stderr, "app", pdb.name) } log.Info().Print("Counting rows in test db.", field.M{"app": pdb.name, "count": rowsReturned}) @@ -335,7 +335,7 @@ func (pdb RDSPostgresDB) Reset(ctx context.Context) error { deleteCommand := []string{"sh", "-c", deleteQuery} _, stderr, err := pdb.execCommand(ctx, deleteCommand) if err != nil { - return errors.Wrapf(err, "Error while deleting data from table: %s, app: %s", stderr, pdb.name) + return errkit.Wrap(err, "Error while deleting data from table", "stderr", stderr, "app", pdb.name) } log.Info().Print("Database reset successful!", field.M{"app": pdb.name}) return nil @@ -349,7 +349,7 @@ func (pdb RDSPostgresDB) Initialize(ctx context.Context) error { createCommand := []string{"sh", "-c", createQuery} _, stderr, err := pdb.execCommand(ctx, createCommand) if err != nil { - return errors.Wrapf(err, "Error while initializing the database: %s, app: %s", stderr, pdb.name) + return errkit.Wrap(err, "Error while initializing the database", "stderr", stderr, "app", pdb.name) } return nil } @@ -378,12 +378,12 @@ func (pdb RDSPostgresDB) Uninstall(ctx context.Context) error { // Create AWS config awsConfig, region, err := pdb.getAWSConfig(ctx) if err != nil { - return errors.Wrapf(err, "app=%s", pdb.name) + return errkit.Wrap(err, "Error getting aws config", "app", pdb.name) } // Create rds client rdsCli, err := rds.NewClient(ctx, awsConfig, region) if err != nil { - return errors.Wrap(err, "Failed to create rds client. You may need to delete RDS resources manually. app=rds-postgresql") + return errkit.Wrap(err, "Failed to create rds client. You may need to delete RDS resources manually. app=rds-postgresql") } // Delete rds instance @@ -395,7 +395,7 @@ func (pdb RDSPostgresDB) Uninstall(ctx context.Context) error { case awsrds.ErrCodeDBInstanceNotFoundFault: log.Info().Print("RDS instance already deleted: ErrCodeDBInstanceNotFoundFault.", field.M{"app": pdb.name, "id": pdb.id}) default: - return errors.Wrapf(err, "Failed to delete rds instance. You may need to delete it manually. app=rds-postgresql id=%s", pdb.id) + return errkit.Wrap(err, "Failed to delete rds instance. You may need to delete it manually.", "app", "rds-postgresql", "id", pdb.id) } } } @@ -405,14 +405,14 @@ func (pdb RDSPostgresDB) Uninstall(ctx context.Context) error { log.Info().Print("Waiting for rds to be deleted", field.M{"app": pdb.name}) err = rdsCli.WaitUntilDBInstanceDeleted(ctx, pdb.id) if err != nil { - return errors.Wrapf(err, "Failed to wait for rds instance till delete succeeds. app=rds-postgresql id=%s", pdb.id) + return errkit.Wrap(err, "Failed to wait for rds instance till delete succeeds.", "app", "rds-postgresql", "id", pdb.id) } } // Create ec2 client ec2Cli, err := ec2.NewClient(ctx, awsConfig, region) if err != nil { - return errors.Wrap(err, "Failed to ec2 client. You may need to delete EC2 resources manually. app=rds-postgresql") + return errkit.Wrap(err, "Failed to ec2 client. You may need to delete EC2 resources manually. app=rds-postgresql") } log.Info().Print("Deleting db subnet group.", field.M{"app": pdb.name}) @@ -424,7 +424,7 @@ func (pdb RDSPostgresDB) Uninstall(ctx context.Context) error { case awsrds.ErrCodeDBSubnetGroupNotFoundFault: log.Info().Print("Subnet Group Does not exist: ErrCodeDBSubnetGroupNotFoundFault.", field.M{"app": pdb.name, "id": pdb.id}) default: - return errors.Wrapf(err, "Failed to delete db subnet group. You may need to delete it manually. app=rds-postgresql name=%s", pdb.dbSubnetGroup) + return errkit.Wrap(err, "Failed to delete db subnet group. You may need to delete it manually.", "app", "rds-postgresql", "name", pdb.dbSubnetGroup) } } } @@ -438,14 +438,14 @@ func (pdb RDSPostgresDB) Uninstall(ctx context.Context) error { case "InvalidGroup.NotFound": log.Error().Print("Security group already deleted: InvalidGroup.NotFound.", field.M{"app": pdb.name, "name": pdb.securityGroupName}) default: - return errors.Wrapf(err, "Failed to delete security group. You may need to delete it manually. app=rds-postgresql name=%s", pdb.securityGroupName) + return errkit.Wrap(err, "Failed to delete security group. You may need to delete it manually.", "app", "rds-postgresql", "name", pdb.securityGroupName) } } } // Remove workload object created for executing commands err = pdb.cli.AppsV1().Deployments(pdb.namespace).Delete(ctx, pdb.bastionDebugWorkloadName, metav1.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { - return errors.Wrapf(err, "Error deleting Workload name=%s app=%s", pdb.bastionDebugWorkloadName, pdb.name) + return errkit.Wrap(err, "Error deleting Workload", "name", pdb.bastionDebugWorkloadName, "app", pdb.name) } return nil diff --git a/pkg/app/utils.go b/pkg/app/utils.go index e1e7bac061..75ae87df9a 100644 --- a/pkg/app/utils.go +++ b/pkg/app/utils.go @@ -19,13 +19,14 @@ import ( "fmt" "os" - "github.com/kanisterio/kanister/pkg/aws/ec2" - "github.com/kanisterio/kanister/pkg/aws/rds" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/rand" + + "github.com/kanisterio/kanister/pkg/aws/ec2" + "github.com/kanisterio/kanister/pkg/aws/rds" ) const ( @@ -118,7 +119,7 @@ func vpcIDForRDSInstance(ctx context.Context, ec2Cli *ec2.EC2) (string, error) { return "", err } if len(defaultVpc.Vpcs) == 0 { - return "", fmt.Errorf("No default VPC found") + return "", errkit.New("No default VPC found") } return *defaultVpc.Vpcs[0].VpcId, nil } @@ -128,7 +129,7 @@ func dbSubnetGroup(ctx context.Context, ec2Cli *ec2.EC2, rdsCli *rds.RDS, vpcID, // describe subnets in the VPC resp, err := ec2Cli.DescribeSubnets(ctx, vpcID) if err != nil { - return "", errors.Wrapf(err, "Failed to describe subnets") + return "", errkit.Wrap(err, "Failed to describe subnets") } // Extract subnet IDs from the response @@ -140,7 +141,7 @@ func dbSubnetGroup(ctx context.Context, ec2Cli *ec2.EC2, rdsCli *rds.RDS, vpcID, // create a subnetgroup with subnets in the VPC subnetGroup, err := rdsCli.CreateDBSubnetGroup(ctx, fmt.Sprintf("%s-subnetgroup", name), subnetGroupDescription, subnetIDs) if err != nil { - return "", errors.Wrapf(err, "Failed to create subnet group") + return "", errkit.Wrap(err, "Failed to create subnet group") } return *subnetGroup.DBSubnetGroup.DBSubnetGroupName, nil