mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-20 18:31:15 +00:00
Update scheduler tests
This commit is contained in:
parent
61b983a66b
commit
c802118e81
@ -22,6 +22,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -59,7 +60,7 @@ type TearDownFunc func()
|
|||||||
|
|
||||||
// StartTestServer runs a kube-apiserver, optionally calling out to the setup.ModifyServerRunOptions and setup.ModifyServerConfig functions
|
// StartTestServer runs a kube-apiserver, optionally calling out to the setup.ModifyServerRunOptions and setup.ModifyServerConfig functions
|
||||||
func StartTestServer(t *testing.T, setup TestServerSetup) (client.Interface, *rest.Config, TearDownFunc) {
|
func StartTestServer(t *testing.T, setup TestServerSetup) (client.Interface, *rest.Config, TearDownFunc) {
|
||||||
certDir, err := os.MkdirTemp("", "test-integration-"+t.Name())
|
certDir, err := os.MkdirTemp("", "test-integration-"+strings.ReplaceAll(t.Name(), "/", "_"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Couldn't create temp dir: %v", err)
|
t.Fatalf("Couldn't create temp dir: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,6 @@ import (
|
|||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
||||||
"k8s.io/apiserver/pkg/admission"
|
"k8s.io/apiserver/pkg/admission"
|
||||||
"k8s.io/client-go/informers"
|
"k8s.io/client-go/informers"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
@ -123,10 +122,9 @@ func TestTaintBasedEvictions(t *testing.T) {
|
|||||||
testCtx := testutils.InitTestAPIServer(t, "taint-based-evictions", admission)
|
testCtx := testutils.InitTestAPIServer(t, "taint-based-evictions", admission)
|
||||||
|
|
||||||
// Build clientset and informers for controllers.
|
// Build clientset and informers for controllers.
|
||||||
externalClientset := kubernetes.NewForConfigOrDie(&restclient.Config{
|
externalClientConfig := restclient.CopyConfig(testCtx.KubeConfig)
|
||||||
QPS: -1,
|
externalClientConfig.QPS = -1
|
||||||
Host: testCtx.HTTPServer.URL,
|
externalClientset := kubernetes.NewForConfigOrDie(externalClientConfig)
|
||||||
ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
||||||
externalInformers := informers.NewSharedInformerFactory(externalClientset, time.Second)
|
externalInformers := informers.NewSharedInformerFactory(externalClientset, time.Second)
|
||||||
podTolerations.SetExternalKubeClientSet(externalClientset)
|
podTolerations.SetExternalKubeClientSet(externalClientset)
|
||||||
podTolerations.SetExternalKubeInformerFactory(externalInformers)
|
podTolerations.SetExternalKubeInformerFactory(externalInformers)
|
||||||
@ -134,10 +132,6 @@ func TestTaintBasedEvictions(t *testing.T) {
|
|||||||
testCtx = testutils.InitTestScheduler(t, testCtx)
|
testCtx = testutils.InitTestScheduler(t, testCtx)
|
||||||
defer testutils.CleanupTest(t, testCtx)
|
defer testutils.CleanupTest(t, testCtx)
|
||||||
cs := testCtx.ClientSet
|
cs := testCtx.ClientSet
|
||||||
_, err := cs.CoreV1().Namespaces().Create(context.TODO(), testCtx.NS, metav1.CreateOptions{})
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Failed to create namespace %+v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start NodeLifecycleController for taint.
|
// Start NodeLifecycleController for taint.
|
||||||
nc, err := nodelifecycle.NewNodeLifecycleController(
|
nc, err := nodelifecycle.NewNodeLifecycleController(
|
||||||
|
@ -30,7 +30,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
"k8s.io/apimachinery/pkg/util/intstr"
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
@ -621,10 +620,9 @@ func TestPodPriorityResolution(t *testing.T) {
|
|||||||
cs := testCtx.ClientSet
|
cs := testCtx.ClientSet
|
||||||
|
|
||||||
// Build clientset and informers for controllers.
|
// Build clientset and informers for controllers.
|
||||||
externalClientset := kubernetes.NewForConfigOrDie(&restclient.Config{
|
externalClientConfig := restclient.CopyConfig(testCtx.KubeConfig)
|
||||||
QPS: -1,
|
externalClientConfig.QPS = -1
|
||||||
Host: testCtx.HTTPServer.URL,
|
externalClientset := kubernetes.NewForConfigOrDie(externalClientConfig)
|
||||||
ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
||||||
externalInformers := informers.NewSharedInformerFactory(externalClientset, time.Second)
|
externalInformers := informers.NewSharedInformerFactory(externalClientset, time.Second)
|
||||||
admission.SetExternalKubeClientSet(externalClientset)
|
admission.SetExternalKubeClientSet(externalClientset)
|
||||||
admission.SetExternalKubeInformerFactory(externalInformers)
|
admission.SetExternalKubeInformerFactory(externalInformers)
|
||||||
|
@ -27,7 +27,6 @@ import (
|
|||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
||||||
"k8s.io/client-go/informers"
|
"k8s.io/client-go/informers"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
@ -68,13 +67,12 @@ func TestTaintNodeByCondition(t *testing.T) {
|
|||||||
// Build PodToleration Admission.
|
// Build PodToleration Admission.
|
||||||
admission := podtolerationrestriction.NewPodTolerationsPlugin(&pluginapi.Configuration{})
|
admission := podtolerationrestriction.NewPodTolerationsPlugin(&pluginapi.Configuration{})
|
||||||
|
|
||||||
testCtx := testutils.InitTestAPIServer(t, "default", admission)
|
testCtx := testutils.InitTestAPIServer(t, "taint-node-by-condition", admission)
|
||||||
|
|
||||||
// Build clientset and informers for controllers.
|
// Build clientset and informers for controllers.
|
||||||
externalClientset := kubernetes.NewForConfigOrDie(&restclient.Config{
|
externalClientConfig := restclient.CopyConfig(testCtx.KubeConfig)
|
||||||
QPS: -1,
|
externalClientConfig.QPS = -1
|
||||||
Host: testCtx.HTTPServer.URL,
|
externalClientset := kubernetes.NewForConfigOrDie(externalClientConfig)
|
||||||
ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
|
||||||
externalInformers := informers.NewSharedInformerFactory(externalClientset, 0)
|
externalInformers := informers.NewSharedInformerFactory(externalClientset, 0)
|
||||||
|
|
||||||
admission.SetExternalKubeClientSet(externalClientset)
|
admission.SetExternalKubeClientSet(externalClientset)
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -28,7 +27,6 @@ import (
|
|||||||
policy "k8s.io/api/policy/v1"
|
policy "k8s.io/api/policy/v1"
|
||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
||||||
"k8s.io/apimachinery/pkg/util/uuid"
|
"k8s.io/apimachinery/pkg/util/uuid"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"k8s.io/apiserver/pkg/admission"
|
"k8s.io/apiserver/pkg/admission"
|
||||||
@ -47,8 +45,10 @@ import (
|
|||||||
pvutil "k8s.io/component-helpers/storage/volume"
|
pvutil "k8s.io/component-helpers/storage/volume"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
"k8s.io/kube-scheduler/config/v1beta3"
|
"k8s.io/kube-scheduler/config/v1beta3"
|
||||||
|
"k8s.io/kubernetes/cmd/kube-apiserver/app/options"
|
||||||
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
|
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
|
||||||
"k8s.io/kubernetes/pkg/controller/disruption"
|
"k8s.io/kubernetes/pkg/controller/disruption"
|
||||||
|
"k8s.io/kubernetes/pkg/controlplane"
|
||||||
"k8s.io/kubernetes/pkg/scheduler"
|
"k8s.io/kubernetes/pkg/scheduler"
|
||||||
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
|
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
|
||||||
configtesting "k8s.io/kubernetes/pkg/scheduler/apis/config/testing"
|
configtesting "k8s.io/kubernetes/pkg/scheduler/apis/config/testing"
|
||||||
@ -162,8 +162,7 @@ func StartFakePVController(clientSet clientset.Interface) ShutdownFunc {
|
|||||||
|
|
||||||
// TestContext store necessary context info
|
// TestContext store necessary context info
|
||||||
type TestContext struct {
|
type TestContext struct {
|
||||||
CloseFn framework.CloseFunc
|
CloseFn framework.TearDownFunc
|
||||||
HTTPServer *httptest.Server
|
|
||||||
NS *v1.Namespace
|
NS *v1.Namespace
|
||||||
ClientSet clientset.Interface
|
ClientSet clientset.Interface
|
||||||
KubeConfig *restclient.Config
|
KubeConfig *restclient.Config
|
||||||
@ -341,32 +340,23 @@ func InitTestAPIServer(t *testing.T, nsPrefix string, admission admission.Interf
|
|||||||
CancelFn: cancelFunc,
|
CancelFn: cancelFunc,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Create API server
|
testCtx.ClientSet, testCtx.KubeConfig, testCtx.CloseFn = framework.StartTestServer(t, framework.TestServerSetup{
|
||||||
controlPlaneConfig := framework.NewIntegrationTestControlPlaneConfig()
|
ModifyServerRunOptions: func(options *options.ServerRunOptions) {
|
||||||
|
options.Admission.GenericAdmission.DisablePlugins = []string{"ServiceAccount", "TaintNodesByCondition", "Priority"}
|
||||||
|
},
|
||||||
|
ModifyServerConfig: func(config *controlplane.Config) {
|
||||||
if admission != nil {
|
if admission != nil {
|
||||||
controlPlaneConfig.GenericConfig.AdmissionControl = admission
|
config.GenericConfig.AdmissionControl = admission
|
||||||
}
|
}
|
||||||
|
},
|
||||||
_, testCtx.HTTPServer, testCtx.CloseFn = framework.RunAnAPIServer(controlPlaneConfig)
|
})
|
||||||
|
|
||||||
if nsPrefix != "default" {
|
if nsPrefix != "default" {
|
||||||
testCtx.NS = framework.CreateTestingNamespace(nsPrefix+string(uuid.NewUUID()), t)
|
testCtx.NS = framework.CreateNamespaceOrDie(testCtx.ClientSet, nsPrefix+string(uuid.NewUUID()), t)
|
||||||
} else {
|
} else {
|
||||||
testCtx.NS = framework.CreateTestingNamespace("default", t)
|
testCtx.NS = framework.CreateNamespaceOrDie(testCtx.ClientSet, "default", t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Create kubeclient
|
|
||||||
kubeConfig := &restclient.Config{
|
|
||||||
QPS: 100,
|
|
||||||
Burst: 100,
|
|
||||||
Host: testCtx.HTTPServer.URL,
|
|
||||||
ContentConfig: restclient.ContentConfig{
|
|
||||||
GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
testCtx.KubeConfig = kubeConfig
|
|
||||||
testCtx.ClientSet = clientset.NewForConfigOrDie(kubeConfig)
|
|
||||||
return &testCtx
|
return &testCtx
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -428,6 +418,11 @@ func InitTestSchedulerWithOptions(
|
|||||||
|
|
||||||
eventBroadcaster.StartRecordingToSink(testCtx.Ctx.Done())
|
eventBroadcaster.StartRecordingToSink(testCtx.Ctx.Done())
|
||||||
|
|
||||||
|
oldCloseFn := testCtx.CloseFn
|
||||||
|
testCtx.CloseFn = func() {
|
||||||
|
oldCloseFn()
|
||||||
|
eventBroadcaster.Shutdown()
|
||||||
|
}
|
||||||
return testCtx
|
return testCtx
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -466,9 +461,9 @@ func InitDisruptionController(t *testing.T, testCtx *TestContext) *disruption.Di
|
|||||||
discoveryClient := cacheddiscovery.NewMemCacheClient(testCtx.ClientSet.Discovery())
|
discoveryClient := cacheddiscovery.NewMemCacheClient(testCtx.ClientSet.Discovery())
|
||||||
mapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
|
mapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
|
||||||
|
|
||||||
config := restclient.Config{Host: testCtx.HTTPServer.URL}
|
config := restclient.CopyConfig(testCtx.KubeConfig)
|
||||||
scaleKindResolver := scale.NewDiscoveryScaleKindResolver(testCtx.ClientSet.Discovery())
|
scaleKindResolver := scale.NewDiscoveryScaleKindResolver(testCtx.ClientSet.Discovery())
|
||||||
scaleClient, err := scale.NewForConfig(&config, mapper, dynamic.LegacyAPIPathResolverFunc, scaleKindResolver)
|
scaleClient, err := scale.NewForConfig(config, mapper, dynamic.LegacyAPIPathResolverFunc, scaleKindResolver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error in create scaleClient: %v", err)
|
t.Fatalf("Error in create scaleClient: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user