[CSI] Add CSI PV and PVCs back into restore resources
@@ -26,6 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
+ "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
@@ -1004,6 +1005,13 @@ func (a *ApplicationRestoreController) restoreResources(
}
}
+ // Add all CSI PVCs and PVs back into resources.
+ // CSI PVs are dynamically generated by the CSI controller for restore,
+ // so we need to get the new PV name after restore volumes finishes
+ if err := a.addCSIVolumeResources(restore); err != nil {
+ return err
+ }
+
restore.Status.LastUpdateTimestamp = metav1.Now()
if err := a.client.Update(context.TODO(), restore); err != nil {
return err
@@ -1012,6 +1020,68 @@ func (a *ApplicationRestoreController) restoreResources(
return nil
}
+func (a *ApplicationRestoreController) addCSIVolumeResources(restore *storkapi.ApplicationRestore) error {
+ for _, vrInfo := range restore.Status.Volumes {
+ if vrInfo.DriverName != "csi" {
+ continue
+ }
+
+ // Update PV resource for this volume
+ pv, err := core.Instance().GetPersistentVolume(vrInfo.RestoreVolume)
+ if err != nil {
+ return fmt.Errorf("failed to get PV %s: %v", vrInfo.RestoreVolume, err)
+ }
+ pvContent, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pv)
+ if err != nil {
+ return fmt.Errorf("failed to convert PV %s to unstructured: %v", vrInfo.RestoreVolume, err)
+ }
+ pvObj := &unstructured.Unstructured{}
+ pvObj.SetUnstructuredContent(pvContent)
+ pvObj.SetGroupVersionKind(schema.GroupVersionKind{
+ Kind: "PersistentVolume",
+ Version: "v1",
+ Group: "core",
+ })
+ if err := a.updateResourceStatus(
+ restore,
+ pvObj,
+ storkapi.ApplicationRestoreStatusSuccessful,
+ "Resource restored successfully"); err != nil {
+ return err
+ }
+
+ // Update PVC resource for this volume
+ ns, ok := restore.Spec.NamespaceMapping[vrInfo.SourceNamespace]
+ if !ok {
+ ns = vrInfo.SourceNamespace
+ }
+ pvc, err := core.Instance().GetPersistentVolumeClaim(vrInfo.PersistentVolumeClaim, ns)
+ if err != nil {
+ return fmt.Errorf("failed to get PVC %s/%s: %v", ns, vrInfo.PersistentVolumeClaim, err)
+ }
+ pvcContent, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pvc)
+ if err != nil {
+ return fmt.Errorf("failed to convert PVC %s to unstructured: %v", vrInfo.RestoreVolume, err)
+ }
+ pvcObj := &unstructured.Unstructured{}
+ pvcObj.SetUnstructuredContent(pvcContent)
+ pvcObj.SetGroupVersionKind(schema.GroupVersionKind{
+ Kind: "PersistentVolumeClaim",
+ Version: "v1",
+ Group: "core",
+ })
+ if err := a.updateResourceStatus(
+ restore,
+ pvcObj,
+ storkapi.ApplicationRestoreStatusSuccessful,
+ "Resource restored successfully"); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
func (a *ApplicationRestoreController) cleanupRestore(restore *storkapi.ApplicationRestore) error {
drivers := a.getDriversForRestore(restore)
for driverName := range drivers {
