Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Zimin <alexandr.zimin@flant.com>
  • Loading branch information
AleksZimin committed Mar 6, 2024
1 parent ed73496 commit 7ff68b3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ type LVMLogicalVolume struct {
}

type LVMLogicalVolumeSpec struct {
Type string `json:"type"`
Size resource.Quantity `json:"size"`
LvmVolumeGroup string `json:"lvmVolumeGroup"`
Thin *ThinLogicalVolumeSpec `json:"thin"`
ActualLVNameOnTheNode string `json:"actualLVNameOnTheNode"`
Type string `json:"type"`
Size resource.Quantity `json:"size"`
LvmVolumeGroupName string `json:"lvmVolumeGroupName"`
Thin *ThinLogicalVolumeSpec `json:"thin"`
}

type ThinLogicalVolumeSpec struct {
Expand Down
23 changes: 15 additions & 8 deletions images/sds-local-volume-csi/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (d *Driver) CreateVolume(ctx context.Context, request *csi.CreateVolumeRequ
}

llvName := request.Name
lvName := request.Name
d.log.Info(fmt.Sprintf("llv name: %s ", llvName))

llvSize := resource.NewQuantity(request.CapacityRange.GetRequiredBytes(), resource.BinarySI)
Expand Down Expand Up @@ -103,8 +104,13 @@ func (d *Driver) CreateVolume(ctx context.Context, request *csi.CreateVolumeRequ
return nil, status.Errorf(codes.Internal, err.Error())
}

llvSpec := utils.GetLLVSpec(*d.log, selectedLVG, storageClassLVGParametersMap, preferredNode, LvmType, *llvSize)
llvSpec := utils.GetLLVSpec(*d.log, lvName, selectedLVG, storageClassLVGParametersMap, preferredNode, LvmType, *llvSize)
d.log.Info(fmt.Sprintf("LVMLogicalVolumeSpec : %+v", llvSpec))
resizeDelta, err := resource.ParseQuantity(internal.ResizeDelta)
if err != nil {
d.log.Error(err, "error ParseQuantity for ResizeDelta")
return nil, err
}

d.log.Trace("------------ CreateLVMLogicalVolume start ------------")
_, err = utils.CreateLVMLogicalVolume(ctx, d.cl, llvName, llvSpec)
Expand All @@ -119,14 +125,15 @@ func (d *Driver) CreateVolume(ctx context.Context, request *csi.CreateVolumeRequ
d.log.Trace("------------ CreateLVMLogicalVolume end ------------")

d.log.Trace("start wait CreateLVMLogicalVolume ")
resizeDelta, err := resource.ParseQuantity(internal.ResizeDelta)
if err != nil {
d.log.Error(err, "error ParseQuantity for ResizeDelta")
return nil, err
}

attemptCounter, err := utils.WaitForStatusUpdate(ctx, d.cl, *d.log, request.Name, "", *llvSize, resizeDelta)
if err != nil {
d.log.Error(err, "error WaitForStatusUpdate")
deleteErr := utils.DeleteLVMLogicalVolume(ctx, d.cl, request.Name)

d.log.Error(err, fmt.Sprintf("error WaitForStatusUpdate. Delete LVMLogicalVolume %s", request.Name))
if deleteErr != nil {
d.log.Error(deleteErr, "error DeleteLVMLogicalVolume")
}
return nil, err
}
d.log.Trace(fmt.Sprintf("stop waiting CreateLVMLogicalVolume, attempt counter = %d ", attemptCounter))
Expand Down Expand Up @@ -284,7 +291,7 @@ func (d *Driver) ControllerExpandVolume(ctx context.Context, request *csi.Contro
}, nil
}

lvg, err := utils.GetLVMVolumeGroup(ctx, d.cl, llv.Spec.LvmVolumeGroup, llv.Namespace)
lvg, err := utils.GetLVMVolumeGroup(ctx, d.cl, llv.Spec.LvmVolumeGroupName, llv.Namespace)
if err != nil {
return nil, status.Errorf(codes.Internal, "error getting LVMVolumeGroup: %v", err)
}
Expand Down
50 changes: 36 additions & 14 deletions images/sds-local-volume-csi/pkg/utils/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"sds-local-volume-csi/api/v1alpha1"
"sds-local-volume-csi/internal"
"sds-local-volume-csi/pkg/logger"
"slices"
"time"

"gopkg.in/yaml.v2"
Expand All @@ -40,6 +41,7 @@ const (
LLVTypeThin = "Thin"
KubernetesApiRequestLimit = 3
KubernetesApiRequestTimeout = 1
SDSLocalVolumeCSIFinalizer = "storage.deckhouse.io/sds-local-volume-csi"
)

func CreateLVMLogicalVolume(ctx context.Context, kc client.Client, name string, LVMLogicalVolumeSpec v1alpha1.LVMLogicalVolumeSpec) (*v1alpha1.LVMLogicalVolume, error) {
Expand All @@ -52,6 +54,7 @@ func CreateLVMLogicalVolume(ctx context.Context, kc client.Client, name string,
ObjectMeta: metav1.ObjectMeta{
Name: name,
OwnerReferences: []metav1.OwnerReference{},
Finalizers: []string{SDSLocalVolumeCSIFinalizer},
},
Spec: LVMLogicalVolumeSpec,
}
Expand All @@ -77,18 +80,19 @@ func CreateLVMLogicalVolume(ctx context.Context, kc client.Client, name string,

func DeleteLVMLogicalVolume(ctx context.Context, kc client.Client, LVMLogicalVolumeName string) error {
var err error
llv := &v1alpha1.LVMLogicalVolume{
ObjectMeta: metav1.ObjectMeta{
Name: LVMLogicalVolumeName,
},
TypeMeta: metav1.TypeMeta{
Kind: v1alpha1.LVMLogicalVolumeKind,
APIVersion: v1alpha1.TypeMediaAPIVersion,
},

llv, err := GetLVMLogicalVolume(ctx, kc, LVMLogicalVolumeName, "")
if err != nil {
return fmt.Errorf("get LVMLogicalVolume %s: %w", LVMLogicalVolumeName, err)
}

err = removeLLVFinalizerIfExist(ctx, kc, llv, SDSLocalVolumeCSIFinalizer)
if err != nil {
return fmt.Errorf("remove finalizers from LVMLogicalVolume %s: %w", LVMLogicalVolumeName, err)
}

for attempt := 0; attempt < KubernetesApiRequestLimit; attempt++ {
err := kc.Delete(ctx, llv)
err = kc.Delete(ctx, llv)
if err == nil {
return nil
}
Expand Down Expand Up @@ -373,18 +377,19 @@ func GetLVGList(ctx context.Context, kc client.Client) (*v1alpha1.LvmVolumeGroup
return nil, fmt.Errorf("after %d attempts of getting LvmVolumeGroupList, last error: %w", KubernetesApiRequestLimit, err)
}

func GetLLVSpec(log logger.Logger, selectedLVG v1alpha1.LvmVolumeGroup, storageClassLVGParametersMap map[string]string, nodeName, lvmType string, llvSize resource.Quantity) v1alpha1.LVMLogicalVolumeSpec {
func GetLLVSpec(log logger.Logger, lvName string, selectedLVG v1alpha1.LvmVolumeGroup, storageClassLVGParametersMap map[string]string, nodeName, lvmType string, llvSize resource.Quantity) v1alpha1.LVMLogicalVolumeSpec {
var llvThin *v1alpha1.ThinLogicalVolumeSpec
if lvmType == internal.LLMTypeThin {
llvThin = &v1alpha1.ThinLogicalVolumeSpec{}
llvThin.PoolName = storageClassLVGParametersMap[selectedLVG.Name]
log.Info(fmt.Sprintf("[GetLLVSpec] Thin pool name: %s", llvThin.PoolName))
}
return v1alpha1.LVMLogicalVolumeSpec{
Type: lvmType,
Size: llvSize,
LvmVolumeGroup: selectedLVG.Name,
Thin: llvThin,
ActualLVNameOnTheNode: lvName,
Type: lvmType,
Size: llvSize,
LvmVolumeGroupName: selectedLVG.Name,
Thin: llvThin,
}
}

Expand All @@ -396,3 +401,20 @@ func SelectLVG(storageClassLVGs []v1alpha1.LvmVolumeGroup, storageClassLVGParame
}
return v1alpha1.LvmVolumeGroup{}, fmt.Errorf("[SelectLVG] no LVMVolumeGroup found for node %s", nodeName)
}

func removeLLVFinalizerIfExist(ctx context.Context, kc client.Client, llv *v1alpha1.LVMLogicalVolume, finalizer string) error {
removed := false

for i, val := range llv.Finalizers {
if val == finalizer {
llv.Finalizers = slices.Delete(llv.Finalizers, i, i+1)
removed = true
break
}
}

if removed {
return UpdateLVMLogicalVolume(ctx, kc, llv)
}
return nil
}

0 comments on commit 7ff68b3

Please sign in to comment.