Skip to content

Commit 645ce4a

Browse files
authored
Merge pull request #1170 from yastij/automated-cherry-pick-of-#1156-upstream-release-0.7
Automated cherry pick of #1156: fix panic when reading from the list of devices
2 parents 9045ee9 + 88c0ba9 commit 645ce4a

File tree

2 files changed

+73
-17
lines changed

2 files changed

+73
-17
lines changed

pkg/util/machines.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

pkg/util/machines_test.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,10 @@ func Test_GetMachinePreferredIPAddress(t *testing.T) {
228228

229229
func Test_GetMachineMetadata(t *testing.T) {
230230
testCases := []struct {
231-
name string
232-
machine *v1alpha3.VSphereVM
233-
expected string
231+
name string
232+
machine *v1alpha3.VSphereVM
233+
networkStatuses []v1alpha3.NetworkStatus
234+
expected string
234235
}{
235236
{
236237
name: "dhcp4",
@@ -584,14 +585,65 @@ network:
584585
nameservers:
585586
search:
586587
- "vmware6.ci"
588+
`,
589+
},
590+
{
591+
name: "2nets+network-statuses",
592+
machine: &v1alpha3.VSphereVM{
593+
Spec: v1alpha3.VSphereVMSpec{
594+
VirtualMachineCloneSpec: v1alpha3.VirtualMachineCloneSpec{
595+
Network: v1alpha3.NetworkSpec{
596+
Devices: []v1alpha3.NetworkDeviceSpec{
597+
{
598+
NetworkName: "network1",
599+
MACAddr: "00:00:00:00:00",
600+
DHCP4: true,
601+
},
602+
{
603+
NetworkName: "network12",
604+
MACAddr: "00:00:00:00:01",
605+
DHCP6: true,
606+
},
607+
},
608+
},
609+
},
610+
},
611+
},
612+
networkStatuses: []v1alpha3.NetworkStatus{
613+
{MACAddr: "00:00:00:00:ab"},
614+
{MACAddr: "00:00:00:00:cd"},
615+
},
616+
expected: `
617+
instance-id: "test-vm"
618+
local-hostname: "test-vm"
619+
wait-on-network:
620+
ipv4: true
621+
ipv6: true
622+
network:
623+
version: 2
624+
ethernets:
625+
id0:
626+
match:
627+
macaddress: "00:00:00:00:ab"
628+
set-name: "eth0"
629+
wakeonlan: true
630+
dhcp4: true
631+
dhcp6: false
632+
id1:
633+
match:
634+
macaddress: "00:00:00:00:cd"
635+
set-name: "eth1"
636+
wakeonlan: true
637+
dhcp4: false
638+
dhcp6: true
587639
`,
588640
},
589641
}
590642
for _, tc := range testCases {
591643
tc := tc
592644
t.Run(tc.name, func(t *testing.T) {
593645
tc.machine.Name = tc.name
594-
actVal, err := util.GetMachineMetadata("test-vm", *tc.machine)
646+
actVal, err := util.GetMachineMetadata("test-vm", *tc.machine, tc.networkStatuses...)
595647
if err != nil {
596648
t.Fatal(err)
597649
}

0 commit comments

Comments
 (0)