Skip to content

Commit 621a7c3

Browse files
committed
fix(zone): ignore cloudflare email routing dkim txt record
1 parent 322d211 commit 621a7c3

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

internal/controller/zone_controller.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ import (
4040
"github.com/fluxcd/pkg/runtime/patch"
4141
)
4242

43+
// ignoredRecords are records that should not be pruned
44+
// the key is the record type and the value is a list of prefixes
45+
// TODO: make this configurable in the Zone CR
46+
var ignoredRecords = map[string][]string{
47+
"TXT": {
48+
"_acme-challenge", // Let's Encrypt DNS-01 challenge
49+
"cf2024-1._domainkey", // Cloudflare Email Routing DKIM
50+
},
51+
}
52+
4353
// ZoneReconciler reconciles a Zone object
4454
type ZoneReconciler struct {
4555
client.Client
@@ -153,7 +163,7 @@ func (r *ZoneReconciler) handlePrune(ctx context.Context, zone *cloudflareoperat
153163
}
154164

155165
for _, cfDnsRecord := range cfDnsRecords {
156-
if cfDnsRecord.Type == "TXT" && strings.HasPrefix(cfDnsRecord.Name, "_acme-challenge") {
166+
if _, found := ignoredRecords[cfDnsRecord.Type]; found && hasPrefix(cfDnsRecord.Name, ignoredRecords[cfDnsRecord.Type]) {
157167
continue
158168
}
159169

@@ -172,3 +182,13 @@ func (r *ZoneReconciler) reconcileDelete(zone *cloudflareoperatoriov1.Zone) {
172182
metrics.ZoneFailureCounter.DeleteLabelValues(zone.Name, zone.Spec.Name)
173183
controllerutil.RemoveFinalizer(zone, cloudflareoperatoriov1.CloudflareOperatorFinalizer)
174184
}
185+
186+
// hasPrefix checks if the name has any of the prefixes
187+
func hasPrefix(name string, prefixes []string) bool {
188+
for _, prefix := range prefixes {
189+
if strings.HasPrefix(name, prefix) {
190+
return true
191+
}
192+
}
193+
return false
194+
}

internal/controller/zone_controller_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,33 @@ func TestZoneReconciler_reconcileZone(t *testing.T) {
8686

8787
zone.Spec.Prune = true
8888

89-
_ = r.reconcileZone(context.TODO(), zone)
89+
acmeRecord, err := cf.CreateDNSRecord(context.TODO(), cloudflare.ZoneIdentifier(zoneID), cloudflare.CreateDNSRecordParams{
90+
Name: "_acme-challenge.abc.containeroo-test.org",
91+
Type: "TXT",
92+
Content: "test",
93+
})
94+
g.Expect(err).ToNot(HaveOccurred())
95+
dkimRecord, err := cf.CreateDNSRecord(context.TODO(), cloudflare.ZoneIdentifier(zoneID), cloudflare.CreateDNSRecordParams{
96+
Name: "cf2024-1._domainkey.containeroo-test.org",
97+
Type: "TXT",
98+
Content: "test",
99+
})
100+
g.Expect(err).ToNot(HaveOccurred())
90101

91-
g.Expect(zone.Status.Conditions).To(conditions.MatchConditions([]metav1.Condition{
92-
*conditions.TrueCondition(cloudflareoperatoriov1.ConditionTypeReady, cloudflareoperatoriov1.ConditionReasonReady, "Zone is ready"),
93-
}))
94-
g.Expect(zone.Status.ID).To(Equal(zoneID))
102+
_ = r.reconcileZone(context.TODO(), zone)
95103

96-
_, err := cf.GetDNSRecord(context.TODO(), cloudflare.ZoneIdentifier(zone.Status.ID), testRecord.ID)
104+
_, err = cf.GetDNSRecord(context.TODO(), cloudflare.ZoneIdentifier(zone.Status.ID), testRecord.ID)
97105
g.Expect(err.Error()).To(ContainSubstring("Record does not exist"))
106+
107+
_, err = cf.GetDNSRecord(context.TODO(), cloudflare.ZoneIdentifier(zone.Status.ID), acmeRecord.ID)
108+
g.Expect(err).ToNot(HaveOccurred())
109+
_, err = cf.GetDNSRecord(context.TODO(), cloudflare.ZoneIdentifier(zone.Status.ID), dkimRecord.ID)
110+
g.Expect(err).ToNot(HaveOccurred())
111+
112+
err = cf.DeleteDNSRecord(context.TODO(), cloudflare.ZoneIdentifier(zoneID), acmeRecord.ID)
113+
g.Expect(err).ToNot(HaveOccurred())
114+
err = cf.DeleteDNSRecord(context.TODO(), cloudflare.ZoneIdentifier(zoneID), dkimRecord.ID)
115+
g.Expect(err).ToNot(HaveOccurred())
98116
})
99117

100118
t.Run("reconcile zone error zone not found", func(t *testing.T) {

0 commit comments

Comments
 (0)