-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
sqldb.go
120 lines (102 loc) · 3.57 KB
/
sqldb.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package sqldb
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"upper.io/db.v3/lib/sqlbuilder"
"upper.io/db.v3/mysql"
"upper.io/db.v3/postgresql"
"github.com/argoproj/argo/config"
"github.com/argoproj/argo/errors"
"github.com/argoproj/argo/util"
)
// CreateDBSession creates the dB session
func CreateDBSession(kubectlConfig kubernetes.Interface, namespace string, persistConfig *config.PersistConfig) (sqlbuilder.Database, string, error) {
if persistConfig == nil {
return nil, "", errors.InternalError("Persistence config is not found")
}
log.Info("Creating DB session")
if persistConfig.PostgreSQL != nil {
return CreatePostGresDBSession(kubectlConfig, namespace, persistConfig.PostgreSQL, persistConfig.ConnectionPool)
} else if persistConfig.MySQL != nil {
return CreateMySQLDBSession(kubectlConfig, namespace, persistConfig.MySQL, persistConfig.ConnectionPool)
}
return nil, "", fmt.Errorf("no databases are configured")
}
// CreatePostGresDBSession creates postgresDB session
func CreatePostGresDBSession(kubectlConfig kubernetes.Interface, namespace string, cfg *config.PostgreSQLConfig, persistPool *config.ConnectionPool) (sqlbuilder.Database, string, error) {
if cfg.TableName == "" {
return nil, "", errors.InternalError("tableName is empty")
}
userNameByte, err := util.GetSecrets(kubectlConfig, namespace, cfg.UsernameSecret.Name, cfg.UsernameSecret.Key)
if err != nil {
return nil, "", err
}
passwordByte, err := util.GetSecrets(kubectlConfig, namespace, cfg.PasswordSecret.Name, cfg.PasswordSecret.Key)
if err != nil {
return nil, "", err
}
var settings = postgresql.ConnectionURL{
User: string(userNameByte),
Password: string(passwordByte),
Host: cfg.Host + ":" + cfg.Port,
Database: cfg.Database,
}
if cfg.SSL {
if cfg.SSLMode != "" {
options := map[string]string{
"sslmode": cfg.SSLMode,
}
settings.Options = options
}
}
session, err := postgresql.Open(settings)
if err != nil {
return nil, "", err
}
if persistPool != nil {
session.SetMaxOpenConns(persistPool.MaxOpenConns)
session.SetMaxIdleConns(persistPool.MaxIdleConns)
session.SetConnMaxLifetime(time.Duration(persistPool.ConnMaxLifetime))
}
return session, cfg.TableName, nil
}
// CreateMySQLDBSession creates Mysql DB session
func CreateMySQLDBSession(kubectlConfig kubernetes.Interface, namespace string, cfg *config.MySQLConfig, persistPool *config.ConnectionPool) (sqlbuilder.Database, string, error) {
if cfg.TableName == "" {
return nil, "", errors.InternalError("tableName is empty")
}
userNameByte, err := util.GetSecrets(kubectlConfig, namespace, cfg.UsernameSecret.Name, cfg.UsernameSecret.Key)
if err != nil {
return nil, "", err
}
passwordByte, err := util.GetSecrets(kubectlConfig, namespace, cfg.PasswordSecret.Name, cfg.PasswordSecret.Key)
if err != nil {
return nil, "", err
}
session, err := mysql.Open(mysql.ConnectionURL{
User: string(userNameByte),
Password: string(passwordByte),
Host: cfg.Host + ":" + cfg.Port,
Database: cfg.Database,
})
if err != nil {
return nil, "", err
}
if persistPool != nil {
session.SetMaxOpenConns(persistPool.MaxOpenConns)
session.SetMaxIdleConns(persistPool.MaxIdleConns)
session.SetConnMaxLifetime(time.Duration(persistPool.ConnMaxLifetime))
}
// this is needed to make MySQL run in a Golang-compatible UTF-8 character set.
_, err = session.Exec("SET NAMES 'utf8mb4'")
if err != nil {
return nil, "", err
}
_, err = session.Exec("SET CHARACTER SET utf8mb4")
if err != nil {
return nil, "", err
}
return session, cfg.TableName, nil
}