Skip to content

Commit

Permalink
allow host name >15 chars if VM is already windows w such a host name
Browse files Browse the repository at this point in the history
  • Loading branch information
akutz committed May 1, 2024
1 parent d998adc commit da9b3e2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
23 changes: 20 additions & 3 deletions webhooks/virtualmachine/validation/virtualmachine_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (v validator) ValidateCreate(ctx *pkgctx.WebhookRequestContext) admission.R
fieldErrs = append(fieldErrs, v.validateAnnotation(ctx, vm, nil)...)
fieldErrs = append(fieldErrs, v.validateLabel(ctx, vm, nil)...)
fieldErrs = append(fieldErrs, v.validateBiosUUID(ctx, vm)...)
fieldErrs = append(fieldErrs, v.validateNetworkHostAndDomainName(ctx, vm)...)
fieldErrs = append(fieldErrs, v.validateNetworkHostAndDomainName(ctx, vm, nil)...)

validationErrs := make([]string, 0, len(fieldErrs))
for _, fieldErr := range fieldErrs {
Expand Down Expand Up @@ -193,7 +193,7 @@ func (v validator) ValidateUpdate(ctx *pkgctx.WebhookRequestContext) admission.R
fieldErrs = append(fieldErrs, v.validateAnnotation(ctx, vm, oldVM)...)
fieldErrs = append(fieldErrs, v.validateMinHardwareVersion(ctx, vm, oldVM)...)
fieldErrs = append(fieldErrs, v.validateLabel(ctx, vm, oldVM)...)
fieldErrs = append(fieldErrs, v.validateNetworkHostAndDomainName(ctx, vm)...)
fieldErrs = append(fieldErrs, v.validateNetworkHostAndDomainName(ctx, vm, oldVM)...)

validationErrs := make([]string, 0, len(fieldErrs))
for _, fieldErr := range fieldErrs {
Expand Down Expand Up @@ -1193,13 +1193,30 @@ func (v validator) validateBiosUUID(ctx *pkgctx.WebhookRequestContext, vm *vmopv

func (v validator) validateNetworkHostAndDomainName(
ctx *pkgctx.WebhookRequestContext,
vm *vmopv1.VirtualMachine) field.ErrorList {
vm, oldVM *vmopv1.VirtualMachine) field.ErrorList {

if vm == nil {
panic("vm is nil")
}

if err := vmopv1util.ValidateHostAndDomainName(*vm); err != nil {

// Do not complain about the host name exceeding 15 characters for a
// Windows guest when the old host name already exceeded 15 characters.
// This prevents breaking updates/patches to VMs that existed prior to
// this restriction.
if oldVM != nil && err.Detail == vmopv1util.ErrInvalidHostNameWindows {
if bs := oldVM.Spec.Bootstrap; bs != nil && bs.Sysprep != nil {
oldHostName := oldVM.Name
if oldVM.Spec.Network != nil && oldVM.Spec.Network.HostName != "" {
oldHostName = oldVM.Spec.Network.HostName
}
if len(oldHostName) > 15 {
return nil
}
}
}

return field.ErrorList{err}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2106,6 +2106,71 @@ func unitTestsValidateUpdate() {
},
),
)

DescribeTable("update network - host and domain names", doTest,

Entry("disallow host name longer than 15 characters if sysprep",
testParams{
setup: func(ctx *unitValidatingWebhookContext) {
ctx.oldVM.Spec.Bootstrap = &vmopv1.VirtualMachineBootstrapSpec{
VAppConfig: &vmopv1.VirtualMachineBootstrapVAppConfigSpec{},
}
ctx.vm.Spec.PowerState = vmopv1.VirtualMachinePowerStateOff
ctx.vm.Spec.Network.HostName = strings.Repeat("a", 16)
ctx.vm.Spec.Bootstrap = &vmopv1.VirtualMachineBootstrapSpec{
Sysprep: &vmopv1.VirtualMachineBootstrapSysprepSpec{
Sysprep: &sysprep.Sysprep{},
},
}
},
validate: doValidateWithMsg(
fmt.Sprintf(`spec.network.hostName: Invalid value: "%s": %s`, strings.Repeat("a", 16), vmopv1util.ErrInvalidHostNameWindows),
),
},
),

Entry("disallow host name longer than 15 characters if sysprep and old VM has host name longer than 15 characters",
testParams{
setup: func(ctx *unitValidatingWebhookContext) {
ctx.oldVM.Spec.Network.HostName = strings.Repeat("a", 16)
ctx.oldVM.Spec.Bootstrap = &vmopv1.VirtualMachineBootstrapSpec{
VAppConfig: &vmopv1.VirtualMachineBootstrapVAppConfigSpec{},
}
ctx.vm.Spec.PowerState = vmopv1.VirtualMachinePowerStateOff
ctx.vm.Spec.Network.HostName = strings.Repeat("a", 16)
ctx.vm.Spec.Bootstrap = &vmopv1.VirtualMachineBootstrapSpec{
Sysprep: &vmopv1.VirtualMachineBootstrapSysprepSpec{
Sysprep: &sysprep.Sysprep{},
},
}
},
validate: doValidateWithMsg(
fmt.Sprintf(`spec.network.hostName: Invalid value: "%s": %s`, strings.Repeat("a", 16), vmopv1util.ErrInvalidHostNameWindows),
),
},
),

Entry("allow host name longer than 15 characters if sysprep and old VM has host name longer than 15 characters and is sysprep",
testParams{
setup: func(ctx *unitValidatingWebhookContext) {
ctx.oldVM.Spec.Network.HostName = strings.Repeat("a", 16)
ctx.oldVM.Spec.Bootstrap = &vmopv1.VirtualMachineBootstrapSpec{
Sysprep: &vmopv1.VirtualMachineBootstrapSysprepSpec{
Sysprep: &sysprep.Sysprep{},
},
}
ctx.vm.Spec.PowerState = vmopv1.VirtualMachinePowerStateOff
ctx.vm.Spec.Network.HostName = strings.Repeat("a", 16)
ctx.vm.Spec.Bootstrap = &vmopv1.VirtualMachineBootstrapSpec{
Sysprep: &vmopv1.VirtualMachineBootstrapSysprepSpec{
Sysprep: &sysprep.Sysprep{},
},
}
},
expectAllowed: true,
},
),
)
})

When("the update is performed while object deletion", func() {
Expand Down

0 comments on commit da9b3e2

Please sign in to comment.