mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-12 20:57:20 +00:00
Refactor PodFitsPorts
This commit is contained in:
@@ -163,30 +163,29 @@ func (n *NodeSelector) PodSelectorMatches(pod api.Pod, existingPods []api.Pod, n
|
||||
}
|
||||
|
||||
func PodFitsPorts(pod api.Pod, existingPods []api.Pod, node string) (bool, error) {
|
||||
for _, scheduledPod := range existingPods {
|
||||
for _, container := range pod.DesiredState.Manifest.Containers {
|
||||
for _, port := range container.Ports {
|
||||
if port.HostPort == 0 {
|
||||
continue
|
||||
}
|
||||
if containsPort(scheduledPod, port) {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
existingPorts := getUsedPorts(existingPods...)
|
||||
wantPorts := getUsedPorts(pod)
|
||||
for wport := range wantPorts {
|
||||
if wport == 0 {
|
||||
continue
|
||||
}
|
||||
if existingPorts[wport] {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func containsPort(pod api.Pod, port api.Port) bool {
|
||||
for _, container := range pod.DesiredState.Manifest.Containers {
|
||||
for _, podPort := range container.Ports {
|
||||
if podPort.HostPort == port.HostPort {
|
||||
return true
|
||||
func getUsedPorts(pods ...api.Pod) map[int]bool {
|
||||
ports := make(map[int]bool)
|
||||
for _, pod := range pods {
|
||||
for _, container := range pod.DesiredState.Manifest.Containers {
|
||||
for _, podPort := range container.Ports {
|
||||
ports[podPort.HostPort] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
return ports
|
||||
}
|
||||
|
||||
// MapPodsToMachines obtains a list of pods and pivots that list into a map where the keys are host names
|
||||
|
Reference in New Issue
Block a user