Skip to content

Commit

Permalink
fix: backup cross-namespace recovery fail when restoring from opsRequ…
Browse files Browse the repository at this point in the history
…est. (#8931)
  • Loading branch information
wangyelei authored Feb 18, 2025
1 parent fd29cfe commit af6fa75
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions apis/operations/v1alpha1/opsrequest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,11 @@ type Restore struct {
// +kubebuilder:validation:Required
BackupName string `json:"backupName"`

// Specifies the namespace of the backup custom resource. If not specified, the namespace of the opsRequest will be used.
//
// +optional
BackupNamespace string `json:"backupNamespace,omitempty"`

// Specifies the point in time to which the restore should be performed.
// Supported time formats:
//
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/operations.kubeblocks.io_opsrequests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4352,6 +4352,10 @@ spec:
backupName:
description: Specifies the name of the Backup custom resource.
type: string
backupNamespace:
description: Specifies the namespace of the backup custom resource.
If not specified, the namespace of the opsRequest will be used.
type: string
deferPostReadyUntilClusterRunning:
description: |-
Controls the timing of PostReady actions during the recovery process.
Expand Down
4 changes: 4 additions & 0 deletions deploy/helm/crds/operations.kubeblocks.io_opsrequests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4352,6 +4352,10 @@ spec:
backupName:
description: Specifies the name of the Backup custom resource.
type: string
backupNamespace:
description: Specifies the namespace of the backup custom resource.
If not specified, the namespace of the opsRequest will be used.
type: string
deferPostReadyUntilClusterRunning:
description: |-
Controls the timing of PostReady actions during the recovery process.
Expand Down
11 changes: 9 additions & 2 deletions pkg/operations/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,19 @@ func (r RestoreOpsHandler) restoreClusterFromBackup(reqCtx intctrlutil.RequestCt
return nil, intctrlutil.NewFatalError("spec.restore can not be empty")
}
backupName := restoreSpec.BackupName

backupNamespace := restoreSpec.BackupNamespace
if backupNamespace == "" {
backupNamespace = opsRequest.Namespace
}
// check if the backup exists
backup := &dpv1alpha1.Backup{}
if err := cli.Get(reqCtx.Ctx, client.ObjectKey{
Name: backupName,
Namespace: opsRequest.Namespace,
Namespace: backupNamespace,
}, backup); err != nil {
if apierrors.IsNotFound(err) {
return nil, intctrlutil.NewFatalError(fmt.Sprintf("backup %s not found in namespace %s", backupName, backupNamespace))
}
return nil, err
}

Expand Down Expand Up @@ -203,6 +209,7 @@ func (r RestoreOpsHandler) getClusterObjFromBackup(backup *dpv1alpha1.Backup, op
}
cluster.Annotations[constant.RestoreFromBackupAnnotationKey] = restoreAnnotation
cluster.Name = opsRequest.Spec.GetClusterName()
cluster.Namespace = opsRequest.Namespace
// Reset cluster services
var services []appsv1.ClusterService
for i := range cluster.Spec.Services {
Expand Down

0 comments on commit af6fa75

Please sign in to comment.