Skip to content

Commit eb54ab3

Browse files
committed
chore: refactor ip change logic
1 parent ce5c40f commit eb54ab3

File tree

4 files changed

+42
-28
lines changed

4 files changed

+42
-28
lines changed

api/v1/dnsrecord_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ type DNSRecordStatus struct {
7676
RecordID string `json:"recordID,omitempty"`
7777
}
7878

79+
const (
80+
// IPRefIndexKey is the key used for indexing DNSRecord objects by their IPRef.
81+
IPRefIndexKey string = ".spec.ipRef.name"
82+
)
83+
7984
// +kubebuilder:object:root=true
8085
// +kubebuilder:subresource:status
8186

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func main() {
152152
Client: mgr.GetClient(),
153153
Scheme: mgr.GetScheme(),
154154
Cf: &cf,
155-
}).SetupWithManager(mgr); err != nil {
155+
}).SetupWithManager(ctx, mgr); err != nil {
156156
setupLog.Error(err, "unable to create controller", "controller", "DNSRecord")
157157
os.Exit(1)
158158
}

internal/controller/dnsrecord_controller.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
ctrl "sigs.k8s.io/controller-runtime"
3434
"sigs.k8s.io/controller-runtime/pkg/client"
3535
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
36+
"sigs.k8s.io/controller-runtime/pkg/handler"
37+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3638

3739
"github.com/cloudflare/cloudflare-go"
3840
cloudflareoperatoriov1 "github.com/containeroo/cloudflare-operator/api/v1"
@@ -48,9 +50,18 @@ type DNSRecordReconciler struct {
4850
}
4951

5052
// SetupWithManager sets up the controller with the Manager.
51-
func (r *DNSRecordReconciler) SetupWithManager(mgr ctrl.Manager) error {
53+
func (r *DNSRecordReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
54+
if err := mgr.GetFieldIndexer().IndexField(ctx, &cloudflareoperatoriov1.DNSRecord{}, cloudflareoperatoriov1.IPRefIndexKey,
55+
func(o client.Object) []string {
56+
dnsRecord := o.(*cloudflareoperatoriov1.DNSRecord)
57+
return []string{dnsRecord.Spec.IPRef.Name}
58+
}); err != nil {
59+
return err
60+
}
61+
5262
return ctrl.NewControllerManagedBy(mgr).
5363
For(&cloudflareoperatoriov1.DNSRecord{}).
64+
Watches(&cloudflareoperatoriov1.IP{}, handler.EnqueueRequestsFromMapFunc(r.requestsForIPChange)).
5465
Complete(r)
5566
}
5667

@@ -372,3 +383,27 @@ func compareDNSRecord(dnsRecordSpec cloudflareoperatoriov1.DNSRecordSpec, existi
372383

373384
return isEqual
374385
}
386+
387+
// requestsForIPChange returns a list of reconcile.Requests for DNSRecords that need to be reconciled
388+
func (r *DNSRecordReconciler) requestsForIPChange(ctx context.Context, o client.Object) []reconcile.Request {
389+
ip, ok := o.(*cloudflareoperatoriov1.IP)
390+
if !ok {
391+
err := fmt.Errorf("expected a DNSRecord, got %T", o)
392+
ctrl.LoggerFrom(ctx).Error(err, "failed to get requests for IP change")
393+
return nil
394+
}
395+
396+
var dnsRecords cloudflareoperatoriov1.DNSRecordList
397+
if err := r.List(ctx, &dnsRecords, client.MatchingFields{
398+
cloudflareoperatoriov1.IPRefIndexKey: client.ObjectKeyFromObject(ip).Name,
399+
}); err != nil {
400+
ctrl.LoggerFrom(ctx).Error(err, "failed to list DNSRecords for IP change")
401+
return nil
402+
}
403+
404+
reqs := make([]reconcile.Request, 0, len(dnsRecords.Items))
405+
for i := range dnsRecords.Items {
406+
reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&dnsRecords.Items[i])})
407+
}
408+
return reqs
409+
}

internal/controller/ip_controller.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,6 @@ func (r *IPReconciler) reconcileIP(ctx context.Context, ip *cloudflareoperatorio
119119
}
120120
}
121121

122-
if err := r.updateDNSRecords(ctx, ip); err != nil {
123-
r.markFailed(ip, err)
124-
return ctrl.Result{}
125-
}
126-
127122
apimeta.SetStatusCondition(&ip.Status.Conditions, metav1.Condition{
128123
Type: "Ready",
129124
Status: "True",
@@ -141,27 +136,6 @@ func (r *IPReconciler) reconcileIP(ctx context.Context, ip *cloudflareoperatorio
141136
return ctrl.Result{}
142137
}
143138

144-
// updateDNSRecords updates the DNS records with the new IP
145-
func (r *IPReconciler) updateDNSRecords(ctx context.Context, ip *cloudflareoperatoriov1.IP) error {
146-
dnsRecords := &cloudflareoperatoriov1.DNSRecordList{}
147-
if err := r.List(ctx, dnsRecords); err != nil {
148-
return err
149-
}
150-
for _, dnsRecord := range dnsRecords.Items {
151-
if dnsRecord.Spec.IPRef.Name != ip.Name {
152-
continue
153-
}
154-
if dnsRecord.Spec.Content == ip.Spec.Address {
155-
continue
156-
}
157-
dnsRecord.Spec.Content = ip.Spec.Address
158-
if err := r.Update(ctx, &dnsRecord); err != nil {
159-
return err
160-
}
161-
}
162-
return nil
163-
}
164-
165139
// getIPSource returns the IP gathered from the IPSource
166140
func (r *IPReconciler) getIPSource(ctx context.Context, source cloudflareoperatoriov1.IPSpecIPSources) (string, error) {
167141
log := ctrl.LoggerFrom(ctx)

0 commit comments

Comments
 (0)