defaults.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright 2015 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 v1beta1
  14. import (
  15. "k8s.io/kubernetes/pkg/api/v1"
  16. "k8s.io/kubernetes/pkg/runtime"
  17. "k8s.io/kubernetes/pkg/util/intstr"
  18. )
  19. func addDefaultingFuncs(scheme *runtime.Scheme) error {
  20. return scheme.AddDefaultingFuncs(
  21. SetDefaults_DaemonSet,
  22. SetDefaults_Deployment,
  23. SetDefaults_Job,
  24. SetDefaults_HorizontalPodAutoscaler,
  25. SetDefaults_ReplicaSet,
  26. SetDefaults_NetworkPolicy,
  27. )
  28. }
  29. func SetDefaults_DaemonSet(obj *DaemonSet) {
  30. labels := obj.Spec.Template.Labels
  31. // TODO: support templates defined elsewhere when we support them in the API
  32. if labels != nil {
  33. if obj.Spec.Selector == nil {
  34. obj.Spec.Selector = &LabelSelector{
  35. MatchLabels: labels,
  36. }
  37. }
  38. if len(obj.Labels) == 0 {
  39. obj.Labels = labels
  40. }
  41. }
  42. }
  43. func SetDefaults_Deployment(obj *Deployment) {
  44. // Default labels and selector to labels from pod template spec.
  45. labels := obj.Spec.Template.Labels
  46. if labels != nil {
  47. if obj.Spec.Selector == nil {
  48. obj.Spec.Selector = &LabelSelector{MatchLabels: labels}
  49. }
  50. if len(obj.Labels) == 0 {
  51. obj.Labels = labels
  52. }
  53. }
  54. // Set DeploymentSpec.Replicas to 1 if it is not set.
  55. if obj.Spec.Replicas == nil {
  56. obj.Spec.Replicas = new(int32)
  57. *obj.Spec.Replicas = 1
  58. }
  59. strategy := &obj.Spec.Strategy
  60. // Set default DeploymentStrategyType as RollingUpdate.
  61. if strategy.Type == "" {
  62. strategy.Type = RollingUpdateDeploymentStrategyType
  63. }
  64. if strategy.Type == RollingUpdateDeploymentStrategyType {
  65. if strategy.RollingUpdate == nil {
  66. rollingUpdate := RollingUpdateDeployment{}
  67. strategy.RollingUpdate = &rollingUpdate
  68. }
  69. if strategy.RollingUpdate.MaxUnavailable == nil {
  70. // Set default MaxUnavailable as 1 by default.
  71. maxUnavailable := intstr.FromInt(1)
  72. strategy.RollingUpdate.MaxUnavailable = &maxUnavailable
  73. }
  74. if strategy.RollingUpdate.MaxSurge == nil {
  75. // Set default MaxSurge as 1 by default.
  76. maxSurge := intstr.FromInt(1)
  77. strategy.RollingUpdate.MaxSurge = &maxSurge
  78. }
  79. }
  80. }
  81. func SetDefaults_Job(obj *Job) {
  82. labels := obj.Spec.Template.Labels
  83. // TODO: support templates defined elsewhere when we support them in the API
  84. if labels != nil {
  85. // if an autoselector is requested, we'll build the selector later with controller-uid and job-name
  86. autoSelector := bool(obj.Spec.AutoSelector != nil && *obj.Spec.AutoSelector)
  87. // otherwise, we are using a manual selector
  88. manualSelector := !autoSelector
  89. // and default behavior for an unspecified manual selector is to use the pod template labels
  90. if manualSelector && obj.Spec.Selector == nil {
  91. obj.Spec.Selector = &LabelSelector{
  92. MatchLabels: labels,
  93. }
  94. }
  95. if len(obj.Labels) == 0 {
  96. obj.Labels = labels
  97. }
  98. }
  99. // For a non-parallel job, you can leave both `.spec.completions` and
  100. // `.spec.parallelism` unset. When both are unset, both are defaulted to 1.
  101. if obj.Spec.Completions == nil && obj.Spec.Parallelism == nil {
  102. obj.Spec.Completions = new(int32)
  103. *obj.Spec.Completions = 1
  104. obj.Spec.Parallelism = new(int32)
  105. *obj.Spec.Parallelism = 1
  106. }
  107. if obj.Spec.Parallelism == nil {
  108. obj.Spec.Parallelism = new(int32)
  109. *obj.Spec.Parallelism = 1
  110. }
  111. }
  112. func SetDefaults_HorizontalPodAutoscaler(obj *HorizontalPodAutoscaler) {
  113. if obj.Spec.MinReplicas == nil {
  114. minReplicas := int32(1)
  115. obj.Spec.MinReplicas = &minReplicas
  116. }
  117. if obj.Spec.CPUUtilization == nil {
  118. obj.Spec.CPUUtilization = &CPUTargetUtilization{TargetPercentage: 80}
  119. }
  120. }
  121. func SetDefaults_ReplicaSet(obj *ReplicaSet) {
  122. labels := obj.Spec.Template.Labels
  123. // TODO: support templates defined elsewhere when we support them in the API
  124. if labels != nil {
  125. if obj.Spec.Selector == nil {
  126. obj.Spec.Selector = &LabelSelector{
  127. MatchLabels: labels,
  128. }
  129. }
  130. if len(obj.Labels) == 0 {
  131. obj.Labels = labels
  132. }
  133. }
  134. if obj.Spec.Replicas == nil {
  135. obj.Spec.Replicas = new(int32)
  136. *obj.Spec.Replicas = 1
  137. }
  138. }
  139. func SetDefaults_NetworkPolicy(obj *NetworkPolicy) {
  140. // Default any undefined Protocol fields to TCP.
  141. for _, i := range obj.Spec.Ingress {
  142. // TODO: Update Ports to be a pointer to slice as soon as auto-generation supports it.
  143. for _, p := range i.Ports {
  144. if p.Protocol == nil {
  145. proto := v1.ProtocolTCP
  146. p.Protocol = &proto
  147. }
  148. }
  149. }
  150. }