Does a Pod Fit on a Node
Problem Statement
Before scheduling, check whether a node has enough free CPU and memory for a pod's requests.
Background
A node has allocatable CPU and memory; some is already used. A pod fits if both its CPU request and memory request are <= the node's remaining CPU and memory respectively.
Your Task
def pod_fits(node, pod):
- node: dict with cpu, mem (allocatable) and used_cpu, used_mem.
- pod: dict with cpu, mem (requests).
- Return True if it fits, else False.
Input Format
- node (dict), pod (dict).
Output Format
- A boolean.
Sample
print(pod_fits({"cpu":4,"mem":8,"used_cpu":2,"used_mem":4}, {"cpu":1,"mem":2}))
Output:
True
Example
print(pod_fits({"cpu":4,"mem":8,"used_cpu":2,"used_mem":4}, {"cpu":1,"mem":2}))True
- Calculate the node's available CPU by subtracting the used amount from the allocatable total: .
- Determine the node's available memory by subtracting the used memory from the allocatable memory: .
- Verify the pod's CPU request against the free CPU capacity: is true.
- Verify the pod's memory request against the free memory capacity: is true.
- Since both the CPU and memory requests fit within the remaining resources, the final output is True.
Constraints
- Free cpu = cpu - used_cpu; free mem = mem - used_mem.
- Fits only if pod.cpu <= free cpu AND pod.mem <= free mem.
- Return a bool.