Skip to content

Commit

Permalink
vrg: remove the use of captureStartConditionally
Browse files Browse the repository at this point in the history
captureStartConditionally was a function pointer that was
* capturing kube objects one last time when the vrg was secondary
* capturing kube objects if time since last capture was greater than the
  capture interval

Refactored into one function that performs the capture based on the vrg
spec.

Signed-off-by: Raghavendra Talur <raghavendra.talur@gmail.com>
  • Loading branch information
raghavendra-talur committed Aug 27, 2024
1 parent bf11389 commit 112096b
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions internal/controller/vrg_kubeobjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,15 @@ func kubeObjectsRecoverName(prefix string, groupNumber int) string {
}

func (v *VRGInstance) kubeObjectsProtectPrimary(result *ctrl.Result) {
v.kubeObjectsProtect(result, kubeObjectsCaptureStartConditionallyPrimary)
v.kubeObjectsProtect(result)
}

func (v *VRGInstance) kubeObjectsProtectSecondary(result *ctrl.Result) {
v.kubeObjectsProtect(result, kubeObjectsCaptureStartConditionallySecondary)
v.kubeObjectsProtect(result)
}

type captureStartConditionally func(*VRGInstance, *ctrl.Result, int64, time.Duration, time.Duration, func())

func (v *VRGInstance) kubeObjectsProtect(
result *ctrl.Result,
captureStartConditionally captureStartConditionally,
) {
if v.kubeObjectProtectionDisabled("capture") {
return
Expand All @@ -95,14 +92,12 @@ func (v *VRGInstance) kubeObjectsProtect(
}

v.kubeObjectsCaptureStartOrResumeOrDelay(result,
captureStartConditionally,
captureToRecoverFrom,
)
}

func (v *VRGInstance) kubeObjectsCaptureStartOrResumeOrDelay(
result *ctrl.Result,
captureStartConditionally captureStartConditionally,
captureToRecoverFrom *ramen.KubeObjectsCaptureIdentifier,
) {
veleroNamespaceName := v.veleroNamespaceName()
Expand All @@ -128,7 +123,6 @@ func (v *VRGInstance) kubeObjectsCaptureStartOrResumeOrDelay(
captureStartOrResume := func(generation int64, startOrResume string) {
log.Info("Kube objects capture "+startOrResume, "generation", generation)
v.kubeObjectsCaptureStartOrResume(result,
captureStartConditionally,
number, pathName, capturePathName, namePrefix, veleroNamespaceName, interval, labels,
generation,
kubeobjects.RequestsMapKeyedByName(requests),
Expand All @@ -142,7 +136,7 @@ func (v *VRGInstance) kubeObjectsCaptureStartOrResumeOrDelay(
return
}

captureStartConditionally(
kubeObjectsCaptureStartConditionally(
v, result, captureToRecoverFrom.StartGeneration, time.Since(captureToRecoverFrom.StartTime.Time), interval,
func() {
if v.kubeObjectsCapturesDelete(result, number, capturePathName) != nil {
Expand All @@ -154,34 +148,34 @@ func (v *VRGInstance) kubeObjectsCaptureStartOrResumeOrDelay(
)
}

func kubeObjectsCaptureStartConditionallySecondary(
func kubeObjectsCaptureStartConditionally(
v *VRGInstance, result *ctrl.Result,
captureStartGeneration int64, captureStartTimeSince, captureStartInterval time.Duration,
captureStart func(),
) {
generation := v.instance.Generation
log := v.log.WithValues("generation", generation)

if captureStartGeneration == generation {
log.Info("Kube objects capture for relocate complete")
// Capture one last time for secondary before relocating
if v.instance.Spec.ReplicationState == ramen.Secondary &&
v.instance.Spec.Action != ramen.VRGActionRelocate {
if captureStartGeneration == generation {
log.Info("Kube objects capture for relocate complete")

return
return
}

v.kubeObjectsCaptureStatusFalse(VRGConditionReasonUploading, "Kube objects capture for relocate pending")
}

v.kubeObjectsCaptureStatusFalse(VRGConditionReasonUploading, "Kube objects capture for relocate pending")
captureStart()
}
// Capture for primary is time since last capture is greater than interval
if v.instance.Spec.ReplicationState == ramen.Primary {
if delay := captureStartInterval - captureStartTimeSince; delay > 0 {
v.log.Info("Kube objects capture start delay", "delay", delay, "interval", captureStartInterval)
delaySetIfLess(result, delay, v.log)

func kubeObjectsCaptureStartConditionallyPrimary(
v *VRGInstance, result *ctrl.Result,
captureStartGeneration int64, captureStartTimeSince, captureStartInterval time.Duration,
captureStart func(),
) {
if delay := captureStartInterval - captureStartTimeSince; delay > 0 {
v.log.Info("Kube objects capture start delay", "delay", delay, "interval", captureStartInterval)
delaySetIfLess(result, delay, v.log)

return
return
}
}

captureStart()
Expand Down Expand Up @@ -216,7 +210,6 @@ const (

func (v *VRGInstance) kubeObjectsCaptureStartOrResume(
result *ctrl.Result,
captureStartConditionally captureStartConditionally,
captureNumber int64,
pathName, capturePathName, namePrefix, veleroNamespaceName string,
interval time.Duration,
Expand Down Expand Up @@ -249,7 +242,6 @@ func (v *VRGInstance) kubeObjectsCaptureStartOrResume(

v.kubeObjectsCaptureComplete(
result,
captureStartConditionally,
captureNumber,
veleroNamespaceName,
interval,
Expand Down Expand Up @@ -344,7 +336,6 @@ func (v *VRGInstance) kubeObjectsCaptureDeleteAndLog(

func (v *VRGInstance) kubeObjectsCaptureComplete(
result *ctrl.Result,
captureStartConditionally captureStartConditionally,
captureNumber int64, veleroNamespaceName string, interval time.Duration,
labels map[string]string, startTime metav1.Time, annotations map[string]string,
) {
Expand All @@ -371,7 +362,6 @@ func (v *VRGInstance) kubeObjectsCaptureComplete(
func() {
v.kubeObjectsCaptureIdentifierUpdateComplete(
result,
captureStartConditionally,
*captureToRecoverFromIdentifier,
veleroNamespaceName,
interval,
Expand All @@ -386,7 +376,6 @@ func (v *VRGInstance) kubeObjectsCaptureComplete(

func (v *VRGInstance) kubeObjectsCaptureIdentifierUpdateComplete(
result *ctrl.Result,
captureStartConditionally captureStartConditionally,
captureToRecoverFromIdentifier *ramen.KubeObjectsCaptureIdentifier,
veleroNamespaceName string,
interval time.Duration,
Expand All @@ -409,7 +398,7 @@ func (v *VRGInstance) kubeObjectsCaptureIdentifierUpdateComplete(
captureStartTimeSince := time.Since(captureToRecoverFromIdentifier.StartTime.Time)
v.log.Info("Kube objects captured", "recovery point", captureToRecoverFromIdentifier,
"duration", captureStartTimeSince)
captureStartConditionally(
kubeObjectsCaptureStartConditionally(
v, result, captureToRecoverFromIdentifier.StartGeneration, captureStartTimeSince, interval,
func() {
v.log.Info("Kube objects capture schedule to run immediately")
Expand Down

0 comments on commit 112096b

Please sign in to comment.