Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 107 additions & 3 deletions controllers/dataprotection/volumepopulator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -125,6 +126,9 @@ func (r *VolumePopulatorReconciler) handleSyncPVCError(reqCtx intctrlutil.Reques
if requeueErr, ok := err.(intctrlutil.RequeueError); ok {
return intctrlutil.RequeueAfter(requeueErr.RequeueAfter(), reqCtx.Log, requeueErr.Reason())
}
if apierrors.IsConflict(err) {
return intctrlutil.RequeueWithError(err, reqCtx.Log, "")
}
if r.ContainPopulatingCondition(pvc) {
// Ignore the error if an external controller handles this PVC.
return intctrlutil.Reconciled()
Expand Down Expand Up @@ -397,6 +401,17 @@ func (r *VolumePopulatorReconciler) decidePVCRestore(reqCtx intctrlutil.RequestC
if restoreData && !skipPostReady {
mode = pvcRestoreModeRestoreData
}
// When no volume-level restore exists and the PVC belongs to the backup
// target component, postReady must not be blocked — it may be the only
// data delivery path (e.g. logical backup via dataLoad). Only clear
// skipPostReady for the target component; non-target component PVCs
// (e.g. pd/tikv in a TiDB backup) must stay skipped.
if !restoreData && skipPostReady {
if target := backupTargetForPVCComponent(backup, pvc); target != nil {
skipPostReady = false
sourceTarget = target
}
}
return &pvcRestoreDecision{
mode: mode,
sourceTarget: sourceTarget,
Expand Down Expand Up @@ -468,6 +483,62 @@ func resolveSingleSourceTargetForPVC(target *dpv1alpha1.BackupStatusTarget, pvc
return target, false, nil
}

// backupTargetForPVCComponent returns the backup target whose component label
// matches the PVC's component. This is a fallback for when backupTargetMatchesPVC
// rejected the PVC due to pod-only labels (role, pod-name, etc.) that PVCs
// never carry. Returns nil if no target matches the PVC's component.
func backupTargetForPVCComponent(backup *dpv1alpha1.Backup, pvc *corev1.PersistentVolumeClaim) *dpv1alpha1.BackupStatusTarget {
pvcComp := pvc.Labels[constant.KBAppComponentLabelKey]
if pvcComp == "" {
return nil
}
if backup.Status.Target != nil {
if targetMatchesComponent(backup.Status.Target, pvcComp) {
return backup.Status.Target
}
return nil
}
for i := range backup.Status.Targets {
if targetMatchesComponent(&backup.Status.Targets[i], pvcComp) {
return &backup.Status.Targets[i]
}
}
return nil
}

func targetMatchesComponent(target *dpv1alpha1.BackupStatusTarget, componentName string) bool {
if target.PodSelector == nil || target.PodSelector.LabelSelector == nil {
return true
}
componentSelector := componentLabelSelector(target.PodSelector.LabelSelector)
if componentSelector == nil {
return false
}
selector, err := metav1.LabelSelectorAsSelector(componentSelector)
if err != nil {
return false
}
return selector.Matches(labels.Set{constant.KBAppComponentLabelKey: componentName})
}

func componentLabelSelector(selector *metav1.LabelSelector) *metav1.LabelSelector {
componentSelector := &metav1.LabelSelector{}
if component, ok := selector.MatchLabels[constant.KBAppComponentLabelKey]; ok {
componentSelector.MatchLabels = map[string]string{
constant.KBAppComponentLabelKey: component,
}
}
for _, expression := range selector.MatchExpressions {
if expression.Key == constant.KBAppComponentLabelKey {
componentSelector.MatchExpressions = append(componentSelector.MatchExpressions, expression)
}
}
if len(componentSelector.MatchLabels) == 0 && len(componentSelector.MatchExpressions) == 0 {
return nil
}
return componentSelector
}

func (r *VolumePopulatorReconciler) resolveShardingSourceTarget(reqCtx intctrlutil.RequestCtx,
pvc *corev1.PersistentVolumeClaim,
backup *dpv1alpha1.Backup) (*dpv1alpha1.BackupStatusTarget, bool, error) {
Expand Down Expand Up @@ -905,7 +976,7 @@ func (r *VolumePopulatorReconciler) Populate(reqCtx intctrlutil.RequestCtx, pvc
if err = r.patchInternalRestoreStatus(reqCtx, restoreMgr); err != nil {
return err
}
return nil
return intctrlutil.NewRequeueError(reconcileInterval, "waiting for prepareData job")
}
if actionFailed {
dprestore.SetRestoreStageCondition(restoreMgr.Restore, dpv1alpha1.PrepareData, dprestore.ReasonFailed, "restore prepareData job failed")
Expand Down Expand Up @@ -969,7 +1040,17 @@ func (r *VolumePopulatorReconciler) patchInternalRestoreStatus(reqCtx intctrluti
if reflect.DeepEqual(restoreMgr.OriginalRestore.Status, restoreMgr.Restore.Status) {
return nil
}
return r.Client.Status().Patch(reqCtx.Ctx, restoreMgr.Restore, client.MergeFrom(restoreMgr.OriginalRestore))
latest := &dpv1alpha1.Restore{}
if err := r.Client.Get(reqCtx.Ctx, client.ObjectKeyFromObject(restoreMgr.Restore), latest); err != nil {
return err
}
mergedStatus := mergeRestoreStatusChanges(restoreMgr.OriginalRestore.Status, restoreMgr.Restore.Status, latest.Status)
if reflect.DeepEqual(latest.Status, mergedStatus) {
return nil
}
statusPatch := client.MergeFrom(latest.DeepCopy())
latest.Status = mergedStatus
return r.Client.Status().Patch(reqCtx.Ctx, latest, statusPatch)
}

func (r *VolumePopulatorReconciler) patchInternalRestoreMetaAndStatus(reqCtx intctrlutil.RequestCtx,
Expand All @@ -987,10 +1068,33 @@ func (r *VolumePopulatorReconciler) patchInternalRestoreMetaAndStatus(reqCtx int
return err
}
statusPatch := client.MergeFrom(latest.DeepCopy())
latest.Status = restore.Status
latest.Status = mergeRestoreStatusChanges(original.Status, restore.Status, latest.Status)
return r.Client.Status().Patch(reqCtx.Ctx, latest, statusPatch)
}

func mergeRestoreStatusChanges(original, desired, latest dpv1alpha1.RestoreStatus) dpv1alpha1.RestoreStatus {
merged := latest
if !reflect.DeepEqual(original.Phase, desired.Phase) {
merged.Phase = desired.Phase
}
if !reflect.DeepEqual(original.StartTimestamp, desired.StartTimestamp) {
merged.StartTimestamp = desired.StartTimestamp
}
if !reflect.DeepEqual(original.CompletionTimestamp, desired.CompletionTimestamp) {
merged.CompletionTimestamp = desired.CompletionTimestamp
}
if !reflect.DeepEqual(original.Duration, desired.Duration) {
merged.Duration = desired.Duration
}
if !reflect.DeepEqual(original.Actions, desired.Actions) {
merged.Actions = desired.Actions
}
if !reflect.DeepEqual(original.Conditions, desired.Conditions) {
merged.Conditions = desired.Conditions
}
return merged
}

func (r *VolumePopulatorReconciler) completeBoundPVCIfNeeded(reqCtx intctrlutil.RequestCtx,
pvc *corev1.PersistentVolumeClaim,
restoreCtx *pvcRestoreContext) error {
Expand Down
Loading
Loading