plugins.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. Copyright 2014 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 factory
  14. import (
  15. "fmt"
  16. "regexp"
  17. "sort"
  18. "strings"
  19. "sync"
  20. "k8s.io/apimachinery/pkg/util/sets"
  21. "k8s.io/kubernetes/pkg/scheduler/algorithm"
  22. "k8s.io/kubernetes/pkg/scheduler/algorithm/predicates"
  23. "k8s.io/kubernetes/pkg/scheduler/algorithm/priorities"
  24. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  25. "k8s.io/kubernetes/pkg/scheduler/volumebinder"
  26. "k8s.io/klog"
  27. )
  28. // PluginFactoryArgs are passed to all plugin factory functions.
  29. type PluginFactoryArgs struct {
  30. PodLister algorithm.PodLister
  31. ServiceLister algorithm.ServiceLister
  32. ControllerLister algorithm.ControllerLister
  33. ReplicaSetLister algorithm.ReplicaSetLister
  34. StatefulSetLister algorithm.StatefulSetLister
  35. NodeLister algorithm.NodeLister
  36. PDBLister algorithm.PDBLister
  37. NodeInfo predicates.NodeInfo
  38. PVInfo predicates.PersistentVolumeInfo
  39. PVCInfo predicates.PersistentVolumeClaimInfo
  40. StorageClassInfo predicates.StorageClassInfo
  41. VolumeBinder *volumebinder.VolumeBinder
  42. HardPodAffinitySymmetricWeight int32
  43. }
  44. // PriorityMetadataProducerFactory produces PriorityMetadataProducer from the given args.
  45. type PriorityMetadataProducerFactory func(PluginFactoryArgs) priorities.PriorityMetadataProducer
  46. // PredicateMetadataProducerFactory produces PredicateMetadataProducer from the given args.
  47. type PredicateMetadataProducerFactory func(PluginFactoryArgs) predicates.PredicateMetadataProducer
  48. // FitPredicateFactory produces a FitPredicate from the given args.
  49. type FitPredicateFactory func(PluginFactoryArgs) predicates.FitPredicate
  50. // PriorityFunctionFactory produces a PriorityConfig from the given args.
  51. // DEPRECATED
  52. // Use Map-Reduce pattern for priority functions.
  53. type PriorityFunctionFactory func(PluginFactoryArgs) priorities.PriorityFunction
  54. type CustomPriorityFunctionFactory func(PluginFactoryArgs) priorities.CustomPriorityFunction
  55. // PriorityFunctionFactory2 produces map & reduce priority functions
  56. // from a given args.
  57. // FIXME: Rename to PriorityFunctionFactory.
  58. type PriorityFunctionFactory2 func(PluginFactoryArgs) (priorities.PriorityMapFunction, priorities.PriorityReduceFunction)
  59. type CustomPriorityFunctionFactory2 func(PluginFactoryArgs) (priorities.CustomPriorityMapFunction, priorities.CustomPriorityReduceFunction)
  60. // PriorityConfigFactory produces a PriorityConfig from the given function and weight
  61. type PriorityConfigFactory struct {
  62. Function PriorityFunctionFactory
  63. MapReduceFunction PriorityFunctionFactory2
  64. Weight int
  65. }
  66. type CustomPriorityConfigFactory struct {
  67. Function CustomPriorityFunctionFactory
  68. MapReduceFunction CustomPriorityFunctionFactory2
  69. Weight int
  70. }
  71. var (
  72. schedulerFactoryMutex sync.RWMutex
  73. // maps that hold registered algorithm types
  74. fitPredicateMap = make(map[string]FitPredicateFactory)
  75. mandatoryFitPredicates = sets.NewString()
  76. priorityFunctionMap = make(map[string]PriorityConfigFactory)
  77. //custom
  78. customPriorityFunctionMap = make(map[string]CustomPriorityConfigFactory)
  79. //custom
  80. algorithmProviderMap = make(map[string]AlgorithmProviderConfig)
  81. // Registered metadata producers
  82. priorityMetadataProducer PriorityMetadataProducerFactory
  83. predicateMetadataProducer PredicateMetadataProducerFactory
  84. )
  85. const (
  86. // DefaultProvider defines the default algorithm provider name.
  87. DefaultProvider = "DefaultProvider"
  88. )
  89. // AlgorithmProviderConfig is used to store the configuration of algorithm providers.
  90. type AlgorithmProviderConfig struct {
  91. FitPredicateKeys sets.String
  92. PriorityFunctionKeys sets.String
  93. }
  94. // RegisterFitPredicate registers a fit predicate with the algorithm
  95. // registry. Returns the name with which the predicate was registered.
  96. func RegisterFitPredicate(name string, predicate predicates.FitPredicate) string {
  97. return RegisterFitPredicateFactory(name, func(PluginFactoryArgs) predicates.FitPredicate { return predicate })
  98. }
  99. // RemoveFitPredicate removes a fit predicate from factory.
  100. func RemoveFitPredicate(name string) {
  101. schedulerFactoryMutex.Lock()
  102. defer schedulerFactoryMutex.Unlock()
  103. validateAlgorithmNameOrDie(name)
  104. delete(fitPredicateMap, name)
  105. mandatoryFitPredicates.Delete(name)
  106. }
  107. // RemovePredicateKeyFromAlgoProvider removes a fit predicate key from algorithmProvider.
  108. func RemovePredicateKeyFromAlgoProvider(providerName, key string) error {
  109. schedulerFactoryMutex.Lock()
  110. defer schedulerFactoryMutex.Unlock()
  111. validateAlgorithmNameOrDie(providerName)
  112. provider, ok := algorithmProviderMap[providerName]
  113. if !ok {
  114. return fmt.Errorf("plugin %v has not been registered", providerName)
  115. }
  116. provider.FitPredicateKeys.Delete(key)
  117. return nil
  118. }
  119. // RemovePredicateKeyFromAlgorithmProviderMap removes a fit predicate key from all algorithmProviders which in algorithmProviderMap.
  120. func RemovePredicateKeyFromAlgorithmProviderMap(key string) {
  121. schedulerFactoryMutex.Lock()
  122. defer schedulerFactoryMutex.Unlock()
  123. for _, provider := range algorithmProviderMap {
  124. provider.FitPredicateKeys.Delete(key)
  125. }
  126. }
  127. // InsertPredicateKeyToAlgoProvider insert a fit predicate key to algorithmProvider.
  128. func InsertPredicateKeyToAlgoProvider(providerName, key string) error {
  129. schedulerFactoryMutex.Lock()
  130. defer schedulerFactoryMutex.Unlock()
  131. validateAlgorithmNameOrDie(providerName)
  132. provider, ok := algorithmProviderMap[providerName]
  133. if !ok {
  134. return fmt.Errorf("plugin %v has not been registered", providerName)
  135. }
  136. provider.FitPredicateKeys.Insert(key)
  137. return nil
  138. }
  139. // InsertPredicateKeyToAlgorithmProviderMap insert a fit predicate key to all algorithmProviders which in algorithmProviderMap.
  140. func InsertPredicateKeyToAlgorithmProviderMap(key string) {
  141. schedulerFactoryMutex.Lock()
  142. defer schedulerFactoryMutex.Unlock()
  143. for _, provider := range algorithmProviderMap {
  144. provider.FitPredicateKeys.Insert(key)
  145. }
  146. return
  147. }
  148. // InsertPriorityKeyToAlgorithmProviderMap inserts a priority function to all algorithmProviders which are in algorithmProviderMap.
  149. func InsertPriorityKeyToAlgorithmProviderMap(key string) {
  150. schedulerFactoryMutex.Lock()
  151. defer schedulerFactoryMutex.Unlock()
  152. for _, provider := range algorithmProviderMap {
  153. provider.PriorityFunctionKeys.Insert(key)
  154. }
  155. return
  156. }
  157. // RegisterMandatoryFitPredicate registers a fit predicate with the algorithm registry, the predicate is used by
  158. // kubelet, DaemonSet; it is always included in configuration. Returns the name with which the predicate was
  159. // registered.
  160. func RegisterMandatoryFitPredicate(name string, predicate predicates.FitPredicate) string {
  161. schedulerFactoryMutex.Lock()
  162. defer schedulerFactoryMutex.Unlock()
  163. validateAlgorithmNameOrDie(name)
  164. fitPredicateMap[name] = func(PluginFactoryArgs) predicates.FitPredicate { return predicate }
  165. mandatoryFitPredicates.Insert(name)
  166. return name
  167. }
  168. // RegisterFitPredicateFactory registers a fit predicate factory with the
  169. // algorithm registry. Returns the name with which the predicate was registered.
  170. func RegisterFitPredicateFactory(name string, predicateFactory FitPredicateFactory) string {
  171. schedulerFactoryMutex.Lock()
  172. defer schedulerFactoryMutex.Unlock()
  173. validateAlgorithmNameOrDie(name)
  174. fitPredicateMap[name] = predicateFactory
  175. return name
  176. }
  177. // RegisterCustomFitPredicate registers a custom fit predicate with the algorithm registry.
  178. // Returns the name, with which the predicate was registered.
  179. func RegisterCustomFitPredicate(policy schedulerapi.PredicatePolicy) string {
  180. var predicateFactory FitPredicateFactory
  181. var ok bool
  182. validatePredicateOrDie(policy)
  183. // generate the predicate function, if a custom type is requested
  184. if policy.Argument != nil {
  185. if policy.Argument.ServiceAffinity != nil {
  186. predicateFactory = func(args PluginFactoryArgs) predicates.FitPredicate {
  187. predicate, precomputationFunction := predicates.NewServiceAffinityPredicate(
  188. args.PodLister,
  189. args.ServiceLister,
  190. args.NodeInfo,
  191. policy.Argument.ServiceAffinity.Labels,
  192. )
  193. // Once we generate the predicate we should also Register the Precomputation
  194. predicates.RegisterPredicateMetadataProducer(policy.Name, precomputationFunction)
  195. return predicate
  196. }
  197. } else if policy.Argument.LabelsPresence != nil {
  198. predicateFactory = func(args PluginFactoryArgs) predicates.FitPredicate {
  199. return predicates.NewNodeLabelPredicate(
  200. policy.Argument.LabelsPresence.Labels,
  201. policy.Argument.LabelsPresence.Presence,
  202. )
  203. }
  204. }
  205. } else if predicateFactory, ok = fitPredicateMap[policy.Name]; ok {
  206. // checking to see if a pre-defined predicate is requested
  207. klog.V(2).Infof("Predicate type %s already registered, reusing.", policy.Name)
  208. return policy.Name
  209. }
  210. if predicateFactory == nil {
  211. klog.Fatalf("Invalid configuration: Predicate type not found for %s", policy.Name)
  212. }
  213. return RegisterFitPredicateFactory(policy.Name, predicateFactory)
  214. }
  215. // IsFitPredicateRegistered is useful for testing providers.
  216. func IsFitPredicateRegistered(name string) bool {
  217. schedulerFactoryMutex.RLock()
  218. defer schedulerFactoryMutex.RUnlock()
  219. _, ok := fitPredicateMap[name]
  220. return ok
  221. }
  222. // RegisterPriorityMetadataProducerFactory registers a PriorityMetadataProducerFactory.
  223. func RegisterPriorityMetadataProducerFactory(factory PriorityMetadataProducerFactory) {
  224. schedulerFactoryMutex.Lock()
  225. defer schedulerFactoryMutex.Unlock()
  226. priorityMetadataProducer = factory
  227. }
  228. // RegisterPredicateMetadataProducerFactory registers a PredicateMetadataProducerFactory.
  229. func RegisterPredicateMetadataProducerFactory(factory PredicateMetadataProducerFactory) {
  230. schedulerFactoryMutex.Lock()
  231. defer schedulerFactoryMutex.Unlock()
  232. predicateMetadataProducer = factory
  233. }
  234. // RegisterPriorityFunction registers a priority function with the algorithm registry. Returns the name,
  235. // with which the function was registered.
  236. // DEPRECATED
  237. // Use Map-Reduce pattern for priority functions.
  238. func RegisterPriorityFunction(name string, function priorities.PriorityFunction, weight int) string {
  239. return RegisterPriorityConfigFactory(name, PriorityConfigFactory{
  240. Function: func(PluginFactoryArgs) priorities.PriorityFunction {
  241. return function
  242. },
  243. Weight: weight,
  244. })
  245. }
  246. // RegisterPriorityFunction2 registers a priority function with the algorithm registry. Returns the name,
  247. // with which the function was registered.
  248. // FIXME: Rename to PriorityFunctionFactory.
  249. func RegisterPriorityFunction2(
  250. name string,
  251. mapFunction priorities.PriorityMapFunction,
  252. reduceFunction priorities.PriorityReduceFunction,
  253. weight int) string {
  254. return RegisterPriorityConfigFactory(name, PriorityConfigFactory{
  255. MapReduceFunction: func(PluginFactoryArgs) (priorities.PriorityMapFunction, priorities.PriorityReduceFunction) {
  256. return mapFunction, reduceFunction
  257. },
  258. Weight: weight,
  259. })
  260. }
  261. //------------------------------------------------------------------------------------------------
  262. //------------------------------------------------------------------------------------------------
  263. // ---------START OF CUSTOMIZATION----------------------------------------------------------------
  264. //------------------------------------------------------------------------------------------------
  265. //------------------------------------------------------------------------------------------------
  266. func CustomRegisterPriorityFunction2(
  267. name string,
  268. mapFunction priorities.CustomPriorityMapFunction,
  269. reduceFunction priorities.CustomPriorityReduceFunction,
  270. weight int) string {
  271. return CustomRegisterPriorityConfigFactory(name, CustomPriorityConfigFactory{
  272. MapReduceFunction: func(PluginFactoryArgs) (priorities.CustomPriorityMapFunction, priorities.CustomPriorityReduceFunction) {
  273. return mapFunction, reduceFunction
  274. },
  275. Weight: weight,
  276. })
  277. }
  278. func CustomRegisterPriorityConfigFactory(name string, pcf CustomPriorityConfigFactory) string {
  279. schedulerFactoryMutex.Lock()
  280. defer schedulerFactoryMutex.Unlock()
  281. validateAlgorithmNameOrDie(name)
  282. customPriorityFunctionMap[name] = pcf
  283. return name
  284. }
  285. //------------------------------------------------------------------------------------------------
  286. //------------------------------------------------------------------------------------------------
  287. // ---------START OF CUSTOMIZATION----------------------------------------------------------------
  288. //------------------------------------------------------------------------------------------------
  289. //------------------------------------------------------------------------------------------------
  290. // RegisterPriorityConfigFactory registers a priority config factory with its name.
  291. func RegisterPriorityConfigFactory(name string, pcf PriorityConfigFactory) string {
  292. schedulerFactoryMutex.Lock()
  293. defer schedulerFactoryMutex.Unlock()
  294. validateAlgorithmNameOrDie(name)
  295. priorityFunctionMap[name] = pcf
  296. return name
  297. }
  298. // RegisterCustomPriorityFunction registers a custom priority function with the algorithm registry.
  299. // Returns the name, with which the priority function was registered.
  300. func RegisterCustomPriorityFunction(policy schedulerapi.PriorityPolicy) string {
  301. var pcf *PriorityConfigFactory
  302. validatePriorityOrDie(policy)
  303. // generate the priority function, if a custom priority is requested
  304. if policy.Argument != nil {
  305. if policy.Argument.ServiceAntiAffinity != nil {
  306. pcf = &PriorityConfigFactory{
  307. MapReduceFunction: func(args PluginFactoryArgs) (priorities.PriorityMapFunction, priorities.PriorityReduceFunction) {
  308. return priorities.NewServiceAntiAffinityPriority(
  309. args.PodLister,
  310. args.ServiceLister,
  311. policy.Argument.ServiceAntiAffinity.Label,
  312. )
  313. },
  314. Weight: policy.Weight,
  315. }
  316. } else if policy.Argument.LabelPreference != nil {
  317. pcf = &PriorityConfigFactory{
  318. MapReduceFunction: func(args PluginFactoryArgs) (priorities.PriorityMapFunction, priorities.PriorityReduceFunction) {
  319. return priorities.NewNodeLabelPriority(
  320. policy.Argument.LabelPreference.Label,
  321. policy.Argument.LabelPreference.Presence,
  322. )
  323. },
  324. Weight: policy.Weight,
  325. }
  326. } else if policy.Argument.RequestedToCapacityRatioArguments != nil {
  327. pcf = &PriorityConfigFactory{
  328. MapReduceFunction: func(args PluginFactoryArgs) (priorities.PriorityMapFunction, priorities.PriorityReduceFunction) {
  329. scoringFunctionShape := buildScoringFunctionShapeFromRequestedToCapacityRatioArguments(policy.Argument.RequestedToCapacityRatioArguments)
  330. p := priorities.RequestedToCapacityRatioResourceAllocationPriority(scoringFunctionShape)
  331. return p.PriorityMap, nil
  332. },
  333. Weight: policy.Weight,
  334. }
  335. }
  336. } else if existingPcf, ok := priorityFunctionMap[policy.Name]; ok {
  337. klog.V(2).Infof("Priority type %s already registered, reusing.", policy.Name)
  338. // set/update the weight based on the policy
  339. pcf = &PriorityConfigFactory{
  340. Function: existingPcf.Function,
  341. MapReduceFunction: existingPcf.MapReduceFunction,
  342. Weight: policy.Weight,
  343. }
  344. }
  345. if pcf == nil {
  346. klog.Fatalf("Invalid configuration: Priority type not found for %s", policy.Name)
  347. }
  348. return RegisterPriorityConfigFactory(policy.Name, *pcf)
  349. }
  350. func buildScoringFunctionShapeFromRequestedToCapacityRatioArguments(arguments *schedulerapi.RequestedToCapacityRatioArguments) priorities.FunctionShape {
  351. n := len(arguments.UtilizationShape)
  352. points := make([]priorities.FunctionShapePoint, 0, n)
  353. for _, point := range arguments.UtilizationShape {
  354. points = append(points, priorities.FunctionShapePoint{Utilization: int64(point.Utilization), Score: int64(point.Score)})
  355. }
  356. shape, err := priorities.NewFunctionShape(points)
  357. if err != nil {
  358. klog.Fatalf("invalid RequestedToCapacityRatioPriority arguments: %s", err.Error())
  359. }
  360. return shape
  361. }
  362. // IsPriorityFunctionRegistered is useful for testing providers.
  363. func IsPriorityFunctionRegistered(name string) bool {
  364. schedulerFactoryMutex.RLock()
  365. defer schedulerFactoryMutex.RUnlock()
  366. _, ok := priorityFunctionMap[name]
  367. return ok
  368. }
  369. // RegisterAlgorithmProvider registers a new algorithm provider with the algorithm registry. This should
  370. // be called from the init function in a provider plugin.
  371. func RegisterAlgorithmProvider(name string, predicateKeys, priorityKeys sets.String) string {
  372. schedulerFactoryMutex.Lock()
  373. defer schedulerFactoryMutex.Unlock()
  374. validateAlgorithmNameOrDie(name)
  375. algorithmProviderMap[name] = AlgorithmProviderConfig{
  376. FitPredicateKeys: predicateKeys,
  377. PriorityFunctionKeys: priorityKeys,
  378. }
  379. return name
  380. }
  381. // GetAlgorithmProvider should not be used to modify providers. It is publicly visible for testing.
  382. func GetAlgorithmProvider(name string) (*AlgorithmProviderConfig, error) {
  383. schedulerFactoryMutex.RLock()
  384. defer schedulerFactoryMutex.RUnlock()
  385. provider, ok := algorithmProviderMap[name]
  386. if !ok {
  387. return nil, fmt.Errorf("plugin %q has not been registered", name)
  388. }
  389. return &provider, nil
  390. }
  391. func getFitPredicateFunctions(names sets.String, args PluginFactoryArgs) (map[string]predicates.FitPredicate, error) {
  392. schedulerFactoryMutex.RLock()
  393. defer schedulerFactoryMutex.RUnlock()
  394. fitPredicates := map[string]predicates.FitPredicate{}
  395. for _, name := range names.List() {
  396. factory, ok := fitPredicateMap[name]
  397. if !ok {
  398. return nil, fmt.Errorf("invalid predicate name %q specified - no corresponding function found", name)
  399. }
  400. fitPredicates[name] = factory(args)
  401. }
  402. // Always include mandatory fit predicates.
  403. for name := range mandatoryFitPredicates {
  404. if factory, found := fitPredicateMap[name]; found {
  405. fitPredicates[name] = factory(args)
  406. }
  407. }
  408. return fitPredicates, nil
  409. }
  410. func getPriorityMetadataProducer(args PluginFactoryArgs) (priorities.PriorityMetadataProducer, error) {
  411. schedulerFactoryMutex.Lock()
  412. defer schedulerFactoryMutex.Unlock()
  413. if priorityMetadataProducer == nil {
  414. return priorities.EmptyPriorityMetadataProducer, nil
  415. }
  416. return priorityMetadataProducer(args), nil
  417. }
  418. func getPredicateMetadataProducer(args PluginFactoryArgs) (predicates.PredicateMetadataProducer, error) {
  419. schedulerFactoryMutex.Lock()
  420. defer schedulerFactoryMutex.Unlock()
  421. if predicateMetadataProducer == nil {
  422. return predicates.EmptyPredicateMetadataProducer, nil
  423. }
  424. return predicateMetadataProducer(args), nil
  425. }
  426. func getPriorityFunctionConfigs(names sets.String, args PluginFactoryArgs) ([]priorities.PriorityConfig, error) {
  427. schedulerFactoryMutex.RLock()
  428. defer schedulerFactoryMutex.RUnlock()
  429. var configs []priorities.PriorityConfig
  430. for _, name := range names.List() {
  431. factory, ok := priorityFunctionMap[name]
  432. if !ok {
  433. return nil, fmt.Errorf("invalid priority name %s specified - no corresponding function found", name)
  434. }
  435. if factory.Function != nil {
  436. configs = append(configs, priorities.PriorityConfig{
  437. Name: name,
  438. Function: factory.Function(args),
  439. Weight: factory.Weight,
  440. })
  441. } else {
  442. mapFunction, reduceFunction := factory.MapReduceFunction(args)
  443. configs = append(configs, priorities.PriorityConfig{
  444. Name: name,
  445. Map: mapFunction,
  446. Reduce: reduceFunction,
  447. Weight: factory.Weight,
  448. })
  449. }
  450. }
  451. if err := validateSelectedConfigs(configs); err != nil {
  452. return nil, err
  453. }
  454. return configs, nil
  455. }
  456. // validateSelectedConfigs validates the config weights to avoid the overflow.
  457. func validateSelectedConfigs(configs []priorities.PriorityConfig) error {
  458. var totalPriority int
  459. for _, config := range configs {
  460. // Checks totalPriority against MaxTotalPriority to avoid overflow
  461. if config.Weight*schedulerapi.MaxPriority > schedulerapi.MaxTotalPriority-totalPriority {
  462. return fmt.Errorf("total priority of priority functions has overflown")
  463. }
  464. totalPriority += config.Weight * schedulerapi.MaxPriority
  465. }
  466. return nil
  467. }
  468. var validName = regexp.MustCompile("^[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])$")
  469. func validateAlgorithmNameOrDie(name string) {
  470. if !validName.MatchString(name) {
  471. klog.Fatalf("Algorithm name %v does not match the name validation regexp \"%v\".", name, validName)
  472. }
  473. }
  474. func validatePredicateOrDie(predicate schedulerapi.PredicatePolicy) {
  475. if predicate.Argument != nil {
  476. numArgs := 0
  477. if predicate.Argument.ServiceAffinity != nil {
  478. numArgs++
  479. }
  480. if predicate.Argument.LabelsPresence != nil {
  481. numArgs++
  482. }
  483. if numArgs != 1 {
  484. klog.Fatalf("Exactly 1 predicate argument is required, numArgs: %v, Predicate: %s", numArgs, predicate.Name)
  485. }
  486. }
  487. }
  488. func validatePriorityOrDie(priority schedulerapi.PriorityPolicy) {
  489. if priority.Argument != nil {
  490. numArgs := 0
  491. if priority.Argument.ServiceAntiAffinity != nil {
  492. numArgs++
  493. }
  494. if priority.Argument.LabelPreference != nil {
  495. numArgs++
  496. }
  497. if priority.Argument.RequestedToCapacityRatioArguments != nil {
  498. numArgs++
  499. }
  500. if numArgs != 1 {
  501. klog.Fatalf("Exactly 1 priority argument is required, numArgs: %v, Priority: %s", numArgs, priority.Name)
  502. }
  503. }
  504. }
  505. // ListRegisteredFitPredicates returns the registered fit predicates.
  506. func ListRegisteredFitPredicates() []string {
  507. schedulerFactoryMutex.RLock()
  508. defer schedulerFactoryMutex.RUnlock()
  509. var names []string
  510. for name := range fitPredicateMap {
  511. names = append(names, name)
  512. }
  513. return names
  514. }
  515. // ListRegisteredPriorityFunctions returns the registered priority functions.
  516. func ListRegisteredPriorityFunctions() []string {
  517. schedulerFactoryMutex.RLock()
  518. defer schedulerFactoryMutex.RUnlock()
  519. var names []string
  520. for name := range priorityFunctionMap {
  521. names = append(names, name)
  522. }
  523. return names
  524. }
  525. // ListAlgorithmProviders is called when listing all available algorithm providers in `kube-scheduler --help`
  526. func ListAlgorithmProviders() string {
  527. var availableAlgorithmProviders []string
  528. for name := range algorithmProviderMap {
  529. availableAlgorithmProviders = append(availableAlgorithmProviders, name)
  530. }
  531. sort.Strings(availableAlgorithmProviders)
  532. return strings.Join(availableAlgorithmProviders, " | ")
  533. }