-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
sqldb.go
138 lines (108 loc) · 4.34 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package sqldb
import (
"fmt"
"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/errors"
"github.com/argoproj/argo/util"
"github.com/argoproj/argo/workflow/config"
)
const (
CodeInvalidDBSession = "ERR_INVALID_DB_SESSION"
CodeDBUpdateRowNotFound = "ERR_DB_UPDATE_ROW_NOT_FOUND"
CodeDBOperationError = "ERR_DB_OPERATION_ERROR"
)
func DBInvalidSession(err error, message ...string) error {
if len(message) == 0 {
return errors.Wrap(err, CodeInvalidDBSession, err.Error())
}
return errors.Wrap(err, CodeInvalidDBSession, message[0])
}
func DBOperationError(err error, message ...string) error {
if len(message) == 0 {
return errors.Wrap(err, CodeDBOperationError, err.Error())
}
return errors.Wrap(err, CodeInvalidDBSession, message[0])
}
func DBUpdateNoRowFoundError(err error, message ...string) error {
if len(message) == 0 {
return errors.Wrap(err, CodeDBUpdateRowNotFound, err.Error())
}
return errors.Wrap(err, CodeDBUpdateRowNotFound, message[0])
}
// InternalWrapErrorf annotates the error with the ERR_INTERNAL code and a stack trace, optional message
func DBUpdateNoRowFoundErrorf(err error, format string, args ...interface{}) error {
return errors.Wrap(err, CodeDBUpdateRowNotFound, fmt.Sprintf(format, args...))
}
// 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")
}
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, "", nil
}
// CreatePostGresDBSession creates postgresDB session
func CreatePostGresDBSession(kubectlConfig kubernetes.Interface, namespace string, postgresConfig *config.PostgreSQLConfig, persistPool *config.ConnectionPool) (sqlbuilder.Database, string, error) {
if postgresConfig.TableName == "" {
return nil, "", errors.InternalError("TableName is empty")
}
userNameByte, err := util.GetSecrets(kubectlConfig, namespace, postgresConfig.UsernameSecret.Name, postgresConfig.UsernameSecret.Key)
if err != nil {
return nil, postgresConfig.TableName, err
}
passwordByte, err := util.GetSecrets(kubectlConfig, namespace, postgresConfig.PasswordSecret.Name, postgresConfig.PasswordSecret.Key)
if err != nil {
return nil, postgresConfig.TableName, err
}
var settings = postgresql.ConnectionURL{
User: string(userNameByte),
Password: string(passwordByte),
Host: postgresConfig.Host + ":" + postgresConfig.Port,
Database: postgresConfig.Database,
}
session, err := postgresql.Open(settings)
if err != nil {
return nil, postgresConfig.TableName, err
}
if persistPool != nil {
session.SetMaxOpenConns(persistPool.MaxOpenConns)
session.SetMaxIdleConns(persistPool.MaxIdleConns)
}
return session, postgresConfig.TableName, err
}
// CreatePostGresDBSession creates Mysql DB session
func CreateMySQLDBSession(kubectlConfig kubernetes.Interface, namespace string, mysqlConfig *config.MySQLConfig, persistPool *config.ConnectionPool) (sqlbuilder.Database, string, error) {
if mysqlConfig.TableName == "" {
return nil, "", errors.InternalError("TableName is empty")
}
userNameByte, err := util.GetSecrets(kubectlConfig, namespace, mysqlConfig.UsernameSecret.Name, mysqlConfig.UsernameSecret.Key)
if err != nil {
return nil, mysqlConfig.TableName, err
}
passwordByte, err := util.GetSecrets(kubectlConfig, namespace, mysqlConfig.PasswordSecret.Name, mysqlConfig.PasswordSecret.Key)
if err != nil {
return nil, mysqlConfig.TableName, err
}
var settings = mysql.ConnectionURL{
User: string(userNameByte),
Password: string(passwordByte),
Host: mysqlConfig.Host + ":" + mysqlConfig.Port,
Database: mysqlConfig.Database,
}
session, err := mysql.Open(settings)
if err != nil {
return nil, mysqlConfig.TableName, err
}
if persistPool != nil {
session.SetMaxOpenConns(persistPool.MaxOpenConns)
session.SetMaxIdleConns(persistPool.MaxIdleConns)
}
return session, mysqlConfig.TableName, err
}