plugins.go 22 KB

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