Skip to content

Commit b4cb167

Browse files
committed
vcsim: only power off if needed and use mac address to ensure to change existing nic
1 parent 357565e commit b4cb167

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

test/infrastructure/vcsim/controllers/vmip_controller.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package controllers
1818

1919
import (
2020
"context"
21+
"time"
2122

2223
"github.com/pkg/errors"
2324
"github.com/vmware/govmomi/vim25/types"
@@ -71,6 +72,30 @@ func (r *vmIPReconciler) ReconcileIP(ctx context.Context) (ctrl.Result, error) {
7172
return reconcile.Result{}, nil
7273
}
7374

75+
var macAddress string
76+
devices, err := vm.Device(ctx)
77+
if err != nil {
78+
return reconcile.Result{}, errors.Wrapf(err, "failed to get devices for vm")
79+
}
80+
for _, device := range devices {
81+
if ethernetCard, ok := device.(types.BaseVirtualEthernetCard); ok {
82+
macAddress = ethernetCard.GetVirtualEthernetCard().MacAddress
83+
}
84+
}
85+
if macAddress == "" {
86+
return reconcile.Result{}, errors.Wrapf(err, "failed to find mac address")
87+
}
88+
89+
powerState, err := vm.PowerState(ctx)
90+
if err != nil {
91+
return reconcile.Result{}, errors.Wrapf(err, "failed to check Power State of vm")
92+
}
93+
94+
if powerState != types.VirtualMachinePowerStatePoweredOn {
95+
// Requeue to wait for the VM to get powered on by CAPV or Supervisor first.
96+
return reconcile.Result{RequeueAfter: time.Second}, nil
97+
}
98+
7499
log.Info("Powering Off the VM before applying an IP")
75100
task, err := vm.PowerOff(ctx)
76101
if err != nil {
@@ -84,6 +109,7 @@ func (r *vmIPReconciler) ReconcileIP(ctx context.Context) (ctrl.Result, error) {
84109
spec := types.CustomizationSpec{
85110
NicSettingMap: []types.CustomizationAdapterMapping{
86111
{
112+
MacAddress: macAddress,
87113
Adapter: types.CustomizationIPSettings{
88114
Ip: &types.CustomizationFixedIp{
89115
IpAddress: "192.168.1.100",

0 commit comments

Comments
 (0)