Skip to content

Commit

Permalink
*: update pvc and modify the default rootHost to localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyass committed Jul 27, 2021
1 parent 377e1ba commit 186f26b
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 31 deletions.
4 changes: 2 additions & 2 deletions api/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions charts/mysql-operator/crds/mysql.radondb.com_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions cluster/container/init_mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var (
},
MysqlVersion: "5.7",
MysqlOpts: mysqlv1alpha1.MysqlOpts{
RootHost: "127.0.0.1",
RootHost: "localhost",
InitTokuDB: false,
},
},
Expand Down Expand Up @@ -78,7 +78,7 @@ var (
},
{
Name: "MYSQL_ROOT_HOST",
Value: "127.0.0.1",
Value: "localhost",
},
{
Name: "MYSQL_INIT_ONLY",
Expand Down
2 changes: 1 addition & 1 deletion cluster/syncer/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
40 changes: 40 additions & 0 deletions cluster/syncer/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/mysql.radondb.com_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion config/samples/mysql_v1alpha1_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spec:

mysqlOpts:
rootPassword: ""
rootHost: "127.0.0.1"
rootHost: localhost
user: qc_usr
password: Qing@123
database: qingcloud
Expand Down
4 changes: 2 additions & 2 deletions sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion sidecar/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
17 changes: 0 additions & 17 deletions sidecar/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ limitations under the License.
package sidecar

import (
"fmt"
"io"
"os"
"strconv"
"strings"

logf "sigs.k8s.io/controller-runtime/pkg/log"

Expand Down Expand Up @@ -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)
Expand Down
20 changes: 19 additions & 1 deletion utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 186f26b

Please sign in to comment.