util.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "net/http"
  16. "net/http/httptest"
  17. "k8s.io/api/core/v1"
  18. "k8s.io/client-go/informers"
  19. clientset "k8s.io/client-go/kubernetes"
  20. clientv1core "k8s.io/client-go/kubernetes/typed/core/v1"
  21. "k8s.io/client-go/tools/record"
  22. "k8s.io/klog"
  23. "k8s.io/kubernetes/pkg/api/legacyscheme"
  24. "k8s.io/kubernetes/pkg/scheduler"
  25. // import DefaultProvider
  26. _ "k8s.io/kubernetes/pkg/scheduler/algorithmprovider/defaults"
  27. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  28. "k8s.io/kubernetes/pkg/scheduler/factory"
  29. "k8s.io/kubernetes/test/integration/framework"
  30. )
  31. // ShutdownFunc represents the function handle to be called, typically in a defer handler, to shutdown a running module
  32. type ShutdownFunc func()
  33. // StartApiserver starts a local API server for testing and returns the handle to the URL and the shutdown function to stop it.
  34. func StartApiserver() (string, ShutdownFunc) {
  35. h := &framework.MasterHolder{Initialized: make(chan struct{})}
  36. s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  37. <-h.Initialized
  38. h.M.GenericAPIServer.Handler.ServeHTTP(w, req)
  39. }))
  40. framework.RunAMasterUsingServer(framework.NewIntegrationTestMasterConfig(), s, h)
  41. shutdownFunc := func() {
  42. klog.Infof("destroying API server")
  43. s.Close()
  44. klog.Infof("destroyed API server")
  45. }
  46. return s.URL, shutdownFunc
  47. }
  48. // StartScheduler configures and starts a scheduler given a handle to the clientSet interface
  49. // and event broadcaster. It returns a handle to the configurator for the running scheduler
  50. // and the shutdown function to stop it.
  51. func StartScheduler(clientSet clientset.Interface) (factory.Configurator, ShutdownFunc) {
  52. informerFactory := informers.NewSharedInformerFactory(clientSet, 0)
  53. evtBroadcaster := record.NewBroadcaster()
  54. evtWatch := evtBroadcaster.StartRecordingToSink(&clientv1core.EventSinkImpl{
  55. Interface: clientSet.CoreV1().Events("")})
  56. stopCh := make(chan struct{})
  57. schedulerConfigurator := createSchedulerConfigurator(clientSet, informerFactory, stopCh)
  58. config, err := schedulerConfigurator.CreateFromConfig(schedulerapi.Policy{})
  59. if err != nil {
  60. klog.Fatalf("Error creating scheduler: %v", err)
  61. }
  62. config.Recorder = evtBroadcaster.NewRecorder(legacyscheme.Scheme, v1.EventSource{Component: "scheduler"})
  63. sched := scheduler.NewFromConfig(config)
  64. scheduler.AddAllEventHandlers(sched,
  65. v1.DefaultSchedulerName,
  66. informerFactory.Core().V1().Nodes(),
  67. informerFactory.Core().V1().Pods(),
  68. informerFactory.Core().V1().PersistentVolumes(),
  69. informerFactory.Core().V1().PersistentVolumeClaims(),
  70. informerFactory.Core().V1().Services(),
  71. informerFactory.Storage().V1().StorageClasses(),
  72. )
  73. informerFactory.Start(stopCh)
  74. sched.Run()
  75. shutdownFunc := func() {
  76. klog.Infof("destroying scheduler")
  77. evtWatch.Stop()
  78. close(stopCh)
  79. klog.Infof("destroyed scheduler")
  80. }
  81. return schedulerConfigurator, shutdownFunc
  82. }
  83. // createSchedulerConfigurator create a configurator for scheduler with given informer factory and default name.
  84. func createSchedulerConfigurator(
  85. clientSet clientset.Interface,
  86. informerFactory informers.SharedInformerFactory,
  87. stopCh <-chan struct{},
  88. ) factory.Configurator {
  89. return factory.NewConfigFactory(&factory.ConfigFactoryArgs{
  90. SchedulerName: v1.DefaultSchedulerName,
  91. Client: clientSet,
  92. NodeInformer: informerFactory.Core().V1().Nodes(),
  93. PodInformer: informerFactory.Core().V1().Pods(),
  94. PvInformer: informerFactory.Core().V1().PersistentVolumes(),
  95. PvcInformer: informerFactory.Core().V1().PersistentVolumeClaims(),
  96. ReplicationControllerInformer: informerFactory.Core().V1().ReplicationControllers(),
  97. ReplicaSetInformer: informerFactory.Apps().V1().ReplicaSets(),
  98. StatefulSetInformer: informerFactory.Apps().V1().StatefulSets(),
  99. ServiceInformer: informerFactory.Core().V1().Services(),
  100. PdbInformer: informerFactory.Policy().V1beta1().PodDisruptionBudgets(),
  101. StorageClassInformer: informerFactory.Storage().V1().StorageClasses(),
  102. HardPodAffinitySymmetricWeight: v1.DefaultHardPodAffinitySymmetricWeight,
  103. DisablePreemption: false,
  104. PercentageOfNodesToScore: schedulerapi.DefaultPercentageOfNodesToScore,
  105. StopCh: stopCh,
  106. })
  107. }