Skip to content

Commit

Permalink
Merge pull request radondb#3 from radondb/fixPVCforV240
Browse files Browse the repository at this point in the history
Fix pvc bug for v240
  • Loading branch information
acekingke authored Mar 22, 2023
2 parents 8c44283 + cc8f329 commit dd82c2f
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion mysqlcluster/syncer/readonly_statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

apiv1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
Expand Down Expand Up @@ -67,6 +68,10 @@ func (s *StatefulSetSyncer) SfsReadOnly(ctx context.Context) error {
if err := s.cli.Update(context.TODO(), &currentStatefulset); err != nil {
return errors.Wrapf(err, "update readonly statefulset '%s'", s.Name)
}
// Update pvc.
if err := deletePvcReadOnly(ctx, s); err != nil {
return errors.Wrapf(err, "delete extra readonly pvc '%s'", s.Name)
}
// 4. if all finished, do the change master
if err = wait.PollImmediate(time.Second*5, time.Minute*2, func() (bool, error) {
err = s.cli.Get(context.TODO(), types.NamespacedName{Name: readonlyStatefulSet.Name, Namespace: readonlyStatefulSet.Namespace}, &currentStatefulset)
Expand Down Expand Up @@ -104,7 +109,7 @@ func GetReadonlyStatefulSet(cr *StatefulSetSyncer) (appsv1.StatefulSet, error) {
}
mysql := container.EnsureContainer(utils.ContainerMysqlName, cr.MysqlCluster)
if cr.Spec.ReadOnlys.Resources != nil {
mysql.Resources = cr.Spec.MysqlOpts.Resources
mysql.Resources = *cr.Spec.ReadOnlys.Resources
}
containers = append(containers, mysql)
return appsv1.StatefulSet{
Expand Down Expand Up @@ -241,3 +246,42 @@ func buildMasterName(cr *StatefulSetSyncer) string {
}

}

func deletePvcReadOnly(ctx context.Context, s *StatefulSetSyncer) error {
if s.Spec.ReadOnlys.Num == 0 {
s.log.Info("skip update pvc because replicas is 0")
return nil
}
pvcs := corev1.PersistentVolumeClaimList{}
if err := s.cli.List(ctx,
&pvcs,
&client.ListOptions{
Namespace: s.sfs.Namespace,
LabelSelector: readOnlyLables(s).AsSelector(),
},
); err != nil {
return err
}

for _, item := range pvcs.Items {
if item.DeletionTimestamp != nil {
s.log.Info("pvc is being deleted", "pvc", item.Name, "key", s.Unwrap())
continue
}

ordinal, err := utils.GetOrdinal(item.Name)
if err != nil {
s.log.Error(err, "pvc deletion error", "key", s.Unwrap())
continue
}

if ordinal >= int(s.Spec.ReadOnlys.Num) {
s.log.Info("cleaning up pvc", "pvc", item.Name, "key", s.Unwrap())
if err := s.cli.Delete(ctx, &item); err != nil {
return err
}
}
}

return nil
}

0 comments on commit dd82c2f

Please sign in to comment.