Skip to content

Commit

Permalink
Merge pull request #598 from etclabscore/devp2p-dns-to-cloudflare-rec…
Browse files Browse the repository at this point in the history
…ord-quota

cmd/devp2p: avoid exceeding cloudflare record quota
  • Loading branch information
meowsbits authored Dec 7, 2023
2 parents 34b35bb + 3f9293c commit 627b882
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions cmd/devp2p/dns_cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,41 @@ func (c *cloudflareClient) uploadRecords(name string, records map[string]string)
}
existing[strings.ToLower(entry.Name)] = entry
}
// if the record exists on cloudflare, but is not in our local records, it is stale
stales := make(map[string]cloudflare.DNSRecord)
for path, entry := range existing {
if _, ok := records[path]; !ok {
stales[path] = entry
}
}
// firstCloudflareRecord is a helper function returning the first path:record value from a path:DNSRecord map.
firstCloudflareRecord := func(cfPathRecordMap map[string]cloudflare.DNSRecord) (string, cloudflare.DNSRecord) {
for path, entry := range cfPathRecordMap {
return path, entry
}
return "", cloudflare.DNSRecord{}
}

// Iterate over the new records and inject anything missing.
log.Info("Updating DNS entries")
created := 0
updated := 0
skipped := 0
deleted := 0
for path, val := range records {
old, exists := existing[path]
if !exists {
// Entry is unknown, push a new one to Cloudflare.
// Entry is unknown, push a new one to Cloudflare after removing first stale record, if any.
// We delete any one stale record before creating a new one to avoid exceeding the Cloudflare
// record quota.
if path, entry := firstCloudflareRecord(stales); path != "" {
log.Debug(fmt.Sprintf("Deleting %s = %q", path, entry.Content))
deleted++
if err := c.DeleteDNSRecord(context.Background(), c.zoneID, entry.ID); err != nil {
return fmt.Errorf("failed to delete %s: %v", path, err)
}
delete(stales, path)
}
log.Debug(fmt.Sprintf("Creating %s = %q", path, val))
created++
ttl := rootTTL
Expand All @@ -157,21 +182,17 @@ func (c *cloudflareClient) uploadRecords(name string, records map[string]string)
return fmt.Errorf("failed to publish %s: %v", path, err)
}
}
log.Info("Updated DNS entries", "new", created, "updated", updated, "untouched", skipped)
// Iterate over the old records and delete anything stale.
deleted := 0
log.Info("Updated DNS entries", "new", created, "updated", updated, "untouched", skipped, "deleted", deleted)
// Iterate over the old records and delete anything left stale.
deleted = 0
log.Info("Deleting stale DNS entries")
for path, entry := range existing {
if _, ok := records[path]; ok {
continue
}
// Stale entry, nuke it.
for path, entry := range stales {
log.Debug(fmt.Sprintf("Deleting %s = %q", path, entry.Content))
deleted++
if err := c.DeleteDNSRecord(context.Background(), c.zoneID, entry.ID); err != nil {
return fmt.Errorf("failed to delete %s: %v", path, err)
}
}
log.Info("Deleted stale DNS entries", "count", deleted)
log.Info("Deleted remaining stale DNS entries", "count", deleted)
return nil
}

0 comments on commit 627b882

Please sign in to comment.