defaults.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "net"
  16. "strconv"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. componentbaseconfigv1alpha1 "k8s.io/component-base/config/v1alpha1"
  19. "k8s.io/kube-scheduler/config/v1alpha2"
  20. "k8s.io/kubernetes/pkg/scheduler/apis/config"
  21. // this package shouldn't really depend on other k8s.io/kubernetes code
  22. api "k8s.io/kubernetes/pkg/apis/core"
  23. "k8s.io/kubernetes/pkg/master/ports"
  24. )
  25. func addDefaultingFuncs(scheme *runtime.Scheme) error {
  26. return RegisterDefaults(scheme)
  27. }
  28. // SetDefaults_KubeSchedulerConfiguration sets additional defaults
  29. func SetDefaults_KubeSchedulerConfiguration(obj *v1alpha2.KubeSchedulerConfiguration) {
  30. if obj.SchedulerName == nil {
  31. val := api.DefaultSchedulerName
  32. obj.SchedulerName = &val
  33. }
  34. if obj.HardPodAffinitySymmetricWeight == nil {
  35. val := api.DefaultHardPodAffinitySymmetricWeight
  36. obj.HardPodAffinitySymmetricWeight = &val
  37. }
  38. if obj.AlgorithmSource.Policy == nil &&
  39. (obj.AlgorithmSource.Provider == nil || len(*obj.AlgorithmSource.Provider) == 0) {
  40. val := v1alpha2.SchedulerDefaultProviderName
  41. obj.AlgorithmSource.Provider = &val
  42. }
  43. if policy := obj.AlgorithmSource.Policy; policy != nil {
  44. if policy.ConfigMap != nil && len(policy.ConfigMap.Namespace) == 0 {
  45. obj.AlgorithmSource.Policy.ConfigMap.Namespace = api.NamespaceSystem
  46. }
  47. }
  48. // For Healthz and Metrics bind addresses, we want to check:
  49. // 1. If the value is nil, default to 0.0.0.0 and default scheduler port
  50. // 2. If there is a value set, attempt to split it. If it's just a port (ie, ":1234"), default to 0.0.0.0 with that port
  51. // 3. If splitting the value fails, check if the value is even a valid IP. If so, use that with the default port.
  52. // Otherwise use the default bind address
  53. defaultBindAddress := net.JoinHostPort("0.0.0.0", strconv.Itoa(ports.InsecureSchedulerPort))
  54. if obj.HealthzBindAddress == nil {
  55. obj.HealthzBindAddress = &defaultBindAddress
  56. } else {
  57. if host, port, err := net.SplitHostPort(*obj.HealthzBindAddress); err == nil {
  58. if len(host) == 0 {
  59. host = "0.0.0.0"
  60. }
  61. hostPort := net.JoinHostPort(host, port)
  62. obj.HealthzBindAddress = &hostPort
  63. } else {
  64. // Something went wrong splitting the host/port, could just be a missing port so check if the
  65. // existing value is a valid IP address. If so, use that with the default scheduler port
  66. if host := net.ParseIP(*obj.HealthzBindAddress); host != nil {
  67. hostPort := net.JoinHostPort(*obj.HealthzBindAddress, strconv.Itoa(ports.InsecureSchedulerPort))
  68. obj.HealthzBindAddress = &hostPort
  69. } else {
  70. // TODO: in v1beta1 we should let this error instead of stomping with a default value
  71. obj.HealthzBindAddress = &defaultBindAddress
  72. }
  73. }
  74. }
  75. if obj.MetricsBindAddress == nil {
  76. obj.MetricsBindAddress = &defaultBindAddress
  77. } else {
  78. if host, port, err := net.SplitHostPort(*obj.MetricsBindAddress); err == nil {
  79. if len(host) == 0 {
  80. host = "0.0.0.0"
  81. }
  82. hostPort := net.JoinHostPort(host, port)
  83. obj.MetricsBindAddress = &hostPort
  84. } else {
  85. // Something went wrong splitting the host/port, could just be a missing port so check if the
  86. // existing value is a valid IP address. If so, use that with the default scheduler port
  87. if host := net.ParseIP(*obj.MetricsBindAddress); host != nil {
  88. hostPort := net.JoinHostPort(*obj.MetricsBindAddress, strconv.Itoa(ports.InsecureSchedulerPort))
  89. obj.MetricsBindAddress = &hostPort
  90. } else {
  91. // TODO: in v1beta1 we should let this error instead of stomping with a default value
  92. obj.MetricsBindAddress = &defaultBindAddress
  93. }
  94. }
  95. }
  96. if obj.DisablePreemption == nil {
  97. disablePreemption := false
  98. obj.DisablePreemption = &disablePreemption
  99. }
  100. if obj.PercentageOfNodesToScore == nil {
  101. percentageOfNodesToScore := int32(config.DefaultPercentageOfNodesToScore)
  102. obj.PercentageOfNodesToScore = &percentageOfNodesToScore
  103. }
  104. if len(obj.LeaderElection.ResourceLock) == 0 {
  105. obj.LeaderElection.ResourceLock = "endpointsleases"
  106. }
  107. if len(obj.LeaderElection.ResourceNamespace) == 0 {
  108. obj.LeaderElection.ResourceNamespace = v1alpha2.SchedulerDefaultLockObjectNamespace
  109. }
  110. if len(obj.LeaderElection.ResourceName) == 0 {
  111. obj.LeaderElection.ResourceName = v1alpha2.SchedulerDefaultLockObjectName
  112. }
  113. if len(obj.ClientConnection.ContentType) == 0 {
  114. obj.ClientConnection.ContentType = "application/vnd.kubernetes.protobuf"
  115. }
  116. // Scheduler has an opinion about QPS/Burst, setting specific defaults for itself, instead of generic settings.
  117. if obj.ClientConnection.QPS == 0.0 {
  118. obj.ClientConnection.QPS = 50.0
  119. }
  120. if obj.ClientConnection.Burst == 0 {
  121. obj.ClientConnection.Burst = 100
  122. }
  123. // Use the default LeaderElectionConfiguration options
  124. componentbaseconfigv1alpha1.RecommendedDefaultLeaderElectionConfiguration(&obj.LeaderElection.LeaderElectionConfiguration)
  125. if obj.BindTimeoutSeconds == nil {
  126. val := int64(600)
  127. obj.BindTimeoutSeconds = &val
  128. }
  129. if obj.PodInitialBackoffSeconds == nil {
  130. val := int64(1)
  131. obj.PodInitialBackoffSeconds = &val
  132. }
  133. if obj.PodMaxBackoffSeconds == nil {
  134. val := int64(10)
  135. obj.PodMaxBackoffSeconds = &val
  136. }
  137. // Enable profiling by default in the scheduler
  138. if obj.EnableProfiling == nil {
  139. enableProfiling := true
  140. obj.EnableProfiling = &enableProfiling
  141. }
  142. // Enable contention profiling by default if profiling is enabled
  143. if *obj.EnableProfiling && obj.EnableContentionProfiling == nil {
  144. enableContentionProfiling := true
  145. obj.EnableContentionProfiling = &enableContentionProfiling
  146. }
  147. }