Skip to content

Commit 3fe8d61

Browse files
authored
feat(resize): adding resize support for lvm volumes (#4)
- adding resize support for lvm volumes Signed-off-by: Pawan <[email protected]>
1 parent 54b7da8 commit 3fe8d61

File tree

10 files changed

+163
-10
lines changed

10 files changed

+163
-10
lines changed

.github/workflows/pull_request.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ jobs:
3434
- name: License test
3535
run: make license-check
3636

37-
- name: Lint test
38-
run: make golint
39-
4037
- name: Shellcheck
4138
uses: reviewdog/action-shellcheck@v1
4239
with:

buildscripts/lvm-driver/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
FROM alpine:3.12
1616
RUN apk add --no-cache lvm2 lvm2-extra util-linux device-mapper
17-
RUN apk add --no-cache btrfs-progs xfsprogs e2fsprogs e2fsprogs-extra
17+
RUN apk add --no-cache btrfs-progs xfsprogs xfsprogs-extra e2fsprogs e2fsprogs-extra
1818
RUN apk add --no-cache ca-certificates libc6-compat
1919

2020
ARG DBUILD_DATE

changelog/unreleased/1-pawanpraka1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
adding multi arch build process for LVM Driver

changelog/unreleased/2-pawanpraka1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
adding resize support for lvm volumes

deploy/lvm-operator.yaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,10 @@ rules:
626626
- apiGroups: [""]
627627
resources: ["nodes"]
628628
verbs: ["get", "list", "watch"]
629-
- apiGroups: ["*"]
629+
- apiGroups: [""]
630+
resources: ["pods"]
631+
verbs: ["get", "list", "watch", "update", "patch"]
632+
- apiGroups: ["local.openebs.io"]
630633
resources: ["lvmvolumes"]
631634
verbs: ["*"]
632635
---
@@ -681,6 +684,19 @@ spec:
681684
priorityClassName: system-cluster-critical
682685
serviceAccount: openebs-lvm-controller-sa
683686
containers:
687+
- name: csi-resizer
688+
image: quay.io/k8scsi/csi-resizer:v1.1.0
689+
args:
690+
- "--v=5"
691+
- "--csi-address=$(ADDRESS)"
692+
- "--leader-election"
693+
env:
694+
- name: ADDRESS
695+
value: /var/lib/csi/sockets/pluginproxy/csi.sock
696+
imagePullPolicy: IfNotPresent
697+
volumeMounts:
698+
- name: socket-dir
699+
mountPath: /var/lib/csi/sockets/pluginproxy/
684700
- name: csi-provisioner
685701
image: k8s.gcr.io/sig-storage/csi-provisioner:v2.1.0
686702
imagePullPolicy: IfNotPresent
@@ -802,7 +818,7 @@ rules:
802818
- apiGroups: [""]
803819
resources: ["persistentvolumes", "nodes", "services"]
804820
verbs: ["get", "list"]
805-
- apiGroups: ["*"]
821+
- apiGroups: ["local.openebs.io"]
806822
resources: ["lvmvolumes"]
807823
verbs: ["get", "list", "watch", "create", "update", "patch"]
808824

deploy/yamls/lvm-driver.yaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,10 @@ rules:
477477
- apiGroups: [""]
478478
resources: ["nodes"]
479479
verbs: ["get", "list", "watch"]
480-
- apiGroups: ["*"]
480+
- apiGroups: [""]
481+
resources: ["pods"]
482+
verbs: ["get", "list", "watch", "update", "patch"]
483+
- apiGroups: ["local.openebs.io"]
481484
resources: ["lvmvolumes"]
482485
verbs: ["*"]
483486
---
@@ -532,6 +535,19 @@ spec:
532535
priorityClassName: system-cluster-critical
533536
serviceAccount: openebs-lvm-controller-sa
534537
containers:
538+
- name: csi-resizer
539+
image: quay.io/k8scsi/csi-resizer:v1.1.0
540+
args:
541+
- "--v=5"
542+
- "--csi-address=$(ADDRESS)"
543+
- "--leader-election"
544+
env:
545+
- name: ADDRESS
546+
value: /var/lib/csi/sockets/pluginproxy/csi.sock
547+
imagePullPolicy: IfNotPresent
548+
volumeMounts:
549+
- name: socket-dir
550+
mountPath: /var/lib/csi/sockets/pluginproxy/
535551
- name: csi-provisioner
536552
image: k8s.gcr.io/sig-storage/csi-provisioner:v2.1.0
537553
imagePullPolicy: IfNotPresent
@@ -653,7 +669,7 @@ rules:
653669
- apiGroups: [""]
654670
resources: ["persistentvolumes", "nodes", "services"]
655671
verbs: ["get", "list"]
656-
- apiGroups: ["*"]
672+
- apiGroups: ["local.openebs.io"]
657673
resources: ["lvmvolumes"]
658674
verbs: ["get", "list", "watch", "create", "update", "patch"]
659675

pkg/driver/agent.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,37 @@ func (ns *node) NodeExpandVolume(
282282
ctx context.Context,
283283
req *csi.NodeExpandVolumeRequest,
284284
) (*csi.NodeExpandVolumeResponse, error) {
285+
volumeID := req.GetVolumeId()
286+
if req.GetVolumePath() == "" || volumeID == "" {
287+
return nil, status.Errorf(
288+
codes.InvalidArgument,
289+
"path not provided for NodeExpandVolume Request %s",
290+
volumeID,
291+
)
292+
}
285293

286-
return nil, status.Error(codes.Unimplemented, "")
294+
vol, err := lvm.GetLVMVolume(volumeID)
295+
296+
if err != nil {
297+
return nil, status.Errorf(
298+
codes.NotFound,
299+
"failed to handle NodeExpandVolume Request for %s, {%s}",
300+
req.VolumeId,
301+
err.Error(),
302+
)
303+
}
304+
if err = lvm.ResizeLVMVolume(vol, req.GetVolumePath()); err != nil {
305+
return nil, status.Errorf(
306+
codes.Internal,
307+
"failed to handle NodeExpandVolume Request for %s, {%s}",
308+
req.VolumeId,
309+
err.Error(),
310+
)
311+
}
312+
313+
return &csi.NodeExpandVolumeResponse{
314+
CapacityBytes: req.GetCapacityRange().GetRequiredBytes(),
315+
}, nil
287316
}
288317

289318
// NodeGetVolumeStats returns statistics for the

pkg/driver/controller.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,59 @@ func (cs *controller) ControllerExpandVolume(
381381
req *csi.ControllerExpandVolumeRequest,
382382
) (*csi.ControllerExpandVolumeResponse, error) {
383383

384-
return nil, status.Error(codes.Unimplemented, "")
384+
volumeID := strings.ToLower(req.GetVolumeId())
385+
if volumeID == "" {
386+
return nil, status.Errorf(
387+
codes.InvalidArgument,
388+
"ControllerExpandVolume: no volumeID provided",
389+
)
390+
}
391+
392+
/* round off the new size */
393+
updatedSize := getRoundedCapacity(req.GetCapacityRange().GetRequiredBytes())
394+
395+
vol, err := lvm.GetLVMVolume(volumeID)
396+
if err != nil {
397+
return nil, status.Errorf(
398+
codes.Internal,
399+
"ControllerExpandVolumeRequest: failed to get LVMVolume for %s, {%s}",
400+
volumeID,
401+
err.Error(),
402+
)
403+
}
404+
405+
volsize, err := strconv.ParseInt(vol.Spec.Capacity, 10, 64)
406+
if err != nil {
407+
return nil, status.Errorf(
408+
codes.Internal,
409+
"ControllerExpandVolumeRequest: failed to parse volsize in for %s, {%s}",
410+
volumeID,
411+
err.Error(),
412+
)
413+
}
414+
/*
415+
* Controller expand volume must be idempotent. If a volume corresponding
416+
* to the specified volume ID is already larger than or equal to the target
417+
* capacity of the expansion request, the plugin should reply 0 OK.
418+
*/
419+
if volsize >= updatedSize {
420+
return csipayload.NewControllerExpandVolumeResponseBuilder().
421+
WithCapacityBytes(volsize).
422+
Build(), nil
423+
}
424+
425+
if err := lvm.ResizeVolume(vol, updatedSize); err != nil {
426+
return nil, status.Errorf(
427+
codes.Internal,
428+
"failed to handle ControllerExpandVolumeRequest for %s, {%s}",
429+
volumeID,
430+
err.Error(),
431+
)
432+
}
433+
return csipayload.NewControllerExpandVolumeResponseBuilder().
434+
WithCapacityBytes(updatedSize).
435+
WithNodeExpansionRequired(true).
436+
Build(), nil
385437
}
386438

387439
// CreateSnapshot creates a snapshot for given volume
@@ -530,6 +582,7 @@ func newControllerCapabilities() []*csi.ControllerServiceCapability {
530582
var capabilities []*csi.ControllerServiceCapability
531583
for _, cap := range []csi.ControllerServiceCapability_RPC_Type{
532584
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
585+
csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
533586
} {
534587
capabilities = append(capabilities, fromType(cap))
535588
}

pkg/lvm/lvm_util.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
VGCreate = "vgcreate"
3737
LVCreate = "lvcreate"
3838
LVRemove = "lvremove"
39+
LVExtend = "lvextend"
3940
)
4041

4142
// builldLVMCreateArgs returns lvcreate command for the volume
@@ -119,3 +120,32 @@ func GetVolumeDevPath(vol *apis.LVMVolume) (string, error) {
119120

120121
return dev, nil
121122
}
123+
124+
// builldVolumeResizeArgs returns resize command for the lvm volume
125+
func buildVolumeResizeArgs(vol *apis.LVMVolume) []string {
126+
var LVMVolArg []string
127+
128+
dev := DevPath + vol.Spec.VolGroup + "/" + vol.Name
129+
size := vol.Spec.Capacity + "b"
130+
131+
LVMVolArg = append(LVMVolArg, dev, "-L", size, "-r")
132+
133+
return LVMVolArg
134+
}
135+
136+
// ResizeLVMVolume resizes the volume
137+
func ResizeLVMVolume(vol *apis.LVMVolume, mountpath string) error {
138+
volume := vol.Spec.VolGroup + "/" + vol.Name
139+
140+
args := buildVolumeResizeArgs(vol)
141+
cmd := exec.Command(LVExtend, args...)
142+
out, err := cmd.CombinedOutput()
143+
144+
if err != nil {
145+
klog.Errorf(
146+
"lvm: could not resize the volume %v cmd %v error: %s", volume, args, string(out),
147+
)
148+
}
149+
150+
return err
151+
}

pkg/lvm/volume.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package lvm
1616

1717
import (
1818
"os"
19+
"strconv"
1920

2021
apis "github.com/openebs/lvm-localpv/pkg/apis/openebs.io/lvm/v1alpha1"
2122
"github.com/openebs/lvm-localpv/pkg/builder/volbuilder"
@@ -147,3 +148,12 @@ func RemoveVolFinalizer(vol *apis.LVMVolume) error {
147148
_, err := volbuilder.NewKubeclient().WithNamespace(LvmNamespace).Update(vol)
148149
return err
149150
}
151+
152+
// ResizeVolume resizes the lvm volume
153+
func ResizeVolume(vol *apis.LVMVolume, newSize int64) error {
154+
155+
vol.Spec.Capacity = strconv.FormatInt(int64(newSize), 10)
156+
157+
_, err := volbuilder.NewKubeclient().WithNamespace(LvmNamespace).Update(vol)
158+
return err
159+
}

0 commit comments

Comments
 (0)