From 186f26beb25d1c668e2d059634d8fa86d301b0c0 Mon Sep 17 00:00:00 2001 From: zhyass <34016424+zhyass@users.noreply.github.com> Date: Mon, 26 Jul 2021 18:20:46 +0800 Subject: [PATCH] *: update pvc and modify the default rootHost to localhost --- api/v1alpha1/cluster_types.go | 4 +- .../crds/mysql.radondb.com_clusters.yaml | 4 +- cluster/cluster.go | 4 ++ cluster/container/init_mysql_test.go | 4 +- cluster/syncer/secret.go | 2 +- cluster/syncer/statefulset.go | 40 +++++++++++++++++++ .../crd/bases/mysql.radondb.com_clusters.yaml | 4 +- config/samples/mysql_v1alpha1_cluster.yaml | 2 +- sidecar/config.go | 4 +- sidecar/init.go | 2 +- sidecar/util.go | 17 -------- utils/common.go | 20 +++++++++- 12 files changed, 76 insertions(+), 31 deletions(-) diff --git a/api/v1alpha1/cluster_types.go b/api/v1alpha1/cluster_types.go index 4610d6c32..0a688234f 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 11861f18c..46c580ef2 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 13ccb0903..dfae722f3 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 d9f9e97aa..06d93b497 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 296d1b672..fd2d7eecd 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 9a6c1081d..0bf204cab 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,10 +401,46 @@ 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 { + log.Info("updating pod", "pod", pod.Name, "key", s.Unwrap()) 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 11861f18c..46c580ef2 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 fa7684bf9..2be0c0506 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 be6f01df6..de353ac18 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 379de8c79..c9c9c4dd1 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 7ce1df00a..1f669d213 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 7f9b15071..2fb23a391 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 +}