defaults.go 3.8 KB

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