Skip to content

Commit

Permalink
Fix linting typos and some tests to sidecar
Browse files Browse the repository at this point in the history
  • Loading branch information
AMecea committed May 8, 2019
1 parent 1473e93 commit d455691
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 57 deletions.
11 changes: 4 additions & 7 deletions pkg/controller/node/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,8 @@ func (r *ReconcileMysqlNode) Reconcile(request reconcile.Request) (reconcile.Res
return reconcile.Result{}, err
}

var sql SQLInterface
sql, err = r.getMySQLConnection(cluster, pod, creds)
if err != nil {
return reconcile.Result{}, err
}
// initialize SQL interface
sql := r.getMySQLConnection(cluster, pod, creds)

err = r.initializeMySQL(sql, cluster, creds)
if err != nil {
Expand Down Expand Up @@ -252,15 +249,15 @@ func (r *ReconcileMysqlNode) getNodeCluster(pod *corev1.Pod) (*mysqlcluster.Mysq
}

// getMySQLConnectionString returns the DSN that contains credentials to connect to given pod from a MySQL cluster
func (r *ReconcileMysqlNode) getMySQLConnection(cluster *mysqlcluster.MysqlCluster, pod *corev1.Pod, c *credentials) (SQLInterface, error) {
func (r *ReconcileMysqlNode) getMySQLConnection(cluster *mysqlcluster.MysqlCluster, pod *corev1.Pod, c *credentials) SQLInterface {
host := fmt.Sprintf("%s.%s.%s", pod.Spec.Hostname,
cluster.GetNameForResource(mysqlcluster.HeadlessSVC), pod.Namespace)

dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/?timeout=5s&multiStatements=true&interpolateParams=true",
c.User, c.Password, host, constants.MysqlPort,
)

return r.newSQLInterface(dsn, host), nil
return r.newSQLInterface(dsn, host)
}

type credentials struct {
Expand Down
18 changes: 2 additions & 16 deletions pkg/controller/node/node_ctrl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ package node

import (
"fmt"
"math/rand"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
gomegatypes "github.com/onsi/gomega/types"
"math/rand"

"golang.org/x/net/context"
corev1 "k8s.io/api/core/v1"
Expand All @@ -41,11 +37,8 @@ import (
"github.com/presslabs/mysql-operator/pkg/internal/mysqlcluster"
)

const timeout = time.Second * 2

var (
one = int32(1)
two = int32(2)
)

var _ = Describe("MysqlNode controller", func() {
Expand Down Expand Up @@ -182,6 +175,7 @@ func podKey(cluster *mysqlcluster.MysqlCluster, index int) types.NamespacedName
}
}

// nolint: unparam
func getOrCreatePod(c client.Client, cluster *mysqlcluster.MysqlCluster, index int) *corev1.Pod {
pod := &corev1.Pod{}
err := c.Get(context.TODO(), podKey(cluster, index), pod)
Expand Down Expand Up @@ -210,11 +204,3 @@ func getOrCreatePod(c client.Client, cluster *mysqlcluster.MysqlCluster, index i
Expect(err).To(BeNil())
return pod
}

func haveLabelWithValue(label, value string) gomegatypes.GomegaMatcher {
return PointTo(MatchFields(IgnoreExtras, Fields{
"ObjectMeta": MatchFields(IgnoreExtras, Fields{
"Labels": HaveKeyWithValue(label, value),
}),
}))
}
3 changes: 3 additions & 0 deletions pkg/controller/node/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
connRetry = 10
)

// SQLInterface expose abstract operations that can be applied on a MySQL node
type SQLInterface interface {
Wait() error
DisableSuperReadOnly() (func(), error)
Expand Down Expand Up @@ -207,6 +208,7 @@ func (r *nodeSQLRunner) dbConn() (*sql.DB, func(), error) {

func (r *nodeSQLRunner) SetPurgedGTID() error {
// first check if the GTID should be set, if the table exists or if the GTID was set before (used)
// nolint: gosec
qq := fmt.Sprintf("SELECT used FROM %[1]s.%[2]s WHERE id=1",
constants.OperatorDbName, constants.OperatorGtidsTableName)

Expand All @@ -226,6 +228,7 @@ func (r *nodeSQLRunner) SetPurgedGTID() error {
}

// GTID exists and should be set in a transaction
// nolint: gosec
query := fmt.Sprintf(`
SET @@SESSION.SQL_LOG_BIN = 0;
START TRANSACTION;
Expand Down
5 changes: 1 addition & 4 deletions pkg/controller/node/sql_fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import (
. "github.com/onsi/gomega"
)

type fakeSQLRunner struct {
dsn string
host string
}
type fakeSQLRunner struct{}

// test if fakeer implements interface
var _ SQLInterface = &fakeSQLRunner{}
Expand Down
2 changes: 2 additions & 0 deletions pkg/sidecar/appconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ func initFileQuery(cfg *Config, gtidPurged string) []byte {
[]string{"REPLICATION CLIENT"}, "*.*")...)

if len(gtidPurged) != 0 {
// If the xtrabackup information has GTID_PURGED then insert it into a table
// nolint: gosec
queries = append(queries, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %[1]s.%[2]s (
id int PRIMARY KEY,
Expand Down
24 changes: 24 additions & 0 deletions pkg/sidecar/appconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package sidecar
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"strings"
)

var _ = Describe("Test sidecar appconf", func() {
Expand Down Expand Up @@ -55,3 +56,26 @@ var _ = Describe("Test sidecar appconf", func() {
Expect(func() { createUserQuery("a", "b", "c", []string{"d"}, 1) }).To(Panic())
})
})

var _ = Describe("Test sidecar GTID extraction", func() {
It("should find the single gtid set in backup", func() {
var (
source string = "mysql-bin.000002 6552870 684ca0cf-495e-11e9-9fe8-0a580af407e9:1-176661\n"
result string = "684ca0cf-495e-11e9-9fe8-0a580af407e9:1-176661"
)
Expect(getGTIDFrom(strings.NewReader(source))).To(Equal(result))
})
It("should find all gtid sets in backup", func() {
var (
source string = `mysql-bin.006394 154349713 00003306-1111-0000-0000-000000000001:1-48861335,
00003306-1111-1111-1111-111111111111:1-11000155952,
00003306-2222-2222-2222-222222222222:1-8706021957
`
result string = "00003306-1111-0000-0000-000000000001:1-48861335," +
"00003306-1111-1111-1111-111111111111:1-11000155952," +
"00003306-2222-2222-2222-222222222222:1-8706021957"
)
Expect(getGTIDFrom(strings.NewReader(source))).To(Equal(result))
})

})
26 changes: 0 additions & 26 deletions pkg/sidecar/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package sidecar

import (
"bufio"
"database/sql"
"fmt"
"io"
"os"
Expand All @@ -30,31 +29,6 @@ import (

var log = logf.Log.WithName("sidecar")

// runQuery executes a query
func runQuery(cfg *Config, q string, args ...interface{}) error {
if len(cfg.MysqlDSN()) == -1 {
log.Info("could not get mysql connection DSN")
return fmt.Errorf("no DSN specified")
}

db, err := sql.Open("mysql", cfg.MysqlDSN())
if err != nil {
return err
}
defer func() {
if cErr := db.Close(); cErr != nil {
log.Error(cErr, "failed closing the database connection")
}
}()

log.V(1).Info("running query", "query", q)
if _, err := db.Exec(q, args...); err != nil {
return err
}

return nil
}

// copyFile the src file to dst. Any existing file will be overwritten and will not
// copy file attributes.
// nolint: gosec
Expand Down
5 changes: 1 addition & 4 deletions pkg/util/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ const (
// SlaveLagQuery in hack/charts/mysql-operator/values.yaml.
OperatorDbName = "sys_operator"

// OperatorInitTableName represents the name of the table that is used to mark configuration complete
// OperatorGtidsTableName represents the name of the table that is used to store the GTID
OperatorGtidsTableName = "gtids"

// ConfVolumeMountPath is the path where mysql configs will be mounted
ConfVolumeMountPath = "/etc/mysql"

// InitDBVolumeMountPath the path where to put *.sql init files. Docker entrypoint runs the scripts from this path.
InitDBVolumeMountPath = "/docker-entrypoint-initdb.d/"

// DataVolumeMountPath is the path to mysql data
DataVolumeMountPath = "/var/lib/mysql"

Expand Down

0 comments on commit d455691

Please sign in to comment.