@@ -48,7 +48,64 @@ var _ webhook.Validator = &MachineDeployment{}
4848
4949// Default implements webhook.Defaulter so a webhook will be registered for the type.
5050func (m * MachineDeployment ) Default () {
51- PopulateDefaultsMachineDeployment (m )
51+ if m .Labels == nil {
52+ m .Labels = make (map [string ]string )
53+ }
54+ m .Labels [ClusterNameLabel ] = m .Spec .ClusterName
55+
56+ if m .Spec .MinReadySeconds == nil {
57+ m .Spec .MinReadySeconds = pointer .Int32 (0 )
58+ }
59+
60+ if m .Spec .RevisionHistoryLimit == nil {
61+ m .Spec .RevisionHistoryLimit = pointer .Int32 (1 )
62+ }
63+
64+ if m .Spec .ProgressDeadlineSeconds == nil {
65+ m .Spec .ProgressDeadlineSeconds = pointer .Int32 (600 )
66+ }
67+
68+ if m .Spec .Selector .MatchLabels == nil {
69+ m .Spec .Selector .MatchLabels = make (map [string ]string )
70+ }
71+
72+ if m .Spec .Strategy == nil {
73+ m .Spec .Strategy = & MachineDeploymentStrategy {}
74+ }
75+
76+ if m .Spec .Strategy .Type == "" {
77+ m .Spec .Strategy .Type = RollingUpdateMachineDeploymentStrategyType
78+ }
79+
80+ if m .Spec .Template .Labels == nil {
81+ m .Spec .Template .Labels = make (map [string ]string )
82+ }
83+
84+ // Default RollingUpdate strategy only if strategy type is RollingUpdate.
85+ if m .Spec .Strategy .Type == RollingUpdateMachineDeploymentStrategyType {
86+ if m .Spec .Strategy .RollingUpdate == nil {
87+ m .Spec .Strategy .RollingUpdate = & MachineRollingUpdateDeployment {}
88+ }
89+ if m .Spec .Strategy .RollingUpdate .MaxSurge == nil {
90+ ios1 := intstr .FromInt (1 )
91+ m .Spec .Strategy .RollingUpdate .MaxSurge = & ios1
92+ }
93+ if m .Spec .Strategy .RollingUpdate .MaxUnavailable == nil {
94+ ios0 := intstr .FromInt (0 )
95+ m .Spec .Strategy .RollingUpdate .MaxUnavailable = & ios0
96+ }
97+ }
98+
99+ // If no selector has been provided, add label and selector for the
100+ // MachineDeployment's name as a default way of providing uniqueness.
101+ if len (m .Spec .Selector .MatchLabels ) == 0 && len (m .Spec .Selector .MatchExpressions ) == 0 {
102+ m .Spec .Selector .MatchLabels [MachineDeploymentNameLabel ] = m .Name
103+ m .Spec .Template .Labels [MachineDeploymentNameLabel ] = m .Name
104+ }
105+ // Make sure selector and template to be in the same cluster.
106+ m .Spec .Selector .MatchLabels [ClusterNameLabel ] = m .Spec .ClusterName
107+ m .Spec .Template .Labels [ClusterNameLabel ] = m .Spec .ClusterName
108+
52109 // tolerate version strings without a "v" prefix: prepend it if it's not there
53110 if m .Spec .Template .Spec .Version != nil && ! strings .HasPrefix (* m .Spec .Template .Spec .Version , "v" ) {
54111 normalizedVersion := "v" + * m .Spec .Template .Spec .Version
@@ -156,65 +213,3 @@ func (m *MachineDeployment) validate(old *MachineDeployment) error {
156213
157214 return apierrors .NewInvalid (GroupVersion .WithKind ("MachineDeployment" ).GroupKind (), m .Name , allErrs )
158215}
159-
160- // PopulateDefaultsMachineDeployment fills in default field values.
161- // This is also called during MachineDeployment sync.
162- func PopulateDefaultsMachineDeployment (d * MachineDeployment ) {
163- if d .Labels == nil {
164- d .Labels = make (map [string ]string )
165- }
166- d .Labels [ClusterNameLabel ] = d .Spec .ClusterName
167-
168- if d .Spec .MinReadySeconds == nil {
169- d .Spec .MinReadySeconds = pointer .Int32 (0 )
170- }
171-
172- if d .Spec .RevisionHistoryLimit == nil {
173- d .Spec .RevisionHistoryLimit = pointer .Int32 (1 )
174- }
175-
176- if d .Spec .ProgressDeadlineSeconds == nil {
177- d .Spec .ProgressDeadlineSeconds = pointer .Int32 (600 )
178- }
179-
180- if d .Spec .Selector .MatchLabels == nil {
181- d .Spec .Selector .MatchLabels = make (map [string ]string )
182- }
183-
184- if d .Spec .Strategy == nil {
185- d .Spec .Strategy = & MachineDeploymentStrategy {}
186- }
187-
188- if d .Spec .Strategy .Type == "" {
189- d .Spec .Strategy .Type = RollingUpdateMachineDeploymentStrategyType
190- }
191-
192- if d .Spec .Template .Labels == nil {
193- d .Spec .Template .Labels = make (map [string ]string )
194- }
195-
196- // Default RollingUpdate strategy only if strategy type is RollingUpdate.
197- if d .Spec .Strategy .Type == RollingUpdateMachineDeploymentStrategyType {
198- if d .Spec .Strategy .RollingUpdate == nil {
199- d .Spec .Strategy .RollingUpdate = & MachineRollingUpdateDeployment {}
200- }
201- if d .Spec .Strategy .RollingUpdate .MaxSurge == nil {
202- ios1 := intstr .FromInt (1 )
203- d .Spec .Strategy .RollingUpdate .MaxSurge = & ios1
204- }
205- if d .Spec .Strategy .RollingUpdate .MaxUnavailable == nil {
206- ios0 := intstr .FromInt (0 )
207- d .Spec .Strategy .RollingUpdate .MaxUnavailable = & ios0
208- }
209- }
210-
211- // If no selector has been provided, add label and selector for the
212- // MachineDeployment's name as a default way of providing uniqueness.
213- if len (d .Spec .Selector .MatchLabels ) == 0 && len (d .Spec .Selector .MatchExpressions ) == 0 {
214- d .Spec .Selector .MatchLabels [MachineDeploymentNameLabel ] = d .Name
215- d .Spec .Template .Labels [MachineDeploymentNameLabel ] = d .Name
216- }
217- // Make sure selector and template to be in the same cluster.
218- d .Spec .Selector .MatchLabels [ClusterNameLabel ] = d .Spec .ClusterName
219- d .Spec .Template .Labels [ClusterNameLabel ] = d .Spec .ClusterName
220- }
0 commit comments