Skip to content

Commit

Permalink
fix todos
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandr Stefurishin <alexandr.stefurishin@flant.com>
  • Loading branch information
Alexandr Stefurishin committed Nov 6, 2024
1 parent 4ef3953 commit f59a720
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 74 deletions.
38 changes: 20 additions & 18 deletions images/sds-local-volume-csi/src/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ import (
"sds-local-volume-csi/pkg/utils"
)

const (
sourceVolumeKindSnapshot = "LVMLogicalVolumeSnapshot"
sourceVolumeKindVolume = "LVMLogicalVolume"
)

func (d *Driver) CreateVolume(ctx context.Context, request *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
traceID := uuid.New().String()

Expand Down Expand Up @@ -91,19 +96,20 @@ func (d *Driver) CreateVolume(ctx context.Context, request *csi.CreateVolumeRequ

var selectedLVG *v1alpha1.LVMVolumeGroup
var preferredNode string
var sourceVolumeId, sourceVolumeKind string
var sourceVolume *v1alpha1.LVMLogicalVolumeSource

if request.VolumeContentSource != nil {
sourceVolume := &v1alpha1.LVMLogicalVolumeSource{}
switch s := request.VolumeContentSource.Type.(type) {
case *csi.VolumeContentSource_Snapshot:
sourceVolumeKind = "LVMLogicalVolumeSnapshot"
sourceVolumeId = s.Snapshot.SnapshotId
sourceVolume.Kind = sourceVolumeKindSnapshot
sourceVolume.Name = s.Snapshot.SnapshotId

// get source volume
sourceVol, err := utils.GetLVMLogicalVolumeSnapshot(ctx, d.cl, sourceVolumeId, "")
sourceVol, err := utils.GetLVMLogicalVolumeSnapshot(ctx, d.cl, sourceVolume.Name, "")
if err != nil {
d.log.Error(err, fmt.Sprintf("[CreateVolume][traceID:%s][volumeID:%s] error getting source LVMLogicalVolumeSnapshot", traceID, sourceVolumeId))
return nil, status.Errorf(codes.NotFound, "error getting LVMLogicalVolumeSnapshot %s: %s", sourceVolumeId, err.Error())
d.log.Error(err, fmt.Sprintf("[CreateVolume][traceID:%s][volumeID:%s] error getting source LVMLogicalVolumeSnapshot", traceID, sourceVolume.Name))
return nil, status.Errorf(codes.NotFound, "error getting LVMLogicalVolumeSnapshot %s: %s", sourceVolume.Name, err.Error())
}

// check size
Expand All @@ -124,16 +130,15 @@ func (d *Driver) CreateVolume(ctx context.Context, request *csi.CreateVolumeRequ

// prefer the same node as the source
preferredNode = selectedLVG.Status.Nodes[0].Name

case *csi.VolumeContentSource_Volume:
sourceVolumeKind = "LVMLogicalVolume"
sourceVolumeId = s.Volume.VolumeId
sourceVolume.Kind = sourceVolumeKindVolume
sourceVolume.Name = s.Volume.VolumeId

// get source volume
sourceVol, err := utils.GetLVMLogicalVolume(ctx, d.cl, sourceVolumeId, "")
sourceVol, err := utils.GetLVMLogicalVolume(ctx, d.cl, sourceVolume.Name, "")
if err != nil {
d.log.Error(err, fmt.Sprintf("[CreateVolume][traceID:%s][volumeID:%s] error getting source LVMLogicalVolume", traceID, sourceVolumeId))
return nil, status.Errorf(codes.NotFound, "error getting LVMLogicalVolume %s: %s", sourceVolumeId, err.Error())
d.log.Error(err, fmt.Sprintf("[CreateVolume][traceID:%s][volumeID:%s] error getting source LVMLogicalVolume", traceID, sourceVolume.Name))
return nil, status.Errorf(codes.NotFound, "error getting LVMLogicalVolume %s: %s", sourceVolume.Name, err.Error())
}

// check size
Expand Down Expand Up @@ -198,14 +203,11 @@ func (d *Driver) CreateVolume(ctx context.Context, request *csi.CreateVolumeRequ
d.log,
lvName,
*selectedLVG,
storageClassLVGParametersMap[selectedLVG.Name],
storageClassLVGParametersMap,
LvmType,
llvSize.String(),
*llvSize,
contiguous,
&v1alpha1.LVMLogicalVolumeSource{
Name: sourceVolumeId,
Kind: sourceVolumeKind,
},
sourceVolume,
)
d.log.Info(fmt.Sprintf("[CreateVolume][traceID:%s][volumeID:%s] LVMLogicalVolumeSpec: %+v", traceID, volumeID, llvSpec))
resizeDelta, err := resource.ParseQuantity(internal.ResizeDelta)
Expand Down
72 changes: 16 additions & 56 deletions images/sds-local-volume-csi/src/pkg/utils/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package utils

import (
"context"
"errors"
"fmt"
"math"
"slices"
Expand All @@ -37,15 +36,14 @@ import (
)

const (
LLVStatusCreated = "Created"
LLVSStatusCreated = "Created"
LLVStatusFailed = "Failed"
LLVSStatusFailed = "Failed"
LLVTypeThin = "Thin"
KubernetesAPIRequestLimit = 3
KubernetesAPIRequestTimeout = 1
SDSLocalVolumeCSIFinalizer = "storage.deckhouse.io/sds-local-volume-csi"
SDSLocalVolumeSnapshotCSIFinalizer = "storage.deckhouse.io/sds-local-volume-snapshot-csi"
LLVStatusCreated = "Created"
LLVSStatusCreated = "Created"
LLVStatusFailed = "Failed"
LLVSStatusFailed = "Failed"
LLVTypeThin = "Thin"
KubernetesAPIRequestLimit = 3
KubernetesAPIRequestTimeout = 1
SDSLocalVolumeCSIFinalizer = "storage.deckhouse.io/sds-local-volume-csi"
)

func CreateLVMLogicalVolumeSnapshot(
Expand All @@ -61,7 +59,7 @@ func CreateLVMLogicalVolumeSnapshot(
ObjectMeta: metav1.ObjectMeta{
Name: name,
OwnerReferences: []metav1.OwnerReference{},
Finalizers: []string{SDSLocalVolumeSnapshotCSIFinalizer},
Finalizers: []string{SDSLocalVolumeCSIFinalizer},
},
Spec: lvmLogicalVolumeSnapshotSpec,
}
Expand All @@ -84,7 +82,7 @@ func DeleteLVMLogicalVolumeSnapshot(ctx context.Context, kc client.Client, log *
log.Trace(fmt.Sprintf("[DeleteLVMLogicalVolumeSnapshot][traceID:%s][volumeID:%s] LVMLogicalVolumeSnapshot found: %+v (status: %+v)", traceID, lvmLogicalVolumeSnapshotName, llvs, llvs.Status))
log.Trace(fmt.Sprintf("[DeleteLVMLogicalVolumeSnapshot][traceID:%s][volumeID:%s] Removing finalizer %s if exists", traceID, lvmLogicalVolumeSnapshotName, SDSLocalVolumeCSIFinalizer))

removed, err := removeLLVSFinalizerIfExist(ctx, kc, log, llvs, SDSLocalVolumeSnapshotCSIFinalizer)
removed, err := removeLLVSFinalizerIfExist(ctx, kc, log, llvs, SDSLocalVolumeCSIFinalizer)
if err != nil {
return fmt.Errorf("remove finalizers from DeleteLVMLogicalVolumeSnapshot %s: %w", lvmLogicalVolumeSnapshotName, err)
}
Expand Down Expand Up @@ -131,12 +129,12 @@ func removeLLVSFinalizerIfExist(ctx context.Context, kc client.Client, log *logg
return false, ctx.Err()
default:
time.Sleep(KubernetesAPIRequestTimeout * time.Second)
freshLLV, getErr := GetLVMLogicalVolumeSnapshot(ctx, kc, llvs.Name, "")
freshLLVS, getErr := GetLVMLogicalVolumeSnapshot(ctx, kc, llvs.Name, "")
if getErr != nil {
return false, fmt.Errorf("[removeLLVSFinalizerIfExist] error getting LVMLogicalVolumeSnapshot %s after update conflict: %w", llvs.Name, getErr)
}
// Update the llvs struct with fresh data (without changing pointers because we need the new resource version outside of this function)
*llvs = *freshLLV
*llvs = *freshLLVS
}
}
}
Expand Down Expand Up @@ -338,44 +336,6 @@ func GetNodeWithMaxFreeSpace(lvgs []snc.LVMVolumeGroup, storageClassLVGParameter
return nodeName, *resource.NewQuantity(maxFreeSpace, resource.BinarySI), nil
}

// TODO: delete the method below?
func GetLVMVolumeGroupParams(ctx context.Context, kc client.Client, log logger.Logger, lvmVG map[string]string, nodeName, lvmType string) (lvgName, vgName string, err error) {
listLvgs := &snc.LVMVolumeGroupList{
ListMeta: metav1.ListMeta{},
Items: []snc.LVMVolumeGroup{},
}

err = kc.List(ctx, listLvgs)
if err != nil {
return "", "", fmt.Errorf("error getting LVMVolumeGroup list: %w", err)
}

for _, lvg := range listLvgs.Items {
log.Trace(fmt.Sprintf("[GetLVMVolumeGroupParams] process lvg: %+v", lvg))

_, ok := lvmVG[lvg.Name]
if ok {
log.Info(fmt.Sprintf("[GetLVMVolumeGroupParams] found lvg from storage class: %s", lvg.Name))
log.Info(fmt.Sprintf("[GetLVMVolumeGroupParams] lvg.Status.Nodes[0].Name: %s, prefferedNode: %s", lvg.Status.Nodes[0].Name, nodeName))
if lvg.Status.Nodes[0].Name == nodeName {
if lvmType == LLVTypeThin {
for _, thinPool := range lvg.Status.ThinPools {
for _, tp := range lvmVG {
if thinPool.Name == tp {
return lvg.Name, lvg.Spec.ActualVGNameOnTheNode, nil
}
}
}
}
return lvg.Name, lvg.Spec.ActualVGNameOnTheNode, nil
}
} else {
log.Info(fmt.Sprintf("[GetLVMVolumeGroupParams] skip lvg: %s", lvg.Name))
}
}
return "", "", errors.New("there are no matches")
}

func GetLVMVolumeGroup(ctx context.Context, kc client.Client, lvgName, namespace string) (*snc.LVMVolumeGroup, error) {
lvg := &snc.LVMVolumeGroup{}

Expand Down Expand Up @@ -466,24 +426,24 @@ func GetLLVSpec(
log *logger.Logger,
lvName string,
selectedLVG snc.LVMVolumeGroup,
poolName string,
storageClassLVGParametersMap map[string]string,
lvmType string,
llvSize string,
llvSize resource.Quantity,
contiguous bool,
source *snc.LVMLogicalVolumeSource,
) snc.LVMLogicalVolumeSpec {
lvmLogicalVolumeSpec := snc.LVMLogicalVolumeSpec{
ActualLVNameOnTheNode: lvName,
Type: lvmType,
Size: llvSize,
Size: llvSize.String(),
LVMVolumeGroupName: selectedLVG.Name,
Source: source,
}

switch lvmType {
case internal.LVMTypeThin:
lvmLogicalVolumeSpec.Thin = &snc.LVMLogicalVolumeThinSpec{
PoolName: poolName,
PoolName: storageClassLVGParametersMap[selectedLVG.Name],
}
log.Info(fmt.Sprintf("[GetLLVSpec] Thin pool name: %s", lvmLogicalVolumeSpec.Thin.PoolName))
case internal.LVMTypeThick:
Expand Down

0 comments on commit f59a720

Please sign in to comment.