k8s.io/kubernetes@v1.29.3/pkg/apis/apps/v1beta1/defaults.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package v1beta1 18 19 import ( 20 appsv1beta1 "k8s.io/api/apps/v1beta1" 21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 22 "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/apimachinery/pkg/util/intstr" 24 utilfeature "k8s.io/apiserver/pkg/util/feature" 25 "k8s.io/kubernetes/pkg/features" 26 "k8s.io/utils/ptr" 27 ) 28 29 func addDefaultingFuncs(scheme *runtime.Scheme) error { 30 return RegisterDefaults(scheme) 31 } 32 33 func SetDefaults_StatefulSet(obj *appsv1beta1.StatefulSet) { 34 if len(obj.Spec.PodManagementPolicy) == 0 { 35 obj.Spec.PodManagementPolicy = appsv1beta1.OrderedReadyPodManagement 36 } 37 38 if obj.Spec.UpdateStrategy.Type == "" { 39 obj.Spec.UpdateStrategy.Type = appsv1beta1.OnDeleteStatefulSetStrategyType 40 } 41 labels := obj.Spec.Template.Labels 42 if labels != nil { 43 if obj.Spec.Selector == nil { 44 obj.Spec.Selector = &metav1.LabelSelector{ 45 MatchLabels: labels, 46 } 47 } 48 if len(obj.Labels) == 0 { 49 obj.Labels = labels 50 } 51 } 52 53 if utilfeature.DefaultFeatureGate.Enabled(features.StatefulSetAutoDeletePVC) { 54 if obj.Spec.PersistentVolumeClaimRetentionPolicy == nil { 55 obj.Spec.PersistentVolumeClaimRetentionPolicy = &appsv1beta1.StatefulSetPersistentVolumeClaimRetentionPolicy{} 56 } 57 if len(obj.Spec.PersistentVolumeClaimRetentionPolicy.WhenDeleted) == 0 { 58 obj.Spec.PersistentVolumeClaimRetentionPolicy.WhenDeleted = appsv1beta1.RetainPersistentVolumeClaimRetentionPolicyType 59 } 60 if len(obj.Spec.PersistentVolumeClaimRetentionPolicy.WhenScaled) == 0 { 61 obj.Spec.PersistentVolumeClaimRetentionPolicy.WhenScaled = appsv1beta1.RetainPersistentVolumeClaimRetentionPolicyType 62 } 63 } 64 65 if obj.Spec.Replicas == nil { 66 obj.Spec.Replicas = new(int32) 67 *obj.Spec.Replicas = 1 68 } 69 if obj.Spec.RevisionHistoryLimit == nil { 70 obj.Spec.RevisionHistoryLimit = new(int32) 71 *obj.Spec.RevisionHistoryLimit = 10 72 } 73 if obj.Spec.UpdateStrategy.Type == appsv1beta1.RollingUpdateStatefulSetStrategyType && 74 obj.Spec.UpdateStrategy.RollingUpdate != nil { 75 76 if obj.Spec.UpdateStrategy.RollingUpdate.Partition == nil { 77 obj.Spec.UpdateStrategy.RollingUpdate.Partition = ptr.To[int32](0) 78 } 79 if utilfeature.DefaultFeatureGate.Enabled(features.MaxUnavailableStatefulSet) { 80 if obj.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable == nil { 81 obj.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable = ptr.To(intstr.FromInt32(1)) 82 } 83 } 84 } 85 } 86 87 // SetDefaults_Deployment sets additional defaults compared to its counterpart 88 // in extensions. These addons are: 89 // - MaxUnavailable during rolling update set to 25% (1 in extensions) 90 // - MaxSurge value during rolling update set to 25% (1 in extensions) 91 // - RevisionHistoryLimit set to 2 (not set in extensions) 92 // - ProgressDeadlineSeconds set to 600s (not set in extensions) 93 func SetDefaults_Deployment(obj *appsv1beta1.Deployment) { 94 // Default labels and selector to labels from pod template spec. 95 labels := obj.Spec.Template.Labels 96 97 if labels != nil { 98 if obj.Spec.Selector == nil { 99 obj.Spec.Selector = &metav1.LabelSelector{MatchLabels: labels} 100 } 101 if len(obj.Labels) == 0 { 102 obj.Labels = labels 103 } 104 } 105 // Set appsv1beta1.DeploymentSpec.Replicas to 1 if it is not set. 106 if obj.Spec.Replicas == nil { 107 obj.Spec.Replicas = new(int32) 108 *obj.Spec.Replicas = 1 109 } 110 strategy := &obj.Spec.Strategy 111 // Set default appsv1beta1.DeploymentStrategyType as RollingUpdate. 112 if strategy.Type == "" { 113 strategy.Type = appsv1beta1.RollingUpdateDeploymentStrategyType 114 } 115 if strategy.Type == appsv1beta1.RollingUpdateDeploymentStrategyType { 116 if strategy.RollingUpdate == nil { 117 rollingUpdate := appsv1beta1.RollingUpdateDeployment{} 118 strategy.RollingUpdate = &rollingUpdate 119 } 120 if strategy.RollingUpdate.MaxUnavailable == nil { 121 // Set default MaxUnavailable as 25% by default. 122 maxUnavailable := intstr.FromString("25%") 123 strategy.RollingUpdate.MaxUnavailable = &maxUnavailable 124 } 125 if strategy.RollingUpdate.MaxSurge == nil { 126 // Set default MaxSurge as 25% by default. 127 maxSurge := intstr.FromString("25%") 128 strategy.RollingUpdate.MaxSurge = &maxSurge 129 } 130 } 131 if obj.Spec.RevisionHistoryLimit == nil { 132 obj.Spec.RevisionHistoryLimit = new(int32) 133 *obj.Spec.RevisionHistoryLimit = 2 134 } 135 if obj.Spec.ProgressDeadlineSeconds == nil { 136 obj.Spec.ProgressDeadlineSeconds = new(int32) 137 *obj.Spec.ProgressDeadlineSeconds = 600 138 } 139 }