Skip to content

Commit 7540897

Browse files
k8s-ci-robotsunnylovestiramisu
authored andcommitted
Merge pull request #2127 from arsiesys/fix/hyperdisk-limits
fix: update Hyperdisk attach limits to match GCP documentation for Gen4 machines
1 parent b8b0251 commit 7540897

File tree

4 files changed

+88
-24
lines changed

4 files changed

+88
-24
lines changed

pkg/common/constants.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,42 @@ const (
6565
AttachLimitOverrideLabel = "gke-volume-attach-limit-override"
6666
)
6767

68-
// doc https://cloud.google.com/compute/docs/disks/hyperdisks#max-total-disks-per-vm
69-
var Gen4MachineHyperdiskAttachLimitMap = []struct {
68+
// doc https://cloud.google.com/compute/docs/general-purpose-machines
69+
// MachineHyperdiskLimit represents the mapping between max vCPUs and hyperdisk (balanced) attach limit
70+
type MachineHyperdiskLimit struct {
7071
max int64
7172
value int64
72-
}{
73+
}
74+
75+
// C4 Machine Types - Hyperdisk Balanced Limits
76+
var C4MachineHyperdiskAttachLimitMap = []MachineHyperdiskLimit{
77+
{max: 2, value: 7},
7378
{max: 4, value: 15},
74-
{max: 8, value: 23},
75-
{max: 16, value: 31},
76-
{max: 32, value: 49},
77-
{max: 64, value: 63},
78-
{max: 1024, value: 127},
79+
{max: 24, value: 31},
80+
{max: 48, value: 63},
81+
{max: 96, value: 127},
82+
}
83+
84+
// C4D Machine Types - Hyperdisk Balanced Limits
85+
var C4DMachineHyperdiskAttachLimitMap = []MachineHyperdiskLimit{
86+
{max: 2, value: 3},
87+
{max: 4, value: 7},
88+
{max: 8, value: 15},
89+
{max: 96, value: 31},
90+
{max: 192, value: 63},
91+
{max: 384, value: 127},
92+
}
93+
94+
// N4 Machine Types - Hyperdisk Balanced Limits
95+
var N4MachineHyperdiskAttachLimitMap = []MachineHyperdiskLimit{
96+
{max: 8, value: 15},
97+
{max: 80, value: 31},
98+
}
99+
100+
// C4A Machine Types - Hyperdisk Balanced Limits
101+
var C4AMachineHyperdiskAttachLimitMap = []MachineHyperdiskLimit{
102+
{max: 2, value: 7},
103+
{max: 8, value: 15},
104+
{max: 48, value: 31},
105+
{max: 72, value: 63},
79106
}

pkg/common/utils.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -762,14 +762,39 @@ func ShortString(s string) string {
762762
return string(short)
763763
}
764764

765-
// MapNumber is a function to map input cpu number to the Hyperdisk attach limit
766-
func MapNumber(num int64) int64 {
767-
for _, r := range Gen4MachineHyperdiskAttachLimitMap {
768-
if num <= r.max {
769-
return r.value
765+
// GetHyperdiskAttachLimit returns the hyperdisk attach limit based on machine type prefix and vCPUs
766+
func GetHyperdiskAttachLimit(machineTypePrefix string, vCPUs int64) int64 {
767+
var limitMap []MachineHyperdiskLimit
768+
769+
switch machineTypePrefix {
770+
case "c4":
771+
limitMap = C4MachineHyperdiskAttachLimitMap
772+
case "c4d":
773+
limitMap = C4DMachineHyperdiskAttachLimitMap
774+
case "n4":
775+
limitMap = N4MachineHyperdiskAttachLimitMap
776+
case "c4a":
777+
limitMap = C4AMachineHyperdiskAttachLimitMap
778+
default:
779+
// Fallback to the most conservative Gen4 map for unknown types
780+
return MapNumber(vCPUs, C4DMachineHyperdiskAttachLimitMap)
781+
}
782+
783+
return MapNumber(vCPUs, limitMap)
784+
}
785+
786+
// mapNumber maps the vCPUs to the appropriate hyperdisk limit
787+
func MapNumber(vCPUs int64, limitMap []MachineHyperdiskLimit) int64 {
788+
for _, limit := range limitMap {
789+
if vCPUs <= limit.max {
790+
return limit.value
770791
}
771792
}
772-
return 0
793+
// Return the last value if vCPUs exceeds all max values
794+
if len(limitMap) > 0 {
795+
return limitMap[len(limitMap)-1].value
796+
}
797+
return 15
773798
}
774799

775800
func DiskTypeLabelKey(diskType string) string {

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ func (ns *GCENodeServer) GetVolumeLimits(ctx context.Context) (int64, error) {
848848
}
849849

850850
// Process gen4 machine attach limits
851-
gen4MachineTypesPrefix := []string{"c4a-", "c4-", "n4-"}
851+
gen4MachineTypesPrefix := []string{"c4a-", "c4-", "n4-", "c4d-"}
852852
for _, gen4Prefix := range gen4MachineTypesPrefix {
853853
if strings.HasPrefix(machineType, gen4Prefix) {
854854
machineTypeSlice := strings.Split(machineType, "-")
@@ -858,17 +858,19 @@ func (ns *GCENodeServer) GetVolumeLimits(ctx context.Context) (int64, error) {
858858
if err != nil {
859859
return volumeLimitBig, fmt.Errorf("invalid cpuString %s for machine type: %v", cpuString, machineType)
860860
}
861-
return common.MapNumber(cpus), nil
861+
// Extract the machine type prefix (e.g., "c4", "c4a", "n4")
862+
prefix := strings.TrimSuffix(gen4Prefix, "-")
863+
return common.GetHyperdiskAttachLimit(prefix, cpus), nil
862864
} else {
863865
return volumeLimitBig, fmt.Errorf("unconventional machine type: %v", machineType)
864866
}
865867
}
866-
if strings.HasPrefix(machineType, "x4-") {
867-
return x4HyperdiskLimit, nil
868-
}
869-
if strings.HasPrefix(machineType, "a4-") {
870-
return a4HyperdiskLimit, nil
871-
}
868+
}
869+
if strings.HasPrefix(machineType, "x4-") {
870+
return x4HyperdiskLimit, nil
871+
}
872+
if strings.HasPrefix(machineType, "a4-") {
873+
return a4HyperdiskLimit, nil
872874
}
873875

874876
return volumeLimitBig, nil

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ func TestNodeGetVolumeLimits(t *testing.T) {
278278
machineType: "c4-standard-48",
279279
expVolumeLimit: 63,
280280
},
281+
{
282+
name: "c4-standard-2",
283+
machineType: "c4-standard-2",
284+
expVolumeLimit: 7,
285+
},
281286
{
282287
name: "c4a-standard-4",
283288
machineType: "c4a-standard-4",
@@ -302,7 +307,7 @@ func TestNodeGetVolumeLimits(t *testing.T) {
302307
{
303308
name: "n4-custom-8-12345-ext",
304309
machineType: "n4-custom-8-12345-ext",
305-
expVolumeLimit: 23,
310+
expVolumeLimit: 15,
306311
},
307312
{
308313
name: "n4-custom-16-12345",
@@ -338,7 +343,12 @@ func TestNodeGetVolumeLimits(t *testing.T) {
338343
{
339344
name: "c4a-standard-32-lssd",
340345
machineType: "c4a-standard-32-lssd",
341-
expVolumeLimit: 49,
346+
expVolumeLimit: 31,
347+
},
348+
{
349+
name: "c4d-standard-32",
350+
machineType: "c4d-standard-32",
351+
expVolumeLimit: 31,
342352
},
343353
}
344354

0 commit comments

Comments
 (0)