Skip to content

Commit 7f9cc68

Browse files
authored
Merge pull request #2064 from sunnylovestiramisu/release-1.16
Add Attach Limit for Hyperdisk + Gen4 VMs
2 parents d39f374 + 97127a9 commit 7f9cc68

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

pkg/common/constants.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ const (
3333
// Label that is set on a disk when it is used by a 'multi-zone' VolumeHandle
3434
MultiZoneLabel = "goog-gke-multi-zone"
3535
)
36+
37+
// doc https://cloud.google.com/compute/docs/disks/hyperdisks#max-total-disks-per-vm
38+
var Gen4MachineHyperdiskAttachLimitMap = []struct {
39+
max int64
40+
value int64
41+
}{
42+
{max: 4, value: 15},
43+
{max: 8, value: 23},
44+
{max: 16, value: 31},
45+
{max: 32, value: 49},
46+
{max: 64, value: 63},
47+
{max: 1024, value: 127},
48+
}

pkg/common/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,17 @@ func NewLimiter(limit, burst int, emptyBucket bool) *rate.Limiter {
696696

697697
return limiter
698698
}
699+
700+
func IsHyperdisk(diskType string) bool {
701+
return strings.HasPrefix(diskType, "hyperdisk-")
702+
}
703+
704+
// MapNumber is a function to map input cpu number to the Hyperdisk attach limit
705+
func MapNumber(num int64) int64 {
706+
for _, r := range Gen4MachineHyperdiskAttachLimitMap {
707+
if num <= r.max {
708+
return r.value
709+
}
710+
}
711+
return 0
712+
}

pkg/gce-pd-csi-driver/node.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"regexp"
2424
"runtime"
2525
"strconv"
26+
"strings"
2627
"time"
2728

2829
"google.golang.org/grpc/codes"
@@ -89,8 +90,12 @@ var _ csi.NodeServer = &GCENodeServer{}
8990
// node boot disk is considered an attachable disk so effective attach limit is
9091
// one less.
9192
const (
92-
volumeLimitSmall int64 = 15
93-
volumeLimitBig int64 = 127
93+
volumeLimitSmall int64 = 15
94+
volumeLimitBig int64 = 127
95+
// doc https://cloud.google.com/compute/docs/memory-optimized-machines#x4_disks
96+
x4HyperdiskLimit int64 = 39
97+
// doc https://cloud.google.com/compute/docs/accelerator-optimized-machines#a4-disks
98+
a4HyperdiskLimit int64 = 127
9499
defaultLinuxFsType = "ext4"
95100
defaultWindowsFsType = "ntfs"
96101
fsTypeExt3 = "ext3"
@@ -527,6 +532,9 @@ func (ns *GCENodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRe
527532
nodeID := common.CreateNodeID(ns.MetadataService.GetProject(), ns.MetadataService.GetZone(), ns.MetadataService.GetName())
528533

529534
volumeLimits, err := ns.GetVolumeLimits()
535+
if err != nil {
536+
klog.Errorf("GetVolumeLimits failed: %v", err.Error())
537+
}
530538

531539
resp := &csi.NodeGetInfoResponse{
532540
NodeId: nodeID,
@@ -693,5 +701,24 @@ func (ns *GCENodeServer) GetVolumeLimits() (int64, error) {
693701
return volumeLimitSmall, nil
694702
}
695703
}
704+
gen4MachineTypesPrefix := []string{"c4a-", "c4-", "n4-"}
705+
for _, gen4Prefix := range gen4MachineTypesPrefix {
706+
if strings.HasPrefix(machineType, gen4Prefix) {
707+
cpuString := machineType[strings.LastIndex(machineType, "-")+1:]
708+
cpus, err := strconv.ParseInt(cpuString, 10, 64)
709+
if err != nil {
710+
return volumeLimitSmall, fmt.Errorf("invalid cpuString %s for machine type: %v", cpuString, machineType)
711+
}
712+
return common.MapNumber(cpus), nil
713+
714+
}
715+
if strings.HasPrefix(machineType, "x4-") {
716+
return x4HyperdiskLimit, nil
717+
}
718+
if strings.HasPrefix(machineType, "a4-") {
719+
return a4HyperdiskLimit, nil
720+
}
721+
}
722+
696723
return volumeLimitBig, nil
697724
}

pkg/gce-pd-csi-driver/node_test.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ func TestNodeGetVolumeLimits(t *testing.T) {
222222
name string
223223
machineType string
224224
expVolumeLimit int64
225+
expectError bool
225226
}{
226227
{
227228
name: "Predifined standard machine",
@@ -253,13 +254,54 @@ func TestNodeGetVolumeLimits(t *testing.T) {
253254
machineType: "e2-micro",
254255
expVolumeLimit: volumeLimitSmall,
255256
},
257+
{
258+
name: "c4-standard-192",
259+
machineType: "c4-standard-192",
260+
expVolumeLimit: 127,
261+
},
262+
{
263+
name: "c4-standard-48",
264+
machineType: "c4-standard-48",
265+
expVolumeLimit: 63,
266+
},
267+
{
268+
name: "c4a-standard-4",
269+
machineType: "c4a-standard-4",
270+
expVolumeLimit: 15,
271+
},
272+
{
273+
name: "n4-standard-16",
274+
machineType: "n4-standard-16",
275+
expVolumeLimit: 31,
276+
},
277+
{
278+
name: "n4-highcpu-4",
279+
machineType: "n4-highcpu-4",
280+
expVolumeLimit: 15,
281+
},
282+
{
283+
name: "invalid gen4 machine type",
284+
machineType: "n4-highcpu-4xyz",
285+
expVolumeLimit: volumeLimitSmall,
286+
expectError: true,
287+
},
288+
{
289+
name: "x4-megamem-960-metal",
290+
machineType: "x4-megamem-960-metal",
291+
expVolumeLimit: x4HyperdiskLimit,
292+
},
293+
{
294+
name: "a4-highgpu-8g",
295+
machineType: "a4-highgpu-8g",
296+
expVolumeLimit: a4HyperdiskLimit,
297+
},
256298
}
257299

258300
for _, tc := range testCases {
259301
t.Logf("Test case: %s", tc.name)
260302
metadataservice.SetMachineType(tc.machineType)
261303
res, err := ns.NodeGetInfo(context.Background(), req)
262-
if err != nil {
304+
if err != nil && !tc.expectError {
263305
t.Fatalf("Failed to get node info: %v", err)
264306
} else {
265307
volumeLimit := res.GetMaxVolumesPerNode()

0 commit comments

Comments
 (0)