@@ -26,7 +26,10 @@ const (
26
26
)
27
27
28
28
var (
29
- zeroDec = inf .NewDec (0 , 0 )
29
+ zeroDec = inf .NewDec (0 , 0 )
30
+ miDec = inf .NewDec (1024 * 1024 , 0 )
31
+ cpuFloor = resource .MustParse ("1m" )
32
+ memFloor = resource .MustParse ("1Mi" )
30
33
)
31
34
32
35
func init () {
@@ -153,24 +156,37 @@ func (a *clusterResourceOverridePlugin) Admit(attr admission.Attributes) error {
153
156
resources := container .Resources
154
157
memLimit , memFound := resources .Limits [kapi .ResourceMemory ]
155
158
if memFound && a .config .memoryRequestToLimitRatio .Cmp (zeroDec ) != 0 {
156
- resources .Requests [kapi .ResourceMemory ] = resource.Quantity {
157
- Amount : multiply (memLimit .Amount , a .config .memoryRequestToLimitRatio ),
158
- Format : resource .BinarySI ,
159
+ // memory is measured in whole bytes.
160
+ // the plugin rounds up to the nearest MiB rather than bytes to improve ease of use for end-users.
161
+ amount := multiply (memLimit .Amount , a .config .memoryRequestToLimitRatio )
162
+ roundDownToNearestMi := multiply (divide (amount , miDec , 0 , inf .RoundDown ), miDec )
163
+ value := resource.Quantity {Amount : roundDownToNearestMi , Format : resource .BinarySI }
164
+ if memFloor .Cmp (value ) > 0 {
165
+ value = * (memFloor .Copy ())
159
166
}
167
+ resources .Requests [kapi .ResourceMemory ] = value
160
168
}
161
169
if memFound && a .config .limitCPUToMemoryRatio .Cmp (zeroDec ) != 0 {
162
- resources .Limits [kapi .ResourceCPU ] = resource.Quantity {
163
- // float math is necessary here as there is no way to create an inf.Dec to represent cpuBaseScaleFactor < 0.001
164
- Amount : multiply (inf .NewDec (int64 (float64 (memLimit .Value ())* cpuBaseScaleFactor ), 3 ), a .config .limitCPUToMemoryRatio ),
165
- Format : resource .DecimalSI ,
170
+ // float math is necessary here as there is no way to create an inf.Dec to represent cpuBaseScaleFactor < 0.001
171
+ // cpu is measured in millicores, so we need to scale and round down the value to nearest millicore scale
172
+ amount := multiply (inf .NewDec (int64 (float64 (memLimit .Value ())* cpuBaseScaleFactor ), 3 ), a .config .limitCPUToMemoryRatio )
173
+ amount .Round (amount , 3 , inf .RoundDown )
174
+ value := resource.Quantity {Amount : amount , Format : resource .DecimalSI }
175
+ if cpuFloor .Cmp (value ) > 0 {
176
+ value = * (cpuFloor .Copy ())
166
177
}
178
+ resources .Limits [kapi .ResourceCPU ] = value
167
179
}
168
180
cpuLimit , cpuFound := resources .Limits [kapi .ResourceCPU ]
169
181
if cpuFound && a .config .cpuRequestToLimitRatio .Cmp (zeroDec ) != 0 {
170
- resources .Requests [kapi .ResourceCPU ] = resource.Quantity {
171
- Amount : multiply (cpuLimit .Amount , a .config .cpuRequestToLimitRatio ),
172
- Format : resource .DecimalSI ,
182
+ // cpu is measured in millicores, so we need to scale and round down the value to nearest millicore scale
183
+ amount := multiply (cpuLimit .Amount , a .config .cpuRequestToLimitRatio )
184
+ amount .Round (amount , 3 , inf .RoundDown )
185
+ value := resource.Quantity {Amount : amount , Format : resource .DecimalSI }
186
+ if cpuFloor .Cmp (value ) > 0 {
187
+ value = * (cpuFloor .Copy ())
173
188
}
189
+ resources .Requests [kapi .ResourceCPU ] = value
174
190
}
175
191
}
176
192
glog .V (5 ).Infof ("%s: pod limits after overrides are: %#v" , api .PluginName , pod .Spec .Containers [0 ].Resources )
@@ -180,3 +196,7 @@ func (a *clusterResourceOverridePlugin) Admit(attr admission.Attributes) error {
180
196
func multiply (x * inf.Dec , y * inf.Dec ) * inf.Dec {
181
197
return inf .NewDec (0 , 0 ).Mul (x , y )
182
198
}
199
+
200
+ func divide (x * inf.Dec , y * inf.Dec , s inf.Scale , r inf.Rounder ) * inf.Dec {
201
+ return inf .NewDec (0 , 0 ).QuoRound (x , y , s , r )
202
+ }
0 commit comments