@@ -18,6 +18,7 @@ package controllers
18
18
19
19
import (
20
20
"context"
21
+ "time"
21
22
22
23
"github.com/pkg/errors"
23
24
"github.com/vmware/govmomi/vim25/types"
@@ -71,6 +72,30 @@ func (r *vmIPReconciler) ReconcileIP(ctx context.Context) (ctrl.Result, error) {
71
72
return reconcile.Result {}, nil
72
73
}
73
74
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
+
74
99
log .Info ("Powering Off the VM before applying an IP" )
75
100
task , err := vm .PowerOff (ctx )
76
101
if err != nil {
@@ -84,6 +109,7 @@ func (r *vmIPReconciler) ReconcileIP(ctx context.Context) (ctrl.Result, error) {
84
109
spec := types.CustomizationSpec {
85
110
NicSettingMap : []types.CustomizationAdapterMapping {
86
111
{
112
+ MacAddress : macAddress ,
87
113
Adapter : types.CustomizationIPSettings {
88
114
Ip : & types.CustomizationFixedIp {
89
115
IpAddress : "192.168.1.100" ,
0 commit comments