From 6023e8d826b7a71d458f3073955c87c6c4d737d5 Mon Sep 17 00:00:00 2001 From: Jack Lin Date: Thu, 14 Sep 2023 17:11:32 +0800 Subject: [PATCH] feat(volume): add condition for wait backing image ref: longhorn/longhorn 6615 Signed-off-by: Jack Lin --- controller/replica_controller.go | 7 +++++++ controller/volume_controller.go | 26 ++++++++++++++++++++++++ controller/volume_controller_test.go | 4 ++++ k8s/pkg/apis/longhorn/v1beta2/replica.go | 5 ++++- k8s/pkg/apis/longhorn/v1beta2/volume.go | 9 +++++--- 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/controller/replica_controller.go b/controller/replica_controller.go index 3827be326d..0564ca5baf 100644 --- a/controller/replica_controller.go +++ b/controller/replica_controller.go @@ -369,13 +369,20 @@ func (rc *ReplicaController) CreateInstance(obj interface{}) (*longhorn.Instance backingImagePath := "" if r.Spec.BackingImage != "" { if backingImagePath, err = rc.GetBackingImagePathForReplicaStarting(r); err != nil { + r.Status.Conditions = types.SetCondition(r.Status.Conditions, longhorn.ReplicaConditionTypeWaitForBackingImage, + longhorn.ConditionStatusTrue, longhorn.ReplicaConditionReasonWaitForBackingImageFailed, err.Error()) return nil, err } if backingImagePath == "" { + r.Status.Conditions = types.SetCondition(r.Status.Conditions, longhorn.ReplicaConditionTypeWaitForBackingImage, + longhorn.ConditionStatusTrue, longhorn.ReplicaConditionReasonWaitForBackingImageWaiting, "") return nil, nil } } + r.Status.Conditions = types.SetCondition(r.Status.Conditions, longhorn.ReplicaConditionTypeWaitForBackingImage, + longhorn.ConditionStatusFalse, "", "") + if IsRebuildingReplica(r) { canStart, err := rc.CanStartRebuildingReplica(r) if err != nil { diff --git a/controller/volume_controller.go b/controller/volume_controller.go index fa998b0367..93e5004bea 100644 --- a/controller/volume_controller.go +++ b/controller/volume_controller.go @@ -571,6 +571,32 @@ func (c *VolumeController) ReconcileEngineReplicaState(v *longhorn.Volume, es ma } }() + // Aggregate replica wait for backing image condition + aggregatedReplicaWaitForBackingImageError := util.NewMultiError() + waitForBackingImage := false + for _, r := range rs { + waitForBackingImageCondition := types.GetCondition(r.Status.Conditions, longhorn.ReplicaConditionTypeWaitForBackingImage) + if waitForBackingImageCondition.Status == longhorn.ConditionStatusTrue { + waitForBackingImage = true + if waitForBackingImageCondition.Reason == longhorn.ReplicaConditionReasonWaitForBackingImageFailed { + aggregatedReplicaWaitForBackingImageError.Append(util.NewMultiError(waitForBackingImageCondition.Message)) + } + } + } + if waitForBackingImage { + if len(aggregatedReplicaWaitForBackingImageError) > 0 { + failureMessage := aggregatedReplicaWaitForBackingImageError.Join() + v.Status.Conditions = types.SetCondition(v.Status.Conditions, longhorn.VolumeConditionTypeWaitForBackingImage, + longhorn.ConditionStatusTrue, longhorn.VolumeConditionReasonWaitForBackingImageFailed, failureMessage) + } else { + v.Status.Conditions = types.SetCondition(v.Status.Conditions, longhorn.VolumeConditionTypeWaitForBackingImage, + longhorn.ConditionStatusTrue, longhorn.VolumeConditionReasonWaitForBackingImageWaiting, "") + } + } else { + v.Status.Conditions = types.SetCondition(v.Status.Conditions, longhorn.VolumeConditionTypeWaitForBackingImage, + longhorn.ConditionStatusFalse, "", "") + } + e, err := c.ds.PickVolumeCurrentEngine(v, es) if err != nil { return err diff --git a/controller/volume_controller_test.go b/controller/volume_controller_test.go index 41238e1743..f55daa9f31 100644 --- a/controller/volume_controller_test.go +++ b/controller/volume_controller_test.go @@ -1090,6 +1090,10 @@ func newVolume(name string, replicaCount int) *longhorn.Volume { Status: longhorn.VolumeStatus{ OwnerID: TestOwnerID1, Conditions: []longhorn.Condition{ + { + Type: string(longhorn.VolumeConditionTypeWaitForBackingImage), + Status: longhorn.ConditionStatusFalse, + }, { Type: string(longhorn.VolumeConditionTypeTooManySnapshots), Status: longhorn.ConditionStatusFalse, diff --git a/k8s/pkg/apis/longhorn/v1beta2/replica.go b/k8s/pkg/apis/longhorn/v1beta2/replica.go index 189e3a0a6d..ceab3a0628 100644 --- a/k8s/pkg/apis/longhorn/v1beta2/replica.go +++ b/k8s/pkg/apis/longhorn/v1beta2/replica.go @@ -10,7 +10,10 @@ const ( ) const ( - ReplicaConditionTypeRebuildFailed = "RebuildFailed" + ReplicaConditionTypeRebuildFailed = "RebuildFailed" + ReplicaConditionTypeWaitForBackingImage = "WaitForBackingImage" + ReplicaConditionReasonWaitForBackingImageFailed = "GetBackingImageFailed" + ReplicaConditionReasonWaitForBackingImageWaiting = "Waiting" ReplicaConditionReasonRebuildFailedDisconnection = "Disconnection" ReplicaConditionReasonRebuildFailedGeneral = "General" diff --git a/k8s/pkg/apis/longhorn/v1beta2/volume.go b/k8s/pkg/apis/longhorn/v1beta2/volume.go index 15c9ad3afb..519f66f897 100644 --- a/k8s/pkg/apis/longhorn/v1beta2/volume.go +++ b/k8s/pkg/apis/longhorn/v1beta2/volume.go @@ -99,9 +99,10 @@ type VolumeCloneStatus struct { } const ( - VolumeConditionTypeScheduled = "scheduled" - VolumeConditionTypeRestore = "restore" - VolumeConditionTypeTooManySnapshots = "toomanysnapshots" + VolumeConditionTypeScheduled = "Scheduled" + VolumeConditionTypeRestore = "Restore" + VolumeConditionTypeTooManySnapshots = "TooManySnapshots" + VolumeConditionTypeWaitForBackingImage = "WaitForBackingImage" ) const ( @@ -110,6 +111,8 @@ const ( VolumeConditionReasonRestoreInProgress = "RestoreInProgress" VolumeConditionReasonRestoreFailure = "RestoreFailure" VolumeConditionReasonTooManySnapshots = "TooManySnapshots" + VolumeConditionReasonWaitForBackingImageFailed = "GetBackingImageFailed" + VolumeConditionReasonWaitForBackingImageWaiting = "Waiting" ) type SnapshotDataIntegrity string