Skip to content

Commit

Permalink
Refactor MySQL app and blueprint to remove unnecessary code (kanister…
Browse files Browse the repository at this point in the history
…io#606)

* Refactore MySQL app and blueprint

* Fixing golint

* Address review comments

* Update examples/stable/mysql-deploymentconfig/mysql-dep-config-blueprint.yaml

Co-Authored-By: Prasad Ghangal <prasad.ghangal@gmail.com>

Co-authored-by: Prasad Ghangal <prasad.ghangal@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 6, 2020
1 parent bcc06f0 commit 57811b8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ actions:
- pipefail
- -c
- |
date
s3_path="/mysql-backups/{{ .DeploymentConfig.Namespace }}/{{ .DeploymentConfig.Name }}/{{ toDate "2006-01-02T15:04:05.999999999Z07:00" .Time | date "2006-01-02T15-04-05" }}/dump.sql.gz"
root_password="{{ index .Phases.dumpToObjectStore.Secrets.mysqlsecret.Data "database-root-password" | toString }}"
mysqldump --column-statistics=0 -u root --password=${root_password} -h {{ .DeploymentConfig.Name }} --single-transaction --all-databases | gzip - | kando location push --profile '{{ toJson .Profile }}' --path ${s3_path} -
Expand Down
4 changes: 1 addition & 3 deletions pkg/app/mongodb-deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ type MongoDBDepConfig struct {
name string
namespace string
user string
label string
osClient openshift.OSClient
params map[string]string
storageType storage
Expand All @@ -56,7 +55,6 @@ func NewMongoDBDepConfig(name string, storageType storage) App {
params: map[string]string{
"MONGODB_ADMIN_PASSWORD": "secretpassword",
},
label: getLabelOfApp(mongoDepConfigName),
osClient: openshift.NewOpenShiftClient(),
storageType: storageType,
}
Expand Down Expand Up @@ -111,7 +109,7 @@ func (mongo *MongoDBDepConfig) Object() crv1alpha1.ObjectReference {
}

func (mongo *MongoDBDepConfig) Uninstall(ctx context.Context) error {
_, err := mongo.osClient.DeleteApp(ctx, mongo.namespace, mongo.label)
_, err := mongo.osClient.DeleteApp(ctx, mongo.namespace, getLabelOfApp(mongoDepConfigName, mongo.storageType))
return err
}

Expand Down
142 changes: 21 additions & 121 deletions pkg/app/mysql-deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,17 @@ import (

osversioned "github.com/openshift/client-go/apps/clientset/versioned"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/log"
"github.com/kanisterio/kanister/pkg/openshift"
"github.com/kanisterio/kanister/pkg/poll"
)

const (
mysqlDepConfigWaitTimeout = 5 * time.Minute
mysqlToolPodName = "mysql-tools-pod"
mysqlToolContainerName = "mysql-tools-container"
mysqlToolImage = "kanisterio/mysql-sidecar:0.26.0"
mysqlDepConfigName = "mysql"
)

Expand All @@ -48,17 +42,23 @@ type MysqlDepConfig struct {
osCli osversioned.Interface
name string
namespace string
envVar map[string]string
params map[string]string
storageType storage
osClient openshift.OSClient
envVar map[string]string
}

func NewMysqlDepConfig(name string, storageType storage) App {
return &MysqlDepConfig{
name: name,
params: map[string]string{
"MYSQL_ROOT_PASSWORD": "secretpassword",
},
envVar: map[string]string{
"MYSQL_ROOT_PASSWORD": "secretpassword",
},
storageType: storageType,
osClient: openshift.NewOpenShiftClient(),
}
}

Expand All @@ -84,78 +84,9 @@ func (mdep *MysqlDepConfig) Install(ctx context.Context, namespace string) error
dbTemplate := getOpenShiftDBTemplate(mysqlDepConfigName, mdep.storageType)

oc := openshift.NewOpenShiftClient()
_, err := oc.NewApp(ctx, mdep.namespace, dbTemplate, mdep.envVar, nil)
if err != nil {
return errors.Wrapf(err, "Error installing app %s on openshift cluster.", mdep.name)
}

// we are creating a tool pod will be executing the commands from this pod
// because we are not able to login to deployment config mysql instance using the env var
// MYSQL_ROOT_PASSWORD that gets set
err = mdep.createMySQLToolsPod(ctx)
if err != nil {
return errors.Wrapf(err, "Error creating mysql tools pod")
}

return mdep.createMySQLSecret(ctx)
}
_, err := oc.NewApp(ctx, mdep.namespace, dbTemplate, nil, mdep.params)

// createMySQLSecret creates a secret that will be used to login to running MYSQL instance
func (mdep *MysqlDepConfig) createMySQLSecret(ctx context.Context) error {
mysqlSecret := &v1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: mdep.name,
Namespace: mdep.namespace,
},
Data: map[string][]byte{
"mysql-root-password": []byte(mdep.envVar["MYSQL_ROOT_PASSWORD"]),
},
}

_, err := mdep.cli.CoreV1().Secrets(mdep.namespace).Create(mysqlSecret)

return errors.Wrapf(err, "Error creating secret for mysqldepconf app.")
}

// createMySQLToolsPod creates a pod that will be use to run command into mysql instance
func (mdep *MysqlDepConfig) createMySQLToolsPod(ctx context.Context) error {
mysqlToolPod := &v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: mysqlToolPodName,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Name: mysqlToolContainerName,
Image: mysqlToolImage,
Resources: v1.ResourceRequirements{},
Env: []v1.EnvVar{
v1.EnvVar{
Name: "MYSQL_ROOT_PASSWORD",
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{Name: mdep.name},
Key: "mysql-root-password",
},
},
},
},
},
},
},
}

_, err := mdep.cli.CoreV1().Pods(mdep.namespace).Create(mysqlToolPod)

return errors.Wrapf(err, "Error creating mysql tools pod")
return errors.Wrapf(err, "Error installing app %s on openshift cluster.", mdep.name)
}

func (mdep *MysqlDepConfig) IsReady(ctx context.Context) (bool, error) {
Expand All @@ -168,17 +99,6 @@ func (mdep *MysqlDepConfig) IsReady(ctx context.Context) (bool, error) {
return false, err
}

log.Print("Waiting for the MySQL tools pod to be ready.", field.M{"app": mdep.name})
// wait for the tools pod to be ready
err = poll.Wait(ctx, func(ctx context.Context) (bool, error) {
running, err := kube.IsPodRunning(mdep.cli, mysqlToolPodName, mdep.namespace)
return running, err
})

if err != nil {
return false, errors.Wrapf(err, "Error while waiting for mysql tools pod to get ready.")
}

log.Print("Application is ready", field.M{"application": mdep.name})
return true, nil
}
Expand All @@ -192,24 +112,14 @@ func (mdep *MysqlDepConfig) Object() crv1alpha1.ObjectReference {
}

func (mdep *MysqlDepConfig) Uninstall(ctx context.Context) error {
// delete MySQL tools pod
err := mdep.cli.CoreV1().Pods(mdep.namespace).Delete(mysqlToolPodName, &metav1.DeleteOptions{})
if err != nil {
return errors.Wrapf(err, fmt.Sprintf("Error deleting pod %s from namespace %s while uninstalling application %s.", mysqlToolPodName, mdep.namespace, mdep.name))
}
// delete secret
err = mdep.cli.CoreV1().Secrets(mdep.namespace).Delete(mdep.name, &metav1.DeleteOptions{})
if err != nil {
return errors.Wrapf(err, fmt.Sprintf("Error deleting secret %s from namespace %s while uninstalling appliation %s.", mdep.name, mdep.namespace, mdep.name))
}
// deleting deployment config that is running the mysql instance
return mdep.osCli.AppsV1().DeploymentConfigs(mdep.namespace).Delete(mysqlDepConfigName, &metav1.DeleteOptions{})
_, err := mdep.osClient.DeleteApp(ctx, mdep.namespace, getLabelOfApp(mysqlDepConfigName, mdep.storageType))
return err
}

func (mdep *MysqlDepConfig) Ping(ctx context.Context) error {
log.Print("Pinging the application", field.M{"app": mdep.name})

pingCMD := []string{"sh", "-c", fmt.Sprintf("mysql -u root --password=$MYSQL_ROOT_PASSWORD -h %s -e 'show databases;'", mysqlDepConfigName)}
pingCMD := []string{"bash", "-c", fmt.Sprintf("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)
Expand All @@ -221,7 +131,7 @@ func (mdep *MysqlDepConfig) Ping(ctx context.Context) error {
func (mdep *MysqlDepConfig) Insert(ctx context.Context) error {
log.Print("Inserting some records in mysql instance.", field.M{"app": mdep.name})

insertRecordCMD := []string{"sh", "-c", fmt.Sprintf("mysql -u root --password=$MYSQL_ROOT_PASSWORD -h %s -e 'use testdb; INSERT INTO pets VALUES (\"Puffball\",\"Diane\",\"hamster\",\"f\",\"1999-03-30\",NULL); '", mysqlDepConfigName)}
insertRecordCMD := []string{"bash", "-c", fmt.Sprintf("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)
Expand All @@ -234,7 +144,7 @@ func (mdep *MysqlDepConfig) Insert(ctx context.Context) error {
func (mdep *MysqlDepConfig) Count(ctx context.Context) (int, error) {
log.Print("Counting the records from the mysql instance.", field.M{"app": mdep.name})

selectRowsCMD := []string{"sh", "-c", fmt.Sprintf("mysql -u root --password=$MYSQL_ROOT_PASSWORD -h %s -e 'use testdb; select count(*) from pets; '", mysqlDepConfigName)}
selectRowsCMD := []string{"bash", "-c", fmt.Sprintf("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)
Expand All @@ -254,14 +164,14 @@ func (mdep *MysqlDepConfig) Reset(ctx context.Context) error {
log.Print("Resetting the mysql instance.", field.M{"app": "mysql"})

// delete all the data from the table
deleteCMD := []string{"sh", "-c", fmt.Sprintf("mysql -u root --password=$MYSQL_ROOT_PASSWORD -h %s -e 'DROP DATABASE IF EXISTS testdb'", mysqlDepConfigName)}
deleteCMD := []string{"bash", "-c", fmt.Sprintf("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)
}

// create the database and a pets dummy table
createCMD := []string{"sh", "-c", fmt.Sprintf("mysql -u root --password=$MYSQL_ROOT_PASSWORD -h %s -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);'", mysqlDepConfigName)}
createCMD := []string{"bash", "-c", fmt.Sprintf("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)
Expand All @@ -271,22 +181,12 @@ func (mdep *MysqlDepConfig) Reset(ctx context.Context) error {
return nil
}

func (mdep *MysqlDepConfig) ConfigMaps() map[string]crv1alpha1.ObjectReference {
return nil
}

func (mdep *MysqlDepConfig) Secrets() map[string]crv1alpha1.ObjectReference {
return map[string]crv1alpha1.ObjectReference{
"mysql": crv1alpha1.ObjectReference{
Kind: "Secret",
Name: mdep.name,
Namespace: mdep.namespace,
},
}
}

func (mdep *MysqlDepConfig) execCommand(ctx context.Context, command []string) (string, string, error) {
stdout, stderr, err := kube.Exec(mdep.cli, mdep.namespace, mysqlToolPodName, mysqlToolContainerName, command, nil)
podName, containerName, err := kube.GetPodContainerFromDeploymentConfig(ctx, mdep.osCli, mdep.cli, mdep.namespace, mysqlDepConfigName)
if err != nil {
return "", "", err
}
stdout, stderr, err := kube.Exec(mdep.cli, mdep.namespace, podName, containerName, command, nil)
if err != nil {
return stdout, stderr, errors.Wrapf(err, "Error executing command in the pod.")
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/app/postgresql-deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type PostgreSQLDepConfig struct {
osCli osversioned.Interface
namespace string
opeshiftClient openshift.OSClient
label string
envVar map[string]string
storageType storage
}
Expand All @@ -54,7 +53,6 @@ func NewPostgreSQLDepConfig(name string, storageType storage) App {
return &PostgreSQLDepConfig{
name: name,
opeshiftClient: openshift.NewOpenShiftClient(),
label: getLabelOfApp(postgresDepConfigName),
envVar: map[string]string{
"POSTGRESQL_ADMIN_PASSWORD": "secretpassword",
},
Expand Down Expand Up @@ -135,7 +133,7 @@ func (pgres *PostgreSQLDepConfig) Object() crv1alpha1.ObjectReference {
}

func (pgres *PostgreSQLDepConfig) Uninstall(ctx context.Context) error {
_, err := pgres.opeshiftClient.DeleteApp(ctx, pgres.namespace, pgres.label)
_, err := pgres.opeshiftClient.DeleteApp(ctx, pgres.namespace, getLabelOfApp(postgresDepConfigName, pgres.storageType))
return err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ func getOpenShiftDBTemplate(appName string, storageType storage) string {

// getLabelOfApp returns label of the passed application this label can be
// used to delete all the resources that were created while deploying this application
func getLabelOfApp(appName string) string {
return fmt.Sprintf("app=%s-persistent", appName)
func getLabelOfApp(appName string, storageType storage) string {
return fmt.Sprintf("app=%s-%s", appName, storageType)
}

0 comments on commit 57811b8

Please sign in to comment.