k8s.io/kubernetes@v1.29.3/pkg/apis/apps/v1/defaults.go (about) 1 /* 2 Copyright 2017 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 v1 18 19 import ( 20 appsv1 "k8s.io/api/apps/v1" 21 "k8s.io/apimachinery/pkg/runtime" 22 "k8s.io/apimachinery/pkg/util/intstr" 23 utilfeature "k8s.io/apiserver/pkg/util/feature" 24 "k8s.io/kubernetes/pkg/features" 25 "k8s.io/utils/ptr" 26 ) 27 28 func addDefaultingFuncs(scheme *runtime.Scheme) error { 29 return RegisterDefaults(scheme) 30 } 31 32 // SetDefaults_Deployment sets additional defaults compared to its counterpart 33 // in extensions. These addons are: 34 // - MaxUnavailable during rolling update set to 25% (1 in extensions) 35 // - MaxSurge value during rolling update set to 25% (1 in extensions) 36 // - RevisionHistoryLimit set to 10 (not set in extensions) 37 // - ProgressDeadlineSeconds set to 600s (not set in extensions) 38 func SetDefaults_Deployment(obj *appsv1.Deployment) { 39 // Set DeploymentSpec.Replicas to 1 if it is not set. 40 if obj.Spec.Replicas == nil { 41 obj.Spec.Replicas = new(int32) 42 *obj.Spec.Replicas = 1 43 } 44 strategy := &obj.Spec.Strategy 45 // Set default DeploymentStrategyType as RollingUpdate. 46 if strategy.Type == "" { 47 strategy.Type = appsv1.RollingUpdateDeploymentStrategyType 48 } 49 if strategy.Type == appsv1.RollingUpdateDeploymentStrategyType { 50 if strategy.RollingUpdate == nil { 51 rollingUpdate := appsv1.RollingUpdateDeployment{} 52 strategy.RollingUpdate = &rollingUpdate 53 } 54 if strategy.RollingUpdate.MaxUnavailable == nil { 55 // Set default MaxUnavailable as 25% by default. 56 maxUnavailable := intstr.FromString("25%") 57 strategy.RollingUpdate.MaxUnavailable = &maxUnavailable 58 } 59 if strategy.RollingUpdate.MaxSurge == nil { 60 // Set default MaxSurge as 25% by default. 61 maxSurge := intstr.FromString("25%") 62 strategy.RollingUpdate.MaxSurge = &maxSurge 63 } 64 } 65 if obj.Spec.RevisionHistoryLimit == nil { 66 obj.Spec.RevisionHistoryLimit = new(int32) 67 *obj.Spec.RevisionHistoryLimit = 10 68 } 69 if obj.Spec.ProgressDeadlineSeconds == nil { 70 obj.Spec.ProgressDeadlineSeconds = new(int32) 71 *obj.Spec.ProgressDeadlineSeconds = 600 72 } 73 } 74 75 func SetDefaults_DaemonSet(obj *appsv1.DaemonSet) { 76 updateStrategy := &obj.Spec.UpdateStrategy 77 if updateStrategy.Type == "" { 78 updateStrategy.Type = appsv1.RollingUpdateDaemonSetStrategyType 79 } 80 if updateStrategy.Type == appsv1.RollingUpdateDaemonSetStrategyType { 81 if updateStrategy.RollingUpdate == nil { 82 rollingUpdate := appsv1.RollingUpdateDaemonSet{} 83 updateStrategy.RollingUpdate = &rollingUpdate 84 } 85 if updateStrategy.RollingUpdate.MaxUnavailable == nil { 86 // Set default MaxUnavailable as 1 by default. 87 updateStrategy.RollingUpdate.MaxUnavailable = ptr.To(intstr.FromInt32(1)) 88 } 89 if updateStrategy.RollingUpdate.MaxSurge == nil { 90 // Set default MaxSurge as 0 by default. 91 updateStrategy.RollingUpdate.MaxSurge = ptr.To(intstr.FromInt32(0)) 92 } 93 } 94 if obj.Spec.RevisionHistoryLimit == nil { 95 obj.Spec.RevisionHistoryLimit = new(int32) 96 *obj.Spec.RevisionHistoryLimit = 10 97 } 98 } 99 100 func SetDefaults_StatefulSet(obj *appsv1.StatefulSet) { 101 if len(obj.Spec.PodManagementPolicy) == 0 { 102 obj.Spec.PodManagementPolicy = appsv1.OrderedReadyPodManagement 103 } 104 105 if obj.Spec.UpdateStrategy.Type == "" { 106 obj.Spec.UpdateStrategy.Type = appsv1.RollingUpdateStatefulSetStrategyType 107 108 if obj.Spec.UpdateStrategy.RollingUpdate == nil { 109 // UpdateStrategy.RollingUpdate will take default values below. 110 obj.Spec.UpdateStrategy.RollingUpdate = &appsv1.RollingUpdateStatefulSetStrategy{} 111 } 112 } 113 114 if obj.Spec.UpdateStrategy.Type == appsv1.RollingUpdateStatefulSetStrategyType && 115 obj.Spec.UpdateStrategy.RollingUpdate != nil { 116 117 if obj.Spec.UpdateStrategy.RollingUpdate.Partition == nil { 118 obj.Spec.UpdateStrategy.RollingUpdate.Partition = ptr.To[int32](0) 119 } 120 if utilfeature.DefaultFeatureGate.Enabled(features.MaxUnavailableStatefulSet) { 121 if obj.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable == nil { 122 obj.Spec.UpdateStrategy.RollingUpdate.MaxUnavailable = ptr.To(intstr.FromInt32(1)) 123 } 124 } 125 } 126 127 if utilfeature.DefaultFeatureGate.Enabled(features.StatefulSetAutoDeletePVC) { 128 if obj.Spec.PersistentVolumeClaimRetentionPolicy == nil { 129 obj.Spec.PersistentVolumeClaimRetentionPolicy = &appsv1.StatefulSetPersistentVolumeClaimRetentionPolicy{} 130 } 131 if len(obj.Spec.PersistentVolumeClaimRetentionPolicy.WhenDeleted) == 0 { 132 obj.Spec.PersistentVolumeClaimRetentionPolicy.WhenDeleted = appsv1.RetainPersistentVolumeClaimRetentionPolicyType 133 } 134 if len(obj.Spec.PersistentVolumeClaimRetentionPolicy.WhenScaled) == 0 { 135 obj.Spec.PersistentVolumeClaimRetentionPolicy.WhenScaled = appsv1.RetainPersistentVolumeClaimRetentionPolicyType 136 } 137 } 138 139 if obj.Spec.Replicas == nil { 140 obj.Spec.Replicas = new(int32) 141 *obj.Spec.Replicas = 1 142 } 143 if obj.Spec.RevisionHistoryLimit == nil { 144 obj.Spec.RevisionHistoryLimit = new(int32) 145 *obj.Spec.RevisionHistoryLimit = 10 146 } 147 } 148 func SetDefaults_ReplicaSet(obj *appsv1.ReplicaSet) { 149 if obj.Spec.Replicas == nil { 150 obj.Spec.Replicas = new(int32) 151 *obj.Spec.Replicas = 1 152 } 153 }