diff --git a/api/v1alpha1/cluster_types.go b/api/v1alpha1/cluster_types.go index 4610d6c3..0a688234 100644 --- a/api/v1alpha1/cluster_types.go +++ b/api/v1alpha1/cluster_types.go @@ -43,7 +43,7 @@ type ClusterSpec struct { // MysqlOpts is the options of MySQL container. // +optional - // +kubebuilder:default:={rootPassword: "", rootHost: "127.0.0.1", user: "qc_usr", password: "Qing@123", database: "qingcloud", initTokuDB: true, resources: {limits: {cpu: "500m", memory: "1Gi"}, requests: {cpu: "100m", memory: "256Mi"}}} + // +kubebuilder:default:={rootPassword: "", rootHost: "localhost", user: "qc_usr", password: "Qing@123", database: "qingcloud", initTokuDB: true, resources: {limits: {cpu: "500m", memory: "1Gi"}, requests: {cpu: "100m", memory: "256Mi"}}} MysqlOpts MysqlOpts `json:"mysqlOpts,omitempty"` // XenonOpts is the options of xenon container. @@ -86,7 +86,7 @@ type MysqlOpts struct { // The root user's host. // +optional - // +kubebuilder:default:="127.0.0.1" + // +kubebuilder:default:="localhost" RootHost string `json:"rootHost,omitempty"` // Username of new user to create. diff --git a/charts/mysql-operator/crds/mysql.radondb.com_clusters.yaml b/charts/mysql-operator/crds/mysql.radondb.com_clusters.yaml index 11861f18..46c580ef 100644 --- a/charts/mysql-operator/crds/mysql.radondb.com_clusters.yaml +++ b/charts/mysql-operator/crds/mysql.radondb.com_clusters.yaml @@ -123,7 +123,7 @@ spec: requests: cpu: 100m memory: 256Mi - rootHost: 127.0.0.1 + rootHost: localhost rootPassword: "" user: qc_usr description: MysqlOpts is the options of MySQL container. @@ -184,7 +184,7 @@ spec: type: object type: object rootHost: - default: 127.0.0.1 + default: localhost description: The root user's host. type: string rootPassword: diff --git a/cluster/cluster.go b/cluster/cluster.go index 13ccb090..dfae722f 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -64,6 +64,10 @@ func (c *Cluster) Validate() error { return fmt.Errorf("spec.mysqlOpts.user cannot be root|%s|%s|%s", utils.ReplicationUser, utils.OperatorUser, utils.MetricsUser) } + if c.Spec.MysqlOpts.RootHost == "127.0.0.1" { + return fmt.Errorf("spec.mysqlOpts.rootHost cannot be 127.0.0.1") + } + return nil } diff --git a/cluster/container/init_mysql_test.go b/cluster/container/init_mysql_test.go index d9f9e97a..06d93b49 100644 --- a/cluster/container/init_mysql_test.go +++ b/cluster/container/init_mysql_test.go @@ -42,7 +42,7 @@ var ( }, MysqlVersion: "5.7", MysqlOpts: mysqlv1alpha1.MysqlOpts{ - RootHost: "127.0.0.1", + RootHost: "localhost", InitTokuDB: false, }, }, @@ -78,7 +78,7 @@ var ( }, { Name: "MYSQL_ROOT_HOST", - Value: "127.0.0.1", + Value: "localhost", }, { Name: "MYSQL_INIT_ONLY", diff --git a/cluster/syncer/secret.go b/cluster/syncer/secret.go index 296d1b67..fd2d7eec 100644 --- a/cluster/syncer/secret.go +++ b/cluster/syncer/secret.go @@ -72,7 +72,7 @@ func NewSecretSyncer(cli client.Client, c *cluster.Cluster) syncer.Interface { return err } - if c.Spec.MysqlOpts.RootHost != "127.0.0.1" && c.Spec.MysqlOpts.RootPassword == "" { + if c.Spec.MysqlOpts.RootHost != "localhost" && c.Spec.MysqlOpts.RootPassword == "" { if err := addRandomPassword(secret.Data, "root-password"); err != nil { return err } diff --git a/cluster/syncer/statefulset.go b/cluster/syncer/statefulset.go index 9a6c1081..f3d068b9 100644 --- a/cluster/syncer/statefulset.go +++ b/cluster/syncer/statefulset.go @@ -169,6 +169,10 @@ func (s *StatefulSetSyncer) createOrUpdate(ctx context.Context) (controllerutil. return controllerutil.OperationResultNone, err } + if err := s.updatePVC(ctx); err != nil { + return controllerutil.OperationResultNone, err + } + return controllerutil.OperationResultUpdated, nil } @@ -397,12 +401,52 @@ func (s *StatefulSetSyncer) ensurePodSpec() corev1.PodSpec { } } +func (s *StatefulSetSyncer) updatePVC(ctx context.Context) error { + pvcs := corev1.PersistentVolumeClaimList{} + if err := s.cli.List(ctx, + &pvcs, + &client.ListOptions{ + Namespace: s.sfs.Namespace, + LabelSelector: s.GetLabels().AsSelector(), + }, + ); err != nil { + return err + } + + for _, item := range pvcs.Items { + if item.DeletionTimestamp != nil { + log.Info("pvc is being deleted", "pvc", item.Name, "key", s.Unwrap()) + continue + } + + ordinal, err := utils.GetOrdinal(item.Name) + if err != nil { + log.Error(err, "pvc deletion error", "key", s.Unwrap()) + continue + } + + if ordinal >= int(*s.Spec.Replicas) { + log.Info("cleaning up pvc", "pvc", item.Name, "key", s.Unwrap()) + if err := s.cli.Delete(ctx, &item); err != nil { + return err + } + } + } + + return nil +} + func (s *StatefulSetSyncer) applyNWait(ctx context.Context, pod *corev1.Pod) error { if pod.ObjectMeta.Labels["controller-revision-hash"] == s.sfs.Status.UpdateRevision { log.Info("pod is already updated", "pod name", pod.Name) } else { - if err := s.cli.Delete(ctx, pod); err != nil { - return err + log.Info("updating pod", "pod", pod.Name, "key", s.Unwrap()) + if pod.DeletionTimestamp != nil { + log.Info("pod is being deleted", "pod", pod.Name, "key", s.Unwrap()) + } else { + if err := s.cli.Delete(ctx, pod); err != nil { + return err + } } } diff --git a/config/crd/bases/mysql.radondb.com_clusters.yaml b/config/crd/bases/mysql.radondb.com_clusters.yaml index 11861f18..46c580ef 100644 --- a/config/crd/bases/mysql.radondb.com_clusters.yaml +++ b/config/crd/bases/mysql.radondb.com_clusters.yaml @@ -123,7 +123,7 @@ spec: requests: cpu: 100m memory: 256Mi - rootHost: 127.0.0.1 + rootHost: localhost rootPassword: "" user: qc_usr description: MysqlOpts is the options of MySQL container. @@ -184,7 +184,7 @@ spec: type: object type: object rootHost: - default: 127.0.0.1 + default: localhost description: The root user's host. type: string rootPassword: diff --git a/config/samples/mysql_v1alpha1_cluster.yaml b/config/samples/mysql_v1alpha1_cluster.yaml index fa7684bf..2be0c050 100644 --- a/config/samples/mysql_v1alpha1_cluster.yaml +++ b/config/samples/mysql_v1alpha1_cluster.yaml @@ -8,7 +8,7 @@ spec: mysqlOpts: rootPassword: "" - rootHost: "127.0.0.1" + rootHost: localhost user: qc_usr password: Qing@123 database: qingcloud diff --git a/sidecar/config.go b/sidecar/config.go index be6f01df..de353ac1 100644 --- a/sidecar/config.go +++ b/sidecar/config.go @@ -146,7 +146,7 @@ func (cfg *Config) buildExtraConfig(filePath string) (*ini.File, error) { conf := ini.Empty() sec := conf.Section("mysqld") - ordinal, err := getOrdinal(cfg.HostName) + ordinal, err := utils.GetOrdinal(cfg.HostName) if err != nil { return nil, err } @@ -280,7 +280,7 @@ func (cfg *Config) buildClientConfig() (*ini.File, error) { } func (cfg *Config) buildPostStart() ([]byte, error) { - ordinal, err := getOrdinal(cfg.HostName) + ordinal, err := utils.GetOrdinal(cfg.HostName) if err != nil { return nil, err } diff --git a/sidecar/init.go b/sidecar/init.go index 379de8c7..c9c9c4dd 100644 --- a/sidecar/init.go +++ b/sidecar/init.go @@ -88,7 +88,7 @@ func runInitCommand(cfg *Config) error { return fmt.Errorf("failed to save client.conf: %s", err) } - if err = os.Mkdir(extraConfPath, os.FileMode(0644)); err != nil { + if err = os.Mkdir(extraConfPath, os.FileMode(0755)); err != nil { if !os.IsExist(err) { return fmt.Errorf("error mkdir %s: %s", extraConfPath, err) } diff --git a/sidecar/util.go b/sidecar/util.go index 7ce1df00..1f669d21 100644 --- a/sidecar/util.go +++ b/sidecar/util.go @@ -17,11 +17,8 @@ limitations under the License. package sidecar import ( - "fmt" "io" "os" - "strconv" - "strings" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -102,20 +99,6 @@ func getEnvValue(key string) string { return value } -func getOrdinal(name string) (int, error) { - idx := strings.LastIndexAny(name, "-") - if idx == -1 { - return -1, fmt.Errorf("failed to extract ordinal from hostname: %s", name) - } - - ordinal, err := strconv.Atoi(name[idx+1:]) - if err != nil { - log.Error(err, "failed to extract ordinal form hostname", "hostname", name) - return -1, fmt.Errorf("failed to extract ordinal from hostname: %s", name) - } - return ordinal, nil -} - // checkIfPathExists check if the path exists. func checkIfPathExists(path string) (bool, error) { f, err := os.Open(path) diff --git a/utils/common.go b/utils/common.go index 7f9b1507..2fb23a39 100644 --- a/utils/common.go +++ b/utils/common.go @@ -16,7 +16,12 @@ limitations under the License. package utils -import "sort" +import ( + "fmt" + "sort" + "strconv" + "strings" +) // Min returns the smallest int64 that was passed in the arguments. func Min(a, b uint64) uint64 { @@ -40,3 +45,16 @@ func StringInArray(str string, strArray []string) bool { index := sort.SearchStrings(strArray, str) return index < len(strArray) && strArray[index] == str } + +func GetOrdinal(name string) (int, error) { + idx := strings.LastIndexAny(name, "-") + if idx == -1 { + return -1, fmt.Errorf("failed to extract ordinal from name: %s", name) + } + + ordinal, err := strconv.Atoi(name[idx+1:]) + if err != nil { + return -1, fmt.Errorf("failed to extract ordinal from name: %s", name) + } + return ordinal, nil +}