mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-13 05:02:50 +00:00
Add requirements based scheduling.
This commit is contained in:
@@ -139,6 +139,29 @@ func NewResourceFitPredicate(info NodeInfo) FitPredicate {
|
||||
return fit.PodFitsResources
|
||||
}
|
||||
|
||||
func NewSelectorMatchPredicate(info NodeInfo) FitPredicate {
|
||||
selector := &NodeSelector{
|
||||
info: info,
|
||||
}
|
||||
return selector.PodSelectorMatches
|
||||
}
|
||||
|
||||
type NodeSelector struct {
|
||||
info NodeInfo
|
||||
}
|
||||
|
||||
func (n *NodeSelector) PodSelectorMatches(pod api.Pod, existingPods []api.Pod, node string) (bool, error) {
|
||||
if len(pod.NodeSelector) == 0 {
|
||||
return true, nil
|
||||
}
|
||||
selector := labels.SelectorFromSet(pod.NodeSelector)
|
||||
minion, err := n.info.GetNodeInfo(node)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return selector.Matches(labels.Set(minion.Labels)), nil
|
||||
}
|
||||
|
||||
func PodFitsPorts(pod api.Pod, existingPods []api.Pod, node string) (bool, error) {
|
||||
for _, scheduledPod := range existingPods {
|
||||
for _, container := range pod.DesiredState.Manifest.Containers {
|
||||
|
@@ -234,3 +234,77 @@ func TestDiskConflicts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPodFitsSelector(t *testing.T) {
|
||||
tests := []struct {
|
||||
pod api.Pod
|
||||
labels map[string]string
|
||||
fits bool
|
||||
test string
|
||||
}{
|
||||
{
|
||||
pod: api.Pod{},
|
||||
fits: true,
|
||||
test: "no selector",
|
||||
},
|
||||
{
|
||||
pod: api.Pod{
|
||||
NodeSelector: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
fits: false,
|
||||
test: "missing labels",
|
||||
},
|
||||
{
|
||||
pod: api.Pod{
|
||||
NodeSelector: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
labels: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
fits: true,
|
||||
test: "same labels",
|
||||
},
|
||||
{
|
||||
pod: api.Pod{
|
||||
NodeSelector: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
labels: map[string]string{
|
||||
"foo": "bar",
|
||||
"baz": "blah",
|
||||
},
|
||||
fits: true,
|
||||
test: "node labels are superset",
|
||||
},
|
||||
{
|
||||
pod: api.Pod{
|
||||
NodeSelector: map[string]string{
|
||||
"foo": "bar",
|
||||
"baz": "blah",
|
||||
},
|
||||
},
|
||||
labels: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
fits: false,
|
||||
test: "node labels are subset",
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
node := api.Minion{Labels: test.labels}
|
||||
|
||||
fit := NodeSelector{FakeNodeInfo(node)}
|
||||
fits, err := fit.PodSelectorMatches(test.pod, []api.Pod{}, "machine")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if fits != test.fits {
|
||||
t.Errorf("%s: expected: %v got %v", test.test, test.fits, fits)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user