@@ -57,7 +57,52 @@ type ZoneData struct {
5757 Available bool
5858}
5959
60- func NewInstanceType (ctx context.Context , overhead corev1.ResourceList ,
60+ func calculateResourceOverhead (pods , cpuM , memoryMi int64 ) corev1.ResourceList {
61+ // referring to: https://help.aliyun.com/zh/ack/ack-managed-and-ack-dedicated/user-guide/resource-reservation-policy#0f5ffe176df7q
62+ // CPU overhead calculation
63+ cpuOverHead := calculateCPUOverhead (cpuM )
64+
65+ // TODO: In a real environment, the formula does not produce accurate results,
66+ // consistently yielding values that are 200MiB larger than expected.
67+ // Memory overhead: min(11*pods + 255, memoryMi*0.25)
68+ memoryOverHead := int64 (math .Min (float64 (11 * pods + 255 ), float64 (memoryMi )* 0.25 )) + 200
69+
70+ return corev1.ResourceList {
71+ corev1 .ResourceCPU : * resource .NewMilliQuantity (cpuOverHead , resource .DecimalSI ),
72+ corev1 .ResourceMemory : * resources .Quantity (fmt .Sprintf ("%dMi" , memoryOverHead )),
73+ }
74+ }
75+
76+ // thresholds defines CPU overhead thresholds and their corresponding percentages
77+ var thresholds = [... ]struct {
78+ cores int64
79+ overhead float64
80+ }{
81+ {1000 , 0.06 },
82+ {3000 , 0.01 },
83+ {3000 , 0.005 },
84+ {4000 , 0.005 },
85+ }
86+
87+ func calculateCPUOverhead (cpuM int64 ) int64 {
88+ var cpuOverHead int64
89+
90+ // Calculate overhead for each threshold
91+ for _ , t := range thresholds {
92+ if cpuM >= t .cores {
93+ cpuOverHead += int64 (1000 * t .overhead )
94+ }
95+ }
96+
97+ // Additional overhead for CPU > 4 cores (0.25%)
98+ if cpuM > 4000 {
99+ cpuOverHead += int64 (float64 (cpuM - 4000 ) * 0.0025 )
100+ }
101+
102+ return cpuOverHead
103+ }
104+
105+ func NewInstanceType (ctx context.Context ,
61106 info * ecsclient.DescribeInstanceTypesResponseBodyInstanceTypesInstanceType ,
62107 kc * v1alpha1.KubeletConfiguration , region string , systemDisk * v1alpha1.SystemDisk ,
63108 offerings cloudprovider.Offerings , clusterCNI string ) * cloudprovider.InstanceType {
@@ -71,12 +116,15 @@ func NewInstanceType(ctx context.Context, overhead corev1.ResourceList,
71116 Offerings : offerings ,
72117 Capacity : computeCapacity (ctx , info , kc .MaxPods , kc .PodsPerCore , systemDisk , clusterCNI ),
73118 Overhead : & cloudprovider.InstanceTypeOverhead {
74- // Follow overhead will be merged, so we can set only one overhead totally
75- KubeReserved : overhead ,
119+ KubeReserved : corev1.ResourceList {},
76120 SystemReserved : corev1.ResourceList {},
77121 EvictionThreshold : corev1.ResourceList {},
78122 },
79123 }
124+
125+ // Follow KubeReserved/SystemReserved/EvictionThreshold will be merged, so we can set only one overhead totally
126+ it .Overhead .KubeReserved = calculateResourceOverhead (it .Capacity .Pods ().Value (),
127+ it .Capacity .Cpu ().MilliValue (), extractMemory (info ).Value ()/ MiBByteRatio )
80128 if it .Requirements .Compatible (scheduling .NewRequirements (scheduling .NewRequirement (corev1 .LabelOSStable , corev1 .NodeSelectorOpIn , string (corev1 .Windows )))) == nil {
81129 it .Capacity [v1alpha1 .ResourcePrivateIPv4Address ] = * privateIPv4Address (info )
82130 }
@@ -206,9 +254,13 @@ func cpu(info *ecsclient.DescribeInstanceTypesResponseBodyInstanceTypesInstanceT
206254 return resources .Quantity (fmt .Sprint (* info .CpuCoreCount ))
207255}
208256
209- func memory ( ctx context. Context , info * ecsclient.DescribeInstanceTypesResponseBodyInstanceTypesInstanceType ) * resource.Quantity {
257+ func extractMemory ( info * ecsclient.DescribeInstanceTypesResponseBodyInstanceTypesInstanceType ) * resource.Quantity {
210258 sizeInGib := tea .Float32Value (info .MemorySize )
211- mem := resources .Quantity (fmt .Sprintf ("%fGi" , sizeInGib ))
259+ return resources .Quantity (fmt .Sprintf ("%fGi" , sizeInGib ))
260+ }
261+
262+ func memory (ctx context.Context , info * ecsclient.DescribeInstanceTypesResponseBodyInstanceTypesInstanceType ) * resource.Quantity {
263+ mem := extractMemory (info )
212264 if mem .IsZero () {
213265 return mem
214266 }
0 commit comments