Skip to content

Commit

Permalink
Fix precision of cpu to millicore and memory to bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwaynecarr committed Apr 7, 2016
1 parent 1054eb4 commit 4675426
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
17 changes: 13 additions & 4 deletions pkg/quota/admission/clusterresourceoverride/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,31 @@ func (a *clusterResourceOverridePlugin) Admit(attr admission.Attributes) error {
resources := container.Resources
memLimit, memFound := resources.Limits[kapi.ResourceMemory]
if memFound && a.config.memoryRequestToLimitRatio.Cmp(zeroDec) != 0 {
// memory is measured in whole bytes, so we need to scale and round up the value to nearest byte
amount := multiply(memLimit.Amount, a.config.memoryRequestToLimitRatio)
amount.Round(amount, 0, inf.RoundUp)
resources.Requests[kapi.ResourceMemory] = resource.Quantity{
Amount: multiply(memLimit.Amount, a.config.memoryRequestToLimitRatio),
Amount: amount,
Format: resource.BinarySI,
}
}
if memFound && a.config.limitCPUToMemoryRatio.Cmp(zeroDec) != 0 {
// float math is necessary here as there is no way to create an inf.Dec to represent cpuBaseScaleFactor < 0.001
// cpu is measured in millicores, so we need to scale and round up the value to nearest millicore scale
amount := multiply(inf.NewDec(int64(float64(memLimit.Value())*cpuBaseScaleFactor), 3), a.config.limitCPUToMemoryRatio)
amount.Round(amount, 3, inf.RoundUp)
resources.Limits[kapi.ResourceCPU] = resource.Quantity{
// float math is necessary here as there is no way to create an inf.Dec to represent cpuBaseScaleFactor < 0.001
Amount: multiply(inf.NewDec(int64(float64(memLimit.Value())*cpuBaseScaleFactor), 3), a.config.limitCPUToMemoryRatio),
Amount: amount,
Format: resource.DecimalSI,
}
}
cpuLimit, cpuFound := resources.Limits[kapi.ResourceCPU]
if cpuFound && a.config.cpuRequestToLimitRatio.Cmp(zeroDec) != 0 {
// cpu is measured in millicores, so we need to scale and round up the value to nearest millicore scale
amount := multiply(cpuLimit.Amount, a.config.cpuRequestToLimitRatio)
amount.Round(amount, 3, inf.RoundUp)
resources.Requests[kapi.ResourceCPU] = resource.Quantity{
Amount: multiply(cpuLimit.Amount, a.config.cpuRequestToLimitRatio),
Amount: amount,
Format: resource.DecimalSI,
}
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/quota/admission/clusterresourceoverride/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,18 @@ func TestLimitRequestAdmission(t *testing.T) {
name: "little values don't get lost",
config: testConfig(500, 10, 10),
object: testPod("1.024Mi", "0", "0", "0"),
expectedMemRequest: resource.MustParse(".0001Gi"),
expectedMemRequest: resource.MustParse("107375"),
expectedCpuLimit: resource.MustParse("5m"),
expectedCpuRequest: resource.MustParse(".5m"),
expectedCpuRequest: resource.MustParse("1m"),
namespace: fakeNamespace(true),
},
{
name: "test fractional memory requests round up",
config: testConfig(500, 10, 60),
object: testPod("512Mi", "0", "0", "0"),
expectedMemRequest: resource.MustParse("322122548"),
expectedCpuLimit: resource.MustParse("2.5"),
expectedCpuRequest: resource.MustParse("250m"),
namespace: fakeNamespace(true),
},
}
Expand Down

0 comments on commit 4675426

Please sign in to comment.