Skip to content

Commit

Permalink
🐛 Prevent deleting the DNS entry
Browse files Browse the repository at this point in the history
  • Loading branch information
adyanth committed Apr 15, 2022
1 parent 7f89dfe commit 7200bb2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 5 additions & 1 deletion controllers/cloudflare_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,11 @@ func (c *CloudflareAPI) InsertOrUpdateCName(fqdn, dnsId string) (string, error)
}

// DeleteDNSId deletes DNS entry for the given dnsId
func (c *CloudflareAPI) DeleteDNSId(fqdn, dnsId string) error {
func (c *CloudflareAPI) DeleteDNSId(fqdn, dnsId string, created bool) error {
// Do not delete if we did not create the DNS in this cycle
if !created {
return nil
}
req, _ := http.NewRequest("DELETE", fmt.Sprintf("%szones/%s/dns_records/%s", CLOUDFLARE_ENDPOINT, c.ValidZoneId, dnsId), nil)
if err := c.addAuthHeader(req, false); err != nil {
return err
Expand Down
15 changes: 10 additions & 5 deletions controllers/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,14 @@ func (r *ServiceReconciler) deletionLogic() error {
r.log.Error(err, "DNS ID from TXT and real DNS record does not match", "Hostname", r.config.Hostname)
r.Recorder.Event(r.service, corev1.EventTypeWarning, "FailedDeletingDns", "DNS/TXT ID Mismatch")
} else {
if err := r.cfAPI.DeleteDNSId(r.config.Hostname, dnsTxtResponse.DnsId); err != nil {
if err := r.cfAPI.DeleteDNSId(r.config.Hostname, dnsTxtResponse.DnsId, true); err != nil {
r.log.Info("Failed to delete DNS entry", "Hostname", r.config.Hostname)
r.Recorder.Event(r.service, corev1.EventTypeWarning, "FailedDeletingDns", fmt.Sprintf("Failed to delete DNS entry: %s", err.Error()))
return err
}
r.log.Info("Deleted DNS entry", "Hostname", r.config.Hostname)
r.Recorder.Event(r.service, corev1.EventTypeNormal, "DeletedDns", "Deleted DNS entry")
if err := r.cfAPI.DeleteDNSId(r.config.Hostname, txtId); err != nil {
if err := r.cfAPI.DeleteDNSId(r.config.Hostname, txtId, true); err != nil {
r.Recorder.Event(r.service, corev1.EventTypeWarning, "FailedDeletingTxt", fmt.Sprintf("Failed to delete TXT entry: %s", err.Error()))
return err
}
Expand Down Expand Up @@ -362,13 +362,18 @@ func (r *ServiceReconciler) createDNSLogic() error {
if err := r.cfAPI.InsertOrUpdateTXT(r.config.Hostname, txtId, newDnsId); err != nil {
r.log.Error(err, "Failed to insert/update TXT entry", "Hostname", r.config.Hostname)
r.Recorder.Event(r.service, corev1.EventTypeWarning, "FailedCreatingTxt", fmt.Sprintf("Failed to insert/update TXT entry: %s", err.Error()))
if err := r.cfAPI.DeleteDNSId(r.config.Hostname, newDnsId); err != nil {
if err := r.cfAPI.DeleteDNSId(r.config.Hostname, newDnsId, dnsTxtResponse.DnsId != ""); err != nil {
r.log.Info("Failed to delete DNS entry, left in broken state", "Hostname", r.config.Hostname)
r.Recorder.Event(r.service, corev1.EventTypeWarning, "FailedDeletingDns", "Failed to delete DNS entry, left in broken state")
return err
}
r.Recorder.Event(r.service, corev1.EventTypeWarning, "DeletedDns", "Deleted DNS entry, retrying")
r.log.Info("Deleted DNS entry", "Hostname", r.config.Hostname)
if dnsTxtResponse.DnsId != "" {
r.Recorder.Event(r.service, corev1.EventTypeWarning, "DeletedDns", "Deleted DNS entry, retrying")
r.log.Info("Deleted DNS entry", "Hostname", r.config.Hostname)
} else {
r.Recorder.Event(r.service, corev1.EventTypeWarning, "PreventDeleteDns", "Prevented DNS entry deletion, retrying")
r.log.Info("Did not delete DNS entry", "Hostname", r.config.Hostname)
}
return err
}

Expand Down

0 comments on commit 7200bb2

Please sign in to comment.