@@ -26,6 +26,7 @@ import (
2626 "github.com/pkg/errors"
2727 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828 apitypes "k8s.io/apimachinery/pkg/types"
29+ "k8s.io/utils/integer"
2930 clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
3031 "sigs.k8s.io/controller-runtime/pkg/client"
3132
@@ -137,22 +138,20 @@ func IsControlPlaneMachine(machine metav1.Object) bool {
137138
138139// GetMachineMetadata returns the cloud-init metadata as a base-64 encoded
139140// string for a given VSphereMachine.
140- func GetMachineMetadata (hostname string , machine infrav1.VSphereVM , networkStatus ... infrav1.NetworkStatus ) ([]byte , error ) {
141+ func GetMachineMetadata (hostname string , vsphereVM infrav1.VSphereVM , networkStatuses ... infrav1.NetworkStatus ) ([]byte , error ) {
141142 // Create a copy of the devices and add their MAC addresses from a network status.
142- devices := make ([]infrav1.NetworkDeviceSpec , len (machine .Spec .Network .Devices ))
143+ devices := make ([]infrav1.NetworkDeviceSpec , integer .IntMax (len (vsphereVM .Spec .Network .Devices ), len (networkStatuses )))
144+
143145 var waitForIPv4 , waitForIPv6 bool
144- for i := range machine .Spec .Network .Devices {
145- machine .Spec .Network .Devices [i ].DeepCopyInto (& devices [i ])
146- if len (networkStatus ) > 0 {
147- devices [i ].MACAddr = networkStatus [i ].MACAddr
148- }
146+ for i := range vsphereVM .Spec .Network .Devices {
147+ vsphereVM .Spec .Network .Devices [i ].DeepCopyInto (& devices [i ])
149148
150149 if waitForIPv4 && waitForIPv6 {
151150 // break early as we already wait for ipv4 and ipv6
152151 continue
153152 }
154153 // check static IPs
155- for _ , ipStr := range machine .Spec .Network .Devices [i ].IPAddrs {
154+ for _ , ipStr := range vsphereVM .Spec .Network .Devices [i ].IPAddrs {
156155 ip := net .ParseIP (ipStr )
157156 // check the IP family
158157 if ip != nil {
@@ -164,14 +163,19 @@ func GetMachineMetadata(hostname string, machine infrav1.VSphereVM, networkStatu
164163 }
165164 }
166165 // check if DHCP is enabled
167- if machine .Spec .Network .Devices [i ].DHCP4 {
166+ if vsphereVM .Spec .Network .Devices [i ].DHCP4 {
168167 waitForIPv4 = true
169168 }
170- if machine .Spec .Network .Devices [i ].DHCP6 {
169+ if vsphereVM .Spec .Network .Devices [i ].DHCP6 {
171170 waitForIPv6 = true
172171 }
173172 }
174173
174+ // Add the MAC Address to the network device
175+ for i , status := range networkStatuses {
176+ devices [i ].MACAddr = status .MACAddr
177+ }
178+
175179 buf := & bytes.Buffer {}
176180 tpl := template .Must (template .New ("t" ).Funcs (
177181 template.FuncMap {
@@ -188,14 +192,14 @@ func GetMachineMetadata(hostname string, machine infrav1.VSphereVM, networkStatu
188192 }{
189193 Hostname : hostname , // note that hostname determines the Kubernetes node name
190194 Devices : devices ,
191- Routes : machine .Spec .Network .Routes ,
195+ Routes : vsphereVM .Spec .Network .Routes ,
192196 WaitForIPv4 : waitForIPv4 ,
193197 WaitForIPv6 : waitForIPv6 ,
194198 }); err != nil {
195199 return nil , errors .Wrapf (
196200 err ,
197- "error getting cloud init metadata for machine %s/%s/%s" ,
198- machine .Namespace , machine .ClusterName , machine .Name )
201+ "error getting cloud init metadata for vsphereVM %s/%s/%s" ,
202+ vsphereVM .Namespace , vsphereVM .ClusterName , vsphereVM .Name )
199203 }
200204 return buf .Bytes (), nil
201205}
0 commit comments