Skip to content

Commit 88073bb

Browse files
committed
fix(backups): fail Velero RestoreJob rename for non-VMInstance kinds
A RestoreJob that set targetApplicationRef.name to a new name and options.targetNamespace to another namespace reported Succeeded for any Velero-backed application, but only a VMInstance was actually renamed. postRestoreRename looks up the HelmRelease under the vm-instance- prefix, gets NotFound for every other kind and returns nil, so a Postgres, MariaDB, ClickHouse, MongoDB or standalone VMDisk copy came up under the source name with no error surfaced. Renaming those kinds through the Velero strategy cannot work: DataUpload restores volumes under their original names, and only a VMInstance keeps using them after its HelmRelease is renamed, because its disks keep their names and CDI adopts the restored PVCs. A renamed database Cluster or VMDisk renders fresh volumes and comes up empty. Reject the rename up front for every kind other than VMInstance, before the RestoreJob enters Running, with a message that points at omitting targetApplicationRef.name or using the engine-native strategy. The existing same-namespace guard keeps precedence, so its message is unchanged. Closes #3537 Assisted-By: LLM Signed-off-by: Andrey Kolkov <[email protected]>
1 parent 45f5698 commit 88073bb

2 files changed

Lines changed: 151 additions & 0 deletions

File tree

‎internal/backupcontroller/velerostrategy_controller.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,17 @@ func (r *RestoreJobReconciler) reconcileVeleroRestore(ctx context.Context, resto
606606
"Use options.targetNamespace to restore into a different namespace")
607607
}
608608

609+
// Validate: rename is only implemented for VMInstance. Failing here beats a
610+
// Succeeded RestoreJob whose application silently keeps the source name.
611+
if target.IsRenamed && !veleroRenameSupported(backup.Spec.ApplicationRef.Kind) {
612+
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf(
613+
"restoring %s %q under a different name (%q) is not supported by the Velero strategy: "+
614+
"Velero DataUpload always restores volumes under their original names and only a VMInstance "+
615+
"can adopt them. Omit targetApplicationRef.name to keep the source name, "+
616+
"or use the application's engine-native strategy to restore into a new application",
617+
backup.Spec.ApplicationRef.Kind, backup.Spec.ApplicationRef.Name, target.AppName))
618+
}
619+
609620
// Step 1: On first reconcile, set startedAt and phase = Running
610621
if restoreJob.Status.StartedAt == nil {
611622
logger.Debug("setting RestoreJob StartedAt and phase to Running")
@@ -958,6 +969,16 @@ var (
958969
dataVolumeGVR = schema.GroupVersionResource{Group: "cdi.kubevirt.io", Version: "v1beta1", Resource: "datavolumes"}
959970
)
960971

972+
// veleroRenameSupported reports whether postRestoreRename can restore the
973+
// given application kind under a new name. Velero DataUpload writes volumes
974+
// under their original names, and only a VMInstance re-attaches them after its
975+
// HelmRelease is renamed: its disks keep their names and CDI adopts the restored
976+
// PVCs. Every other kind (a standalone VMDisk, a database operator's Cluster)
977+
// renders fresh volumes for the new name and comes up empty.
978+
func veleroRenameSupported(appKind string) bool {
979+
return appKind == vmInstanceKind
980+
}
981+
961982
// helmReleaseNameForApp returns the HelmRelease name for the given application
962983
// kind and name. Returns empty string for unsupported kinds.
963984
func helmReleaseNameForApp(appKind, appName string) string {

‎internal/backupcontroller/velerostrategy_controller_test.go‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package backupcontroller
33
import (
44
"context"
55
"encoding/json"
6+
"strings"
67
"testing"
78

89
corev1 "k8s.io/api/core/v1"
@@ -1122,6 +1123,135 @@ func TestHelmReleaseNameForApp(t *testing.T) {
11221123
}
11231124
}
11241125

1126+
func TestVeleroRenameSupported(t *testing.T) {
1127+
tests := []struct {
1128+
kind string
1129+
want bool
1130+
}{
1131+
{vmInstanceKind, true},
1132+
{vmDiskAppKind, false},
1133+
{"Postgres", false},
1134+
{"MariaDB", false},
1135+
{"ClickHouse", false},
1136+
{"MongoDB", false},
1137+
{"", false},
1138+
}
1139+
for _, tt := range tests {
1140+
t.Run(tt.kind, func(t *testing.T) {
1141+
if got := veleroRenameSupported(tt.kind); got != tt.want {
1142+
t.Errorf("veleroRenameSupported(%q) = %v, want %v", tt.kind, got, tt.want)
1143+
}
1144+
})
1145+
}
1146+
}
1147+
1148+
// TestReconcileVeleroRestore_RenameGuard covers the fix for a RestoreJob that
1149+
// asked for a new application name via targetApplicationRef.name but whose
1150+
// kind cannot be renamed by postRestoreRename: it used to report Succeeded
1151+
// with the application restored under the source name.
1152+
func TestReconcileVeleroRestore_RenameGuard(t *testing.T) {
1153+
const (
1154+
sourceNS = "tenant-src"
1155+
targetNS = "tenant-dst"
1156+
)
1157+
1158+
tests := []struct {
1159+
name string
1160+
appKind string
1161+
targetNS string
1162+
wantPhase backupsv1alpha1.RestoreJobPhase
1163+
wantMessage string
1164+
}{
1165+
{
1166+
name: "postgres cross-namespace rename is rejected",
1167+
appKind: "Postgres",
1168+
targetNS: targetNS,
1169+
wantPhase: backupsv1alpha1.RestoreJobPhaseFailed,
1170+
wantMessage: `restoring Postgres "src" under a different name ("dst") is not supported by the Velero strategy`,
1171+
},
1172+
{
1173+
name: "vmdisk cross-namespace rename is rejected",
1174+
appKind: vmDiskAppKind,
1175+
targetNS: targetNS,
1176+
wantPhase: backupsv1alpha1.RestoreJobPhaseFailed,
1177+
wantMessage: `restoring VMDisk "src" under a different name ("dst") is not supported by the Velero strategy`,
1178+
},
1179+
{
1180+
name: "postgres same-namespace rename keeps the DataUpload message",
1181+
appKind: "Postgres",
1182+
targetNS: "",
1183+
wantPhase: backupsv1alpha1.RestoreJobPhaseFailed,
1184+
wantMessage: "restoring to the same namespace with a different application name is not supported",
1185+
},
1186+
{
1187+
name: "vminstance cross-namespace rename passes the guard",
1188+
appKind: vmInstanceKind,
1189+
targetNS: targetNS,
1190+
wantPhase: backupsv1alpha1.RestoreJobPhaseRunning,
1191+
},
1192+
}
1193+
for _, tt := range tests {
1194+
t.Run(tt.name, func(t *testing.T) {
1195+
backup := &backupsv1alpha1.Backup{
1196+
ObjectMeta: metav1.ObjectMeta{Name: "src-sb", Namespace: sourceNS},
1197+
Spec: backupsv1alpha1.BackupSpec{
1198+
ApplicationRef: corev1.TypedLocalObjectReference{
1199+
APIGroup: stringPtr("apps.cozystack.io"),
1200+
Kind: tt.appKind,
1201+
Name: "src",
1202+
},
1203+
StrategyRef: corev1.TypedLocalObjectReference{Kind: "Velero", Name: "cozy-default-velero"},
1204+
},
1205+
}
1206+
restoreJob := &backupsv1alpha1.RestoreJob{
1207+
ObjectMeta: metav1.ObjectMeta{Name: "src-restore", Namespace: sourceNS},
1208+
Spec: backupsv1alpha1.RestoreJobSpec{
1209+
BackupRef: corev1.LocalObjectReference{Name: "src-sb"},
1210+
TargetApplicationRef: &corev1.TypedLocalObjectReference{
1211+
APIGroup: stringPtr("apps.cozystack.io"),
1212+
Kind: tt.appKind,
1213+
Name: "dst",
1214+
},
1215+
},
1216+
}
1217+
if tt.targetNS != "" {
1218+
raw, err := json.Marshal(RestoreOptions{CommonRestoreOptions: CommonRestoreOptions{TargetNamespace: tt.targetNS}})
1219+
if err != nil {
1220+
t.Fatalf("marshal options: %v", err)
1221+
}
1222+
restoreJob.Spec.Options = &runtime.RawExtension{Raw: raw}
1223+
}
1224+
targetNamespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: targetNS}}
1225+
1226+
reconciler := newTestRestoreJobReconcilerWithDynamic(t, nil, restoreJob, backup, targetNamespace)
1227+
reconciler.Client = newRestoreJobTestClient(t, restoreJob, backup, targetNamespace)
1228+
ctx := context.Background()
1229+
1230+
if _, err := reconciler.reconcileVeleroRestore(ctx, restoreJob, backup); err != nil {
1231+
t.Fatalf("reconcileVeleroRestore() error = %v", err)
1232+
}
1233+
1234+
got := &backupsv1alpha1.RestoreJob{}
1235+
if err := reconciler.Get(ctx, client.ObjectKeyFromObject(restoreJob), got); err != nil {
1236+
t.Fatalf("get RestoreJob: %v", err)
1237+
}
1238+
if got.Status.Phase != tt.wantPhase {
1239+
t.Fatalf("phase = %q, want %q", got.Status.Phase, tt.wantPhase)
1240+
}
1241+
if tt.wantMessage == "" {
1242+
return
1243+
}
1244+
ready := meta.FindStatusCondition(got.Status.Conditions, "Ready")
1245+
if ready == nil {
1246+
t.Fatalf("Ready condition missing, conditions: %+v", got.Status.Conditions)
1247+
}
1248+
if !strings.Contains(ready.Message, tt.wantMessage) {
1249+
t.Errorf("Ready message = %q, want it to contain %q", ready.Message, tt.wantMessage)
1250+
}
1251+
})
1252+
}
1253+
}
1254+
11251255
func TestTargetHelmReleaseExists_VMDisk(t *testing.T) {
11261256
ns := "tenant-copy"
11271257
hr := makeUnstructuredHelmRelease("vm-disk-ubuntu-source", ns, nil)

0 commit comments

Comments
 (0)