defaults.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright 2016 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 v1beta2
  14. import (
  15. appsv1beta2 "k8s.io/api/apps/v1beta2"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "k8s.io/apimachinery/pkg/util/intstr"
  18. )
  19. func addDefaultingFuncs(scheme *runtime.Scheme) error {
  20. return RegisterDefaults(scheme)
  21. }
  22. func SetDefaults_DaemonSet(obj *appsv1beta2.DaemonSet) {
  23. updateStrategy := &obj.Spec.UpdateStrategy
  24. if updateStrategy.Type == "" {
  25. updateStrategy.Type = appsv1beta2.RollingUpdateDaemonSetStrategyType
  26. }
  27. if updateStrategy.Type == appsv1beta2.RollingUpdateDaemonSetStrategyType {
  28. if updateStrategy.RollingUpdate == nil {
  29. rollingUpdate := appsv1beta2.RollingUpdateDaemonSet{}
  30. updateStrategy.RollingUpdate = &rollingUpdate
  31. }
  32. if updateStrategy.RollingUpdate.MaxUnavailable == nil {
  33. // Set default MaxUnavailable as 1 by default.
  34. maxUnavailable := intstr.FromInt(1)
  35. updateStrategy.RollingUpdate.MaxUnavailable = &maxUnavailable
  36. }
  37. }
  38. if obj.Spec.RevisionHistoryLimit == nil {
  39. obj.Spec.RevisionHistoryLimit = new(int32)
  40. *obj.Spec.RevisionHistoryLimit = 10
  41. }
  42. }
  43. func SetDefaults_StatefulSet(obj *appsv1beta2.StatefulSet) {
  44. if len(obj.Spec.PodManagementPolicy) == 0 {
  45. obj.Spec.PodManagementPolicy = appsv1beta2.OrderedReadyPodManagement
  46. }
  47. if obj.Spec.UpdateStrategy.Type == "" {
  48. obj.Spec.UpdateStrategy.Type = appsv1beta2.RollingUpdateStatefulSetStrategyType
  49. // UpdateStrategy.RollingUpdate will take default values below.
  50. obj.Spec.UpdateStrategy.RollingUpdate = &appsv1beta2.RollingUpdateStatefulSetStrategy{}
  51. }
  52. if obj.Spec.UpdateStrategy.Type == appsv1beta2.RollingUpdateStatefulSetStrategyType &&
  53. obj.Spec.UpdateStrategy.RollingUpdate != nil &&
  54. obj.Spec.UpdateStrategy.RollingUpdate.Partition == nil {
  55. obj.Spec.UpdateStrategy.RollingUpdate.Partition = new(int32)
  56. *obj.Spec.UpdateStrategy.RollingUpdate.Partition = 0
  57. }
  58. if obj.Spec.Replicas == nil {
  59. obj.Spec.Replicas = new(int32)
  60. *obj.Spec.Replicas = 1
  61. }
  62. if obj.Spec.RevisionHistoryLimit == nil {
  63. obj.Spec.RevisionHistoryLimit = new(int32)
  64. *obj.Spec.RevisionHistoryLimit = 10
  65. }
  66. }
  67. // SetDefaults_Deployment sets additional defaults compared to its counterpart
  68. // in extensions. These addons are:
  69. // - MaxUnavailable during rolling update set to 25% (1 in extensions)
  70. // - MaxSurge value during rolling update set to 25% (1 in extensions)
  71. // - RevisionHistoryLimit set to 10 (not set in extensions)
  72. // - ProgressDeadlineSeconds set to 600s (not set in extensions)
  73. func SetDefaults_Deployment(obj *appsv1beta2.Deployment) {
  74. // Set appsv1beta2.DeploymentSpec.Replicas to 1 if it is not set.
  75. if obj.Spec.Replicas == nil {
  76. obj.Spec.Replicas = new(int32)
  77. *obj.Spec.Replicas = 1
  78. }
  79. strategy := &obj.Spec.Strategy
  80. // Set default appsv1beta2.DeploymentStrategyType as RollingUpdate.
  81. if strategy.Type == "" {
  82. strategy.Type = appsv1beta2.RollingUpdateDeploymentStrategyType
  83. }
  84. if strategy.Type == appsv1beta2.RollingUpdateDeploymentStrategyType {
  85. if strategy.RollingUpdate == nil {
  86. rollingUpdate := appsv1beta2.RollingUpdateDeployment{}
  87. strategy.RollingUpdate = &rollingUpdate
  88. }
  89. if strategy.RollingUpdate.MaxUnavailable == nil {
  90. // Set default MaxUnavailable as 25% by default.
  91. maxUnavailable := intstr.FromString("25%")
  92. strategy.RollingUpdate.MaxUnavailable = &maxUnavailable
  93. }
  94. if strategy.RollingUpdate.MaxSurge == nil {
  95. // Set default MaxSurge as 25% by default.
  96. maxSurge := intstr.FromString("25%")
  97. strategy.RollingUpdate.MaxSurge = &maxSurge
  98. }
  99. }
  100. if obj.Spec.RevisionHistoryLimit == nil {
  101. obj.Spec.RevisionHistoryLimit = new(int32)
  102. *obj.Spec.RevisionHistoryLimit = 10
  103. }
  104. if obj.Spec.ProgressDeadlineSeconds == nil {
  105. obj.Spec.ProgressDeadlineSeconds = new(int32)
  106. *obj.Spec.ProgressDeadlineSeconds = 600
  107. }
  108. }
  109. func SetDefaults_ReplicaSet(obj *appsv1beta2.ReplicaSet) {
  110. if obj.Spec.Replicas == nil {
  111. obj.Spec.Replicas = new(int32)
  112. *obj.Spec.Replicas = 1
  113. }
  114. }