Skip to content

Commit 0be5a10

Browse files
authored
Merge pull request #2019 from sunnylovestiramisu/updatek8s
Add Attach Limit for Hyperdisk + Gen4 VMs
2 parents 65febf5 + 8498951 commit 0be5a10

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

Diff for: pkg/common/constants.go

+13
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,16 @@ const (
5757
NodeLabelPrefix = "cloud.google.com/%s"
5858
DataCacheLssdCountLabel = "gke-data-cache-disk"
5959
)
60+
61+
// doc https://cloud.google.com/compute/docs/disks/hyperdisks#max-total-disks-per-vm
62+
var Gen4MachineHyperdiskAttachLimitMap = []struct {
63+
max int64
64+
value int64
65+
}{
66+
{max: 4, value: 16},
67+
{max: 8, value: 24},
68+
{max: 16, value: 32},
69+
{max: 32, value: 48},
70+
{max: 64, value: 64},
71+
{max: 1024, value: 128},
72+
}

Diff for: pkg/common/utils.go

+10
Original file line numberDiff line numberDiff line change
@@ -754,3 +754,13 @@ func ShortString(s string) string {
754754
}
755755
return string(short)
756756
}
757+
758+
// MapNumber is a function to map input cpu number to the Hyperdisk attach limit
759+
func MapNumber(num int64) int64 {
760+
for _, r := range Gen4MachineHyperdiskAttachLimitMap {
761+
if num <= r.max {
762+
return r.value
763+
}
764+
}
765+
return 0
766+
}

Diff for: pkg/gce-pd-csi-driver/node.go

+29-2
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"
@@ -95,8 +96,12 @@ var _ csi.NodeServer = &GCENodeServer{}
9596
// node boot disk is considered an attachable disk so effective attach limit is
9697
// one less.
9798
const (
98-
volumeLimitSmall int64 = 15
99-
volumeLimitBig int64 = 127
99+
volumeLimitSmall int64 = 15
100+
volumeLimitBig int64 = 127
101+
// doc https://cloud.google.com/compute/docs/memory-optimized-machines#x4_disks
102+
x4HyperdiskLimit int64 = 40
103+
// doc https://cloud.google.com/compute/docs/accelerator-optimized-machines#a4-disks
104+
a4HyperdiskLimit int64 = 128
100105
defaultLinuxFsType = "ext4"
101106
defaultWindowsFsType = "ntfs"
102107
fsTypeExt3 = "ext3"
@@ -567,6 +572,9 @@ func (ns *GCENodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRe
567572
nodeID := common.CreateNodeID(ns.MetadataService.GetProject(), ns.MetadataService.GetZone(), ns.MetadataService.GetName())
568573

569574
volumeLimits, err := ns.GetVolumeLimits()
575+
if err != nil {
576+
klog.Errorf("GetVolumeLimits failed: %v", err.Error())
577+
}
570578

571579
resp := &csi.NodeGetInfoResponse{
572580
NodeId: nodeID,
@@ -733,5 +741,24 @@ func (ns *GCENodeServer) GetVolumeLimits() (int64, error) {
733741
return volumeLimitSmall, nil
734742
}
735743
}
744+
gen4MachineTypesPrefix := []string{"c4a-", "c4-", "n4-"}
745+
for _, gen4Prefix := range gen4MachineTypesPrefix {
746+
if strings.HasPrefix(machineType, gen4Prefix) {
747+
cpuString := machineType[strings.LastIndex(machineType, "-")+1:]
748+
cpus, err := strconv.ParseInt(cpuString, 10, 64)
749+
if err != nil {
750+
return volumeLimitSmall, fmt.Errorf("invalid cpuString %s for machine type: %v", cpuString, machineType)
751+
}
752+
return common.MapNumber(cpus), nil
753+
754+
}
755+
if strings.HasPrefix(machineType, "x4-") {
756+
return x4HyperdiskLimit, nil
757+
}
758+
if strings.HasPrefix(machineType, "a4-") {
759+
return a4HyperdiskLimit, nil
760+
}
761+
}
762+
736763
return volumeLimitBig, nil
737764
}

Diff for: pkg/gce-pd-csi-driver/node_test.go

+43-1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ func TestNodeGetVolumeLimits(t *testing.T) {
224224
name string
225225
machineType string
226226
expVolumeLimit int64
227+
expectError bool
227228
}{
228229
{
229230
name: "Predifined standard machine",
@@ -255,13 +256,54 @@ func TestNodeGetVolumeLimits(t *testing.T) {
255256
machineType: "e2-micro",
256257
expVolumeLimit: volumeLimitSmall,
257258
},
259+
{
260+
name: "c4-standard-192",
261+
machineType: "c4-standard-192",
262+
expVolumeLimit: 128,
263+
},
264+
{
265+
name: "c4-standard-48",
266+
machineType: "c4-standard-48",
267+
expVolumeLimit: 64,
268+
},
269+
{
270+
name: "c4a-standard-4",
271+
machineType: "c4a-standard-4",
272+
expVolumeLimit: 16,
273+
},
274+
{
275+
name: "n4-standard-16",
276+
machineType: "n4-standard-16",
277+
expVolumeLimit: 32,
278+
},
279+
{
280+
name: "n4-highcpu-4",
281+
machineType: "n4-highcpu-4",
282+
expVolumeLimit: 16,
283+
},
284+
{
285+
name: "invalid gen4 machine type",
286+
machineType: "n4-highcpu-4xyz",
287+
expVolumeLimit: volumeLimitSmall,
288+
expectError: true,
289+
},
290+
{
291+
name: "x4-megamem-960-metal",
292+
machineType: "x4-megamem-960-metal",
293+
expVolumeLimit: x4HyperdiskLimit,
294+
},
295+
{
296+
name: "a4-highgpu-8g",
297+
machineType: "a4-highgpu-8g",
298+
expVolumeLimit: a4HyperdiskLimit,
299+
},
258300
}
259301

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

0 commit comments

Comments
 (0)