Skip to content

Commit

Permalink
Merge pull request #1811 from CecileRobertMichon/re-add-ssh-defaults
Browse files Browse the repository at this point in the history
Re-add defaulting for AzureMachineTemplate ssh key
  • Loading branch information
k8s-ci-robot authored Nov 1, 2021
2 parents 2955024 + 7ca2e24 commit 8b24810
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
10 changes: 7 additions & 3 deletions api/v1beta1/azuremachinetemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func (r *AzureMachineTemplate) ValidateUpdate(oldRaw runtime.Object) error {
// This means if the old object was in v1alpha3, it would not get the new defaults set in v1beta1 resulting
// in object inequality. To workaround this, we set the v1beta1 defaults here so that the old object also gets
// the new defaults.

// We need to set ssh key explicitly, otherwise Default() will create a new one.
if old.Spec.Template.Spec.SSHPublicKey == "" {
old.Spec.Template.Spec.SSHPublicKey = r.Spec.Template.Spec.SSHPublicKey
}

old.Default()

// if it's still not equal, return error.
Expand All @@ -93,7 +99,5 @@ func (r *AzureMachineTemplate) ValidateDelete() error {
// Default implements webhookutil.defaulter so a webhook will be registered for the type.
func (r *AzureMachineTemplate) Default() {
machinetemplatelog.Info("default", "name", r.Name)
r.Spec.Template.Spec.SetDefaultCachingType()
r.Spec.Template.Spec.SetDataDisksDefaults()
r.Spec.Template.Spec.SetIdentityDefaults()
r.Spec.Template.Spec.SetDefaults(machinetemplatelog)
}
53 changes: 49 additions & 4 deletions api/v1beta1/azuremachinetemplate_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestAzureMachineTemplate_ValidateUpdate(t *testing.T) {
DiskSizeGB: to.Int32Ptr(11),
},
DataDisks: []DataDisk{},
SSHPublicKey: "",
SSHPublicKey: "fake ssh key",
},
},
},
Expand All @@ -197,7 +197,7 @@ func TestAzureMachineTemplate_ValidateUpdate(t *testing.T) {
DiskSizeGB: to.Int32Ptr(11),
},
DataDisks: []DataDisk{},
SSHPublicKey: "",
SSHPublicKey: "fake ssh key",
},
},
},
Expand All @@ -216,7 +216,7 @@ func TestAzureMachineTemplate_ValidateUpdate(t *testing.T) {
DiskSizeGB: to.Int32Ptr(11),
},
DataDisks: []DataDisk{},
SSHPublicKey: "",
SSHPublicKey: "fake ssh key",
},
},
},
Expand Down Expand Up @@ -259,7 +259,8 @@ func TestAzureMachineTemplate_ValidateUpdate(t *testing.T) {
DiskSizeGB: to.Int32Ptr(11),
CachingType: "None",
},
DataDisks: []DataDisk{},
DataDisks: []DataDisk{},
SSHPublicKey: "fake ssh key",
},
},
},
Expand All @@ -269,6 +270,50 @@ func TestAzureMachineTemplate_ValidateUpdate(t *testing.T) {
},
wantErr: false,
},
{
name: "AzureMachineTemplate ssh key removed",
oldTemplate: &AzureMachineTemplate{
Spec: AzureMachineTemplateSpec{
Template: AzureMachineTemplateResource{
Spec: AzureMachineSpec{
VMSize: "size",
FailureDomain: &failureDomain,
OSDisk: OSDisk{
OSType: "type",
DiskSizeGB: to.Int32Ptr(11),
CachingType: "None",
},
DataDisks: []DataDisk{},
SSHPublicKey: "some key",
},
},
},
ObjectMeta: metav1.ObjectMeta{
Name: "OldTemplate",
},
},
template: &AzureMachineTemplate{
Spec: AzureMachineTemplateSpec{
Template: AzureMachineTemplateResource{
Spec: AzureMachineSpec{
VMSize: "size",
FailureDomain: &failureDomain,
OSDisk: OSDisk{
OSType: "type",
DiskSizeGB: to.Int32Ptr(11),
CachingType: "None",
},
DataDisks: []DataDisk{},
SSHPublicKey: "",
},
},
},
ObjectMeta: metav1.ObjectMeta{
Name: "NewTemplate",
},
},
wantErr: true,
},
}

for _, amt := range tests {
Expand Down

0 comments on commit 8b24810

Please sign in to comment.