defaults.go 6.0 KB

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