Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
mikechengwei committed Jan 25, 2022
1 parent 86c50fb commit 1a7dd1b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/pingcap/v1alpha1/tidbcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ func (tc *TidbCluster) IsTLSClusterEnabled() bool {
}

func (tc *TidbCluster) NeedToSyncTiDBInitializer() bool {
return tc.Spec.TiDB != nil && tc.Spec.TiDB.Initializer != nil && tc.Spec.TiDB.Initializer.CreatePassword && !tc.Status.TiDB.PasswordInitialized
return tc.Spec.TiDB != nil && tc.Spec.TiDB.Initializer != nil && tc.Spec.TiDB.Initializer.CreatePassword && tc.Status.TiDB.PasswordInitialized == nil
}

func (tc *TidbCluster) Scheme() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pingcap/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ type TiDBStatus struct {
FailureMembers map[string]TiDBFailureMember `json:"failureMembers,omitempty"`
ResignDDLOwnerRetryCount int32 `json:"resignDDLOwnerRetryCount,omitempty"`
Image string `json:"image,omitempty"`
PasswordInitialized bool `json:"passwordInitialized,omitempty"`
PasswordInitialized *bool `json:"passwordInitialized,omitempty"`
}

// TiDBMember is TiDB member
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion pkg/manager/member/tidb_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ func (m *tidbMemberManager) syncInitializer(tc *v1alpha1.TidbCluster) {
db, err = util.OpenDB(ctx, dsn)

if err != nil {
if strings.Contains(fmt.Sprint(err), "Access denied") {
klog.Errorf("Can't connect to the TiDB service of the TiDB cluster [%s:%s], error: %s", ns, tcName, err)
val := false
tc.Status.TiDB.PasswordInitialized = &val
return
}
if ctx.Err() != nil {
klog.Errorf("Can't connect to the TiDB service of the TiDB cluster [%s:%s], error: %s, context error: %s", ns, tcName, err, ctx.Err())
} else {
Expand All @@ -330,7 +336,8 @@ func (m *tidbMemberManager) syncInitializer(tc *v1alpha1.TidbCluster) {
klog.Errorf("Fail to set TiDB password for TiDB cluster %s/%s, err: %s", ns, tcName, err)
return
}
tc.Status.TiDB.PasswordInitialized = true
val := true
tc.Status.TiDB.PasswordInitialized = &val
klog.Infof("Set password successfully for TiDB cluster %s/%s", ns, tcName)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func OpenDB(ctx context.Context, dsn string) (*sql.DB, error) {
}
if err := db.PingContext(ctx); err != nil {
db.Close()
return nil, fmt.Errorf("cannot connect to mysql, err: %v", err)
return nil, fmt.Errorf("cannot connect to tidb cluster, err: %v", err)
}
return db, nil
}
Expand Down

0 comments on commit 1a7dd1b

Please sign in to comment.