defaults_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. Copyright 2020 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 v1alpha2
  14. import (
  15. "testing"
  16. "time"
  17. "github.com/google/go-cmp/cmp"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. componentbaseconfig "k8s.io/component-base/config/v1alpha1"
  20. "k8s.io/kube-scheduler/config/v1alpha2"
  21. "k8s.io/utils/pointer"
  22. )
  23. func TestSchedulerDefaults(t *testing.T) {
  24. enable := true
  25. tests := []struct {
  26. name string
  27. config *v1alpha2.KubeSchedulerConfiguration
  28. expected *v1alpha2.KubeSchedulerConfiguration
  29. }{
  30. {
  31. name: "empty config",
  32. config: &v1alpha2.KubeSchedulerConfiguration{},
  33. expected: &v1alpha2.KubeSchedulerConfiguration{
  34. AlgorithmSource: v1alpha2.SchedulerAlgorithmSource{Provider: pointer.StringPtr("DefaultProvider")},
  35. HealthzBindAddress: pointer.StringPtr("0.0.0.0:10251"),
  36. MetricsBindAddress: pointer.StringPtr("0.0.0.0:10251"),
  37. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  38. EnableProfiling: &enable,
  39. EnableContentionProfiling: &enable,
  40. },
  41. LeaderElection: v1alpha2.KubeSchedulerLeaderElectionConfiguration{
  42. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  43. LeaderElect: pointer.BoolPtr(true),
  44. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  45. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  46. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  47. ResourceLock: "endpointsleases",
  48. ResourceNamespace: "kube-system",
  49. ResourceName: "kube-scheduler",
  50. },
  51. },
  52. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  53. QPS: 50,
  54. Burst: 100,
  55. ContentType: "application/vnd.kubernetes.protobuf",
  56. },
  57. DisablePreemption: pointer.BoolPtr(false),
  58. PercentageOfNodesToScore: pointer.Int32Ptr(0),
  59. BindTimeoutSeconds: pointer.Int64Ptr(600),
  60. PodInitialBackoffSeconds: pointer.Int64Ptr(1),
  61. PodMaxBackoffSeconds: pointer.Int64Ptr(10),
  62. Profiles: []v1alpha2.KubeSchedulerProfile{
  63. {SchedulerName: pointer.StringPtr("default-scheduler")},
  64. },
  65. },
  66. },
  67. {
  68. name: "no scheduler name",
  69. config: &v1alpha2.KubeSchedulerConfiguration{
  70. Profiles: []v1alpha2.KubeSchedulerProfile{
  71. {
  72. PluginConfig: []v1alpha2.PluginConfig{
  73. {Name: "FooPlugin"},
  74. },
  75. },
  76. },
  77. },
  78. expected: &v1alpha2.KubeSchedulerConfiguration{
  79. AlgorithmSource: v1alpha2.SchedulerAlgorithmSource{Provider: pointer.StringPtr("DefaultProvider")},
  80. HealthzBindAddress: pointer.StringPtr("0.0.0.0:10251"),
  81. MetricsBindAddress: pointer.StringPtr("0.0.0.0:10251"),
  82. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  83. EnableProfiling: &enable,
  84. EnableContentionProfiling: &enable,
  85. },
  86. LeaderElection: v1alpha2.KubeSchedulerLeaderElectionConfiguration{
  87. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  88. LeaderElect: pointer.BoolPtr(true),
  89. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  90. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  91. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  92. ResourceLock: "endpointsleases",
  93. ResourceNamespace: "kube-system",
  94. ResourceName: "kube-scheduler",
  95. },
  96. },
  97. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  98. QPS: 50,
  99. Burst: 100,
  100. ContentType: "application/vnd.kubernetes.protobuf",
  101. },
  102. DisablePreemption: pointer.BoolPtr(false),
  103. PercentageOfNodesToScore: pointer.Int32Ptr(0),
  104. BindTimeoutSeconds: pointer.Int64Ptr(600),
  105. PodInitialBackoffSeconds: pointer.Int64Ptr(1),
  106. PodMaxBackoffSeconds: pointer.Int64Ptr(10),
  107. Profiles: []v1alpha2.KubeSchedulerProfile{
  108. {
  109. SchedulerName: pointer.StringPtr("default-scheduler"),
  110. PluginConfig: []v1alpha2.PluginConfig{
  111. {Name: "FooPlugin"},
  112. },
  113. },
  114. },
  115. },
  116. },
  117. {
  118. name: "two profiles",
  119. config: &v1alpha2.KubeSchedulerConfiguration{
  120. Profiles: []v1alpha2.KubeSchedulerProfile{
  121. {
  122. PluginConfig: []v1alpha2.PluginConfig{
  123. {Name: "FooPlugin"},
  124. },
  125. },
  126. {
  127. SchedulerName: pointer.StringPtr("custom-scheduler"),
  128. Plugins: &v1alpha2.Plugins{
  129. Bind: &v1alpha2.PluginSet{
  130. Enabled: []v1alpha2.Plugin{
  131. {Name: "BarPlugin"},
  132. },
  133. },
  134. },
  135. },
  136. },
  137. },
  138. expected: &v1alpha2.KubeSchedulerConfiguration{
  139. AlgorithmSource: v1alpha2.SchedulerAlgorithmSource{Provider: pointer.StringPtr("DefaultProvider")},
  140. HealthzBindAddress: pointer.StringPtr("0.0.0.0:10251"),
  141. MetricsBindAddress: pointer.StringPtr("0.0.0.0:10251"),
  142. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  143. EnableProfiling: &enable,
  144. EnableContentionProfiling: &enable,
  145. },
  146. LeaderElection: v1alpha2.KubeSchedulerLeaderElectionConfiguration{
  147. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  148. LeaderElect: pointer.BoolPtr(true),
  149. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  150. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  151. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  152. ResourceLock: "endpointsleases",
  153. ResourceNamespace: "kube-system",
  154. ResourceName: "kube-scheduler",
  155. },
  156. },
  157. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  158. QPS: 50,
  159. Burst: 100,
  160. ContentType: "application/vnd.kubernetes.protobuf",
  161. },
  162. DisablePreemption: pointer.BoolPtr(false),
  163. PercentageOfNodesToScore: pointer.Int32Ptr(0),
  164. BindTimeoutSeconds: pointer.Int64Ptr(600),
  165. PodInitialBackoffSeconds: pointer.Int64Ptr(1),
  166. PodMaxBackoffSeconds: pointer.Int64Ptr(10),
  167. Profiles: []v1alpha2.KubeSchedulerProfile{
  168. {
  169. PluginConfig: []v1alpha2.PluginConfig{
  170. {Name: "FooPlugin"},
  171. },
  172. },
  173. {
  174. SchedulerName: pointer.StringPtr("custom-scheduler"),
  175. Plugins: &v1alpha2.Plugins{
  176. Bind: &v1alpha2.PluginSet{
  177. Enabled: []v1alpha2.Plugin{
  178. {Name: "BarPlugin"},
  179. },
  180. },
  181. },
  182. },
  183. },
  184. },
  185. },
  186. {
  187. name: "metrics and healthz address with no port",
  188. config: &v1alpha2.KubeSchedulerConfiguration{
  189. MetricsBindAddress: pointer.StringPtr("1.2.3.4"),
  190. HealthzBindAddress: pointer.StringPtr("1.2.3.4"),
  191. },
  192. expected: &v1alpha2.KubeSchedulerConfiguration{
  193. AlgorithmSource: v1alpha2.SchedulerAlgorithmSource{Provider: pointer.StringPtr("DefaultProvider")},
  194. HealthzBindAddress: pointer.StringPtr("1.2.3.4:10251"),
  195. MetricsBindAddress: pointer.StringPtr("1.2.3.4:10251"),
  196. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  197. EnableProfiling: &enable,
  198. EnableContentionProfiling: &enable,
  199. },
  200. LeaderElection: v1alpha2.KubeSchedulerLeaderElectionConfiguration{
  201. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  202. LeaderElect: pointer.BoolPtr(true),
  203. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  204. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  205. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  206. ResourceLock: "endpointsleases",
  207. ResourceNamespace: "kube-system",
  208. ResourceName: "kube-scheduler",
  209. },
  210. },
  211. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  212. QPS: 50,
  213. Burst: 100,
  214. ContentType: "application/vnd.kubernetes.protobuf",
  215. },
  216. DisablePreemption: pointer.BoolPtr(false),
  217. PercentageOfNodesToScore: pointer.Int32Ptr(0),
  218. BindTimeoutSeconds: pointer.Int64Ptr(600),
  219. PodInitialBackoffSeconds: pointer.Int64Ptr(1),
  220. PodMaxBackoffSeconds: pointer.Int64Ptr(10),
  221. Profiles: []v1alpha2.KubeSchedulerProfile{
  222. {SchedulerName: pointer.StringPtr("default-scheduler")},
  223. },
  224. },
  225. },
  226. {
  227. name: "metrics and healthz port with no address",
  228. config: &v1alpha2.KubeSchedulerConfiguration{
  229. MetricsBindAddress: pointer.StringPtr(":12345"),
  230. HealthzBindAddress: pointer.StringPtr(":12345"),
  231. },
  232. expected: &v1alpha2.KubeSchedulerConfiguration{
  233. AlgorithmSource: v1alpha2.SchedulerAlgorithmSource{Provider: pointer.StringPtr("DefaultProvider")},
  234. HealthzBindAddress: pointer.StringPtr("0.0.0.0:12345"),
  235. MetricsBindAddress: pointer.StringPtr("0.0.0.0:12345"),
  236. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  237. EnableProfiling: &enable,
  238. EnableContentionProfiling: &enable,
  239. },
  240. LeaderElection: v1alpha2.KubeSchedulerLeaderElectionConfiguration{
  241. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  242. LeaderElect: pointer.BoolPtr(true),
  243. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  244. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  245. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  246. ResourceLock: "endpointsleases",
  247. ResourceNamespace: "kube-system",
  248. ResourceName: "kube-scheduler",
  249. },
  250. },
  251. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  252. QPS: 50,
  253. Burst: 100,
  254. ContentType: "application/vnd.kubernetes.protobuf",
  255. },
  256. DisablePreemption: pointer.BoolPtr(false),
  257. PercentageOfNodesToScore: pointer.Int32Ptr(0),
  258. BindTimeoutSeconds: pointer.Int64Ptr(600),
  259. PodInitialBackoffSeconds: pointer.Int64Ptr(1),
  260. PodMaxBackoffSeconds: pointer.Int64Ptr(10),
  261. Profiles: []v1alpha2.KubeSchedulerProfile{
  262. {SchedulerName: pointer.StringPtr("default-scheduler")},
  263. },
  264. },
  265. },
  266. }
  267. for _, tc := range tests {
  268. t.Run(tc.name, func(t *testing.T) {
  269. SetDefaults_KubeSchedulerConfiguration(tc.config)
  270. if diff := cmp.Diff(tc.expected, tc.config); diff != "" {
  271. t.Errorf("Got unexpected defaults (-want, +got):\n%s", diff)
  272. }
  273. })
  274. }
  275. }