Skip to content

Commit

Permalink
fix: tableName is empty if wfc.session != nil (#10887)
Browse files Browse the repository at this point in the history
Signed-off-by: mikutas <23391543+mikutas@users.noreply.github.com>
Co-authored-by: Yuan Tang <terrytangyuan@gmail.com>
  • Loading branch information
mikutas and terrytangyuan committed Apr 13, 2023
1 parent 8582618 commit b904851
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
51 changes: 31 additions & 20 deletions persist/sqldb/sqldb.go
Expand Up @@ -15,34 +15,45 @@ import (
"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) (sqlbuilder.Database, string, error) {
func CreateDBSession(kubectlConfig kubernetes.Interface, namespace string, persistConfig *config.PersistConfig) (sqlbuilder.Database, error) {
if persistConfig == nil {
return nil, "", errors.InternalError("Persistence config is not found")
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")
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")
}

func CreatePostGresDBSession(kubectlConfig kubernetes.Interface, namespace string, cfg *config.PostgreSQLConfig, persistPool *config.ConnectionPool) (sqlbuilder.Database, error) {
ctx := context.Background()
userNameByte, err := util.GetSecrets(ctx, kubectlConfig, namespace, cfg.UsernameSecret.Name, cfg.UsernameSecret.Key)
if err != nil {
return nil, "", err
return nil, err
}
passwordByte, err := util.GetSecrets(ctx, kubectlConfig, namespace, cfg.PasswordSecret.Name, cfg.PasswordSecret.Key)
if err != nil {
return nil, "", err
return nil, err
}

settings := postgresql.ConnectionURL{
Expand All @@ -63,26 +74,26 @@ func CreatePostGresDBSession(kubectlConfig kubernetes.Interface, namespace strin

session, err := postgresql.Open(settings)
if err != nil {
return nil, "", err
return nil, err
}
session = ConfigureDBSession(session, persistPool)
return session, cfg.TableName, nil
return session, nil
}

// CreateMySQLDBSession creates Mysql DB session
func CreateMySQLDBSession(kubectlConfig kubernetes.Interface, namespace string, cfg *config.MySQLConfig, persistPool *config.ConnectionPool) (sqlbuilder.Database, string, error) {
func CreateMySQLDBSession(kubectlConfig kubernetes.Interface, namespace string, cfg *config.MySQLConfig, persistPool *config.ConnectionPool) (sqlbuilder.Database, error) {
if cfg.TableName == "" {
return nil, "", errors.InternalError("tableName is empty")
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
return nil, err
}
passwordByte, err := util.GetSecrets(ctx, kubectlConfig, namespace, cfg.PasswordSecret.Name, cfg.PasswordSecret.Key)
if err != nil {
return nil, "", err
return nil, err
}

session, err := mysql.Open(mysql.ConnectionURL{
Expand All @@ -93,19 +104,19 @@ func CreateMySQLDBSession(kubectlConfig kubernetes.Interface, namespace string,
Options: cfg.Options,
})
if err != nil {
return nil, "", err
return nil, err
}
session = ConfigureDBSession(session, persistPool)
// 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
return nil, err
}
_, err = session.Exec("SET CHARACTER SET utf8mb4")
if err != nil {
return nil, "", err
return nil, err
}
return session, cfg.TableName, nil
return session, nil
}

// ConfigureDBSession configures the DB session
Expand Down
6 changes: 5 additions & 1 deletion server/apiserver/argoserver.go
Expand Up @@ -204,7 +204,11 @@ func (as *argoServer) Run(ctx context.Context, port int, browserOpenFunc func(st
wfArchive := sqldb.NullWorkflowArchive
persistence := config.Persistence
if persistence != nil {
session, tableName, err := sqldb.CreateDBSession(as.clients.Kubernetes, as.namespace, persistence)
session, err := sqldb.CreateDBSession(as.clients.Kubernetes, as.namespace, persistence)
if err != nil {
log.Fatal(err)
}
tableName, err := sqldb.GetTableName(persistence)
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 5 additions & 1 deletion test/e2e/fixtures/persistence.go
Expand Up @@ -24,7 +24,11 @@ func newPersistence(kubeClient kubernetes.Interface, wcConfig *config.Config) *P
if persistence.MySQL != nil {
persistence.MySQL.Host = "localhost"
}
session, tableName, err := sqldb.CreateDBSession(kubeClient, Namespace, persistence)
session, err := sqldb.CreateDBSession(kubeClient, Namespace, persistence)
if err != nil {
panic(err)
}
tableName, err := sqldb.GetTableName(persistence)
if err != nil {
panic(err)
}
Expand Down
7 changes: 5 additions & 2 deletions workflow/controller/config.go
Expand Up @@ -30,9 +30,12 @@ func (wfc *WorkflowController) updateConfig() error {
persistence := wfc.Config.Persistence
if persistence != nil {
log.Info("Persistence configuration enabled")
var tableName string
tableName, err := sqldb.GetTableName(persistence)
if err != nil {
return err
}
if wfc.session == nil {
session, tableName, err := sqldb.CreateDBSession(wfc.kubeclientset, wfc.namespace, persistence)
session, err := sqldb.CreateDBSession(wfc.kubeclientset, wfc.namespace, persistence)
if err != nil {
return err
}
Expand Down

0 comments on commit b904851

Please sign in to comment.