Files
velero/pkg/exposer/generic_restore_priority_test.go
T
b7d83a6f2b Cherry pick the in-place restore implementation PRs from feature branch to main (#10415)
* Update CRDs and CLI to support in-place restore (#10038)

Update CRDs(Restore, DataDownload, PodVolumeRestore) and restore create CLI to support in-place restore

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

* Update Kopia(filesystem) uploader to support incremental and deleteExtraFile during restore (#10066)

Update Kopia(filesystem) uploader to support incremental and deleteExtraFile during restore

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

* Update Restore Exposer and PVC CSI to support in-place restore (#10104)

1. Update Restore Exposer to support exposing with existing PV for in-place restore
2. Update PVC CSI RIA to continue the restore process for in-place restore

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

* Update Block uploader to support increase restore (#10244)

Update Block uploader to support increase restore

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

* Update Exposer to recreate the target PV if the volume mode is different with the restore PVC (#10257)

Update Exposer to recreate the target PV if the volume mode is different with t
he restore PVC

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

* Preserve PVC selected-node annotation via carrier annotation for in-place restore

For in-place volume data restore, the existing PVC is deleted and
recreated. For StorageClasses with the WaitForFirstConsumer volume
binding mode, losing the volume.kubernetes.io/selected-node annotation
could let the scheduler place the recreated workload Pod in a different
zone than the original PV, leaving it stuck in ContainerCreating.

Instead of relying on RestoreItemAction execution order (the generic
PVC RIA unconditionally strips the selected-node annotation), the PVC
CSI RIA now captures the annotation from the existing PVC right before
deleting it and carries it on the target PVC via the Velero-internal
restore.velero.io/inplace-restore-selected-node annotation. The restore
engine translates the carrier back to the Kubernetes annotation after
all RestoreItemActions have run and always strips the carrier so it
never lands on the cluster.

This makes the behavior independent of RIA ordering: the Kubernetes
annotation is stripped by default on every path (including when the
target PVC does not exist and Velero falls back to provisioning a new
PVC), and preservation only happens when the CSI RIA explicitly
captured a value from the existing PVC.

Signed-off-by: chlins <chlins.zhang@gmail.com>

* Update the control path to make the in-place incremental restore with block data mover work E2E (#10410)

Update the control path to make the in-place incremental restore with block data mover work E2E

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>

---------

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
Signed-off-by: chlins <chlins.zhang@gmail.com>
Co-authored-by: chlins <chlins.zhang@gmail.com>
2026-08-26 10:21:32 -04:00

245 lines
6.7 KiB
Go

/*
Copyright the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package exposer
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1api "k8s.io/api/apps/v1"
corev1api "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
velerotest "github.com/vmware-tanzu/velero/pkg/test"
"github.com/vmware-tanzu/velero/pkg/util/kube"
)
// TestCreateRestorePodWithPriorityClass verifies that the priority class name is properly set in the restore pod
func TestCreateRestorePodWithPriorityClass(t *testing.T) {
testCases := []struct {
name string
nodeAgentConfigMapData string
expectedPriorityClass string
description string
}{
{
name: "with priority class in config map",
nodeAgentConfigMapData: `{
"priorityClassName": "low-priority"
}`,
expectedPriorityClass: "low-priority",
description: "Should set priority class from node-agent-configmap",
},
{
name: "without priority class in config map",
nodeAgentConfigMapData: `{
"loadAffinity": []
}`,
expectedPriorityClass: "",
description: "Should have empty priority class when not specified",
},
{
name: "empty config map",
nodeAgentConfigMapData: `{}`,
expectedPriorityClass: "",
description: "Should handle empty config map gracefully",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := t.Context()
// Create fake Kubernetes client
kubeClient := fake.NewSimpleClientset()
// Create node-agent daemonset (required for getInheritedPodInfo)
daemonSet := &appsv1api.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: "node-agent",
Namespace: velerov1api.DefaultNamespace,
},
Spec: appsv1api.DaemonSetSpec{
Template: corev1api.PodTemplateSpec{
Spec: corev1api.PodSpec{
Containers: []corev1api.Container{
{
Name: "node-agent",
Image: "velero/velero:latest",
},
},
},
},
},
}
_, err := kubeClient.AppsV1().DaemonSets(velerov1api.DefaultNamespace).Create(ctx, daemonSet, metav1.CreateOptions{})
require.NoError(t, err)
// Create node-agent config map
configMap := &corev1api.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "node-agent-config",
Namespace: velerov1api.DefaultNamespace,
},
Data: map[string]string{
"config": tc.nodeAgentConfigMapData,
},
}
_, err = kubeClient.CoreV1().ConfigMaps(velerov1api.DefaultNamespace).Create(ctx, configMap, metav1.CreateOptions{})
require.NoError(t, err)
// Create owner object for the restore pod
ownerObject := corev1api.ObjectReference{
APIVersion: velerov1api.SchemeGroupVersion.String(),
Kind: "DataDownload",
Name: "test-datadownload",
Namespace: velerov1api.DefaultNamespace,
UID: "test-uid",
}
// Create a target PVC
targetPVC := &corev1api.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "test-target-pvc",
Namespace: velerov1api.DefaultNamespace,
},
Spec: corev1api.PersistentVolumeClaimSpec{
AccessModes: []corev1api.PersistentVolumeAccessMode{
corev1api.ReadWriteOnce,
},
},
}
// Create generic restore exposer
exposer := &genericRestoreExposer{
kubeClient: kubeClient,
log: velerotest.NewLogger(),
}
// Call createRestorePod
pod, err := exposer.createRestorePod(
ctx,
ownerObject,
targetPVC,
time.Minute*5,
nil, // labels
nil, // annotations
nil, // tolerations
"", // selectedNode
corev1api.ResourceRequirements{},
kube.NodeOSLinux,
nil, // affinity
tc.expectedPriorityClass,
nil,
"", // volumeSnapshotNamespace
"", // volumeID
nil,
)
require.NoError(t, err, tc.description)
assert.NotNil(t, pod)
assert.Equal(t, tc.expectedPriorityClass, pod.Spec.PriorityClassName, tc.description)
})
}
}
func TestCreateRestorePodWithMissingConfigMap(t *testing.T) {
ctx := t.Context()
// Create fake Kubernetes client without config map
kubeClient := fake.NewSimpleClientset()
// Create node-agent daemonset (required for getInheritedPodInfo)
daemonSet := &appsv1api.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: "node-agent",
Namespace: velerov1api.DefaultNamespace,
},
Spec: appsv1api.DaemonSetSpec{
Template: corev1api.PodTemplateSpec{
Spec: corev1api.PodSpec{
Containers: []corev1api.Container{
{
Name: "node-agent",
Image: "velero/velero:latest",
},
},
},
},
},
}
_, err := kubeClient.AppsV1().DaemonSets(velerov1api.DefaultNamespace).Create(ctx, daemonSet, metav1.CreateOptions{})
require.NoError(t, err)
// Create owner object for the restore pod
ownerObject := corev1api.ObjectReference{
APIVersion: velerov1api.SchemeGroupVersion.String(),
Kind: "DataDownload",
Name: "test-datadownload",
Namespace: velerov1api.DefaultNamespace,
UID: "test-uid",
}
// Create a target PVC
targetPVC := &corev1api.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "test-target-pvc",
Namespace: velerov1api.DefaultNamespace,
},
Spec: corev1api.PersistentVolumeClaimSpec{
AccessModes: []corev1api.PersistentVolumeAccessMode{
corev1api.ReadWriteOnce,
},
},
}
// Create generic restore exposer
exposer := &genericRestoreExposer{
kubeClient: kubeClient,
log: velerotest.NewLogger(),
}
// Call createRestorePod
pod, err := exposer.createRestorePod(
ctx,
ownerObject,
targetPVC,
time.Minute*5,
nil, // labels
nil, // annotations
nil, // tolerations
"", // selectedNode
corev1api.ResourceRequirements{},
kube.NodeOSLinux,
nil, // affinity
"", // empty priority class since config map is missing
nil,
"", // volumeSnapshotNamespace
"", // volumeID
nil,
)
// Should succeed even when config map is missing
require.NoError(t, err, "Should succeed even when config map is missing")
assert.NotNil(t, pod)
assert.Empty(t, pod.Spec.PriorityClassName, "Should have empty priority class when config map is missing")
}