Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
inelpandzic committed Aug 20, 2024
1 parent 8397b7e commit 2192f7f
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions pkg/controller/ps/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ func (r *PerconaServerMySQLReconciler) applyFinalizers(ctx context.Context, cr *

var err error

finalizers := cr.GetFinalizers()
// Sorting finalizers to make sure that delete-mysql-pods-in-order runs before
// delete-mysql-pvc since the latter removes secrets needed for the former.
slices.Sort(cr.GetFinalizers())

for _, finalizer := range cr.GetFinalizers() {
switch finalizer {
Expand All @@ -154,27 +156,20 @@ func (r *PerconaServerMySQLReconciler) applyFinalizers(ctx context.Context, cr *
case naming.FinalizerDeleteMySQLPvc:
err = r.deleteMySQLPvc(ctx, cr)
}

if err != nil {
return err
}

finalizers = slices.DeleteFunc(finalizers, func(f string) bool {
if f == finalizer {
return true
}
return false
})
}

cr.SetFinalizers(finalizers)

return k8sretry.RetryOnConflict(k8sretry.DefaultRetry, func() error {
err = r.Client.Update(ctx, cr)
c := new(apiv1alpha1.PerconaServerMySQL)
err := r.Client.Get(ctx, types.NamespacedName{Name: cr.Name, Namespace: cr.Namespace}, c)
if err != nil {
log.Error(err, "Client.Update failed")
return errors.Wrap(err, "get cr")
}
return err

c.SetFinalizers([]string{})
return r.Client.Update(ctx, c)
})
}

Expand Down Expand Up @@ -382,6 +377,10 @@ func (r *PerconaServerMySQLReconciler) deleteMySQLPvc(ctx context.Context, cr *a
return errors.Wrap(err, "get PVC list")
}

if list.Size() == 0 {
return nil
}

for _, pvc := range list.Items {
err := r.Client.Delete(ctx, &pvc, &client.DeleteOptions{Preconditions: &metav1.Preconditions{UID: &pvc.UID}})
if err != nil {
Expand Down

0 comments on commit 2192f7f

Please sign in to comment.