Normalize optional pod fields in scheduling preflight
This commit is contained in:
parent
adc141f609
commit
3f7ff10131
|
|
@ -51,9 +51,9 @@ def quantity(value):
|
|||
|
||||
|
||||
def container_requests(container):
|
||||
resources = container.get("resources", {})
|
||||
limits = resources.get("limits", {})
|
||||
requests = dict(resources.get("requests", {}))
|
||||
resources = container.get("resources") or {}
|
||||
limits = resources.get("limits") or {}
|
||||
requests = dict(resources.get("requests") or {})
|
||||
for name, value in limits.items():
|
||||
requests.setdefault(name, value)
|
||||
return Counter({name: quantity(value) for name, value in requests.items()})
|
||||
|
|
@ -63,9 +63,9 @@ def pod_requests(spec):
|
|||
app = Counter()
|
||||
sidecars = Counter()
|
||||
peak = Counter()
|
||||
for container in spec.get("containers", []):
|
||||
for container in spec.get("containers") or []:
|
||||
app.update(container_requests(container))
|
||||
for container in spec.get("initContainers", []):
|
||||
for container in spec.get("initContainers") or []:
|
||||
request = container_requests(container)
|
||||
if container.get("restartPolicy") == "Always":
|
||||
sidecars.update(request)
|
||||
|
|
@ -79,14 +79,14 @@ def pod_requests(spec):
|
|||
app[resource] = max(app[resource], peak[resource])
|
||||
for resource, value in container_requests(spec).items():
|
||||
app[resource] = max(app[resource], value)
|
||||
app.update({name: quantity(value) for name, value in spec.get("overhead", {}).items()})
|
||||
app.update({name: quantity(value) for name, value in (spec.get("overhead") or {}).items()})
|
||||
app["pods"] = 1
|
||||
return app
|
||||
|
||||
|
||||
def expression_matches(expression, labels):
|
||||
key, operator = expression["key"], expression["operator"]
|
||||
values = expression.get("values", [])
|
||||
values = expression.get("values") or []
|
||||
present = key in labels
|
||||
value = labels.get(key)
|
||||
if operator == "In":
|
||||
|
|
@ -106,6 +106,7 @@ def expression_matches(expression, labels):
|
|||
|
||||
|
||||
def selector_matches(selector, labels):
|
||||
selector = selector or {}
|
||||
if selector is None:
|
||||
return False
|
||||
return all(labels.get(k) == v for k, v in selector.get("matchLabels", {}).items()) and all(
|
||||
|
|
@ -205,7 +206,7 @@ class Estimate:
|
|||
labels = node["metadata"].get("labels", {})
|
||||
affinity = pod["spec"].get("affinity", {})
|
||||
for field in ("podAffinity", "podAntiAffinity"):
|
||||
for term in affinity.get(field, {}).get("requiredDuringSchedulingIgnoredDuringExecution", []):
|
||||
for term in (affinity.get(field) or {}).get("requiredDuringSchedulingIgnoredDuringExecution") or []:
|
||||
key = term["topologyKey"]
|
||||
peers = [p for p in self.pods if p["spec"].get("nodeName") in self.nodes and self.peer_matches(term, pod, p)]
|
||||
same_domain = any(self.nodes[p["spec"]["nodeName"]]["metadata"].get("labels", {}).get(key) == labels.get(key) for p in peers)
|
||||
|
|
@ -220,11 +221,11 @@ class Estimate:
|
|||
peer_node = self.nodes.get(peer["spec"].get("nodeName"))
|
||||
if not peer_node:
|
||||
continue
|
||||
for term in peer["spec"].get("affinity", {}).get("podAntiAffinity", {}).get("requiredDuringSchedulingIgnoredDuringExecution", []):
|
||||
for term in (((peer["spec"].get("affinity") or {}).get("podAntiAffinity") or {}).get("requiredDuringSchedulingIgnoredDuringExecution") or []):
|
||||
key = term["topologyKey"]
|
||||
if key in labels and peer_node["metadata"].get("labels", {}).get(key) == labels[key] and self.peer_matches(term, peer, pod):
|
||||
reasons.append(f"existing pod required anti-affinity on {key}")
|
||||
for constraint in pod["spec"].get("topologySpreadConstraints", []):
|
||||
for constraint in pod["spec"].get("topologySpreadConstraints") or []:
|
||||
if constraint.get("whenUnsatisfiable") != "DoNotSchedule":
|
||||
continue
|
||||
key = constraint["topologyKey"]
|
||||
|
|
@ -295,7 +296,7 @@ class Estimate:
|
|||
if taint["effect"] in ("NoSchedule", "NoExecute") and not tolerates(taint, spec.get("tolerations", [])):
|
||||
reasons.append(f"untolerated taint {taint['key']}")
|
||||
namespace = pod["metadata"].get("namespace", "default")
|
||||
for volume in spec.get("volumes", []):
|
||||
for volume in spec.get("volumes") or []:
|
||||
claim = volume.get("persistentVolumeClaim", {}).get("claimName")
|
||||
if not claim:
|
||||
continue
|
||||
|
|
@ -339,8 +340,8 @@ class Estimate:
|
|||
def host_ports(spec):
|
||||
return [
|
||||
(p.get("hostIP", "0.0.0.0"), p.get("protocol", "TCP"), p.get("hostPort") or p.get("containerPort"))
|
||||
for c in spec.get("containers", []) + spec.get("initContainers", [])
|
||||
for p in c.get("ports", [])
|
||||
for c in (spec.get("containers") or []) + (spec.get("initContainers") or [])
|
||||
for p in c.get("ports") or []
|
||||
if p.get("hostPort") or spec.get("hostNetwork")
|
||||
]
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue