2014-08-20 21:34:55 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-08-20 21:34:55 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package factory
|
|
|
|
|
|
|
|
import (
|
2018-02-08 08:40:56 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-08-29 03:18:27 +00:00
|
|
|
"net/http"
|
2014-08-20 21:34:55 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-08-20 22:03:32 +00:00
|
|
|
"time"
|
2014-08-20 21:34:55 +00:00
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-01-17 03:38:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2018-02-05 18:00:17 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-06-23 20:56:37 +00:00
|
|
|
"k8s.io/client-go/informers"
|
|
|
|
clientset "k8s.io/client-go/kubernetes"
|
2017-01-19 18:27:59 +00:00
|
|
|
restclient "k8s.io/client-go/rest"
|
2017-01-24 14:11:51 +00:00
|
|
|
"k8s.io/client-go/tools/cache"
|
2017-01-23 18:37:22 +00:00
|
|
|
utiltesting "k8s.io/client-go/util/testing"
|
2017-10-16 11:41:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
2015-09-14 21:56:51 +00:00
|
|
|
apitesting "k8s.io/kubernetes/pkg/api/testing"
|
2018-02-05 18:00:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/scheduler"
|
2018-01-04 02:12:18 +00:00
|
|
|
"k8s.io/kubernetes/pkg/scheduler/algorithm"
|
|
|
|
schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
|
|
|
|
latestschedulerapi "k8s.io/kubernetes/pkg/scheduler/api/latest"
|
|
|
|
"k8s.io/kubernetes/pkg/scheduler/core"
|
|
|
|
"k8s.io/kubernetes/pkg/scheduler/schedulercache"
|
|
|
|
schedulertesting "k8s.io/kubernetes/pkg/scheduler/testing"
|
|
|
|
"k8s.io/kubernetes/pkg/scheduler/util"
|
2014-08-20 21:34:55 +00:00
|
|
|
)
|
|
|
|
|
2017-02-15 09:00:50 +00:00
|
|
|
const enableEquivalenceCache = true
|
|
|
|
|
2014-08-20 21:34:55 +00:00
|
|
|
func TestCreate(t *testing.T) {
|
2016-01-15 06:33:50 +00:00
|
|
|
handler := utiltesting.FakeHandler{
|
2014-08-20 21:34:55 +00:00
|
|
|
StatusCode: 500,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
2016-04-21 11:50:55 +00:00
|
|
|
defer server.Close()
|
2017-10-16 11:41:50 +00:00
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
|
2018-02-05 18:00:17 +00:00
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
2014-12-10 04:25:45 +00:00
|
|
|
factory.Create()
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 17:59:29 +00:00
|
|
|
// Test configures a scheduler from a policies defined in a file
|
|
|
|
// It combines some configurable predicate/priorities with some pre-defined ones
|
2015-02-27 20:53:04 +00:00
|
|
|
func TestCreateFromConfig(t *testing.T) {
|
|
|
|
var configData []byte
|
|
|
|
var policy schedulerapi.Policy
|
|
|
|
|
2016-01-15 06:33:50 +00:00
|
|
|
handler := utiltesting.FakeHandler{
|
2015-02-27 20:53:04 +00:00
|
|
|
StatusCode: 500,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
2016-04-21 11:50:55 +00:00
|
|
|
defer server.Close()
|
2017-10-16 11:41:50 +00:00
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
|
2018-02-05 18:00:17 +00:00
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
2015-02-27 20:53:04 +00:00
|
|
|
|
2015-03-02 17:59:29 +00:00
|
|
|
// Pre-register some predicate and priority functions
|
|
|
|
RegisterFitPredicate("PredicateOne", PredicateOne)
|
|
|
|
RegisterFitPredicate("PredicateTwo", PredicateTwo)
|
|
|
|
RegisterPriorityFunction("PriorityOne", PriorityOne, 1)
|
|
|
|
RegisterPriorityFunction("PriorityTwo", PriorityTwo, 1)
|
2015-02-27 20:53:04 +00:00
|
|
|
|
|
|
|
configData = []byte(`{
|
|
|
|
"kind" : "Policy",
|
|
|
|
"apiVersion" : "v1",
|
|
|
|
"predicates" : [
|
|
|
|
{"name" : "TestZoneAffinity", "argument" : {"serviceAffinity" : {"labels" : ["zone"]}}},
|
|
|
|
{"name" : "TestRequireZone", "argument" : {"labelsPresence" : {"labels" : ["zone"], "presence" : true}}},
|
2015-03-02 17:59:29 +00:00
|
|
|
{"name" : "PredicateOne"},
|
|
|
|
{"name" : "PredicateTwo"}
|
2015-02-27 20:53:04 +00:00
|
|
|
],
|
|
|
|
"priorities" : [
|
2015-03-02 17:59:29 +00:00
|
|
|
{"name" : "RackSpread", "weight" : 3, "argument" : {"serviceAntiAffinity" : {"label" : "rack"}}},
|
|
|
|
{"name" : "PriorityOne", "weight" : 2},
|
|
|
|
{"name" : "PriorityTwo", "weight" : 1} ]
|
2015-02-27 20:53:04 +00:00
|
|
|
}`)
|
2016-01-22 05:06:52 +00:00
|
|
|
if err := runtime.DecodeInto(latestschedulerapi.Codec, configData, &policy); err != nil {
|
2015-02-27 20:53:04 +00:00
|
|
|
t.Errorf("Invalid configuration: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
factory.CreateFromConfig(policy)
|
2017-04-06 13:24:32 +00:00
|
|
|
hpa := factory.GetHardPodAffinitySymmetricWeight()
|
|
|
|
if hpa != v1.DefaultHardPodAffinitySymmetricWeight {
|
|
|
|
t.Errorf("Wrong hardPodAffinitySymmetricWeight, ecpected: %d, got: %d", v1.DefaultHardPodAffinitySymmetricWeight, hpa)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateFromConfigWithHardPodAffinitySymmetricWeight(t *testing.T) {
|
|
|
|
var configData []byte
|
|
|
|
var policy schedulerapi.Policy
|
|
|
|
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
StatusCode: 500,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
defer server.Close()
|
2017-10-16 11:41:50 +00:00
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
|
2018-02-05 18:00:17 +00:00
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
2017-04-06 13:24:32 +00:00
|
|
|
|
|
|
|
// Pre-register some predicate and priority functions
|
|
|
|
RegisterFitPredicate("PredicateOne", PredicateOne)
|
|
|
|
RegisterFitPredicate("PredicateTwo", PredicateTwo)
|
|
|
|
RegisterPriorityFunction("PriorityOne", PriorityOne, 1)
|
|
|
|
RegisterPriorityFunction("PriorityTwo", PriorityTwo, 1)
|
|
|
|
|
|
|
|
configData = []byte(`{
|
|
|
|
"kind" : "Policy",
|
|
|
|
"apiVersion" : "v1",
|
|
|
|
"predicates" : [
|
|
|
|
{"name" : "TestZoneAffinity", "argument" : {"serviceAffinity" : {"labels" : ["zone"]}}},
|
|
|
|
{"name" : "TestRequireZone", "argument" : {"labelsPresence" : {"labels" : ["zone"], "presence" : true}}},
|
|
|
|
{"name" : "PredicateOne"},
|
|
|
|
{"name" : "PredicateTwo"}
|
|
|
|
],
|
|
|
|
"priorities" : [
|
|
|
|
{"name" : "RackSpread", "weight" : 3, "argument" : {"serviceAntiAffinity" : {"label" : "rack"}}},
|
|
|
|
{"name" : "PriorityOne", "weight" : 2},
|
|
|
|
{"name" : "PriorityTwo", "weight" : 1}
|
|
|
|
],
|
|
|
|
"hardPodAffinitySymmetricWeight" : 10
|
|
|
|
}`)
|
|
|
|
if err := runtime.DecodeInto(latestschedulerapi.Codec, configData, &policy); err != nil {
|
|
|
|
t.Errorf("Invalid configuration: %v", err)
|
|
|
|
}
|
|
|
|
factory.CreateFromConfig(policy)
|
|
|
|
hpa := factory.GetHardPodAffinitySymmetricWeight()
|
|
|
|
if hpa != 10 {
|
|
|
|
t.Errorf("Wrong hardPodAffinitySymmetricWeight, ecpected: %d, got: %d", 10, hpa)
|
|
|
|
}
|
2015-02-27 20:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateFromEmptyConfig(t *testing.T) {
|
|
|
|
var configData []byte
|
|
|
|
var policy schedulerapi.Policy
|
|
|
|
|
2016-01-15 06:33:50 +00:00
|
|
|
handler := utiltesting.FakeHandler{
|
2015-02-27 20:53:04 +00:00
|
|
|
StatusCode: 500,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
2016-04-21 11:50:55 +00:00
|
|
|
defer server.Close()
|
2017-10-16 11:41:50 +00:00
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
|
2018-02-05 18:00:17 +00:00
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
2015-02-27 20:53:04 +00:00
|
|
|
|
|
|
|
configData = []byte(`{}`)
|
2016-01-22 05:06:52 +00:00
|
|
|
if err := runtime.DecodeInto(latestschedulerapi.Codec, configData, &policy); err != nil {
|
2015-02-27 20:53:04 +00:00
|
|
|
t.Errorf("Invalid configuration: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
factory.CreateFromConfig(policy)
|
|
|
|
}
|
|
|
|
|
2018-02-05 18:00:17 +00:00
|
|
|
// Test configures a scheduler from a policy that does not specify any
|
|
|
|
// predicate/priority.
|
|
|
|
// The predicate/priority from DefaultProvider will be used.
|
|
|
|
func TestCreateFromConfigWithUnspecifiedPredicatesOrPriorities(t *testing.T) {
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
StatusCode: 500,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
defer server.Close()
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
|
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
|
|
|
|
|
|
|
RegisterFitPredicate("PredicateOne", PredicateOne)
|
|
|
|
RegisterPriorityFunction("PriorityOne", PriorityOne, 1)
|
|
|
|
|
|
|
|
RegisterAlgorithmProvider(DefaultProvider, sets.NewString("PredicateOne"), sets.NewString("PriorityOne"))
|
|
|
|
|
|
|
|
configData := []byte(`{
|
|
|
|
"kind" : "Policy",
|
|
|
|
"apiVersion" : "v1"
|
|
|
|
}`)
|
|
|
|
var policy schedulerapi.Policy
|
|
|
|
if err := runtime.DecodeInto(latestschedulerapi.Codec, configData, &policy); err != nil {
|
|
|
|
t.Fatalf("Invalid configuration: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := factory.CreateFromConfig(policy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create scheduler from configuration: %v", err)
|
|
|
|
}
|
|
|
|
if _, found := config.Algorithm.Predicates()["PredicateOne"]; !found {
|
|
|
|
t.Errorf("Expected predicate PredicateOne from %q", DefaultProvider)
|
|
|
|
}
|
|
|
|
if len(config.Algorithm.Prioritizers()) != 1 || config.Algorithm.Prioritizers()[0].Name != "PriorityOne" {
|
|
|
|
t.Errorf("Expected priority PriorityOne from %q", DefaultProvider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test configures a scheduler from a policy that contains empty
|
|
|
|
// predicate/priority.
|
|
|
|
// Empty predicate/priority sets will be used.
|
|
|
|
func TestCreateFromConfigWithEmptyPredicatesOrPriorities(t *testing.T) {
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
StatusCode: 500,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
defer server.Close()
|
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
|
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
|
|
|
|
|
|
|
RegisterFitPredicate("PredicateOne", PredicateOne)
|
|
|
|
RegisterPriorityFunction("PriorityOne", PriorityOne, 1)
|
|
|
|
|
|
|
|
RegisterAlgorithmProvider(DefaultProvider, sets.NewString("PredicateOne"), sets.NewString("PriorityOne"))
|
|
|
|
|
|
|
|
configData := []byte(`{
|
|
|
|
"kind" : "Policy",
|
|
|
|
"apiVersion" : "v1",
|
|
|
|
"predicates" : [],
|
|
|
|
"priorities" : []
|
|
|
|
}`)
|
|
|
|
var policy schedulerapi.Policy
|
|
|
|
if err := runtime.DecodeInto(latestschedulerapi.Codec, configData, &policy); err != nil {
|
|
|
|
t.Fatalf("Invalid configuration: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := factory.CreateFromConfig(policy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create scheduler from configuration: %v", err)
|
|
|
|
}
|
|
|
|
if len(config.Algorithm.Predicates()) != 0 {
|
|
|
|
t.Error("Expected empty predicate sets")
|
|
|
|
}
|
|
|
|
if len(config.Algorithm.Prioritizers()) != 0 {
|
|
|
|
t.Error("Expected empty priority sets")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 00:08:41 +00:00
|
|
|
func PredicateOne(pod *v1.Pod, meta algorithm.PredicateMetadata, nodeInfo *schedulercache.NodeInfo) (bool, []algorithm.PredicateFailureReason, error) {
|
2016-08-09 12:01:46 +00:00
|
|
|
return true, nil, nil
|
2015-03-02 17:59:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 00:08:41 +00:00
|
|
|
func PredicateTwo(pod *v1.Pod, meta algorithm.PredicateMetadata, nodeInfo *schedulercache.NodeInfo) (bool, []algorithm.PredicateFailureReason, error) {
|
2016-08-09 12:01:46 +00:00
|
|
|
return true, nil, nil
|
2015-03-02 17:59:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 20:52:35 +00:00
|
|
|
func PriorityOne(pod *v1.Pod, nodeNameToInfo map[string]*schedulercache.NodeInfo, nodes []*v1.Node) (schedulerapi.HostPriorityList, error) {
|
2015-09-04 06:50:14 +00:00
|
|
|
return []schedulerapi.HostPriority{}, nil
|
2015-03-02 17:59:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 20:52:35 +00:00
|
|
|
func PriorityTwo(pod *v1.Pod, nodeNameToInfo map[string]*schedulercache.NodeInfo, nodes []*v1.Node) (schedulerapi.HostPriorityList, error) {
|
2015-09-04 06:50:14 +00:00
|
|
|
return []schedulerapi.HostPriority{}, nil
|
2015-03-02 17:59:29 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 22:03:32 +00:00
|
|
|
func TestDefaultErrorFunc(t *testing.T) {
|
2016-11-18 20:52:35 +00:00
|
|
|
testPod := &v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "bar"},
|
2016-11-18 20:52:35 +00:00
|
|
|
Spec: apitesting.V1DeepEqualSafePodSpec(),
|
2015-01-26 17:52:50 +00:00
|
|
|
}
|
2016-01-15 06:33:50 +00:00
|
|
|
handler := utiltesting.FakeHandler{
|
2014-08-20 22:03:32 +00:00
|
|
|
StatusCode: 200,
|
2017-07-03 01:11:13 +00:00
|
|
|
ResponseBody: runtime.EncodeOrDie(util.Test.Codec(), testPod),
|
2014-08-20 22:03:32 +00:00
|
|
|
T: t,
|
|
|
|
}
|
2014-08-29 03:18:27 +00:00
|
|
|
mux := http.NewServeMux()
|
2014-12-19 21:32:42 +00:00
|
|
|
|
2018-02-09 06:53:53 +00:00
|
|
|
// FakeHandler mustn't be sent requests other than the one you want to test.
|
2017-06-30 09:27:36 +00:00
|
|
|
mux.Handle(util.Test.ResourcePath(string(v1.ResourcePods), "bar", "foo"), &handler)
|
2014-08-29 03:18:27 +00:00
|
|
|
server := httptest.NewServer(mux)
|
2016-04-21 11:50:55 +00:00
|
|
|
defer server.Close()
|
2017-10-16 11:41:50 +00:00
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
|
2018-02-05 18:00:17 +00:00
|
|
|
factory := newConfigFactory(client, v1.DefaultHardPodAffinitySymmetricWeight)
|
2017-10-24 02:26:59 +00:00
|
|
|
queue := &core.FIFO{FIFO: cache.NewFIFO(cache.MetaNamespaceKeyFunc)}
|
2017-01-13 23:51:38 +00:00
|
|
|
podBackoff := util.CreatePodBackoff(1*time.Millisecond, 1*time.Second)
|
|
|
|
errFunc := factory.MakeDefaultErrorFunc(podBackoff, queue)
|
2014-08-20 22:03:32 +00:00
|
|
|
|
|
|
|
errFunc(testPod, nil)
|
|
|
|
for {
|
|
|
|
// This is a terrible way to do this but I plan on replacing this
|
|
|
|
// whole error handling system in the future. The test will time
|
|
|
|
// out if something doesn't work.
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2015-01-26 21:44:53 +00:00
|
|
|
got, exists, _ := queue.Get(testPod)
|
2014-08-20 22:03:32 +00:00
|
|
|
if !exists {
|
|
|
|
continue
|
|
|
|
}
|
2017-06-30 09:27:36 +00:00
|
|
|
handler.ValidateRequest(t, util.Test.ResourcePath(string(v1.ResourcePods), "bar", "foo"), "GET", nil)
|
2014-08-20 22:03:32 +00:00
|
|
|
if e, a := testPod, got; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 08:40:22 +00:00
|
|
|
func TestNodeEnumerator(t *testing.T) {
|
2016-11-18 20:52:35 +00:00
|
|
|
testList := &v1.NodeList{
|
|
|
|
Items: []v1.Node{
|
2017-01-17 03:38:19 +00:00
|
|
|
{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
|
|
|
|
{ObjectMeta: metav1.ObjectMeta{Name: "bar"}},
|
|
|
|
{ObjectMeta: metav1.ObjectMeta{Name: "baz"}},
|
2014-08-20 21:34:55 +00:00
|
|
|
},
|
|
|
|
}
|
2014-12-08 03:44:27 +00:00
|
|
|
me := nodeEnumerator{testList}
|
2014-08-20 21:34:55 +00:00
|
|
|
|
|
|
|
if e, a := 3, me.Len(); e != a {
|
|
|
|
t.Fatalf("expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
for i := range testList.Items {
|
2015-01-26 21:44:53 +00:00
|
|
|
gotObj := me.Get(i)
|
2016-11-18 20:52:35 +00:00
|
|
|
if e, a := testList.Items[i].Name, gotObj.(*v1.Node).Name; e != a {
|
2014-08-20 21:34:55 +00:00
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
if e, a := &testList.Items[i], gotObj; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected %#v, got %v#", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBind(t *testing.T) {
|
|
|
|
table := []struct {
|
2016-11-18 20:52:35 +00:00
|
|
|
binding *v1.Binding
|
2014-08-20 21:34:55 +00:00
|
|
|
}{
|
2016-11-18 20:52:35 +00:00
|
|
|
{binding: &v1.Binding{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2015-03-04 20:55:41 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
2016-11-18 20:52:35 +00:00
|
|
|
Target: v1.ObjectReference{
|
2015-03-04 20:55:41 +00:00
|
|
|
Name: "foohost.kubernetes.mydomain.com",
|
2015-02-16 04:43:45 +00:00
|
|
|
},
|
|
|
|
}},
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range table {
|
2016-01-15 06:33:50 +00:00
|
|
|
handler := utiltesting.FakeHandler{
|
2014-08-20 21:34:55 +00:00
|
|
|
StatusCode: 200,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
2016-04-21 11:50:55 +00:00
|
|
|
defer server.Close()
|
2017-10-16 11:41:50 +00:00
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
|
2014-08-28 13:56:38 +00:00
|
|
|
b := binder{client}
|
2014-08-20 21:34:55 +00:00
|
|
|
|
2014-08-28 13:56:38 +00:00
|
|
|
if err := b.Bind(item.binding); err != nil {
|
2014-08-20 21:34:55 +00:00
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2017-07-03 01:11:13 +00:00
|
|
|
expectedBody := runtime.EncodeOrDie(util.Test.Codec(), item.binding)
|
2017-04-14 03:15:15 +00:00
|
|
|
handler.ValidateRequest(t,
|
2017-06-30 09:27:36 +00:00
|
|
|
util.Test.SubResourcePath(string(v1.ResourcePods), metav1.NamespaceDefault, "foo", "binding"),
|
2017-04-14 03:15:15 +00:00
|
|
|
"POST", &expectedBody)
|
2014-08-20 21:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-03 16:59:39 +00:00
|
|
|
|
2016-05-04 06:50:31 +00:00
|
|
|
func TestInvalidHardPodAffinitySymmetricWeight(t *testing.T) {
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
StatusCode: 500,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
2017-11-16 07:03:08 +00:00
|
|
|
defer server.Close()
|
2017-10-16 11:41:50 +00:00
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
|
2016-05-04 06:50:31 +00:00
|
|
|
// factory of "default-scheduler"
|
2018-02-05 18:00:17 +00:00
|
|
|
factory := newConfigFactory(client, -1)
|
2016-05-04 06:50:31 +00:00
|
|
|
_, err := factory.Create()
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected err: invalid hardPodAffinitySymmetricWeight, got nothing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidFactoryArgs(t *testing.T) {
|
|
|
|
handler := utiltesting.FakeHandler{
|
|
|
|
StatusCode: 500,
|
|
|
|
ResponseBody: "",
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(&handler)
|
|
|
|
defer server.Close()
|
2017-10-16 11:41:50 +00:00
|
|
|
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
|
2016-05-04 06:50:31 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
2017-10-12 14:04:43 +00:00
|
|
|
hardPodAffinitySymmetricWeight int32
|
2016-05-04 06:50:31 +00:00
|
|
|
expectErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
hardPodAffinitySymmetricWeight: -1,
|
|
|
|
expectErr: "invalid hardPodAffinitySymmetricWeight: -1, must be in the range 0-100",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hardPodAffinitySymmetricWeight: 101,
|
|
|
|
expectErr: "invalid hardPodAffinitySymmetricWeight: 101, must be in the range 0-100",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
2018-02-05 18:00:17 +00:00
|
|
|
factory := newConfigFactory(client, test.hardPodAffinitySymmetricWeight)
|
2016-05-04 06:50:31 +00:00
|
|
|
_, err := factory.Create()
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected err: %s, got nothing", test.expectErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-10-16 20:30:25 +00:00
|
|
|
|
|
|
|
func TestSkipPodUpdate(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
pod *v1.Pod
|
|
|
|
isAssumedPodFunc func(*v1.Pod) bool
|
|
|
|
getPodFunc func(*v1.Pod) *v1.Pod
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
// Non-assumed pod should not be skipped.
|
|
|
|
{
|
|
|
|
pod: &v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "pod-0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isAssumedPodFunc: func(*v1.Pod) bool { return false },
|
|
|
|
getPodFunc: func(*v1.Pod) *v1.Pod {
|
|
|
|
return &v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "pod-0",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
// Pod update (with changes on ResourceVersion, Spec.NodeName and/or
|
|
|
|
// Annotations) for an already assumed pod should be skipped.
|
|
|
|
{
|
|
|
|
pod: &v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "pod-0",
|
|
|
|
Annotations: map[string]string{"a": "b"},
|
|
|
|
ResourceVersion: "0",
|
|
|
|
},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
NodeName: "node-0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isAssumedPodFunc: func(*v1.Pod) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
getPodFunc: func(*v1.Pod) *v1.Pod {
|
|
|
|
return &v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "pod-0",
|
|
|
|
Annotations: map[string]string{"c": "d"},
|
|
|
|
ResourceVersion: "1",
|
|
|
|
},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
NodeName: "node-1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
// Pod update (with changes on Labels) for an already assumed pod
|
|
|
|
// should not be skipped.
|
|
|
|
{
|
|
|
|
pod: &v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "pod-0",
|
|
|
|
Labels: map[string]string{"a": "b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isAssumedPodFunc: func(*v1.Pod) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
getPodFunc: func(*v1.Pod) *v1.Pod {
|
|
|
|
return &v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "pod-0",
|
|
|
|
Labels: map[string]string{"c": "d"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
c := &configFactory{
|
|
|
|
schedulerCache: &schedulertesting.FakeCache{
|
|
|
|
IsAssumedPodFunc: test.isAssumedPodFunc,
|
|
|
|
GetPodFunc: test.getPodFunc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
got := c.skipPodUpdate(test.pod)
|
|
|
|
if got != test.expected {
|
|
|
|
t.Errorf("skipPodUpdate() = %t, expected = %t", got, test.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 18:00:17 +00:00
|
|
|
|
|
|
|
func newConfigFactory(client *clientset.Clientset, hardPodAffinitySymmetricWeight int32) scheduler.Configurator {
|
|
|
|
informerFactory := informers.NewSharedInformerFactory(client, 0)
|
|
|
|
return NewConfigFactory(
|
|
|
|
v1.DefaultSchedulerName,
|
|
|
|
client,
|
|
|
|
informerFactory.Core().V1().Nodes(),
|
|
|
|
informerFactory.Core().V1().Pods(),
|
|
|
|
informerFactory.Core().V1().PersistentVolumes(),
|
|
|
|
informerFactory.Core().V1().PersistentVolumeClaims(),
|
|
|
|
informerFactory.Core().V1().ReplicationControllers(),
|
|
|
|
informerFactory.Extensions().V1beta1().ReplicaSets(),
|
|
|
|
informerFactory.Apps().V1beta1().StatefulSets(),
|
|
|
|
informerFactory.Core().V1().Services(),
|
|
|
|
informerFactory.Policy().V1beta1().PodDisruptionBudgets(),
|
|
|
|
informerFactory.Storage().V1().StorageClasses(),
|
|
|
|
hardPodAffinitySymmetricWeight,
|
|
|
|
enableEquivalenceCache,
|
|
|
|
)
|
|
|
|
}
|
2018-02-08 08:40:56 +00:00
|
|
|
|
|
|
|
type fakeExtender struct {
|
|
|
|
isBinder bool
|
|
|
|
interestedPodName string
|
2018-03-19 19:15:24 +00:00
|
|
|
ignorable bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeExtender) IsIgnorable() bool {
|
|
|
|
return f.ignorable
|
2018-02-08 08:40:56 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 08:34:55 +00:00
|
|
|
func (f *fakeExtender) ProcessPreemption(
|
|
|
|
pod *v1.Pod,
|
|
|
|
nodeToVictims map[*v1.Node]*schedulerapi.Victims,
|
|
|
|
nodeNameToInfo map[string]*schedulercache.NodeInfo,
|
|
|
|
) (map[*v1.Node]*schedulerapi.Victims, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeExtender) SupportsPreemption() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeExtender) Filter(
|
|
|
|
pod *v1.Pod,
|
|
|
|
nodes []*v1.Node,
|
|
|
|
nodeNameToInfo map[string]*schedulercache.NodeInfo,
|
|
|
|
) (filteredNodes []*v1.Node, failedNodesMap schedulerapi.FailedNodesMap, err error) {
|
2018-02-08 08:40:56 +00:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2018-03-02 08:34:55 +00:00
|
|
|
func (f *fakeExtender) Prioritize(
|
|
|
|
pod *v1.Pod,
|
|
|
|
nodes []*v1.Node,
|
|
|
|
) (hostPriorities *schedulerapi.HostPriorityList, weight int, err error) {
|
2018-02-08 08:40:56 +00:00
|
|
|
return nil, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeExtender) Bind(binding *v1.Binding) error {
|
|
|
|
if f.isBinder {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("not a binder")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeExtender) IsBinder() bool {
|
|
|
|
return f.isBinder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeExtender) IsInterested(pod *v1.Pod) bool {
|
|
|
|
return pod != nil && pod.Name == f.interestedPodName
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBinderFunc(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
podName string
|
|
|
|
extenders []algorithm.SchedulerExtender
|
|
|
|
|
|
|
|
expectedBinderType string
|
|
|
|
}{
|
|
|
|
// Expect to return the default binder because the extender is not a
|
|
|
|
// binder, even though it's interested in the pod.
|
|
|
|
{
|
|
|
|
podName: "pod0",
|
|
|
|
extenders: []algorithm.SchedulerExtender{
|
|
|
|
&fakeExtender{isBinder: false, interestedPodName: "pod0"},
|
|
|
|
},
|
|
|
|
expectedBinderType: "*factory.binder",
|
|
|
|
},
|
|
|
|
// Expect to return the fake binder because one of the extenders is a
|
|
|
|
// binder and it's interested in the pod.
|
|
|
|
{
|
|
|
|
podName: "pod0",
|
|
|
|
extenders: []algorithm.SchedulerExtender{
|
|
|
|
&fakeExtender{isBinder: false, interestedPodName: "pod0"},
|
|
|
|
&fakeExtender{isBinder: true, interestedPodName: "pod0"},
|
|
|
|
},
|
|
|
|
expectedBinderType: "*factory.fakeExtender",
|
|
|
|
},
|
|
|
|
// Expect to return the default binder because one of the extenders is
|
|
|
|
// a binder but the binder is not interested in the pod.
|
|
|
|
{
|
|
|
|
podName: "pod1",
|
|
|
|
extenders: []algorithm.SchedulerExtender{
|
|
|
|
&fakeExtender{isBinder: false, interestedPodName: "pod1"},
|
|
|
|
&fakeExtender{isBinder: true, interestedPodName: "pod0"},
|
|
|
|
},
|
|
|
|
expectedBinderType: "*factory.binder",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
pod := &v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: test.podName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
f := &configFactory{}
|
|
|
|
binderFunc := f.getBinderFunc(test.extenders)
|
|
|
|
binder := binderFunc(pod)
|
|
|
|
|
|
|
|
binderType := fmt.Sprintf("%s", reflect.TypeOf(binder))
|
|
|
|
if binderType != test.expectedBinderType {
|
|
|
|
t.Errorf("Expected binder %q but got %q", test.expectedBinderType, binderType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|