From 45f489b0d41915014bcc755eb3a7620de2fd9670 Mon Sep 17 00:00:00 2001 From: Aleksandr Zimin Date: Sun, 18 Aug 2024 01:59:09 +0300 Subject: [PATCH] some fixes Signed-off-by: Aleksandr Zimin --- .../sds-local-volume-csi/driver/controller.go | 3 +- images/sds-local-volume-csi/pkg/utils/func.go | 75 +++++++++++++------ 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/images/sds-local-volume-csi/driver/controller.go b/images/sds-local-volume-csi/driver/controller.go index e5dd517a..679c9676 100644 --- a/images/sds-local-volume-csi/driver/controller.go +++ b/images/sds-local-volume-csi/driver/controller.go @@ -340,8 +340,7 @@ func (d *Driver) ControllerExpandVolume(ctx context.Context, request *csi.Contro d.log.Info(fmt.Sprintf("[ControllerExpandVolume][traceID:%s][volumeID:%s] start resize LVMLogicalVolume", traceID, volumeID)) d.log.Info(fmt.Sprintf("[ControllerExpandVolume][traceID:%s][volumeID:%s] requested size: %s, actual size: %s", traceID, volumeID, requestCapacity.String(), llv.Status.ActualSize.String())) - llv.Spec.Size = requestCapacity.String() - err = utils.UpdateLVMLogicalVolume(ctx, d.cl, llv) + err = utils.ExpandLVMLogicalVolume(ctx, d.cl, llv, requestCapacity.String()) if err != nil { d.log.Error(err, fmt.Sprintf("[ControllerExpandVolume][traceID:%s][volumeID:%s] error updating LVMLogicalVolume", traceID, volumeID)) return nil, status.Errorf(codes.Internal, "error updating LVMLogicalVolume: %v", err) diff --git a/images/sds-local-volume-csi/pkg/utils/func.go b/images/sds-local-volume-csi/pkg/utils/func.go index eb93ac39..c7019fb3 100644 --- a/images/sds-local-volume-csi/pkg/utils/func.go +++ b/images/sds-local-volume-csi/pkg/utils/func.go @@ -290,20 +290,33 @@ func GetLVMThinPoolFreeSpace(lvg snc.LvmVolumeGroup, thinPoolName string) (thinP return storagePoolThinPool.AvailableSpace, nil } -func UpdateLVMLogicalVolume(ctx context.Context, kc client.Client, llv *snc.LVMLogicalVolume) error { - var err error +func ExpandLVMLogicalVolume(ctx context.Context, kc client.Client, llv *snc.LVMLogicalVolume, newSize string) error { for attempt := 0; attempt < KubernetesAPIRequestLimit; attempt++ { - err = kc.Update(ctx, llv) + llv.Spec.Size = newSize + err := kc.Update(ctx, llv) if err == nil { return nil } - time.Sleep(KubernetesAPIRequestTimeout) - } - if err != nil { - return fmt.Errorf("after %d attempts of updating LVMLogicalVolume %s, last error: %w", KubernetesAPIRequestLimit, llv.Name, err) + if attempt < KubernetesAPIRequestLimit-1 { + select { + case <-ctx.Done(): + return ctx.Err() + default: + time.Sleep(KubernetesAPIRequestTimeout) + } + + if kerrors.IsConflict(err) { + freshLLV, getErr := GetLVMLogicalVolume(ctx, kc, llv.Name, "") + if getErr != nil { + return fmt.Errorf("[ExpandLVMLogicalVolume] error getting LVMLogicalVolume %s after update conflict: %w", llv.Name, getErr) + } + llv = freshLLV + } + } } - return nil + + return fmt.Errorf("after %d attempts of expanding LVMLogicalVolume %s, last error: %w", KubernetesAPIRequestLimit, llv.Name, nil) } func GetStorageClassLVGsAndParameters(ctx context.Context, kc client.Client, log *logger.Logger, storageClassLVGParametersString string) (storageClassLVGs []snc.LvmVolumeGroup, storageClassLVGParametersMap map[string]string, err error) { @@ -393,24 +406,44 @@ func SelectLVG(storageClassLVGs []snc.LvmVolumeGroup, nodeName string) (snc.LvmV } func removeLLVFinalizerIfExist(ctx context.Context, kc client.Client, llv *snc.LVMLogicalVolume, finalizer string) (bool, error) { - removed := false + for attempt := 0; attempt < KubernetesAPIRequestLimit; attempt++ { + removed := false + for i, val := range llv.Finalizers { + if val == finalizer { + llv.Finalizers = slices.Delete(llv.Finalizers, i, i+1) + removed = true + break + } + } - for i, val := range llv.Finalizers { - if val == finalizer { - llv.Finalizers = slices.Delete(llv.Finalizers, i, i+1) - removed = true - break + if !removed { + return false, nil } - } - if removed { - err := UpdateLVMLogicalVolume(ctx, kc, llv) - if err != nil { - return false, err + err := kc.Update(ctx, llv) + if err == nil { + return true, nil + } + + if attempt < KubernetesAPIRequestLimit-1 { + select { + case <-ctx.Done(): + return false, ctx.Err() + default: + time.Sleep(KubernetesAPIRequestTimeout) + } + + if kerrors.IsConflict(err) { + freshLLV, getErr := GetLVMLogicalVolume(ctx, kc, llv.Name, "") + if getErr != nil { + return false, fmt.Errorf("[removeLLVFinalizerIfExist] error getting LVMLogicalVolume %s after update conflict: %w", llv.Name, getErr) + } + llv = freshLLV + } } - return true, nil } - return false, nil + + return false, fmt.Errorf("after %d attempts of removing finalizer %s from LVMLogicalVolume %s, last error: %w", KubernetesAPIRequestLimit, finalizer, llv.Name, nil) } func IsContiguous(request *csi.CreateVolumeRequest, lvmType string) bool {