Skip to content

Commit 5ba8e60

Browse files
authored
fix: substract scratch volumes from MaxVolumesPerNode (#78)
1 parent 5d37aff commit 5ba8e60

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

pkg/driver/helpers.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import (
99
"os"
1010
"path/filepath"
1111
"reflect"
12+
"slices"
1213
"strconv"
1314
"strings"
1415

1516
"github.com/container-storage-interface/spec/lib/go/csi"
1617
"github.com/scaleway/scaleway-csi/pkg/scaleway"
1718
block "github.com/scaleway/scaleway-sdk-go/api/block/v1"
19+
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
1820
"github.com/scaleway/scaleway-sdk-go/scw"
19-
"golang.org/x/exp/slices"
2021
"google.golang.org/grpc/codes"
2122
"google.golang.org/grpc/status"
2223
"google.golang.org/protobuf/types/known/timestamppb"
@@ -520,3 +521,29 @@ func uint64ToInt64(v uint64) int64 {
520521

521522
return int64(v)
522523
}
524+
525+
// attachedScratchVolumes returns the number of attached scratch volumes, based
526+
// on the instance metadata.
527+
func attachedScratchVolumes(md *instance.Metadata) int {
528+
var count int
529+
530+
for _, vol := range md.Volumes {
531+
if vol.VolumeType == "scratch" {
532+
count++
533+
}
534+
}
535+
536+
return count
537+
}
538+
539+
// maxVolumesPerNode returns the maximum number of volumes that can be attached to a node,
540+
// after substracting the system root volume and the provided number of reserved volumes.
541+
// It returns an error if the result is 0 or less.
542+
func maxVolumesPerNode(reservedCount int) (int64, error) {
543+
max := scaleway.MaxVolumesPerNode - reservedCount - 1
544+
if max <= 0 {
545+
return 0, fmt.Errorf("max number of volumes that can be attached to this node must be at least 1, currently is %d", max)
546+
}
547+
548+
return int64(max), nil
549+
}

pkg/driver/node.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ type nodeService struct {
2525

2626
diskUtils DiskUtils
2727

28-
nodeID string
29-
nodeZone scw.Zone
28+
nodeID string
29+
nodeZone scw.Zone
30+
maxVolumesPerNode int64
3031
}
3132

3233
func newNodeService() (*nodeService, error) {
@@ -40,10 +41,16 @@ func newNodeService() (*nodeService, error) {
4041
return nil, fmt.Errorf("invalid zone in metadata: %w", err)
4142
}
4243

44+
maxVolumesPerNode, err := maxVolumesPerNode(attachedScratchVolumes(metadata))
45+
if err != nil {
46+
return nil, err
47+
}
48+
4349
return &nodeService{
44-
diskUtils: newDiskUtils(),
45-
nodeID: metadata.ID,
46-
nodeZone: zone,
50+
diskUtils: newDiskUtils(),
51+
nodeID: metadata.ID,
52+
nodeZone: zone,
53+
maxVolumesPerNode: maxVolumesPerNode,
4754
}, nil
4855
}
4956

@@ -431,7 +438,7 @@ func (d *nodeService) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetC
431438
func (d *nodeService) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
432439
return &csi.NodeGetInfoResponse{
433440
NodeId: d.nodeZone.String() + "/" + d.nodeID,
434-
MaxVolumesPerNode: scaleway.MaxVolumesPerNode - 1, // One is already used by the l_ssd or b_ssd root volume
441+
MaxVolumesPerNode: d.maxVolumesPerNode,
435442
AccessibleTopology: &csi.Topology{
436443
Segments: map[string]string{
437444
ZoneTopologyKey: d.nodeZone.String(),

0 commit comments

Comments
 (0)