@@ -961,6 +961,7 @@ func markInProgressBackupsFailed(ctx context.Context, client ctrlclient.Client,
961961 }
962962 log .WithField ("backup" , backup .GetName ()).Warn (updated .Status .FailureReason )
963963 markDataUploadsCancel (ctx , client , backup , log )
964+ markPodVolumeBackupsCancel (ctx , client , backup , log )
964965 }
965966}
966967
@@ -983,8 +984,10 @@ func markInProgressRestoresFailed(ctx context.Context, client ctrlclient.Client,
983984 log .WithError (errors .WithStack (err )).Errorf ("failed to patch restore %q" , restore .GetName ())
984985 continue
985986 }
987+
986988 log .WithField ("restore" , restore .GetName ()).Warn (updated .Status .FailureReason )
987989 markDataDownloadsCancel (ctx , client , restore , log )
990+ markPodVolumeRestoresCancel (ctx , client , restore , log )
988991 }
989992}
990993
@@ -1069,3 +1072,90 @@ func markDataDownloadsCancel(ctx context.Context, client ctrlclient.Client, rest
10691072 }
10701073 }
10711074}
1075+
1076+ func markPodVolumeBackupsCancel (ctx context.Context , client ctrlclient.Client , backup velerov1api.Backup , log logrus.FieldLogger ) {
1077+ pvbs := & velerov1api.PodVolumeBackupList {}
1078+
1079+ if err := client .List (ctx , pvbs , & ctrlclient.ListOptions {
1080+ Namespace : backup .GetNamespace (),
1081+ LabelSelector : labels .Set (map [string ]string {
1082+ velerov1api .BackupUIDLabel : string (backup .GetUID ()),
1083+ }).AsSelector (),
1084+ }); err != nil {
1085+ log .WithError (errors .WithStack (err )).Error ("failed to list PVBs" )
1086+ return
1087+ }
1088+
1089+ for i := range pvbs .Items {
1090+ pvb := pvbs .Items [i ]
1091+ if pvb .Status .Phase == velerov1api .PodVolumeBackupPhaseAccepted ||
1092+ pvb .Status .Phase == velerov1api .PodVolumeBackupPhasePrepared ||
1093+ pvb .Status .Phase == velerov1api .PodVolumeBackupPhaseInProgress ||
1094+ pvb .Status .Phase == velerov1api .PodVolumeBackupPhaseNew ||
1095+ pvb .Status .Phase == "" {
1096+ err := controller .UpdatePVBWithRetry (ctx , client , types.NamespacedName {Namespace : pvb .Namespace , Name : pvb .Name }, log .WithField ("PVB" , pvb .Name ),
1097+ func (pvb * velerov1api.PodVolumeBackup ) bool {
1098+ if pvb .Spec .Cancel {
1099+ return false
1100+ }
1101+
1102+ pvb .Spec .Cancel = true
1103+ pvb .Status .Message = fmt .Sprintf ("PVB is in status %q during the velero server starting, mark it as cancel" , pvb .Status .Phase )
1104+
1105+ return true
1106+ })
1107+
1108+ if err != nil {
1109+ log .WithError (errors .WithStack (err )).Errorf ("failed to mark PVB %q cancel" , pvb .GetName ())
1110+ continue
1111+ }
1112+ log .WithField ("PVB is mark for cancel due to server restart" , pvb .GetName ()).Warn (pvb .Status .Message )
1113+ }
1114+ }
1115+ }
1116+
1117+ func markPodVolumeRestoresCancel (ctx context.Context , client ctrlclient.Client , restore velerov1api.Restore , log logrus.FieldLogger ) {
1118+ pvrs := & velerov1api.PodVolumeRestoreList {}
1119+
1120+ if err := client .List (ctx , pvrs , & ctrlclient.ListOptions {
1121+ Namespace : restore .GetNamespace (),
1122+ LabelSelector : labels .Set (map [string ]string {
1123+ velerov1api .RestoreUIDLabel : string (restore .GetUID ()),
1124+ }).AsSelector (),
1125+ }); err != nil {
1126+ log .WithError (errors .WithStack (err )).Error ("failed to list PVRs" )
1127+ return
1128+ }
1129+
1130+ for i := range pvrs .Items {
1131+ pvr := pvrs .Items [i ]
1132+ if controller .IsLegacyPVR (& pvr ) {
1133+ log .WithField ("PVR" , pvr .GetName ()).Warn ("Found a legacy PVR during velero server restart, cannot stop it" )
1134+ continue
1135+ }
1136+
1137+ if pvr .Status .Phase == velerov1api .PodVolumeRestorePhaseAccepted ||
1138+ pvr .Status .Phase == velerov1api .PodVolumeRestorePhasePrepared ||
1139+ pvr .Status .Phase == velerov1api .PodVolumeRestorePhaseInProgress ||
1140+ pvr .Status .Phase == velerov1api .PodVolumeRestorePhaseNew ||
1141+ pvr .Status .Phase == "" {
1142+ err := controller .UpdatePVRWithRetry (ctx , client , types.NamespacedName {Namespace : pvr .Namespace , Name : pvr .Name }, log .WithField ("PVR" , pvr .Name ),
1143+ func (pvr * velerov1api.PodVolumeRestore ) bool {
1144+ if pvr .Spec .Cancel {
1145+ return false
1146+ }
1147+
1148+ pvr .Spec .Cancel = true
1149+ pvr .Status .Message = fmt .Sprintf ("PVR is in status %q during the velero server starting, mark it as cancel" , pvr .Status .Phase )
1150+
1151+ return true
1152+ })
1153+
1154+ if err != nil {
1155+ log .WithError (errors .WithStack (err )).Errorf ("failed to mark PVR %q cancel" , pvr .GetName ())
1156+ continue
1157+ }
1158+ log .WithField ("PVR is mark for cancel due to server restart" , pvr .GetName ()).Warn (pvr .Status .Message )
1159+ }
1160+ }
1161+ }
0 commit comments