validation_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 validation
  14. import (
  15. "errors"
  16. "fmt"
  17. "testing"
  18. "time"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. componentbaseconfig "k8s.io/component-base/config"
  21. "k8s.io/kubernetes/pkg/scheduler/apis/config"
  22. )
  23. func TestValidateKubeSchedulerConfiguration(t *testing.T) {
  24. testTimeout := int64(0)
  25. podInitialBackoffSeconds := int64(1)
  26. podMaxBackoffSeconds := int64(1)
  27. validConfig := &config.KubeSchedulerConfiguration{
  28. HealthzBindAddress: "0.0.0.0:10254",
  29. MetricsBindAddress: "0.0.0.0:10254",
  30. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  31. AcceptContentTypes: "application/json",
  32. ContentType: "application/json",
  33. QPS: 10,
  34. Burst: 10,
  35. },
  36. AlgorithmSource: config.SchedulerAlgorithmSource{
  37. Policy: &config.SchedulerPolicySource{
  38. ConfigMap: &config.SchedulerPolicyConfigMapSource{
  39. Namespace: "name",
  40. Name: "name",
  41. },
  42. },
  43. },
  44. LeaderElection: config.KubeSchedulerLeaderElectionConfiguration{
  45. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  46. ResourceLock: "configmap",
  47. LeaderElect: true,
  48. LeaseDuration: metav1.Duration{Duration: 30 * time.Second},
  49. RenewDeadline: metav1.Duration{Duration: 15 * time.Second},
  50. RetryPeriod: metav1.Duration{Duration: 5 * time.Second},
  51. ResourceNamespace: "name",
  52. ResourceName: "name",
  53. },
  54. },
  55. PodInitialBackoffSeconds: podInitialBackoffSeconds,
  56. PodMaxBackoffSeconds: podMaxBackoffSeconds,
  57. BindTimeoutSeconds: testTimeout,
  58. PercentageOfNodesToScore: 35,
  59. Profiles: []config.KubeSchedulerProfile{
  60. {
  61. SchedulerName: "me",
  62. Plugins: &config.Plugins{
  63. QueueSort: &config.PluginSet{
  64. Enabled: []config.Plugin{{Name: "CustomSort"}},
  65. },
  66. Score: &config.PluginSet{
  67. Disabled: []config.Plugin{{Name: "*"}},
  68. },
  69. },
  70. },
  71. {
  72. SchedulerName: "other",
  73. Plugins: &config.Plugins{
  74. QueueSort: &config.PluginSet{
  75. Enabled: []config.Plugin{{Name: "CustomSort"}},
  76. },
  77. Bind: &config.PluginSet{
  78. Enabled: []config.Plugin{{Name: "CustomBind"}},
  79. },
  80. },
  81. },
  82. },
  83. }
  84. resourceNameNotSet := validConfig.DeepCopy()
  85. resourceNameNotSet.LeaderElection.ResourceName = ""
  86. resourceNamespaceNotSet := validConfig.DeepCopy()
  87. resourceNamespaceNotSet.LeaderElection.ResourceNamespace = ""
  88. metricsBindAddrHostInvalid := validConfig.DeepCopy()
  89. metricsBindAddrHostInvalid.MetricsBindAddress = "0.0.0.0.0:9090"
  90. metricsBindAddrPortInvalid := validConfig.DeepCopy()
  91. metricsBindAddrPortInvalid.MetricsBindAddress = "0.0.0.0:909090"
  92. healthzBindAddrHostInvalid := validConfig.DeepCopy()
  93. healthzBindAddrHostInvalid.HealthzBindAddress = "0.0.0.0.0:9090"
  94. healthzBindAddrPortInvalid := validConfig.DeepCopy()
  95. healthzBindAddrPortInvalid.HealthzBindAddress = "0.0.0.0:909090"
  96. enableContentProfilingSetWithoutEnableProfiling := validConfig.DeepCopy()
  97. enableContentProfilingSetWithoutEnableProfiling.EnableProfiling = false
  98. enableContentProfilingSetWithoutEnableProfiling.EnableContentionProfiling = true
  99. percentageOfNodesToScore101 := validConfig.DeepCopy()
  100. percentageOfNodesToScore101.PercentageOfNodesToScore = int32(101)
  101. schedulerNameNotSet := validConfig.DeepCopy()
  102. schedulerNameNotSet.Profiles[1].SchedulerName = ""
  103. repeatedSchedulerName := validConfig.DeepCopy()
  104. repeatedSchedulerName.Profiles[0].SchedulerName = "other"
  105. differentQueueSort := validConfig.DeepCopy()
  106. differentQueueSort.Profiles[1].Plugins.QueueSort.Enabled[0].Name = "AnotherSort"
  107. oneEmptyQueueSort := validConfig.DeepCopy()
  108. oneEmptyQueueSort.Profiles[0].Plugins = nil
  109. scenarios := map[string]struct {
  110. expectedToFail bool
  111. config *config.KubeSchedulerConfiguration
  112. }{
  113. "good": {
  114. expectedToFail: false,
  115. config: validConfig,
  116. },
  117. "bad-resource-name-not-set": {
  118. expectedToFail: true,
  119. config: resourceNameNotSet,
  120. },
  121. "bad-resource-namespace-not-set": {
  122. expectedToFail: true,
  123. config: resourceNamespaceNotSet,
  124. },
  125. "bad-healthz-port-invalid": {
  126. expectedToFail: true,
  127. config: healthzBindAddrPortInvalid,
  128. },
  129. "bad-healthz-host-invalid": {
  130. expectedToFail: true,
  131. config: healthzBindAddrHostInvalid,
  132. },
  133. "bad-metrics-port-invalid": {
  134. expectedToFail: true,
  135. config: metricsBindAddrPortInvalid,
  136. },
  137. "bad-metrics-host-invalid": {
  138. expectedToFail: true,
  139. config: metricsBindAddrHostInvalid,
  140. },
  141. "bad-percentage-of-nodes-to-score": {
  142. expectedToFail: true,
  143. config: percentageOfNodesToScore101,
  144. },
  145. "scheduler-name-not-set": {
  146. expectedToFail: true,
  147. config: schedulerNameNotSet,
  148. },
  149. "repeated-scheduler-name": {
  150. expectedToFail: true,
  151. config: repeatedSchedulerName,
  152. },
  153. "different-queue-sort": {
  154. expectedToFail: true,
  155. config: differentQueueSort,
  156. },
  157. "one-empty-queue-sort": {
  158. expectedToFail: true,
  159. config: oneEmptyQueueSort,
  160. },
  161. }
  162. for name, scenario := range scenarios {
  163. t.Run(name, func(t *testing.T) {
  164. errs := ValidateKubeSchedulerConfiguration(scenario.config)
  165. if len(errs) == 0 && scenario.expectedToFail {
  166. t.Error("Unexpected success")
  167. }
  168. if len(errs) > 0 && !scenario.expectedToFail {
  169. t.Errorf("Unexpected failure: %+v", errs)
  170. }
  171. })
  172. }
  173. }
  174. func TestValidatePolicy(t *testing.T) {
  175. tests := []struct {
  176. policy config.Policy
  177. expected error
  178. name string
  179. }{
  180. {
  181. name: "no weight defined in policy",
  182. policy: config.Policy{Priorities: []config.PriorityPolicy{{Name: "NoWeightPriority"}}},
  183. expected: errors.New("Priority NoWeightPriority should have a positive weight applied to it or it has overflown"),
  184. },
  185. {
  186. name: "policy weight is not positive",
  187. policy: config.Policy{Priorities: []config.PriorityPolicy{{Name: "NoWeightPriority", Weight: 0}}},
  188. expected: errors.New("Priority NoWeightPriority should have a positive weight applied to it or it has overflown"),
  189. },
  190. {
  191. name: "valid weight priority",
  192. policy: config.Policy{Priorities: []config.PriorityPolicy{{Name: "WeightPriority", Weight: 2}}},
  193. expected: nil,
  194. },
  195. {
  196. name: "invalid negative weight policy",
  197. policy: config.Policy{Priorities: []config.PriorityPolicy{{Name: "WeightPriority", Weight: -2}}},
  198. expected: errors.New("Priority WeightPriority should have a positive weight applied to it or it has overflown"),
  199. },
  200. {
  201. name: "policy weight exceeds maximum",
  202. policy: config.Policy{Priorities: []config.PriorityPolicy{{Name: "WeightPriority", Weight: config.MaxWeight}}},
  203. expected: errors.New("Priority WeightPriority should have a positive weight applied to it or it has overflown"),
  204. },
  205. {
  206. name: "valid weight in policy extender config",
  207. policy: config.Policy{Extenders: []config.Extender{{URLPrefix: "http://127.0.0.1:8081/extender", PrioritizeVerb: "prioritize", Weight: 2}}},
  208. expected: nil,
  209. },
  210. {
  211. name: "invalid negative weight in policy extender config",
  212. policy: config.Policy{Extenders: []config.Extender{{URLPrefix: "http://127.0.0.1:8081/extender", PrioritizeVerb: "prioritize", Weight: -2}}},
  213. expected: errors.New("Priority for extender http://127.0.0.1:8081/extender should have a positive weight applied to it"),
  214. },
  215. {
  216. name: "valid filter verb and url prefix",
  217. policy: config.Policy{Extenders: []config.Extender{{URLPrefix: "http://127.0.0.1:8081/extender", FilterVerb: "filter"}}},
  218. expected: nil,
  219. },
  220. {
  221. name: "valid preemt verb and urlprefix",
  222. policy: config.Policy{Extenders: []config.Extender{{URLPrefix: "http://127.0.0.1:8081/extender", PreemptVerb: "preempt"}}},
  223. expected: nil,
  224. },
  225. {
  226. name: "invalid multiple extenders",
  227. policy: config.Policy{
  228. Extenders: []config.Extender{
  229. {URLPrefix: "http://127.0.0.1:8081/extender", BindVerb: "bind"},
  230. {URLPrefix: "http://127.0.0.1:8082/extender", BindVerb: "bind"},
  231. }},
  232. expected: errors.New("Only one extender can implement bind, found 2"),
  233. },
  234. {
  235. name: "invalid duplicate extender resource name",
  236. policy: config.Policy{
  237. Extenders: []config.Extender{
  238. {URLPrefix: "http://127.0.0.1:8081/extender", ManagedResources: []config.ExtenderManagedResource{{Name: "foo.com/bar"}}},
  239. {URLPrefix: "http://127.0.0.1:8082/extender", BindVerb: "bind", ManagedResources: []config.ExtenderManagedResource{{Name: "foo.com/bar"}}},
  240. }},
  241. expected: errors.New("Duplicate extender managed resource name foo.com/bar"),
  242. },
  243. {
  244. name: "invalid extended resource name",
  245. policy: config.Policy{
  246. Extenders: []config.Extender{
  247. {URLPrefix: "http://127.0.0.1:8081/extender", ManagedResources: []config.ExtenderManagedResource{{Name: "kubernetes.io/foo"}}},
  248. }},
  249. expected: errors.New("kubernetes.io/foo is an invalid extended resource name"),
  250. },
  251. {
  252. name: "invalid redeclared RequestedToCapacityRatio custom priority",
  253. policy: config.Policy{
  254. Priorities: []config.PriorityPolicy{
  255. {Name: "customPriority1", Weight: 1, Argument: &config.PriorityArgument{RequestedToCapacityRatioArguments: &config.RequestedToCapacityRatioArguments{}}},
  256. {Name: "customPriority2", Weight: 1, Argument: &config.PriorityArgument{RequestedToCapacityRatioArguments: &config.RequestedToCapacityRatioArguments{}}},
  257. },
  258. },
  259. expected: errors.New("Priority \"customPriority2\" redeclares custom priority \"RequestedToCapacityRatio\", from:\"customPriority1\""),
  260. },
  261. {
  262. name: "different weights for LabelPreference custom priority",
  263. policy: config.Policy{
  264. Priorities: []config.PriorityPolicy{
  265. {Name: "customPriority1", Weight: 1, Argument: &config.PriorityArgument{LabelPreference: &config.LabelPreference{}}},
  266. {Name: "customPriority2", Weight: 2, Argument: &config.PriorityArgument{LabelPreference: &config.LabelPreference{}}},
  267. },
  268. },
  269. expected: errors.New("LabelPreference priority \"customPriority2\" has a different weight with \"customPriority1\""),
  270. },
  271. {
  272. name: "different weights for ServiceAntiAffinity custom priority",
  273. policy: config.Policy{
  274. Priorities: []config.PriorityPolicy{
  275. {Name: "customPriority1", Weight: 1, Argument: &config.PriorityArgument{ServiceAntiAffinity: &config.ServiceAntiAffinity{}}},
  276. {Name: "customPriority2", Weight: 2, Argument: &config.PriorityArgument{ServiceAntiAffinity: &config.ServiceAntiAffinity{}}},
  277. },
  278. },
  279. expected: errors.New("ServiceAntiAffinity priority \"customPriority2\" has a different weight with \"customPriority1\""),
  280. },
  281. {
  282. name: "invalid hardPodAffinitySymmetricWeight, above the range",
  283. policy: config.Policy{
  284. HardPodAffinitySymmetricWeight: 101,
  285. },
  286. expected: errors.New("hardPodAffinitySymmetricWeight: Invalid value: 101: not in valid range [0-100]"),
  287. },
  288. {
  289. name: "invalid hardPodAffinitySymmetricWeight, below the range",
  290. policy: config.Policy{
  291. HardPodAffinitySymmetricWeight: -1,
  292. },
  293. expected: errors.New("hardPodAffinitySymmetricWeight: Invalid value: -1: not in valid range [0-100]"),
  294. },
  295. }
  296. for _, test := range tests {
  297. t.Run(test.name, func(t *testing.T) {
  298. actual := ValidatePolicy(test.policy)
  299. if fmt.Sprint(test.expected) != fmt.Sprint(actual) {
  300. t.Errorf("expected: %s, actual: %s", test.expected, actual)
  301. }
  302. })
  303. }
  304. }