Java Code Examples for io.fabric8.kubernetes.api.model.ResourceRequirements#getRequests()

The following examples show how to use io.fabric8.kubernetes.api.model.ResourceRequirements#getRequests() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractModel.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Adds KAFKA_HEAP_OPTS variable to the EnvVar list if any heap related options were specified.
 * NOTE: If Xmx Java Options are not set DYNAMIC_HEAP_FRACTION and DYNAMIC_HEAP_MAX may also be set
 *
 * @param envVars List of Environment Variables to add to
 * @param dynamicHeapFraction List of Environment Variables
 * @param dynamicHeapMaxBytes List of Environment Variables
 */
protected void heapOptions(List<EnvVar> envVars, double dynamicHeapFraction, long dynamicHeapMaxBytes) {
    StringBuilder kafkaHeapOpts = new StringBuilder();

    String xms = jvmOptions != null ? jvmOptions.getXms() : null;
    if (xms != null) {
        kafkaHeapOpts.append("-Xms")
                .append(xms);
    }

    String xmx = jvmOptions != null ? jvmOptions.getXmx() : null;
    if (xmx != null) {
        // Honour user provided explicit max heap
        kafkaHeapOpts.append(' ').append("-Xmx").append(xmx);
    } else {
        ResourceRequirements resources = getResources();
        Map<String, Quantity> cpuMemory = resources != null ? resources.getRequests() : null;
        // Delegate to the container to figure out only when CGroup memory limits are defined to prevent allocating
        // too much memory on the kubelet.
        if (cpuMemory != null && cpuMemory.get("memory") != null) {
            envVars.add(buildEnvVar(ENV_VAR_DYNAMIC_HEAP_FRACTION, Double.toString(dynamicHeapFraction)));
            if (dynamicHeapMaxBytes > 0) {
                envVars.add(buildEnvVar(ENV_VAR_DYNAMIC_HEAP_MAX, Long.toString(dynamicHeapMaxBytes)));
            }
        // When no memory limit, `Xms`, and `Xmx` are defined then set a default `Xms` and
        // leave `Xmx` undefined.
        } else if (xms == null) {
            kafkaHeapOpts.append("-Xms").append(DEFAULT_JVM_XMS);
        }
    }

    String kafkaHeapOptsString = kafkaHeapOpts.toString().trim();
    if (!kafkaHeapOptsString.isEmpty()) {
        envVars.add(buildEnvVar(ENV_VAR_KAFKA_HEAP_OPTS, kafkaHeapOptsString));
    }
}
 
Example 2
Source File: Containers.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the RAM request in bytes, if it is present in given container otherwise 0 will be
 * returned.
 */
public static long getRamRequest(Container container) {
  final ResourceRequirements resources = container.getResources();
  final Quantity quantity;
  if (resources != null
      && resources.getRequests() != null
      && (quantity = resources.getRequests().get("memory")) != null
      && quantity.getAmount() != null) {
    return Quantity.getAmountInBytes(quantity).longValue();
  }
  return 0;
}
 
Example 3
Source File: Containers.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the CPU request in bytes, if it is present in given container otherwise 0 will be
 * returned.
 */
public static float getCpuRequest(Container container) {
  final ResourceRequirements resources = container.getResources();
  final Quantity quantity;
  if (resources != null
      && resources.getRequests() != null
      && (quantity = resources.getRequests().get("cpu")) != null
      && quantity.getAmount() != null) {
    return KubernetesSize.toCores(quantity.getAmount());
  }
  return 0;
}
 
Example 4
Source File: PodTemplateBuilderTest.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultRequests() throws Exception {
    PodTemplate template = new PodTemplate();
    Pod pod = new PodTemplateBuilder(template).build();
    ResourceRequirements resources = pod.getSpec().getContainers().get(0).getResources();
    assertNotNull(resources);
    Map<String, Quantity> requests = resources.getRequests();
    assertNotNull(requests);
    PodTemplateUtilsTest.assertQuantity(PodTemplateBuilder.DEFAULT_JNLP_CONTAINER_CPU_REQUEST, requests.get("cpu"));
    PodTemplateUtilsTest.assertQuantity(PodTemplateBuilder.DEFAULT_JNLP_CONTAINER_MEMORY_REQUEST, requests.get("memory"));
}
 
Example 5
Source File: InitTaskManagerDecoratorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMainContainerResourceRequirements() {
	final ResourceRequirements resourceRequirements = this.resultMainContainer.getResources();

	final Map<String, Quantity> requests = resourceRequirements.getRequests();
	assertEquals(Double.toString(TASK_MANAGER_CPU), requests.get("cpu").getAmount());
	assertEquals(String.valueOf(TOTAL_PROCESS_MEMORY), requests.get("memory").getAmount());

	final Map<String, Quantity> limits = resourceRequirements.getLimits();
	assertEquals(Double.toString(TASK_MANAGER_CPU), limits.get("cpu").getAmount());
	assertEquals(String.valueOf(TOTAL_PROCESS_MEMORY), limits.get("memory").getAmount());
}
 
Example 6
Source File: InitTaskManagerDecoratorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testExternalResourceInResourceRequirements() {
	final ResourceRequirements resourceRequirements = this.resultMainContainer.getResources();

	final Map<String, Quantity> requests = resourceRequirements.getRequests();
	assertEquals(Long.toString(RESOURCE_AMOUNT), requests.get(RESOURCE_CONFIG_KEY).getAmount());

	final Map<String, Quantity> limits = resourceRequirements.getLimits();
	assertEquals(Long.toString(RESOURCE_AMOUNT), limits.get(RESOURCE_CONFIG_KEY).getAmount());
}
 
Example 7
Source File: InitJobManagerDecoratorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMainContainerResourceRequirements() {
	final ResourceRequirements resourceRequirements = this.resultMainContainer.getResources();

	final Map<String, Quantity> requests = resourceRequirements.getRequests();
	assertEquals(Double.toString(JOB_MANAGER_CPU), requests.get("cpu").getAmount());
	assertEquals(String.valueOf(JOB_MANAGER_MEMORY), requests.get("memory").getAmount());

	final Map<String, Quantity> limits = resourceRequirements.getLimits();
	assertEquals(Double.toString(JOB_MANAGER_CPU), limits.get("cpu").getAmount());
	assertEquals(String.valueOf(JOB_MANAGER_MEMORY), limits.get("memory").getAmount());
}