@@ -24,7 +24,7 @@ import (
24
24
"errors"
25
25
"fmt"
26
26
"io"
27
- "math/rand"
27
+ "math/rand/v2 "
28
28
"net"
29
29
"net/http"
30
30
"net/url"
@@ -223,11 +223,12 @@ func (r *IPReconciler) getIPSource(ctx context.Context, source cloudflareoperato
223
223
extractedIP = match [1 ]
224
224
}
225
225
226
+ extractedIP = strings .TrimSpace (extractedIP )
226
227
if net .ParseIP (extractedIP ) == nil {
227
228
return "" , fmt .Errorf ("ip from source %s is invalid: %s" , source .URL , extractedIP )
228
229
}
229
230
230
- return strings . TrimSpace ( extractedIP ) , nil
231
+ return extractedIP , nil
231
232
}
232
233
233
234
// handleStatic handles the static ip
@@ -249,19 +250,21 @@ func (r *IPReconciler) handleDynamic(ctx context.Context, ip *cloudflareoperator
249
250
if len (ip .Spec .IPSources ) == 0 {
250
251
return errors .New ("IP sources are required for dynamic IPs" )
251
252
}
252
- if len (ip .Spec .IPSources ) > 1 {
253
- rand .Shuffle (len (ip .Spec .IPSources ), func (i , j int ) {
254
- ip .Spec .IPSources [i ], ip .Spec .IPSources [j ] = ip .Spec .IPSources [j ], ip .Spec .IPSources [i ]
255
- })
256
- }
253
+ // DeepCopy the ip sources to avoid modifying the original slice which would cause the object to be updated on every reconcile
254
+ // which would lead to an infinite loop
255
+ ipSources := ip .Spec .DeepCopy ().IPSources
256
+ rand .Shuffle (len (ipSources ), func (i , j int ) {
257
+ ipSources [i ], ipSources [j ] = ipSources [j ], ipSources [i ]
258
+ })
257
259
var ipSourceError error
258
- for _ , source := range ip . Spec . IPSources {
260
+ for _ , source := range ipSources {
259
261
response , err := r .getIPSource (ctx , source )
260
262
if err != nil {
261
263
ipSourceError = err
262
264
continue
263
265
}
264
266
ip .Spec .Address = response
267
+ ipSourceError = nil
265
268
break
266
269
}
267
270
if ipSourceError != nil {
0 commit comments