Skip to content

Commit

Permalink
set mandatory user data fields
Browse files Browse the repository at this point in the history
  • Loading branch information
srm09 committed Dec 18, 2023
1 parent f74dcbf commit 7572741
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
8 changes: 2 additions & 6 deletions api/v1alpha2/sysprep/sysprep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/vmoperator.vmware.com_virtualmachines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,9 @@ spec:
- key
- name
type: object
required:
- fullName
- orgName
type: object
type: object
type: object
Expand Down
2 changes: 2 additions & 0 deletions pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type BootstrapArgs struct {

TemplateRenderFn TemplateRenderFunc
NetworkResults network.NetworkInterfaceResults
ComputerName string
Hostname string
DNSServers []string
SearchSuffixes []string
Expand Down Expand Up @@ -126,6 +127,7 @@ func getBootstrapArgs(
BootstrapData: bootstrapData,
NetworkResults: networkResults,
Hostname: vmCtx.VM.Spec.Network.HostName,
ComputerName: vmCtx.VM.Name,
}

if bootstrapArgs.Hostname == "" {
Expand Down
19 changes: 12 additions & 7 deletions pkg/vmprovider/providers/vsphere2/vmlifecycle/bootstrap_sysprep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -139,6 +140,7 @@ var _ = Describe("SysPrep Bootstrap", func() {
Password: password,
DomainPassword: domainPassword,
}
bsArgs.ComputerName = computerName
})

It("should return expected customization spec", func() {
Expand All @@ -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))

Expand All @@ -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() {
Expand Down

0 comments on commit 7572741

Please sign in to comment.