Skip to content

Commit

Permalink
*: add validate for cluster api
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyass committed Jul 23, 2021
1 parent 49b9eea commit 0789ed6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
8 changes: 8 additions & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ func (c *Cluster) Unwrap() *apiv1alpha1.Cluster {
return c.Cluster
}

func (c *Cluster) Validate() error {
if utils.StringInArray(c.Spec.MysqlOpts.User, []string{"root", utils.ReplicationUser, utils.OperatorUser, utils.MetricsUser}) {
return fmt.Errorf("spec.mysqlOpts.user cannot be root|%s|%s|%s", utils.ReplicationUser, utils.OperatorUser, utils.MetricsUser)
}

return nil
}

// GetLabels returns cluster labels
func (c *Cluster) GetLabels() labels.Set {
instance := c.Name
Expand Down
4 changes: 4 additions & 0 deletions controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, err
}

if err = instance.Validate(); err != nil {
return ctrl.Result{}, err
}

status := *instance.Status.DeepCopy()
defer func() {
if !reflect.DeepEqual(status, instance.Status) {
Expand Down
12 changes: 3 additions & 9 deletions internal/sql_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ package internal
import (
"database/sql"
"fmt"
"sort"
"strconv"
"strings"
"time"

_ "github.com/go-sql-driver/mysql"
corev1 "k8s.io/api/core/v1"

"github.com/radondb/radondb-mysql-kubernetes/utils"
)

var (
Expand Down Expand Up @@ -118,7 +119,7 @@ func (s *SQLRunner) checkSlaveStatus() (isLagged, isReplicating corev1.Condition
lastSQLError := columnValue(scanArgs, cols, "Last_SQL_Error")
secondsBehindMaster := columnValue(scanArgs, cols, "Seconds_Behind_Master")

if stringInArray(slaveIOState, errorConnectionStates) {
if utils.StringInArray(slaveIOState, errorConnectionStates) {
return isLagged, corev1.ConditionFalse, fmt.Errorf("Slave_IO_State: %s", slaveIOState)
}

Expand Down Expand Up @@ -227,10 +228,3 @@ func columnValue(scanArgs []interface{}, slaveCols []string, colName string) str

return string(*scanArgs[columnIndex].(*sql.RawBytes))
}

// stringInArray check whether the str is in the strArray.
func stringInArray(str string, strArray []string) bool {
sort.Strings(strArray)
index := sort.SearchStrings(strArray, str)
return index < len(strArray) && strArray[index] == str
}
9 changes: 9 additions & 0 deletions utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package utils

import "sort"

// Min returns the smallest int64 that was passed in the arguments.
func Min(a, b uint64) uint64 {
if a < b {
Expand All @@ -31,3 +33,10 @@ func Max(a, b uint64) uint64 {
}
return b
}

// StringInArray check whether the str is in the strArray.
func StringInArray(str string, strArray []string) bool {
sort.Strings(strArray)
index := sort.SearchStrings(strArray, str)
return index < len(strArray) && strArray[index] == str
}

0 comments on commit 0789ed6

Please sign in to comment.