How to set resource requirements on a container with the fabric8 kubernetes Java & Scala client API How to set resource requirements on a container with the fabric8 kubernetes Java & Scala client API kubernetes kubernetes

How to set resource requirements on a container with the fabric8 kubernetes Java & Scala client API


If you are using the fabric8 kubernetes-client API for Java and Scala, here is a snippet of code that demonstrates how to add resource requirements to a container running in a pod. This code was copied from Scala, but Java code would be very similar:

// other fabric8 imports not included; just focusing on resource// requirements logic in this exampleimport io.fabric8.kubernetes.api.model.Quantityimport io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder// Use Java style Map (as opposed to Scala's Map class)val reqMap: java.util.Map[String, Quantity] =  new java.util.HashMap[String, Quantity]()// add CPU and memory requirements to the mapreqMap.put("cpu", new Quantity("1"))reqMap.put("memory", new Quantity("1500Mi"))// Build a ResourceRequirements object from the mapval reqs = new ResourceRequirementsBuilder()  .withRequests(reqMap)  .build()// pass the ResourceRequirements object to the container specval pod = new PodBuilder()  .withNewMetadata()  .withName(podName)  .endMetadata()  .withNewSpec()  .withRestartPolicy("OnFailure")  .addNewContainer()  .withName(containerName)  .withImage(containerImage)  .withImagePullPolicy("Always")  .withResources(reqs)            // <-- resource reqs here  .withCommand(commandName)  .withArgs(commandArguments)  .endContainer()  .endSpec()  .build()// create the new pod with resource requirements via the // fabric8 kube client:client.pods().inNamespace(nameSpace).withName(podName).create(pod)


    if(EmptyUtil.isNotNull(template.getComputeRequest()) ||            EmptyUtil.isNotNull(template.getComputeLimit())) {        containerBuilder.withResources(buildResources(template.getComputeRequest(), template.getComputeLimit()));    }private ResourceRequirements buildResources(InstanceType request, InstanceType limit){    _logger.info("Building computeResources");    ResourceRequirementsBuilder requirementsBuilder = new ResourceRequirementsBuilder(isValidationEnabled);    if(EmptyUtil.isNotNull(request)){        requirementsBuilder.withRequests(K8ComputeResourceUtil.buildCompute(request));    }    if(EmptyUtil.isNotNull(limit)){        requirementsBuilder.withLimits(K8ComputeResourceUtil.buildCompute(limit));    }    return requirementsBuilder.build();}public static Map<String, Quantity> buildCompute(InstanceType compute){    Map<String, Quantity> computeResourceMap = new HashMap<>();    if(EmptyUtil.isNotNull(compute.getCpu())) {        computeResourceMap.putAll(buildCpu(compute.getCpu()));    }    if(EmptyUtil.isNotNull(compute.getMemory())) {        computeResourceMap.putAll(buildMemory(compute.getMemory()));    }    return computeResourceMap;}private static Map<String, Quantity> buildCpu(Float cpu){    Map<String, Quantity> cpuMap = new HashMap<>();    try {        Quantity cpuQuantity = new Quantity();        if (EmptyUtil.isNotNull(cpu)) {            cpuQuantity.setAmount(String.valueOf(cpu));            cpuMap.put(K8Constants.CPU, cpuQuantity);        }    } catch (NumberFormatException nfe){        _logger.error("Failed to convert cpu '{}'", cpu, nfe);    }    return cpuMap;}private static Map<String, Quantity> buildMemory(Integer memory){    Map<String, Quantity> cpuMap = new HashMap<>();    try {        Quantity cpu = new Quantity();        if (EmptyUtil.isNotNull(memory)) {            cpu.setAmount(String.valueOf(memory));            cpuMap.put(K8Constants.MEMORY, cpu);        }    } catch (NumberFormatException nfe){        _logger.error("Failed to convert memory '{}'", memory, nfe);    }    return cpuMap;}

Here I have some builders to build cpu and memory.This is just to understand the flow.You can give cpu/memory value in integer or string.