From 7572741934de1ebfee487be255a06eed73b45c28 Mon Sep 17 00:00:00 2001 From: Sagar Muchhal Date: Mon, 18 Dec 2023 00:18:45 -0800 Subject: [PATCH] set mandatory user data fields --- api/v1alpha2/sysprep/sysprep.go | 8 ++---- ...vmoperator.vmware.com_virtualmachines.yaml | 3 +++ .../vsphere2/vmlifecycle/bootstrap.go | 2 ++ .../vsphere2/vmlifecycle/bootstrap_sysprep.go | 19 +++++++++----- .../vmlifecycle/bootstrap_sysprep_test.go | 26 +++++++++++++++++++ 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/api/v1alpha2/sysprep/sysprep.go b/api/v1alpha2/sysprep/sysprep.go index 7b021ca63..f6854fb1b 100644 --- a/api/v1alpha2/sysprep/sysprep.go +++ b/api/v1alpha2/sysprep/sysprep.go @@ -196,14 +196,10 @@ type LicenseFilePrintData struct { type UserData struct { // FullName is the user's full name. - // - // +optional - FullName string `json:"fullName,omitempty"` + FullName string `json:"fullName"` // OrgName is the name of the user's organization. - // - // +optional - OrgName string `json:"orgName,omitempty"` + OrgName string `json:"orgName"` // ProductID is a valid serial number. // diff --git a/config/crd/bases/vmoperator.vmware.com_virtualmachines.yaml b/config/crd/bases/vmoperator.vmware.com_virtualmachines.yaml index ae1c20fe4..4890adff5 100644 --- a/config/crd/bases/vmoperator.vmware.com_virtualmachines.yaml +++ b/config/crd/bases/vmoperator.vmware.com_virtualmachines.yaml @@ -1237,6 +1237,9 @@ spec: - key - name type: object + required: + - fullName + - orgName type: object type: object type: object diff --git a/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap.go b/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap.go index bb5b49568..86a8bc8d1 100644 --- a/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap.go +++ b/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap.go @@ -44,6 +44,7 @@ type BootstrapArgs struct { TemplateRenderFn TemplateRenderFunc NetworkResults network.NetworkInterfaceResults + ComputerName string Hostname string DNSServers []string SearchSuffixes []string @@ -126,6 +127,7 @@ func getBootstrapArgs( BootstrapData: bootstrapData, NetworkResults: networkResults, Hostname: vmCtx.VM.Spec.Network.HostName, + ComputerName: vmCtx.VM.Name, } if bootstrapArgs.Hostname == "" { diff --git a/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap_sysprep.go b/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap_sysprep.go index 43f2dcdd4..635853c12 100644 --- a/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap_sysprep.go +++ b/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap_sysprep.go @@ -54,7 +54,7 @@ func BootstrapSysPrep( Value: data, } } else if sysPrep := sysPrepSpec.Sysprep; sysPrep != nil { - identity = convertTo(sysPrep, bsArgs.BootstrapData) + identity = convertTo(sysPrep, bsArgs) } nicSettingMap, err := network.GuestOSCustomization(bsArgs.NetworkResults) @@ -85,7 +85,8 @@ func BootstrapSysPrep( return configSpec, customSpec, nil } -func convertTo(from *vmopv1sysprep.Sysprep, bootstrapData BootstrapData) *vimTypes.CustomizationSysprep { +func convertTo(from *vmopv1sysprep.Sysprep, bsArgs *BootstrapArgs) *vimTypes.CustomizationSysprep { + bootstrapData := bsArgs.BootstrapData sysprepCustomization := &vimTypes.CustomizationSysprep{} if from.GUIUnattended != nil { @@ -102,13 +103,17 @@ func convertTo(from *vmopv1sysprep.Sysprep, bootstrapData BootstrapData) *vimTyp } } + sysprepCustomization.UserData = vimTypes.CustomizationUserData{ + // This is a mandatory field + ComputerName: &vimTypes.CustomizationFixedName{ + Name: bsArgs.ComputerName, + }, + } if from.UserData != nil { - sysprepCustomization.UserData = vimTypes.CustomizationUserData{ - FullName: from.UserData.FullName, - OrgName: from.UserData.OrgName, - } + sysprepCustomization.UserData.FullName = from.UserData.FullName + sysprepCustomization.UserData.OrgName = from.UserData.OrgName // In the case of a VMI with volume license key, this might not be set. - // Hence add a check to see if the productID is set to empty. + // Hence, add a check to see if the productID is set to empty. if bootstrapData.Sysprep != nil && bootstrapData.Sysprep.ProductID != "" { sysprepCustomization.UserData.ProductId = bootstrapData.Sysprep.ProductID } diff --git a/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap_sysprep_test.go b/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap_sysprep_test.go index 3306e4d81..0ab70b7ca 100644 --- a/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap_sysprep_test.go +++ b/pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap_sysprep_test.go @@ -100,6 +100,7 @@ var _ = Describe("SysPrep Bootstrap", func() { Context("Inlined Sysprep", func() { autoUsers := int32(5) password, domainPassword, productID := "password_foo", "admin_password_foo", "product_id_foo" + computerName := "foo-win-vm" BeforeEach(func() { sysPrepSpec.Sysprep = &vmopv1sysprep.Sysprep{ @@ -139,6 +140,7 @@ var _ = Describe("SysPrep Bootstrap", func() { Password: password, DomainPassword: domainPassword, } + bsArgs.ComputerName = computerName }) It("should return expected customization spec", func() { @@ -156,6 +158,9 @@ var _ = Describe("SysPrep Bootstrap", func() { Expect(sysPrep.UserData.FullName).To(Equal("foo-bar")) Expect(sysPrep.UserData.OrgName).To(Equal("foo-org")) Expect(sysPrep.UserData.ProductId).To(Equal(productID)) + name, ok := sysPrep.UserData.ComputerName.(*types.CustomizationFixedName) + Expect(ok).To(BeTrue()) + Expect(name.Name).To(Equal(computerName)) Expect(sysPrep.GuiRunOnce.CommandList).To(HaveLen(2)) @@ -167,6 +172,27 @@ var _ = Describe("SysPrep Bootstrap", func() { Expect(sysPrep.LicenseFilePrintData.AutoMode).To(Equal(types.CustomizationLicenseDataModePerServer)) Expect(sysPrep.LicenseFilePrintData.AutoUsers).To(Equal(autoUsers)) }) + + When("no section is set", func() { + + BeforeEach(func() { + sysPrepSpec.Sysprep = &vmopv1sysprep.Sysprep{} + + bsArgs.Sysprep = nil + }) + + It("does not set", func() { + Expect(err).ToNot(HaveOccurred()) + Expect(custSpec).ToNot(BeNil()) + + sysPrep, ok := custSpec.Identity.(*types.CustomizationSysprep) + Expect(ok).To(BeTrue()) + + name, ok := sysPrep.UserData.ComputerName.(*types.CustomizationFixedName) + Expect(ok).To(BeTrue()) + Expect(name.Name).To(Equal(computerName)) + }) + }) }) Context("RawSysPrep", func() {