Filter Nodes by Taints and Tolerations
Problem Statement
A pod can only schedule onto nodes whose NoSchedule taints it tolerates. Return the names of schedulable nodes.
Background
Each node has a list of taints (key, value, effect). Only effect == "NoSchedule" taints block scheduling. A pod carries tolerations (key, value). A node is schedulable if for every one of its NoSchedule taints, the pod has a matching toleration (same key and value). Taints with other effects are ignored here.
Your Task
def schedulable_nodes(nodes, tolerations):
- nodes: list of {"name": str, "taints": [(key, value, effect), ...]}.
- tolerations: list of (key, value) tuples.
- Return the sorted list of schedulable node names.
Input Format
- nodes (list of dicts), tolerations (list of tuples).
Output Format
- A sorted list of strings.
Sample
nodes = [{"name":"n1","taints":[("gpu","true","NoSchedule")]}, {"name":"n2","taints":[]}]
print(schedulable_nodes(nodes, [("gpu","true")]))
Output:
['n1', 'n2']
Example
nodes = [{"name":"n1","taints":[("gpu","true","NoSchedule")]}, {"name":"n2","taints":[]}]
print(schedulable_nodes(nodes, [("gpu","true")]))['n1', 'n2']
- Convert the input tolerations into a set for efficient lookup: .
- Evaluate node "n1": Identify its
NoScheduletaints as . Since the pair exists in the toleration set, the node is schedulable. - Evaluate node "n2": It has an empty taint list, so there are no blocking taints. The condition is vacuously true, making the node schedulable.
- Collect the names of all schedulable nodes: .
- Sort the collected names alphabetically, which remains in this case.
- The final output is ['n1', 'n2']
Constraints
- Only
NoScheduletaints block scheduling. - A node is OK if every NoSchedule taint's (key,value) is in tolerations.
- Return node names sorted ascending.