Skip to content

Commit c0a53ee

Browse files
committed
WIP: vmservice/ip_test.go: correctly requeue machine when creating ipclaim
1 parent 80bf134 commit c0a53ee

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

internal/service/vmservice/ip.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ func reconcileIPAddresses(ctx context.Context, machineScope *scope.MachineScope)
4848
netPoolAddresses := make(map[string]map[string][]string)
4949

5050
if machineScope.ProxmoxMachine.Spec.Network != nil {
51-
// fmt.Println( handleDevices(ctx, machineScope, addresses))
5251
if requeue, err = handleDevices(ctx, machineScope, netPoolAddresses); err != nil || requeue {
53-
return true, errors.Wrap(err, "unable to handle network devices")
52+
if err == nil {
53+
return true, errors.Wrap(err, "requeuing network reconcillation")
54+
} else {
55+
return true, errors.Wrap(err, "unable to handle network devices")
56+
}
5457
}
5558
}
5659

@@ -82,8 +85,8 @@ func formatIPAddressName(name, device string) string {
8285
return fmt.Sprintf("%s-%s", name, device)
8386
}
8487

85-
// findIPAddressV2 takes the likely name of an ipaddress and returns an ipamv1.IPAddress object or nil
86-
func findIPAddressV2(ctx context.Context, poolRef *corev1.TypedLocalObjectReference, machineScope *scope.MachineScope) ([]ipamv1.IPAddress, error) {
88+
// findIPAddress returns all IPAddresses owned by a pool and a machine
89+
func findIPAddress(ctx context.Context, poolRef *corev1.TypedLocalObjectReference, machineScope *scope.MachineScope) ([]ipamv1.IPAddress, error) {
8790
return machineScope.IPAMHelper.GetIPAddressV2(ctx, *poolRef, machineScope.ProxmoxMachine)
8891
}
8992

@@ -121,24 +124,35 @@ func findIPAddressGatewayMetric(ctx context.Context, machineScope *scope.Machine
121124
}
122125

123126
func machineHasIPAddress(machine *infrav1.ProxmoxMachine) bool {
124-
// TODO: does this work?
125-
return machine.Status.IPAddresses[infrav1.DefaultNetworkDevice] != nil
127+
// Every machine needs to have at least one IPv4 or IPv6 host network address
128+
if machine.Status.IPAddresses[infrav1.DefaultNetworkDevice] == nil {
129+
return false
130+
} else {
131+
return len(machine.Status.IPAddresses[infrav1.DefaultNetworkDevice].IPV4) > 0 ||
132+
len(machine.Status.IPAddresses[infrav1.DefaultNetworkDevice].IPV6) > 0
133+
}
126134
}
127135

128-
func handleIPAddress(ctx context.Context, machineScope *scope.MachineScope, dev *string, poolNum int, ipamRef *corev1.TypedLocalObjectReference) ([]string, error) {
136+
func handleIPAddresses(ctx context.Context, machineScope *scope.MachineScope, dev *string, poolNum int, poolRef *corev1.TypedLocalObjectReference) ([]string, error) {
129137
device := ptr.Deref(dev, infrav1.DefaultNetworkDevice)
130138

131-
ipAddresses, err := findIPAddressV2(ctx, ipamRef, machineScope)
139+
ipAddresses, err := findIPAddress(ctx, poolRef, machineScope)
132140
if err != nil {
141+
// Technically this error can not occure, as fieldselectors just return empty lists
133142
if !apierrors.IsNotFound(err) {
134143
return []string{}, err
135144
}
145+
}
146+
147+
if len(ipAddresses) == 0 {
136148
machineScope.Logger.V(4).Info("IPAddress not found, creating it.", "device", device)
137149
// IpAddress not yet created.
138-
err = machineScope.IPAMHelper.CreateIPAddressClaimV2(ctx, machineScope.ProxmoxMachine, device, poolNum, machineScope.InfraCluster.Cluster.GetName(), ipamRef)
150+
err = machineScope.IPAMHelper.CreateIPAddressClaimV2(ctx, machineScope.ProxmoxMachine, device, poolNum, machineScope.InfraCluster.Cluster.GetName(), poolRef)
139151
if err != nil {
140152
return []string{}, errors.Wrapf(err, "unable to create Ip address claim for machine %s", machineScope.Name())
141153
}
154+
155+
// send the machine to requeue so ipaddress can be created
142156
return []string{}, nil
143157
}
144158

@@ -160,6 +174,8 @@ func handleIPAddress(ctx context.Context, machineScope *scope.MachineScope, dev
160174
return []string{}, errors.Wrapf(err, "unable to add Ip tag to VirtualMachine %s", machineScope.Name())
161175
}
162176
machineScope.ProxmoxMachine.Status.TaskRef = ptr.To(string(t.UPID))
177+
178+
// send the machine to requeue so promoxclient can execute
163179
return []string{}, nil
164180
}
165181
}
@@ -170,7 +186,13 @@ func handleIPAddress(ctx context.Context, machineScope *scope.MachineScope, dev
170186
func handleDevices(ctx context.Context, machineScope *scope.MachineScope, addresses map[string]map[string][]string) (bool, error) {
171187
for _, net := range machineScope.ProxmoxMachine.Spec.Network.NetworkDevices {
172188
for i, ipPool := range net.InterfaceConfig.IPPoolRef {
173-
ipAddresses, err := handleIPAddress(ctx, machineScope, net.Name, i, &ipPool)
189+
ipAddresses, err := handleIPAddresses(ctx, machineScope, net.Name, i, &ipPool)
190+
191+
// requeue machine if tag or ipaddress need creation
192+
if len(ipAddresses) == 0 {
193+
return true, nil
194+
}
195+
174196
for _, ip := range ipAddresses {
175197
if err != nil || ip == "" {
176198
fmt.Println("handleDevices", "err", err, "ip", ip)

internal/service/vmservice/ip_test.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ func TestReconcileIPAddresses_CreateAdditionalClaim(t *testing.T) {
4242
machineScope, _, kubeClient := setupReconcilerTest(t)
4343

4444
defaultPool := corev1.TypedLocalObjectReference{APIGroup: ptr.To("ipam.cluster.x-k8s.io"), Kind: "InClusterIPPool", Name: "custom"}
45+
extraPool0 := corev1.TypedLocalObjectReference{APIGroup: ptr.To("ipam.cluster.x-k8s.io"), Kind: "GlobalInClusterIPPool", Name: "additional0"}
4546

4647
machineScope.ProxmoxMachine.Spec.Network = &infrav1.NetworkSpec{
4748
NetworkDevices: []infrav1.NetworkDevice{
4849
{Name: ptr.To("net0"), InterfaceConfig: infrav1.InterfaceConfig{IPPoolRef: []corev1.TypedLocalObjectReference{defaultPool}}},
49-
{Name: ptr.To("net1"), InterfaceConfig: infrav1.InterfaceConfig{}},
50+
{Name: ptr.To("net1"), InterfaceConfig: infrav1.InterfaceConfig{IPPoolRef: []corev1.TypedLocalObjectReference{extraPool0}}},
5051
},
5152
}
5253

@@ -58,27 +59,43 @@ func TestReconcileIPAddresses_CreateAdditionalClaim(t *testing.T) {
5859
createIP4AddressResource(t, kubeClient, machineScope, infrav1.DefaultNetworkDevice, "10.10.10.10", &defaultPool)
5960

6061
requeue, err := reconcileIPAddresses(context.Background(), machineScope)
61-
require.NotNil(t, machineScope.ProxmoxMachine.Status.IPAddresses["net0"])
62-
require.Equal(t, *(machineScope.ProxmoxMachine.Status.IPAddresses["net0"]), infrav1.IPAddresses{IPV4: []string{"10.10.10.10"}, IPV6: nil})
62+
63+
// Since an IPAddress for extraPool0 still needs to be created, the machine should
64+
// requeue without error.
6365
require.NoError(t, err)
6466
require.True(t, requeue)
67+
68+
// net0 should not exist yet
69+
require.Nil(t, machineScope.ProxmoxMachine.Status.IPAddresses["net0"])
70+
6571
requireConditionIsFalse(t, machineScope.ProxmoxMachine, infrav1.VMProvisionedCondition)
6672
}
6773

68-
/*func TestReconcileIPAddresses_AddIPTag(t *testing.T) {
74+
func TestReconcileIPAddresses_AddIPTag(t *testing.T) {
6975
machineScope, proxmoxClient, kubeClient := setupReconcilerTest(t)
76+
77+
defaultPool := corev1.TypedLocalObjectReference{APIGroup: ptr.To("ipam.cluster.x-k8s.io"), Kind: "InClusterIPPool", Name: "custom"}
78+
machineScope.ProxmoxMachine.Spec.Network = &infrav1.NetworkSpec{
79+
NetworkDevices: []infrav1.NetworkDevice{
80+
{Name: ptr.To("net0"), InterfaceConfig: infrav1.InterfaceConfig{IPPoolRef: []corev1.TypedLocalObjectReference{defaultPool}}},
81+
},
82+
}
83+
7084
vm := newStoppedVM()
7185
task := newTask()
7286
machineScope.SetVirtualMachine(vm)
73-
createIP4AddressResource(t, kubeClient, machineScope, infrav1.DefaultNetworkDevice, "10.10.10.10")
87+
88+
createIPPools(t, kubeClient, machineScope)
89+
createIP4AddressResource(t, kubeClient, machineScope, infrav1.DefaultNetworkDevice, "10.10.10.10", &defaultPool)
7490

7591
proxmoxClient.EXPECT().TagVM(context.Background(), vm, ipTag).Return(task, nil).Once()
7692

7793
requeue, err := reconcileIPAddresses(context.Background(), machineScope)
7894
require.NoError(t, err)
7995
require.True(t, requeue)
96+
require.Equal(t, ipTag, machineScope.VirtualMachine.VirtualMachineConfig.Tags)
8097
requireConditionIsFalse(t, machineScope.ProxmoxMachine, infrav1.VMProvisionedCondition)
81-
}*/
98+
}
8299

83100
func TestReconcileIPAddresses_SetIPAddresses(t *testing.T) {
84101
machineScope, _, kubeClient := setupReconcilerTest(t)

0 commit comments

Comments
 (0)