Skip to content

Commit

Permalink
Errkit migration 3 (pkg/app) (#2892)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
e-sumin and mergify[bot] committed Jun 26, 2024
1 parent 3f35d52 commit 9919e93
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 248 deletions.
3 changes: 2 additions & 1 deletion pkg/app/bp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
24 changes: 12 additions & 12 deletions pkg/app/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -210,23 +210,23 @@ 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
createTab := []string{"sh", "-c", fmt.Sprintf("cqlsh -u cassandra -p $CASSANDRA_PASSWORD -e \"create table "+
"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
}

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)
}
51 changes: 26 additions & 25 deletions pkg/app/cockroachdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand All @@ -92,66 +92,67 @@ 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"])

createCrtDirCmd := "mkdir -p /cockroach/cockroach-client-certs"
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
Expand All @@ -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 {
Expand All @@ -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})
Expand All @@ -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
}
Expand All @@ -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})
Expand All @@ -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
Expand All @@ -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"})
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Loading

0 comments on commit 9919e93

Please sign in to comment.