fix container cpu resource conversion (#18527)

Signed-off-by: Michal Vala <mvala@redhat.com>
7.24.x
Michal Vala 2020-12-04 13:08:28 +01:00 committed by GitHub
parent 7cf011ba38
commit 31dde19914
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View File

@ -128,7 +128,7 @@ public class Containers {
&& resources.getLimits() != null
&& (quantity = resources.getLimits().get("cpu")) != null
&& quantity.getAmount() != null) {
return KubernetesSize.toCores(quantity.getAmount());
return KubernetesSize.toCores(quantity);
}
return 0;
}
@ -174,7 +174,7 @@ public class Containers {
&& resources.getRequests() != null
&& (quantity = resources.getRequests().get("cpu")) != null
&& quantity.getAmount() != null) {
return KubernetesSize.toCores(quantity.getAmount());
return KubernetesSize.toCores(quantity);
}
return 0;
}

View File

@ -11,6 +11,7 @@
*/
package org.eclipse.che.workspace.infrastructure.kubernetes.util;
import io.fabric8.kubernetes.api.model.Quantity;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -154,4 +155,16 @@ public class KubernetesSize {
}
throw new IllegalArgumentException("Invalid Kubernetes CPU size format provided: " + cpuString);
}
/**
* Converts CPU resource from {@link Quantity} object to cores.
*
* <p>see {@link KubernetesSize#toCores(String)} for conversion rules
*
* @param quantity to convert
* @return value in cores
*/
public static float toCores(Quantity quantity) {
return toCores(quantity.getAmount() + quantity.getFormat());
}
}

View File

@ -288,4 +288,13 @@ public class ContainersTest {
{"155m", "155", "m", null},
};
}
@Test
public void testReturnContainerCPULimitAndRequestConvertedToFullCores() {
when(resource.getLimits()).thenReturn(ImmutableMap.of("cpu", new Quantity("1000", "m")));
when(resource.getRequests()).thenReturn(ImmutableMap.of("cpu", new Quantity("30", "m")));
assertEquals(Containers.getCpuLimit(container), 1);
assertEquals(Containers.getCpuRequest(container), 0.03, 0.000000001);
}
}