defaults.go 3.1 KB

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