-
Notifications
You must be signed in to change notification settings - Fork 57
/
cn_mysql.go
94 lines (83 loc) · 2.89 KB
/
cn_mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cn
import (
"context"
"database/sql"
"fmt"
"github.com/go-logr/logr"
_ "github.com/go-sql-driver/mysql" // import mysql driver
appv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
srapi "github.com/StarRocks/starrocks-kubernetes-operator/pkg/apis/starrocks/v1"
"github.com/StarRocks/starrocks-kubernetes-operator/pkg/k8sutils"
"github.com/StarRocks/starrocks-kubernetes-operator/pkg/k8sutils/load"
)
// SQLExecutor is used to execute sql statements.
// Component CN needs to connect to mysql and execute sql statements. E.g.: When StarRocksWarehouse is deleted, the
// related 'DROP WAREHOUSE <name>' statement needs to be executed.
type SQLExecutor struct {
RootPassword string
FeServiceName string
FeServicePort string
}
// NewSQLExecutor creates a SQLExecutor instance. It will get the root password, fe service name, and fe service port
// from the environment variables of the component CN.
func NewSQLExecutor(ctx context.Context, k8sClient client.Client, namespace, aliasName string) (*SQLExecutor, error) {
rootPassword := ""
feServiceName := ""
feServicePort := ""
logger := logr.FromContextOrDiscard(ctx)
var est appv1.StatefulSet
if err := k8sClient.Get(ctx,
types.NamespacedName{
Namespace: namespace,
Name: load.Name(aliasName, (*srapi.StarRocksCnSpec)(nil)),
},
&est); err != nil {
return nil, err
}
var err error
for _, envVar := range est.Spec.Template.Spec.Containers[0].Env {
if envVar.Name == "MYSQL_PWD" {
rootPassword, err = k8sutils.GetEnvVarValue(ctx, k8sClient, namespace, envVar)
if err != nil {
logger.Error(err, "failed to get MYSQL_PWD from env vars, use the default password: empty string")
}
} else if envVar.Name == "FE_SERVICE_NAME" {
feServiceName, err = k8sutils.GetEnvVarValue(ctx, k8sClient, namespace, envVar)
if err != nil {
logger.Error(err, "failed to get FE_SERVICE_NAME from env vars")
return nil, err
}
} else if envVar.Name == "FE_QUERY_PORT" {
feServicePort, err = k8sutils.GetEnvVarValue(ctx, k8sClient, namespace, envVar)
if err != nil {
logger.Error(err, "failed to get FE_QUERY_PORT from env vars")
return nil, err
}
}
}
return &SQLExecutor{
RootPassword: rootPassword,
FeServiceName: feServiceName,
FeServicePort: feServicePort,
}, nil
}
// Execute sql statements. Every time a SQL statement needs to be executed, a new sql.DB instance will be created.
// This is because SQL statements are executed infrequently.
func (executor *SQLExecutor) Execute(ctx context.Context, db *sql.DB, statements string) error {
var err error
if db == nil {
db, err = sql.Open("mysql", fmt.Sprintf("root:%s@tcp(%s:%s)/",
executor.RootPassword, executor.FeServiceName, executor.FeServicePort))
if err != nil {
return err
}
defer db.Close()
}
_, err = db.ExecContext(ctx, statements)
if err != nil {
return err
}
return nil
}