@@ -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
123126func 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
170186func 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 )
0 commit comments