Skip to content

Commit

Permalink
fix: backup and restore bug (#8303)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyelei authored Oct 21, 2024
1 parent a44fa53 commit 7cc59d0
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions apis/dataprotection/v1alpha1/backup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ const (
// +kubebuilder:printcolumn:name="STATUS",type=string,JSONPath=`.status.phase`
// +kubebuilder:printcolumn:name="TOTAL-SIZE",type=string,JSONPath=`.status.totalSize`
// +kubebuilder:printcolumn:name="DURATION",type=string,JSONPath=`.status.duration`
// +kubebuilder:printcolumn:name="DELETION-POLICY",type=string,JSONPath=`.spec.deletionPolicy`
// +kubebuilder:printcolumn:name="CREATION-TIME",type=string,JSONPath=".metadata.creationTimestamp"
// +kubebuilder:printcolumn:name="COMPLETION-TIME",type=string,JSONPath=`.status.completionTimestamp`
// +kubebuilder:printcolumn:name="EXPIRATION-TIME",type=string,JSONPath=`.status.expiration`
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/dataprotection.kubeblocks.io_backups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ spec:
- jsonPath: .status.duration
name: DURATION
type: string
- jsonPath: .spec.deletionPolicy
name: DELETION-POLICY
type: string
- jsonPath: .metadata.creationTimestamp
name: CREATION-TIME
type: string
Expand Down
2 changes: 1 addition & 1 deletion controllers/apps/component_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (r *ComponentReconciler) setupWithMultiClusterManager(mgr ctrl.Manager, mul

func (r *ComponentReconciler) filterComponentRestoreResources(ctx context.Context, obj client.Object) []reconcile.Request {
labels := obj.GetLabels()
if v, ok := labels[constant.KBManagedByKey]; !ok || v != "cluster" {
if v, ok := labels[constant.KBManagedByKey]; !ok || v != constant.AppName {
return []reconcile.Request{}
}
if _, ok := labels[constant.AppInstanceLabelKey]; !ok {
Expand Down
2 changes: 1 addition & 1 deletion controllers/apps/component_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ var _ = Describe("Component Controller", func() {
ml := client.MatchingLabels{
constant.AppInstanceLabelKey: clusterKey.Name,
constant.KBAppComponentLabelKey: comp.Name,
constant.KBManagedByKey: "cluster",
constant.KBManagedByKey: constant.AppName,
}
if bpt != nil {
By(fmt.Sprintf("Checking backup of component %s created", comp.Name))
Expand Down
2 changes: 1 addition & 1 deletion controllers/apps/component_hscale_volume_populator.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (d *baseDataClone) getBRLabels() map[string]string {
return map[string]string{
constant.AppInstanceLabelKey: d.cluster.Name,
constant.KBAppComponentLabelKey: d.component.Name,
constant.KBManagedByKey: "cluster", // the resources are managed by which controller
constant.KBManagedByKey: constant.AppName, // the resources are managed by which controller
}
}

Expand Down
4 changes: 3 additions & 1 deletion controllers/dataprotection/backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,9 @@ func PatchBackupObjectMeta(
}

for _, v := range getClusterLabelKeys() {
request.Labels[v] = targetPod.Labels[v]
if labelValue, ok := targetPod.Labels[v]; ok {
request.Labels[v] = labelValue
}
}

if _, ok := request.Labels[constant.AppManagedByLabelKey]; !ok {
Expand Down
3 changes: 3 additions & 0 deletions deploy/helm/crds/dataprotection.kubeblocks.io_backups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ spec:
- jsonPath: .status.duration
name: DURATION
type: string
- jsonPath: .spec.deletionPolicy
name: DELETION-POLICY
type: string
- jsonPath: .metadata.creationTimestamp
name: CREATION-TIME
type: string
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/factory/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func BuildBackup(cluster *appsv1.Cluster,
return builder.NewBackupBuilder(backupKey.Namespace, backupKey.Name).
AddLabels(dptypes.BackupMethodLabelKey, backupMethod).
AddLabels(dptypes.BackupPolicyLabelKey, backupPolicyName).
AddLabels(constant.KBManagedByKey, "cluster").
AddLabels(constant.KBManagedByKey, constant.AppName).
AddLabels(constant.AppNameLabelKey, synthesizedComp.ClusterDefName).
AddLabels(constant.AppInstanceLabelKey, cluster.Name).
AddLabels(constant.AppManagedByLabelKey, constant.AppName).
Expand Down
2 changes: 1 addition & 1 deletion pkg/dataprotection/restore/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func isTimeInRange(t time.Time, start time.Time, end time.Time) bool {
func GetRestoreFromBackupAnnotation(backup *dpv1alpha1.Backup, volumeRestorePolicy, restoreTime string, env []corev1.EnvVar, doReadyRestoreAfterClusterRunning bool) (string, error) {
componentName := component.GetComponentNameFromObj(backup)
if len(componentName) == 0 {
return "", fmt.Errorf("unable to obtain the name of the component to be recovered, please ensure that Backup.status.componentName exists")
return "", intctrlutil.NewFatalError("unable to obtain the name of the component to be recovered, please ensure that Backup.status.componentName exists")
}
restoreInfoMap := map[string]string{}
restoreInfoMap[constant.BackupNameKeyForRestore] = backup.Name
Expand Down
4 changes: 4 additions & 0 deletions pkg/operations/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (r RestoreOpsHandler) Action(reqCtx intctrlutil.RequestCtx, cli client.Clie

// create cluster
if err = cli.Create(reqCtx.Ctx, cluster); err != nil {
if apierrors.IsAlreadyExists(err) && opsRequest.Labels[constant.AppInstanceLabelKey] != "" {
// already create by this opsRequest
return nil
}
return err
}
opsRes.Cluster = cluster
Expand Down
2 changes: 1 addition & 1 deletion pkg/testutil/dataprotection/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func CheckRestoreAndSetCompleted(testCtx *testutil.TestContext, clusterKey types
ml := client.MatchingLabels{
constant.AppInstanceLabelKey: clusterKey.Name,
constant.KBAppComponentLabelKey: compName,
constant.KBManagedByKey: "cluster",
constant.KBManagedByKey: constant.AppName,
}
Eventually(testapps.List(testCtx, generics.RestoreSignature,
ml, client.InNamespace(clusterKey.Namespace))).Should(HaveLen(scaleOutReplicas))
Expand Down

0 comments on commit 7cc59d0

Please sign in to comment.