Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[csi] Add field ActualLVNameOnTheNode. Add SDSLocalVolumeCSIFinalizer. Add delete LVMLogicalVolume after error in CreateVolume. Bump go builder to 1.21 #18

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions images/sds-local-volume-csi/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG BASE_ALPINE=registry.deckhouse.io/base_images/alpine:3.16.3@sha256:5548e9172c24a1b0ca9afdd2bf534e265c94b12b36b3e0c0302f5853eaf00abb
ARG BASE_GOLANG_20_ALPINE_BUILDER=registry.deckhouse.io/base_images/golang:1.20.5-alpine3.18@sha256:51a47fb0851397db2f506c15c426735bc23de31177cbdd962880c0879d1906a4
ARG BASE_GOLANG_21_ALPINE_BUILDER=registry.deckhouse.io/base_images/golang:1.21.4-alpine3.18@sha256:cf84f3d6882c49ea04b6478ac514a2582c8922d7e5848b43d2918fff8329f6e6

FROM $BASE_GOLANG_20_ALPINE_BUILDER as builder
FROM $BASE_GOLANG_21_ALPINE_BUILDER as builder

WORKDIR /go/src

Expand Down
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
}
Loading