From 7200bb243863b9e008cf95a660090b73b9b53e60 Mon Sep 17 00:00:00 2001 From: Adyanth H Date: Fri, 15 Apr 2022 12:47:03 +0530 Subject: [PATCH] :bug: Prevent deleting the DNS entry --- controllers/cloudflare_api.go | 6 +++++- controllers/service_controller.go | 15 ++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/controllers/cloudflare_api.go b/controllers/cloudflare_api.go index 65d6005..20d4b53 100644 --- a/controllers/cloudflare_api.go +++ b/controllers/cloudflare_api.go @@ -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 diff --git a/controllers/service_controller.go b/controllers/service_controller.go index a059f7e..c2ec53a 100644 --- a/controllers/service_controller.go +++ b/controllers/service_controller.go @@ -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 } @@ -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 }