Skip to content

Commit

Permalink
fix: oceanbase switchover failed with wrong password (#7124)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuriwuyun authored Apr 22, 2024
1 parent b074d32 commit 9dcd491
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
1 change: 1 addition & 0 deletions pkg/lorry/dcs/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func (store *KubernetesStore) GetMembers() ([]Member, error) {
member.Name = pod.Name
// member.Name = fmt.Sprintf("%s.%s-headless.%s.svc", pod.Name, store.clusterCompName, store.namespace)
member.Role = pod.Labels[constant.RoleLabelKey]
member.ComponentName = pod.Labels[constant.KBAppComponentLabelKey]
member.PodIP = pod.Status.PodIP
member.DBPort = getDBPort(&pod)
member.LorryPort = getLorryPort(&pod)
Expand Down
21 changes: 11 additions & 10 deletions pkg/lorry/dcs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,17 @@ type DBState struct {
Extra map[string]string
}
type Member struct {
Index string
Name string
Role string
PodIP string
DBPort string
LorryPort string
HAPort string
UID string
UseIP bool
resource any
Index string
Name string
Role string
PodIP string
DBPort string
LorryPort string
HAPort string
UID string
UseIP bool
resource any
ComponentName string
}

func (m *Member) GetName() string {
Expand Down
50 changes: 47 additions & 3 deletions pkg/lorry/engines/oceanbase/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package oceanbase

import (
"github.com/apecloud/kubeblocks/pkg/lorry/engines/mysql"
"database/sql"
"strings"
"time"

"github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
"github.com/spf13/viper"

"github.com/apecloud/kubeblocks/pkg/lorry/dcs"
mysqlengine "github.com/apecloud/kubeblocks/pkg/lorry/engines/mysql"
)

type Config struct {
*mysql.Config
*mysqlengine.Config
}

var config *Config

func NewConfig(properties map[string]string) (*Config, error) {
mysqlConfig, err := mysql.NewConfig(properties)
mysqlConfig, err := mysqlengine.NewConfig(properties)
if err != nil {
return nil, err
}
Expand All @@ -43,3 +52,38 @@ func NewConfig(properties map[string]string) (*Config, error) {
}
return config, nil
}

func getRootPassword(compName string) string {
rootPasswordEnv := "OB_ROOT_PASSWD"

if compName == "" {
compName = viper.GetString("KB_COMP_NAME")
}

if compName != "" {
compName = strings.ToUpper(compName)
compName = strings.ReplaceAll(compName, "-", "_")
rootPasswordEnv = rootPasswordEnv + "_" + compName
}
return viper.GetString(rootPasswordEnv)
}

func (config *Config) GetMemberRootDBConn(cluster *dcs.Cluster, member *dcs.Member) (*sql.DB, error) {
addr := cluster.GetMemberAddrWithPort(*member)
mysqlConfig, err := mysql.ParseDSN(config.URL)
if err != nil {
return nil, errors.Wrapf(err, "illegal Data Source Name (DNS) specified by %s", config.URL)
}
mysqlConfig.User = config.Username
mysqlConfig.Passwd = getRootPassword(member.ComponentName)
mysqlConfig.Addr = addr
mysqlConfig.Timeout = time.Second * 5
mysqlConfig.ReadTimeout = time.Second * 5
mysqlConfig.WriteTimeout = time.Second * 5
db, err := mysqlengine.GetDBConnection(mysqlConfig.FormatDSN())
if err != nil {
return nil, errors.Wrap(err, "get DB connection failed")
}

return db, nil
}
5 changes: 3 additions & 2 deletions pkg/lorry/engines/oceanbase/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (mgr *Manager) GetMySQLDBConn() (*sql.DB, error) {
return nil, errors.Wrapf(err, "illegal Data Source Name (DNS) specified by %s", config.URL)
}
mysqlConfig.User = fmt.Sprintf("%s@%s", "root", mgr.ReplicaTenant)
mysqlConfig.Passwd = config.Password
mysqlConfig.Passwd = ""
db, err := mysqlengine.GetDBConnection(mysqlConfig.FormatDSN())
if err != nil {
return nil, errors.Wrap(err, "get DB connection failed")
Expand All @@ -65,7 +65,8 @@ func (mgr *Manager) GetMySQLDBConnWithAddr(addr string) (*sql.DB, error) {
return nil, errors.Wrapf(err, "illegal Data Source Name (DNS) specified by %s", config.URL)
}
mysqlConfig.User = fmt.Sprintf("%s@%s", "root", mgr.ReplicaTenant)
mysqlConfig.Passwd = config.Password
// mysqlConfig.Passwd = config.Password
mysqlConfig.Passwd = ""
mysqlConfig.Addr = addr
db, err := mysqlengine.GetDBConnection(mysqlConfig.FormatDSN())
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/lorry/engines/oceanbase/get_replica_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func (mgr *Manager) GetReplicaRoleForMember(ctx context.Context, cluster *dcs.Cl

db := mgr.DB
if member != nil && member.Name != mgr.CurrentMemberName {
addr := cluster.GetMemberAddrWithPort(*member)
db, err = config.GetDBConnWithAddr(addr)
db, err = config.GetMemberRootDBConn(cluster, member)
if err != nil {
return "", errors.Wrap(err, "new db connection failed")
}
Expand Down

0 comments on commit 9dcd491

Please sign in to comment.