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