Skip to content

Commit 8b24810

Browse files
authored
Merge pull request #1811 from CecileRobertMichon/re-add-ssh-defaults
Re-add defaulting for AzureMachineTemplate ssh key
2 parents 2955024 + 7ca2e24 commit 8b24810

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

api/v1beta1/azuremachinetemplate_webhook.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ func (r *AzureMachineTemplate) ValidateUpdate(oldRaw runtime.Object) error {
6969
// This means if the old object was in v1alpha3, it would not get the new defaults set in v1beta1 resulting
7070
// in object inequality. To workaround this, we set the v1beta1 defaults here so that the old object also gets
7171
// the new defaults.
72+
73+
// We need to set ssh key explicitly, otherwise Default() will create a new one.
74+
if old.Spec.Template.Spec.SSHPublicKey == "" {
75+
old.Spec.Template.Spec.SSHPublicKey = r.Spec.Template.Spec.SSHPublicKey
76+
}
77+
7278
old.Default()
7379

7480
// if it's still not equal, return error.
@@ -93,7 +99,5 @@ func (r *AzureMachineTemplate) ValidateDelete() error {
9399
// Default implements webhookutil.defaulter so a webhook will be registered for the type.
94100
func (r *AzureMachineTemplate) Default() {
95101
machinetemplatelog.Info("default", "name", r.Name)
96-
r.Spec.Template.Spec.SetDefaultCachingType()
97-
r.Spec.Template.Spec.SetDataDisksDefaults()
98-
r.Spec.Template.Spec.SetIdentityDefaults()
102+
r.Spec.Template.Spec.SetDefaults(machinetemplatelog)
99103
}

api/v1beta1/azuremachinetemplate_webhook_test.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func TestAzureMachineTemplate_ValidateUpdate(t *testing.T) {
177177
DiskSizeGB: to.Int32Ptr(11),
178178
},
179179
DataDisks: []DataDisk{},
180-
SSHPublicKey: "",
180+
SSHPublicKey: "fake ssh key",
181181
},
182182
},
183183
},
@@ -197,7 +197,7 @@ func TestAzureMachineTemplate_ValidateUpdate(t *testing.T) {
197197
DiskSizeGB: to.Int32Ptr(11),
198198
},
199199
DataDisks: []DataDisk{},
200-
SSHPublicKey: "",
200+
SSHPublicKey: "fake ssh key",
201201
},
202202
},
203203
},
@@ -216,7 +216,7 @@ func TestAzureMachineTemplate_ValidateUpdate(t *testing.T) {
216216
DiskSizeGB: to.Int32Ptr(11),
217217
},
218218
DataDisks: []DataDisk{},
219-
SSHPublicKey: "",
219+
SSHPublicKey: "fake ssh key",
220220
},
221221
},
222222
},
@@ -259,7 +259,8 @@ func TestAzureMachineTemplate_ValidateUpdate(t *testing.T) {
259259
DiskSizeGB: to.Int32Ptr(11),
260260
CachingType: "None",
261261
},
262-
DataDisks: []DataDisk{},
262+
DataDisks: []DataDisk{},
263+
SSHPublicKey: "fake ssh key",
263264
},
264265
},
265266
},
@@ -269,6 +270,50 @@ func TestAzureMachineTemplate_ValidateUpdate(t *testing.T) {
269270
},
270271
wantErr: false,
271272
},
273+
{
274+
name: "AzureMachineTemplate ssh key removed",
275+
oldTemplate: &AzureMachineTemplate{
276+
Spec: AzureMachineTemplateSpec{
277+
Template: AzureMachineTemplateResource{
278+
Spec: AzureMachineSpec{
279+
VMSize: "size",
280+
FailureDomain: &failureDomain,
281+
OSDisk: OSDisk{
282+
OSType: "type",
283+
DiskSizeGB: to.Int32Ptr(11),
284+
CachingType: "None",
285+
},
286+
DataDisks: []DataDisk{},
287+
SSHPublicKey: "some key",
288+
},
289+
},
290+
},
291+
ObjectMeta: metav1.ObjectMeta{
292+
Name: "OldTemplate",
293+
},
294+
},
295+
template: &AzureMachineTemplate{
296+
Spec: AzureMachineTemplateSpec{
297+
Template: AzureMachineTemplateResource{
298+
Spec: AzureMachineSpec{
299+
VMSize: "size",
300+
FailureDomain: &failureDomain,
301+
OSDisk: OSDisk{
302+
OSType: "type",
303+
DiskSizeGB: to.Int32Ptr(11),
304+
CachingType: "None",
305+
},
306+
DataDisks: []DataDisk{},
307+
SSHPublicKey: "",
308+
},
309+
},
310+
},
311+
ObjectMeta: metav1.ObjectMeta{
312+
Name: "NewTemplate",
313+
},
314+
},
315+
wantErr: true,
316+
},
272317
}
273318

274319
for _, amt := range tests {

0 commit comments

Comments
 (0)