Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ require (
github.com/vmware/vmw-guestinfo v0.0.0-20220317130741-510905f0efa3
github.com/xlab/treeprint v1.2.0
golang.org/x/text v0.40.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
114 changes: 112 additions & 2 deletions simulator/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
"path/filepath"
"reflect"
"slices"
"sort"
"strconv"
"strings"
"sync/atomic"
"time"

"github.com/google/uuid"
"gopkg.in/yaml.v3"

"github.com/vmware/govmomi/internal"
"github.com/vmware/govmomi/object"
Expand Down Expand Up @@ -2980,7 +2982,7 @@ func (vm *VirtualMachine) customize(ctx *Context) types.BaseMethodFault {
{Name: "config.tools.pendingCustomization", Val: ""},
}

if len(vm.Guest.Net) != len(vm.imc.NicSettingMap) {
if !customizationUsesCloudInitNetworkConfig(vm.imc) && len(vm.Guest.Net) != len(vm.imc.NicSettingMap) {
fault := &types.NicSettingMismatch{
NumberOfNicsInSpec: int32(len(vm.imc.NicSettingMap)),
NumberOfNicsInVM: int32(len(vm.Guest.Net)),
Expand All @@ -3004,12 +3006,15 @@ func (vm *VirtualMachine) customize(ctx *Context) types.BaseMethodFault {

hostname := ""
address := ""
guestNetChanged := false

switch c := vm.imc.Identity.(type) {
case *types.CustomizationLinuxPrep:
hostname = customizeName(vm, c.HostName)
case *types.CustomizationSysprep:
hostname = customizeName(vm, c.UserData.ComputerName)
case *types.CustomizationCloudinitPrep:
hostname, address, guestNetChanged = vm.applyCloudInitCustomization(c)
}

cards := object.VirtualDeviceList(vm.Config.Hardware.Device).SelectByType((*types.VirtualEthernetCard)(nil))
Expand Down Expand Up @@ -3057,6 +3062,9 @@ func (vm *VirtualMachine) customize(ctx *Context) types.BaseMethodFault {
}

if len(vm.imc.NicSettingMap) != 0 {
guestNetChanged = true
}
if guestNetChanged {
changes = append(changes, types.PropertyChange{Name: "guest.net", Val: vm.Guest.Net})
}
if hostname != "" {
Expand All @@ -3078,6 +3086,108 @@ func (vm *VirtualMachine) customize(ctx *Context) types.BaseMethodFault {
return nil
}

func customizationUsesCloudInitNetworkConfig(spec *types.CustomizationSpec) bool {
if spec == nil || len(spec.NicSettingMap) != 0 {
return false
}
_, ok := spec.Identity.(*types.CustomizationCloudinitPrep)
return ok
}

type cloudInitMetadata struct {
Hostname string `yaml:"hostname"`
LocalHostname string `yaml:"local-hostname"`
Network struct {
Ethernets map[string]cloudInitEthernet `yaml:"ethernets"`
} `yaml:"network"`
}

type cloudInitEthernet struct {
Addresses []string `yaml:"addresses"`
Nameservers struct {
Addresses []string `yaml:"addresses"`
Search []string `yaml:"search"`
} `yaml:"nameservers"`
}

func (vm *VirtualMachine) applyCloudInitCustomization(prep *types.CustomizationCloudinitPrep) (string, string, bool) {
if prep == nil || strings.TrimSpace(prep.Metadata) == "" {
return "", "", false
}

var metadata cloudInitMetadata
if err := yaml.Unmarshal([]byte(prep.Metadata), &metadata); err != nil {
vm.logPrintf("cloud-init metadata parse failed: %s", err)
return "", "", false
}

hostname := metadata.LocalHostname
if hostname == "" {
hostname = metadata.Hostname
}

ethernets := metadata.Network.Ethernets
if len(ethernets) == 0 {
return hostname, "", false
}

names := make([]string, 0, len(ethernets))
for name := range ethernets {
names = append(names, name)
}
sort.Strings(names)

address := ""
changed := false
for i, name := range names {
if i >= len(vm.Guest.Net) {
break
}

config := ethernets[name]
nic := &vm.Guest.Net[i]

if len(config.Addresses) != 0 {
nic.IpAddress = cloudInitIPAddresses(config.Addresses)
nic.IpConfig = &types.NetIpConfigInfo{
IpAddress: make([]types.NetIpConfigInfoIpAddress, len(nic.IpAddress)),
}
for j, ip := range nic.IpAddress {
nic.IpConfig.IpAddress[j].IpAddress = ip
}
if address == "" {
address = nic.IpAddress[0]
}
changed = true
}

if len(config.Nameservers.Addresses) != 0 || len(config.Nameservers.Search) != 0 {
if nic.DnsConfig == nil {
nic.DnsConfig = new(types.NetDnsConfigInfo)
}
nic.DnsConfig.IpAddress = config.Nameservers.Addresses
nic.DnsConfig.SearchDomain = config.Nameservers.Search
changed = true
}
}

return hostname, address, changed
}

func cloudInitIPAddresses(addresses []string) []string {
ips := make([]string, 0, len(addresses))
for _, address := range addresses {
ip, _, err := net.ParseCIDR(address)
if err == nil {
ips = append(ips, ip.String())
continue
}
value, _, _ := strings.Cut(address, "/")
ips = append(ips, value)
}
return ips
}

func (vm *VirtualMachine) customizationInfo(status types.GuestInfoCustomizationStatus, err string) *types.GuestInfoCustomizationInfo {
info := &types.GuestInfoCustomizationInfo{
CustomizationStatus: string(status),
Expand Down Expand Up @@ -3112,7 +3222,7 @@ func (vm *VirtualMachine) setPendingCustomization(ctx *Context, spec *types.Cust
if vm.Config.Tools.PendingCustomization != "" {
return new(types.CustomizationPending)
}
if len(vm.Guest.Net) != len(spec.NicSettingMap) {
if !customizationUsesCloudInitNetworkConfig(spec) && len(vm.Guest.Net) != len(spec.NicSettingMap) {
return &types.NicSettingMismatch{
NumberOfNicsInSpec: int32(len(spec.NicSettingMap)),
NumberOfNicsInVM: int32(len(vm.Guest.Net)),
Expand Down
132 changes: 132 additions & 0 deletions simulator/virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,138 @@ func TestCloneVmPowerOnAndCustomization(t *testing.T) {
}, m)
}

func TestCloneVmCloudInitCustomizationSkipsNicSettingMismatch(t *testing.T) {
m := VPX()
defer m.Remove()

Test(func(ctx context.Context, c *vim25.Client) {
finder := find.NewFinder(c, false)
dc, err := finder.DefaultDatacenter(ctx)
if err != nil {
t.Fatal(err)
}
finder.SetDatacenter(dc)

folders, err := dc.Folders(ctx)
if err != nil {
t.Fatal(err)
}

vmm := m.Map().Any("VirtualMachine").(*VirtualMachine)
vm := object.NewVirtualMachine(c, vmm.Reference())

devices, err := vm.Device(ctx)
if err != nil {
t.Fatal(err)
}
nics := devices.SelectByType((*types.VirtualEthernetCard)(nil))
if len(nics) == 0 {
t.Fatal("expected source VM to have at least one NIC")
}
if err := vm.RemoveDevice(ctx, false, nics...); err != nil {
t.Fatal(err)
}

var source mo.VirtualMachine
if err := vm.Properties(ctx, vm.Reference(), []string{"guest.net"}, &source); err != nil {
t.Fatal(err)
}
if len(source.Guest.Net) != 0 {
t.Fatalf("expected source VM to have no NICs; got %d", len(source.Guest.Net))
}

network, err := finder.Network(ctx, "VM Network")
if err != nil {
t.Fatal(err)
}
backing, err := network.EthernetCardBackingInfo(ctx)
if err != nil {
t.Fatal(err)
}

var added object.VirtualDeviceList
for _, cardType := range []string{"vmxnet3", "e1000"} {
nic, err := added.CreateEthernetCard(cardType, backing)
if err != nil {
t.Fatal(err)
}
added = append(added, nic)
}
deviceChange, err := added.ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd)
if err != nil {
t.Fatal(err)
}

config := types.VirtualMachineCloneSpec{
PowerOn: true,
Config: &types.VirtualMachineConfigSpec{
DeviceChange: deviceChange,
},
Customization: &types.CustomizationSpec{
Identity: &types.CustomizationCloudinitPrep{
Metadata: `instance-id: clone-cloud-init
local-hostname: clone-cloud-init
network:
version: 2
ethernets:
eth0:
addresses:
- 192.168.1.120/24
eth1:
addresses:
- 192.168.1.121/24
`,
},
},
}

cloneTask, err := vm.Clone(ctx, folders.VmFolder, "cloned-vm-cloud-init-customization", config)
if err != nil {
t.Fatal(err)
}
info, err := cloneTask.WaitForResult(ctx, nil)
if err != nil {
if taskErr, ok := err.(task.Error); ok {
if _, ok := taskErr.Fault().(*types.NicSettingMismatch); ok {
t.Fatalf("unexpected NicSettingMismatch for cloud-init network customization: %v", err)
}
}
t.Fatal(err)
}

clone := object.NewVirtualMachine(c, info.Result.(types.ManagedObjectReference))
var moVM mo.VirtualMachine
if err := clone.Properties(ctx, clone.Reference(), []string{
"runtime.powerState",
"guest",
"config.tools",
}, &moVM); err != nil {
t.Fatal(err)
}
if moVM.Runtime.PowerState != types.VirtualMachinePowerStatePoweredOn {
t.Fatalf("expected clone to be powered on; got %s", moVM.Runtime.PowerState)
}
if len(moVM.Guest.Net) != len(added) {
t.Fatalf("expected clone to have %d guest NICs; got %d", len(added), len(moVM.Guest.Net))
}
if moVM.Guest.HostName != "clone-cloud-init" {
t.Fatalf("expected guest hostname %q; got %q", "clone-cloud-init", moVM.Guest.HostName)
}
if moVM.Guest.IpAddress != "192.168.1.120" {
t.Fatalf("expected guest IP %q; got %q", "192.168.1.120", moVM.Guest.IpAddress)
}
for i, ip := range []string{"192.168.1.120", "192.168.1.121"} {
if got := moVM.Guest.Net[i].IpAddress; !reflect.DeepEqual(got, []string{ip}) {
t.Fatalf("expected guest NIC %d IPs %v; got %v", i, []string{ip}, got)
}
}
if moVM.Config.Tools.PendingCustomization != "" {
t.Fatalf("expected pending customization to be cleared; got %q", moVM.Config.Tools.PendingCustomization)
}
assertGuestCustomizationInfo(ctx, t, clone, types.GuestInfoCustomizationStatusTOOLSDEPLOYPKG_SUCCEEDED, true, true)
}, m)
}

func TestCustomizeVmCustomizationInfo(t *testing.T) {
m := VPX()
defer m.Remove()
Expand Down
5 changes: 4 additions & 1 deletion vcsim/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ require (
github.com/vmware/govmomi v0.0.0-00010101000000-000000000000
)

require golang.org/x/text v0.40.0 // indirect
require (
golang.org/x/text v0.40.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions vcsim/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ golang.org/x/text v0.40.0 h1:Ub2Z6/xjgF1WrYQz2nuITOEegKFtiIy+rieRJ5lHZKs=
golang.org/x/text v0.40.0/go.mod h1:hpnzDAfGV753zIKo+wk3u1bVKCGPbrnF7+7LBF/UHVY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Loading