Skip to content

Commit 1e32331

Browse files
1aalapecloud-bot
authored andcommitted
fix: backup failed during the cluster creation phase (#9705)
(cherry picked from commit d9d5a9e)
1 parent ef91fac commit 1e32331

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

controllers/dataprotection/backup_controller.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,12 @@ func (r *BackupReconciler) handleDeletingPhase(reqCtx intctrlutil.RequestCtx, ba
283283
func (r *BackupReconciler) handleNewPhase(
284284
reqCtx intctrlutil.RequestCtx,
285285
backup *dpv1alpha1.Backup) (ctrl.Result, error) {
286+
286287
request, err := r.prepareBackupRequest(reqCtx, backup)
287288
if err != nil {
288289
return r.updateStatusIfFailed(reqCtx, backup.DeepCopy(), backup, err)
289290
}
291+
290292
// record the status.target/status.targets infos for continuous backup.
291293
if err = r.recordBackupStatusTargets(reqCtx, request); err != nil {
292294
return r.updateStatusIfFailed(reqCtx, backup, request.Backup, err)
@@ -1083,6 +1085,10 @@ func setClusterSnapshotAnnotation(request *dpbackup.Request, cluster *appsv1alph
10831085

10841086
// validateContinuousBackup validates the continuous backup.
10851087
func validateContinuousBackup(backup *dpv1alpha1.Backup, reqCtx intctrlutil.RequestCtx, cli client.Client) error {
1088+
// filter the cluster that is creating when the backup is continuous
1089+
if clusterIsCreating(reqCtx, backup, cli) {
1090+
return intctrlutil.NewErrorf(intctrlutil.ErrorTypeRequeue, "requeue to wait for cluster %s creation to finish", backup.Labels[constant.AppInstanceLabelKey])
1091+
}
10861092
// validate if the continuous backup is created by a backupSchedule.
10871093
if _, ok := backup.Labels[dptypes.BackupScheduleLabelKey]; !ok {
10881094
return fmt.Errorf("continuous backup is only allowed to be created by backupSchedule")
@@ -1136,3 +1142,20 @@ func prepare4Incremental(request *dpbackup.Request) (*dpbackup.Request, error) {
11361142
}
11371143
return request, nil
11381144
}
1145+
1146+
// clusterIsCreating will return true when the backup is Continuous backup and the cluster is creating
1147+
func clusterIsCreating(reqCtx intctrlutil.RequestCtx, backup *dpv1alpha1.Backup, cli client.Client) bool {
1148+
if backup.Labels[constant.AppInstanceLabelKey] != "" {
1149+
ownerCluster := &kbappsv1.Cluster{}
1150+
if err := cli.Get(reqCtx.Ctx, types.NamespacedName{
1151+
Namespace: backup.Namespace,
1152+
Name: backup.Labels[constant.AppInstanceLabelKey],
1153+
}, ownerCluster); err != nil {
1154+
return false
1155+
}
1156+
if ownerCluster.Status.Phase == kbappsv1.CreatingClusterPhase {
1157+
return true
1158+
}
1159+
}
1160+
return false
1161+
}

pkg/dataprotection/backup/utils.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"golang.org/x/mod/semver"
3131
appsv1 "k8s.io/api/apps/v1"
3232
corev1 "k8s.io/api/core/v1"
33+
apierrors "k8s.io/apimachinery/pkg/api/errors"
3334
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3435
"k8s.io/utils/pointer"
3536
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -335,8 +336,10 @@ func StopStatefulSetsWhenFailed(ctx context.Context, cli client.Client, backup *
335336
}
336337
sts := &appsv1.StatefulSet{}
337338
stsName := GenerateBackupStatefulSetName(backup, targetName, BackupDataJobNamePrefix)
338-
if err := cli.Get(ctx, client.ObjectKey{Name: stsName, Namespace: backup.Namespace}, sts); client.IgnoreNotFound(err) != nil {
339+
if err := cli.Get(ctx, client.ObjectKey{Name: stsName, Namespace: backup.Namespace}, sts); apierrors.IsNotFound(err) {
339340
return nil
341+
} else if err != nil {
342+
return err
340343
}
341344
sts.Spec.Replicas = pointer.Int32(0)
342345
return cli.Update(ctx, sts)

0 commit comments

Comments
 (0)