From 0789ed6d6c125e64f66564635d4ae74d890124e3 Mon Sep 17 00:00:00 2001 From: zhyass <34016424+zhyass@users.noreply.github.com> Date: Fri, 23 Jul 2021 15:17:43 +0800 Subject: [PATCH] *: add validate for cluster api --- cluster/cluster.go | 8 ++++++++ controllers/cluster_controller.go | 4 ++++ internal/sql_runner.go | 12 +++--------- utils/common.go | 9 +++++++++ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/cluster/cluster.go b/cluster/cluster.go index 08b30e8f3..432289029 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -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 diff --git a/controllers/cluster_controller.go b/controllers/cluster_controller.go index 8591569a0..6a139012a 100644 --- a/controllers/cluster_controller.go +++ b/controllers/cluster_controller.go @@ -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) { diff --git a/internal/sql_runner.go b/internal/sql_runner.go index ac595e306..a51e425ec 100644 --- a/internal/sql_runner.go +++ b/internal/sql_runner.go @@ -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 ( @@ -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) } @@ -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 -} diff --git a/utils/common.go b/utils/common.go index b6a868d85..7f9b15071 100644 --- a/utils/common.go +++ b/utils/common.go @@ -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 { @@ -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 +}