@@ -148,37 +148,33 @@ func (k K8sClient) prepareCreateIpamIP(subnetName string, macKey string) (*ipamv
148148 if err != nil {
149149 return nil , fmt .Errorf ("error listing IPs with MAC %v: %w" , macKey , err )
150150 }
151- if len (ipList .Items ) == 0 {
152- noop ()
153- } else {
154- for _ , existingIpamIP := range ipList .Items {
155- if existingIpamIP .Spec .Subnet .Name != subnetName {
156- // IP with that MAC is assigned to a different subnet (v4 vs v6?)
157- log .Debugf ("IPAM IP with MAC %v and wrong subnet %s/%s found, ignoring" , macKey ,
158- existingIpamIP .Namespace , existingIpamIP .Spec .Subnet .Name )
159- continue
160- } else if existingIpamIP .Status .State == ipamv1alpha1 .CFailedIPState {
161- log .Infof ("Failed IP %s/%s in subnet %s found, deleting" , existingIpamIP .Namespace ,
162- existingIpamIP .Name , existingIpamIP .Spec .Subnet .Name )
163- log .Debugf ("Deleting old IP %s/%s:\n %v" , existingIpamIP .Namespace , existingIpamIP .Name ,
164- prettyFormat (existingIpamIP .Status ))
165- err = k .Client .Delete (k .Ctx , & existingIpamIP )
166- if err != nil {
167- return nil , fmt .Errorf ("failed to delete IP %s/%s: %w" , existingIpamIP .Namespace , existingIpamIP .Name , err )
168- }
169-
170- err = k .waitForDeletion (& existingIpamIP )
171- if err != nil {
172- return nil , fmt .Errorf ("failed to delete IP %s/%s: %w" , existingIpamIP .Namespace , existingIpamIP .Name , err )
173- }
151+ for _ , existingIpamIP := range ipList .Items {
152+ if existingIpamIP .Spec .Subnet .Name != subnetName {
153+ // IP with that MAC is assigned to a different subnet (v4 vs v6?)
154+ log .Debugf ("IPAM IP with MAC %v and wrong subnet %s/%s found, ignoring" , macKey ,
155+ existingIpamIP .Namespace , existingIpamIP .Spec .Subnet .Name )
156+ continue
157+ } else if existingIpamIP .Status .State == ipamv1alpha1 .CFailedIPState {
158+ log .Infof ("Failed IP %s/%s in subnet %s found, deleting" , existingIpamIP .Namespace ,
159+ existingIpamIP .Name , existingIpamIP .Spec .Subnet .Name )
160+ log .Debugf ("Deleting old IP %s/%s:\n %v" , existingIpamIP .Namespace , existingIpamIP .Name ,
161+ prettyFormat (existingIpamIP .Status ))
162+ err = k .Client .Delete (k .Ctx , & existingIpamIP )
163+ if err != nil {
164+ return nil , fmt .Errorf ("failed to delete IP %s/%s: %w" , existingIpamIP .Namespace , existingIpamIP .Name , err )
165+ }
174166
175- k .EventRecorder .Eventf (& existingIpamIP , corev1 .EventTypeNormal , "Deleted" , "Deleted old IPAM IP" )
176- log .Debugf ("Old IP %s/%s deleted from subnet %s" , existingIpamIP .Namespace ,
177- existingIpamIP .Name , existingIpamIP .Spec .Subnet .Name )
178- } else {
179- // IP already exists
180- return & existingIpamIP , nil
167+ err = k .waitForDeletion (& existingIpamIP )
168+ if err != nil {
169+ return nil , fmt .Errorf ("failed to delete IP %s/%s: %w" , existingIpamIP .Namespace , existingIpamIP .Name , err )
181170 }
171+
172+ k .EventRecorder .Eventf (& existingIpamIP , corev1 .EventTypeNormal , "Deleted" , "Deleted old IPAM IP" )
173+ log .Debugf ("Old IP %s/%s deleted from subnet %s" , existingIpamIP .Namespace ,
174+ existingIpamIP .Name , existingIpamIP .Spec .Subnet .Name )
175+ } else {
176+ // IP already exists
177+ return & existingIpamIP , nil
182178 }
183179 }
184180
@@ -193,70 +189,54 @@ func (k K8sClient) doCreateIpamIP(
193189 oobLabelKey := strings .Split (k .OobLabel , "=" )[0 ]
194190 oobLabelValue := strings .Split (k .OobLabel , "=" )[1 ]
195191 var ipamIP * ipamv1alpha1.IP
196- if ipaddr .String () == UNKNOWN_IP || ! exactIP {
197- ipamIP = & ipamv1alpha1.IP {
198- ObjectMeta : metav1.ObjectMeta {
199- GenerateName : macKey + "-" + origin + "-" ,
200- Namespace : k .Namespace ,
201- Labels : map [string ]string {
202- "mac" : macKey ,
203- "origin" : origin ,
204- oobLabelKey : oobLabelValue ,
205- },
192+
193+ ipamIP = & ipamv1alpha1.IP {
194+ ObjectMeta : metav1.ObjectMeta {
195+ GenerateName : macKey + "-" + origin + "-" ,
196+ Namespace : k .Namespace ,
197+ Labels : map [string ]string {
198+ "mac" : macKey ,
199+ "origin" : origin ,
200+ oobLabelKey : oobLabelValue ,
206201 },
207- Spec : ipamv1alpha1. IPSpec {
208- Subnet : corev1. LocalObjectReference {
209- Name : subnetName ,
210- } ,
202+ },
203+ Spec : ipamv1alpha1. IPSpec {
204+ Subnet : corev1. LocalObjectReference {
205+ Name : subnetName ,
211206 },
212- }
213- } else {
207+ },
208+ }
209+
210+ if exactIP && ipaddr .String () != UNKNOWN_IP {
214211 ip , _ := ipamv1alpha1 .IPAddrFromString (ipaddr .String ())
215- ipamIP = & ipamv1alpha1.IP {
216- ObjectMeta : metav1.ObjectMeta {
217- GenerateName : macKey + "-" + origin + "-" ,
218- Namespace : k .Namespace ,
219- Labels : map [string ]string {
220- "mac" : macKey ,
221- "origin" : origin ,
222- oobLabelKey : oobLabelValue ,
223- },
224- },
225- Spec : ipamv1alpha1.IPSpec {
226- IP : ip ,
227- Subnet : corev1.LocalObjectReference {
228- Name : subnetName ,
229- },
230- },
231- }
212+ ipamIP .Spec .IP = ip
232213 }
233214
234- err := k .Client .Create (k .Ctx , ipamIP )
235- if err != nil && ! apierrors .IsAlreadyExists (err ) {
236- return nil , fmt .Errorf ("failed to create IP %s/%s: %w" , ipamIP .Namespace , ipamIP .Name , err )
237- } else if apierrors .IsAlreadyExists (err ) {
238- // do not create IP, because the deletion is not yet ready
239- noop ()
240- } else {
241- ipamIP , err = k .waitForCreation (ipamIP )
242- if err != nil {
215+ if err := k .Client .Create (k .Ctx , ipamIP ); err != nil {
216+ if ! apierrors .IsAlreadyExists (err ) {
243217 return nil , fmt .Errorf ("failed to create IP %s/%s: %w" , ipamIP .Namespace , ipamIP .Name , err )
244218 } else {
245- log .Infof ("New IP %s (%s/%s) created in subnet %s" , ipamIP .Status .Reserved .String (),
246- ipamIP .Namespace , ipamIP .Name , ipamIP .Spec .Subnet .Name )
247- k .EventRecorder .Eventf (ipamIP , corev1 .EventTypeNormal , "Created" , "Created IPAM IP" )
248-
249- // update IP attributes
250- createdIpamIP := ipamIP .DeepCopy ()
251- err := k .Client .Get (k .Ctx , client .ObjectKeyFromObject (createdIpamIP ), createdIpamIP )
252- if err != nil && ! apierrors .IsNotFound (err ) {
253- return nil , fmt .Errorf ("failed to get IP %s/%s: %w" , createdIpamIP .Namespace , createdIpamIP .Name , err )
254- }
255- return createdIpamIP , nil
219+ // do not create IP, because the deletion is not yet ready
220+ return nil , nil
256221 }
257222 }
258223
259- return nil , nil
224+ ipamIP , err := k .waitForCreation (ipamIP )
225+ if err != nil {
226+ return nil , fmt .Errorf ("failed to create IP %w" , err )
227+ } else {
228+ log .Infof ("New IP %s (%s/%s) created in subnet %s" , ipamIP .Status .Reserved .String (),
229+ ipamIP .Namespace , ipamIP .Name , ipamIP .Spec .Subnet .Name )
230+ k .EventRecorder .Eventf (ipamIP , corev1 .EventTypeNormal , "Created" , "Created IPAM IP" )
231+
232+ // update IP attributes
233+ createdIpamIP := ipamIP .DeepCopy ()
234+ err := k .Client .Get (k .Ctx , client .ObjectKeyFromObject (createdIpamIP ), createdIpamIP )
235+ if err != nil && ! apierrors .IsNotFound (err ) {
236+ return nil , fmt .Errorf ("failed to get IP %s/%s: %w" , createdIpamIP .Namespace , createdIpamIP .Name , err )
237+ }
238+ return createdIpamIP , nil
239+ }
260240}
261241
262242func (k K8sClient ) waitForDeletion (ipamIP * ipamv1alpha1.IP ) error {
@@ -421,5 +401,3 @@ func checkIPInCIDR(ip net.IP, cidrStr string) bool {
421401 // Check if the CIDR contains the IP
422402 return cidrNet .Contains (ip )
423403}
424-
425- func noop () {}
0 commit comments