Skip to content

Commit 3912ed3

Browse files
authored
Merge pull request #391 from containeroo/fix/ip
Fix ip controller issues
2 parents 3b42bf6 + b3726dd commit 3912ed3

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

internal/controller/ingress_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,10 @@ func (r *IngressReconciler) parseAnnotations(annotations map[string]string) clou
179179

180180
// createDNSRecord creates a DNSRecord object
181181
func (r *IngressReconciler) createDNSRecord(ctx context.Context, ingress *networkingv1.Ingress, dnsRecordSpec cloudflareoperatoriov1.DNSRecordSpec) error {
182+
replacer := strings.NewReplacer(".", "-", "*", "wildcard")
182183
dnsRecord := &cloudflareoperatoriov1.DNSRecord{
183184
ObjectMeta: metav1.ObjectMeta{
184-
Name: strings.ReplaceAll(dnsRecordSpec.Name, ".", "-"),
185+
Name: replacer.Replace(dnsRecordSpec.Name),
185186
Namespace: ingress.Namespace,
186187
Labels: map[string]string{
187188
"app.kubernetes.io/managed-by": "cloudflare-operator",

internal/controller/ip_controller.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"errors"
2525
"fmt"
2626
"io"
27-
"math/rand"
27+
"math/rand/v2"
2828
"net"
2929
"net/http"
3030
"net/url"
@@ -223,11 +223,12 @@ func (r *IPReconciler) getIPSource(ctx context.Context, source cloudflareoperato
223223
extractedIP = match[1]
224224
}
225225

226+
extractedIP = strings.TrimSpace(extractedIP)
226227
if net.ParseIP(extractedIP) == nil {
227228
return "", fmt.Errorf("ip from source %s is invalid: %s", source.URL, extractedIP)
228229
}
229230

230-
return strings.TrimSpace(extractedIP), nil
231+
return extractedIP, nil
231232
}
232233

233234
// handleStatic handles the static ip
@@ -249,19 +250,21 @@ func (r *IPReconciler) handleDynamic(ctx context.Context, ip *cloudflareoperator
249250
if len(ip.Spec.IPSources) == 0 {
250251
return errors.New("IP sources are required for dynamic IPs")
251252
}
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+
})
257259
var ipSourceError error
258-
for _, source := range ip.Spec.IPSources {
260+
for _, source := range ipSources {
259261
response, err := r.getIPSource(ctx, source)
260262
if err != nil {
261263
ipSourceError = err
262264
continue
263265
}
264266
ip.Spec.Address = response
267+
ipSourceError = nil
265268
break
266269
}
267270
if ipSourceError != nil {

0 commit comments

Comments
 (0)