mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Merge pull request #2166 from claire921/refactor_sched
Refactor PodFitsPorts
This commit is contained in:
commit
3260d300e5
@ -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) {
|
func PodFitsPorts(pod api.Pod, existingPods []api.Pod, node string) (bool, error) {
|
||||||
for _, scheduledPod := range existingPods {
|
existingPorts := getUsedPorts(existingPods...)
|
||||||
for _, container := range pod.DesiredState.Manifest.Containers {
|
wantPorts := getUsedPorts(pod)
|
||||||
for _, port := range container.Ports {
|
for wport := range wantPorts {
|
||||||
if port.HostPort == 0 {
|
if wport == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if containsPort(scheduledPod, port) {
|
if existingPorts[wport] {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func containsPort(pod api.Pod, port api.Port) bool {
|
func getUsedPorts(pods ...api.Pod) map[int]bool {
|
||||||
for _, container := range pod.DesiredState.Manifest.Containers {
|
ports := make(map[int]bool)
|
||||||
for _, podPort := range container.Ports {
|
for _, pod := range pods {
|
||||||
if podPort.HostPort == port.HostPort {
|
for _, container := range pod.DesiredState.Manifest.Containers {
|
||||||
return true
|
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
|
// MapPodsToMachines obtains a list of pods and pivots that list into a map where the keys are host names
|
||||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package scheduler
|
package scheduler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
@ -182,6 +183,42 @@ func TestPodFitsPorts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetUsedPorts(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
pods []api.Pod
|
||||||
|
|
||||||
|
ports map[int]bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
[]api.Pod{
|
||||||
|
newPod("m1", 9090),
|
||||||
|
},
|
||||||
|
map[int]bool{9090: true},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]api.Pod{
|
||||||
|
newPod("m1", 9090),
|
||||||
|
newPod("m1", 9091),
|
||||||
|
},
|
||||||
|
map[int]bool{9090: true, 9091: true},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]api.Pod{
|
||||||
|
newPod("m1", 9090),
|
||||||
|
newPod("m2", 9091),
|
||||||
|
},
|
||||||
|
map[int]bool{9090: true, 9091: true},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
ports := getUsedPorts(test.pods...)
|
||||||
|
if !reflect.DeepEqual(test.ports, ports) {
|
||||||
|
t.Errorf("expect %v, got %v", test.ports, ports)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDiskConflicts(t *testing.T) {
|
func TestDiskConflicts(t *testing.T) {
|
||||||
volState := api.PodState{
|
volState := api.PodState{
|
||||||
Manifest: api.ContainerManifest{
|
Manifest: api.ContainerManifest{
|
||||||
|
Loading…
Reference in New Issue
Block a user