Skip to content

Commit

Permalink
fix: backup cross-namespace recovery failed when restoring from opsre…
Browse files Browse the repository at this point in the history
…quest. (#8932)
  • Loading branch information
wangyelei authored Feb 17, 2025
1 parent 8297e32 commit 0a05b2d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions apis/apps/v1alpha1/opsrequest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,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
8 changes: 8 additions & 0 deletions config/crd/bases/apps.kubeblocks.io_opsrequests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4226,6 +4226,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 Expand Up @@ -4389,6 +4393,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
10 changes: 9 additions & 1 deletion controllers/apps/operations/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,20 @@ 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 @@ -197,6 +204,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 []appsv1alpha1.ClusterService
for i := range cluster.Spec.Services {
Expand Down
8 changes: 8 additions & 0 deletions deploy/helm/crds/apps.kubeblocks.io_opsrequests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4226,6 +4226,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 Expand Up @@ -4389,6 +4393,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
12 changes: 12 additions & 0 deletions docs/developer_docs/api-reference/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -18423,6 +18423,18 @@ string
</tr>
<tr>
<td>
<code>backupNamespace</code><br/>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Specifies the namespace of the backup custom resource. If not specified, the namespace of the opsRequest will be used.</p>
</td>
</tr>
<tr>
<td>
<code>restorePointInTime</code><br/>
<em>
string
Expand Down

0 comments on commit 0a05b2d

Please sign in to comment.