forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqldb.go
More file actions
130 lines (115 loc) · 3.93 KB
/
Copy pathsqldb.go
File metadata and controls
130 lines (115 loc) · 3.93 KB
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
package sqldb
import (
"context"
"fmt"
"time"
"github.com/upper/db/v4"
mysqladp "github.com/upper/db/v4/adapter/mysql"
postgresqladp "github.com/upper/db/v4/adapter/postgresql"
"k8s.io/client-go/kubernetes"
"github.com/argoproj/argo-workflows/v3/config"
"github.com/argoproj/argo-workflows/v3/errors"
"github.com/argoproj/argo-workflows/v3/util"
)
func GetTableName(persistConfig *config.PersistConfig) (string, error) {
var tableName string
if persistConfig.PostgreSQL != nil {
tableName = persistConfig.PostgreSQL.TableName
} else if persistConfig.MySQL != nil {
tableName = persistConfig.MySQL.TableName
}
if tableName == "" {
return "", errors.InternalError("TableName is empty")
} else {
return tableName, nil
}
}
// CreateDBSession creates the dB session
func CreateDBSession(kubectlConfig kubernetes.Interface, namespace string, persistConfig *config.PersistConfig) (db.Session, 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, fmt.Errorf("no databases are configured")
}
// CreatePostGresDBSession creates postgresDB session
func CreatePostGresDBSession(kubectlConfig kubernetes.Interface, namespace string, cfg *config.PostgreSQLConfig, persistPool *config.ConnectionPool) (db.Session, error) {
ctx := context.Background()
userNameByte, err := util.GetSecrets(ctx, kubectlConfig, namespace, cfg.UsernameSecret.Name, cfg.UsernameSecret.Key)
if err != nil {
return nil, err
}
passwordByte, err := util.GetSecrets(ctx, kubectlConfig, namespace, cfg.PasswordSecret.Name, cfg.PasswordSecret.Key)
if err != nil {
return nil, err
}
settings := postgresqladp.ConnectionURL{
User: string(userNameByte),
Password: string(passwordByte),
Host: cfg.GetHostname(),
Database: cfg.Database,
}
if cfg.SSL {
if cfg.SSLMode != "" {
options := map[string]string{
"sslmode": cfg.SSLMode,
}
settings.Options = options
}
}
session, err := postgresqladp.Open(settings)
if err != nil {
return nil, err
}
session = ConfigureDBSession(session, persistPool)
return session, nil
}
// CreateMySQLDBSession creates Mysql DB session
func CreateMySQLDBSession(kubectlConfig kubernetes.Interface, namespace string, cfg *config.MySQLConfig, persistPool *config.ConnectionPool) (db.Session, error) {
if cfg.TableName == "" {
return nil, errors.InternalError("tableName is empty")
}
ctx := context.Background()
userNameByte, err := util.GetSecrets(ctx, kubectlConfig, namespace, cfg.UsernameSecret.Name, cfg.UsernameSecret.Key)
if err != nil {
return nil, err
}
passwordByte, err := util.GetSecrets(ctx, kubectlConfig, namespace, cfg.PasswordSecret.Name, cfg.PasswordSecret.Key)
if err != nil {
return nil, err
}
session, err := mysqladp.Open(mysqladp.ConnectionURL{
User: string(userNameByte),
Password: string(passwordByte),
Host: cfg.GetHostname(),
Database: cfg.Database,
Options: cfg.Options,
})
if err != nil {
return nil, err
}
session = ConfigureDBSession(session, persistPool)
// this is needed to make MySQL run in a Golang-compatible UTF-8 character set.
_, err = session.SQL().Exec("SET NAMES 'utf8mb4'")
if err != nil {
return nil, err
}
_, err = session.SQL().Exec("SET CHARACTER SET utf8mb4")
if err != nil {
return nil, err
}
return session, nil
}
// ConfigureDBSession configures the DB session
func ConfigureDBSession(session db.Session, persistPool *config.ConnectionPool) db.Session {
if persistPool != nil {
session.SetMaxOpenConns(persistPool.MaxOpenConns)
session.SetMaxIdleConns(persistPool.MaxIdleConns)
session.SetConnMaxLifetime(time.Duration(persistPool.ConnMaxLifetime))
}
return session
}