defaults_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 v1alpha1
  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. kubeschedulerconfigv1alpha1 "k8s.io/kube-scheduler/config/v1alpha1"
  21. "k8s.io/utils/pointer"
  22. )
  23. func TestSchedulerDefaults(t *testing.T) {
  24. enable := true
  25. tests := []struct {
  26. name string
  27. config *kubeschedulerconfigv1alpha1.KubeSchedulerConfiguration
  28. expected *kubeschedulerconfigv1alpha1.KubeSchedulerConfiguration
  29. }{
  30. {
  31. name: "empty config",
  32. config: &kubeschedulerconfigv1alpha1.KubeSchedulerConfiguration{},
  33. expected: &kubeschedulerconfigv1alpha1.KubeSchedulerConfiguration{
  34. SchedulerName: pointer.StringPtr("default-scheduler"),
  35. AlgorithmSource: kubeschedulerconfigv1alpha1.SchedulerAlgorithmSource{Provider: pointer.StringPtr("DefaultProvider")},
  36. HealthzBindAddress: pointer.StringPtr("0.0.0.0:10251"),
  37. MetricsBindAddress: pointer.StringPtr("0.0.0.0:10251"),
  38. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  39. EnableProfiling: &enable,
  40. EnableContentionProfiling: &enable,
  41. },
  42. LeaderElection: kubeschedulerconfigv1alpha1.KubeSchedulerLeaderElectionConfiguration{
  43. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  44. LeaderElect: pointer.BoolPtr(true),
  45. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  46. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  47. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  48. ResourceLock: "endpointsleases",
  49. ResourceNamespace: "",
  50. ResourceName: "",
  51. },
  52. LockObjectName: "kube-scheduler",
  53. LockObjectNamespace: "kube-system",
  54. },
  55. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  56. QPS: 50,
  57. Burst: 100,
  58. ContentType: "application/vnd.kubernetes.protobuf",
  59. },
  60. DisablePreemption: pointer.BoolPtr(false),
  61. PercentageOfNodesToScore: pointer.Int32Ptr(0),
  62. BindTimeoutSeconds: pointer.Int64Ptr(600),
  63. PodInitialBackoffSeconds: pointer.Int64Ptr(1),
  64. PodMaxBackoffSeconds: pointer.Int64Ptr(10),
  65. Plugins: nil,
  66. },
  67. },
  68. {
  69. name: "custom hard pod affinity",
  70. config: &kubeschedulerconfigv1alpha1.KubeSchedulerConfiguration{
  71. HardPodAffinitySymmetricWeight: pointer.Int32Ptr(42),
  72. },
  73. expected: &kubeschedulerconfigv1alpha1.KubeSchedulerConfiguration{
  74. SchedulerName: pointer.StringPtr("default-scheduler"),
  75. AlgorithmSource: kubeschedulerconfigv1alpha1.SchedulerAlgorithmSource{Provider: pointer.StringPtr("DefaultProvider")},
  76. HardPodAffinitySymmetricWeight: pointer.Int32Ptr(42),
  77. HealthzBindAddress: pointer.StringPtr("0.0.0.0:10251"),
  78. MetricsBindAddress: pointer.StringPtr("0.0.0.0:10251"),
  79. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  80. EnableProfiling: &enable,
  81. EnableContentionProfiling: &enable,
  82. },
  83. LeaderElection: kubeschedulerconfigv1alpha1.KubeSchedulerLeaderElectionConfiguration{
  84. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  85. LeaderElect: pointer.BoolPtr(true),
  86. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  87. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  88. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  89. ResourceLock: "endpointsleases",
  90. ResourceNamespace: "",
  91. ResourceName: "",
  92. },
  93. LockObjectName: "kube-scheduler",
  94. LockObjectNamespace: "kube-system",
  95. },
  96. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  97. QPS: 50,
  98. Burst: 100,
  99. ContentType: "application/vnd.kubernetes.protobuf",
  100. },
  101. DisablePreemption: pointer.BoolPtr(false),
  102. PercentageOfNodesToScore: pointer.Int32Ptr(0),
  103. BindTimeoutSeconds: pointer.Int64Ptr(600),
  104. PodInitialBackoffSeconds: pointer.Int64Ptr(1),
  105. PodMaxBackoffSeconds: pointer.Int64Ptr(10),
  106. Plugins: nil,
  107. },
  108. },
  109. {
  110. name: "metrics and healthz address with no port",
  111. config: &kubeschedulerconfigv1alpha1.KubeSchedulerConfiguration{
  112. MetricsBindAddress: pointer.StringPtr("1.2.3.4"),
  113. HealthzBindAddress: pointer.StringPtr("1.2.3.4"),
  114. },
  115. expected: &kubeschedulerconfigv1alpha1.KubeSchedulerConfiguration{
  116. SchedulerName: pointer.StringPtr("default-scheduler"),
  117. AlgorithmSource: kubeschedulerconfigv1alpha1.SchedulerAlgorithmSource{Provider: pointer.StringPtr("DefaultProvider")},
  118. HealthzBindAddress: pointer.StringPtr("1.2.3.4:10251"),
  119. MetricsBindAddress: pointer.StringPtr("1.2.3.4:10251"),
  120. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  121. EnableProfiling: &enable,
  122. EnableContentionProfiling: &enable,
  123. },
  124. LeaderElection: kubeschedulerconfigv1alpha1.KubeSchedulerLeaderElectionConfiguration{
  125. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  126. LeaderElect: pointer.BoolPtr(true),
  127. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  128. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  129. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  130. ResourceLock: "endpointsleases",
  131. ResourceNamespace: "",
  132. ResourceName: "",
  133. },
  134. LockObjectName: "kube-scheduler",
  135. LockObjectNamespace: "kube-system",
  136. },
  137. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  138. QPS: 50,
  139. Burst: 100,
  140. ContentType: "application/vnd.kubernetes.protobuf",
  141. },
  142. DisablePreemption: pointer.BoolPtr(false),
  143. PercentageOfNodesToScore: pointer.Int32Ptr(0),
  144. BindTimeoutSeconds: pointer.Int64Ptr(600),
  145. PodInitialBackoffSeconds: pointer.Int64Ptr(1),
  146. PodMaxBackoffSeconds: pointer.Int64Ptr(10),
  147. Plugins: nil,
  148. },
  149. },
  150. {
  151. name: "metrics and healthz port with no address",
  152. config: &kubeschedulerconfigv1alpha1.KubeSchedulerConfiguration{
  153. MetricsBindAddress: pointer.StringPtr(":12345"),
  154. HealthzBindAddress: pointer.StringPtr(":12345"),
  155. },
  156. expected: &kubeschedulerconfigv1alpha1.KubeSchedulerConfiguration{
  157. SchedulerName: pointer.StringPtr("default-scheduler"),
  158. AlgorithmSource: kubeschedulerconfigv1alpha1.SchedulerAlgorithmSource{Provider: pointer.StringPtr("DefaultProvider")},
  159. HealthzBindAddress: pointer.StringPtr("0.0.0.0:12345"),
  160. MetricsBindAddress: pointer.StringPtr("0.0.0.0:12345"),
  161. DebuggingConfiguration: componentbaseconfig.DebuggingConfiguration{
  162. EnableProfiling: &enable,
  163. EnableContentionProfiling: &enable,
  164. },
  165. LeaderElection: kubeschedulerconfigv1alpha1.KubeSchedulerLeaderElectionConfiguration{
  166. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  167. LeaderElect: pointer.BoolPtr(true),
  168. LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
  169. RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
  170. RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
  171. ResourceLock: "endpointsleases",
  172. ResourceNamespace: "",
  173. ResourceName: "",
  174. },
  175. LockObjectName: "kube-scheduler",
  176. LockObjectNamespace: "kube-system",
  177. },
  178. ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
  179. QPS: 50,
  180. Burst: 100,
  181. ContentType: "application/vnd.kubernetes.protobuf",
  182. },
  183. DisablePreemption: pointer.BoolPtr(false),
  184. PercentageOfNodesToScore: pointer.Int32Ptr(0),
  185. BindTimeoutSeconds: pointer.Int64Ptr(600),
  186. PodInitialBackoffSeconds: pointer.Int64Ptr(1),
  187. PodMaxBackoffSeconds: pointer.Int64Ptr(10),
  188. Plugins: nil,
  189. },
  190. },
  191. }
  192. for _, tc := range tests {
  193. t.Run(tc.name, func(t *testing.T) {
  194. SetDefaults_KubeSchedulerConfiguration(tc.config)
  195. if diff := cmp.Diff(tc.expected, tc.config); diff != "" {
  196. t.Errorf("wrong defaults set (-want, +got):\n%s", diff)
  197. }
  198. })
  199. }
  200. }