ContainerResourceProvisioner implements ConfigurationProvisioner { @Override @Traced public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException { TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId); final Map<String, InternalMachineConfig> machines = k8sEnv.getMachines(); for (PodData pod : k8sEnv.getPodsData().values()) { for (Container container : pod.getSpec().getContainers()) { InternalMachineConfig machineConfig = machines.get(machineName(pod, container)); ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, Containers.getRamLimit(container), Containers.getRamRequest(container), defaultMachineMaxMemorySizeAttribute, defaultMachineRequestMemorySizeAttribute); ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, Containers.getCpuLimit(container), Containers.getCpuRequest(container), defaultMachineCpuLimitAttribute, defaultMachineCpuRequestAttribute); final Map<String, String> attributes = machineConfig.getAttributes(); long memLimit = Long.parseLong(attributes.get(MEMORY_LIMIT_ATTRIBUTE)); if (memLimit > 0) { Containers.addRamLimit(container, memLimit); } long memRequest = Long.parseLong(attributes.get(MEMORY_REQUEST_ATTRIBUTE)); if (memRequest > 0) { Containers.addRamRequest(container, memRequest); } float cpuLimit = Float.parseFloat(attributes.get(CPU_LIMIT_ATTRIBUTE)); if (cpuLimit > 0) { Containers.addCpuLimit(container, cpuLimit); } float cpuRequest = Float.parseFloat(attributes.get(CPU_REQUEST_ATTRIBUTE)); if (cpuRequest > 0) Containers.addCpuRequest(container, cpuRequest); } } } @Inject  ContainerResourceProvisioner(
      @Named("che.workspace.default_memory_limit_mb") long defaultMachineMaxMemorySizeAttribute,
      @Named("che.workspace.default_memory_request_mb")
          long defaultMachineRequestMemorySizeAttribute,
      @Named("che.workspace.default_cpu_limit_cores") String defaultMachineCpuLimitAttribute,
      @Named("che.workspace.default_cpu_request_cores") String defaultMachineCpuRequestAttribute); @Override @Traced void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity);  }
@Test public void testProvisionResourcesLimitAndRequestAttributeToContainer() throws Exception { resourceProvisioner.provision(k8sEnv, identity); assertEquals(container.getResources().getLimits().get("memory").getAmount(), RAM_LIMIT_VALUE); assertEquals(container.getResources().getLimits().get("cpu").getAmount(), CPU_LIMIT_VALUE); assertEquals( container.getResources().getRequests().get("memory").getAmount(), RAM_REQUEST_VALUE); assertEquals(container.getResources().getRequests().get("cpu").getAmount(), CPU_REQUEST_VALUE); }
@Test public void testIgnoreNegativeRAMResourcesLimitAndRequestAttributeToContainer() throws Exception { ContainerResourceProvisioner localResourceProvisioner = new ContainerResourceProvisioner(-1, -1, "-1", "-1"); Map<String, String> attributes = new HashMap<>(); attributes.put(MEMORY_LIMIT_ATTRIBUTE, "-1"); attributes.put(MEMORY_REQUEST_ATTRIBUTE, "-1"); attributes.put(CPU_LIMIT_ATTRIBUTE, CPU_LIMIT_VALUE); attributes.put(CPU_REQUEST_ATTRIBUTE, CPU_REQUEST_VALUE); when(internalMachineConfig.getAttributes()).thenReturn(attributes); localResourceProvisioner.provision(k8sEnv, identity); assertNull(container.getResources().getLimits().get("memory")); assertEquals(container.getResources().getLimits().get("cpu").getAmount(), CPU_LIMIT_VALUE); assertNull(container.getResources().getRequests().get("memory")); assertEquals(container.getResources().getRequests().get("cpu").getAmount(), CPU_REQUEST_VALUE); }
@Test public void testIgnoreNegativeCPUResourcesLimitAndRequestAttributeToContainer() throws Exception { ContainerResourceProvisioner localResourceProvisioner = new ContainerResourceProvisioner(-1, -1, "-1", "-1"); Map<String, String> attributes = new HashMap<>(); attributes.put(MEMORY_LIMIT_ATTRIBUTE, RAM_LIMIT_VALUE); attributes.put(MEMORY_REQUEST_ATTRIBUTE, RAM_REQUEST_VALUE); attributes.put(CPU_LIMIT_ATTRIBUTE, "-1"); attributes.put(CPU_REQUEST_ATTRIBUTE, "-1"); when(internalMachineConfig.getAttributes()).thenReturn(attributes); localResourceProvisioner.provision(k8sEnv, identity); assertEquals(container.getResources().getLimits().get("memory").getAmount(), RAM_LIMIT_VALUE); assertNull(container.getResources().getLimits().get("cpu")); assertEquals( container.getResources().getRequests().get("memory").getAmount(), RAM_REQUEST_VALUE); assertNull(container.getResources().getRequests().get("cpu")); }
@Test public void testOverridesContainerRamLimitAndRequestFromMachineAttribute() throws Exception { ResourceRequirements resourceRequirements = new ResourceRequirementsBuilder() .addToLimits(of("memory", new Quantity("3221225472"), "cpu", new Quantity("0.678"))) .addToRequests(of("memory", new Quantity("1231231423"), "cpu", new Quantity("0.333"))) .build(); container.setResources(resourceRequirements); resourceProvisioner.provision(k8sEnv, identity); assertEquals(container.getResources().getLimits().get("memory").getAmount(), RAM_LIMIT_VALUE); assertEquals(container.getResources().getLimits().get("cpu").getAmount(), CPU_LIMIT_VALUE); assertEquals( container.getResources().getRequests().get("memory").getAmount(), RAM_REQUEST_VALUE); assertEquals(container.getResources().getRequests().get("cpu").getAmount(), CPU_REQUEST_VALUE); }