Skip to content

Commit

Permalink
api: forbidden lower_case_table_names modify in my.cnf radondb#477
Browse files Browse the repository at this point in the history
  • Loading branch information
acekingke committed May 25, 2022
1 parent b236456 commit e35a7c1
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions api/v1alpha1/mysqlcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -59,10 +60,12 @@ func (r *MysqlCluster) ValidateUpdate(old runtime.Object) error {
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected an MysqlCluster but got a %T", old))
}
if err := r.validateVolumeSize(*oldCluster); err != nil {
if err := r.validateVolumeSize(oldCluster); err != nil {
return err
}
if err := r.validateLowTableCase(oldCluster); err != nil {
return err
}

return nil
}

Expand All @@ -74,9 +77,31 @@ func (r *MysqlCluster) ValidateDelete() error {
return nil
}

func (r *MysqlCluster) validateVolumeSize(old MysqlCluster) error {
if r.Spec.Persistence.Size < old.Spec.Persistence.Size {
// Validate volume size, forbidden shrink storage size.
func (r *MysqlCluster) validateVolumeSize(oldCluster *MysqlCluster) error {

oldStorageSize, err := resource.ParseQuantity(oldCluster.Spec.Persistence.Size)
if err != nil {
return err
}
newStorageSize, err := resource.ParseQuantity(r.Spec.Persistence.Size)
if err != nil {
return err
}
// =1 means that old storage size is greater then new
if oldStorageSize.Cmp(newStorageSize) == 1 {
return apierrors.NewForbidden(schema.GroupResource{}, "", fmt.Errorf("volesize can not be decreased"))
}
return nil
}

// Validate low table case for mysqlcluster.
func (r *MysqlCluster) validateLowTableCase(oldCluster *MysqlCluster) error {
oldmyconf := oldCluster.Spec.MysqlOpts.MysqlConf
newmyconf := r.Spec.MysqlOpts.MysqlConf
if r.Spec.MysqlVersion == "8.0" &&
oldmyconf["lower_case_table_names"] != newmyconf["lower_case_table_names"] {
return apierrors.NewForbidden(schema.GroupResource{}, "", fmt.Errorf("lower_case_table_names cannot be changed in MySQL8.0+"))
}
return nil
}

0 comments on commit e35a7c1

Please sign in to comment.