From 8ed036f03f7b9249c8fde2cfb1b121a925058b12 Mon Sep 17 00:00:00 2001 From: Anthony Treuillier Date: Wed, 6 Nov 2024 09:56:12 +0100 Subject: [PATCH] fix: catalog have to be canonical (#48) Signed-off-by: Anthony TREUILLIER --- internal/controller/pdns_helper.go | 8 ++++++-- internal/controller/zone_controller.go | 18 +++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/internal/controller/pdns_helper.go b/internal/controller/pdns_helper.go index fde91de..5b0b24a 100644 --- a/internal/controller/pdns_helper.go +++ b/internal/controller/pdns_helper.go @@ -42,7 +42,7 @@ type PdnsClienter struct { // zoneIsIdenticalToExternalZone return True, True if respectively kind and Catalog are identical // and nameservers are identical between Zone and External Resource func zoneIsIdenticalToExternalZone(zone *dnsv1alpha1.Zone, externalZone *powerdns.Zone, ns []string) (bool, bool) { - zoneCatalog := ptr.Deref(zone.Spec.Catalog, "") + zoneCatalog := makeCanonical(ptr.Deref(zone.Spec.Catalog, "")) externalZoneCatalog := ptr.Deref(externalZone.Catalog, "") return zone.Spec.Kind == string(*externalZone.Kind) && zoneCatalog == externalZoneCatalog, reflect.DeepEqual(zone.Spec.Nameservers, ns) } @@ -71,7 +71,11 @@ func rrsetIsIdenticalToExternalRRset(rrset *dnsv1alpha1.RRset, externalRecord po } func makeCanonical(in string) string { - return fmt.Sprintf("%s.", strings.TrimSuffix(in, ".")) + var result string + if in != "" { + result = fmt.Sprintf("%s.", strings.TrimSuffix(in, ".")) + } + return result } func getRRsetName(rrset *dnsv1alpha1.RRset) string { diff --git a/internal/controller/zone_controller.go b/internal/controller/zone_controller.go index 54bc62a..be81dc5 100644 --- a/internal/controller/zone_controller.go +++ b/internal/controller/zone_controller.go @@ -203,12 +203,18 @@ func (r *ZoneReconciler) deleteExternalResources(ctx context.Context, zone *dnsv func (r *ZoneReconciler) updateExternalResources(ctx context.Context, zone *dnsv1alpha1.Zone) error { log := log.FromContext(ctx) zoneKind := powerdns.ZoneKind(zone.Spec.Kind) - catalog := ptr.Deref(zone.Spec.Catalog, "") + + // Make Catalog canonical + var catalog *string + if zone.Spec.Catalog != nil { + catalog = ptr.To(makeCanonical(ptr.Deref(zone.Spec.Catalog, ""))) + } + err := r.PDNSClient.Zones.Change(ctx, zone.ObjectMeta.Name, &powerdns.Zone{ Name: &zone.ObjectMeta.Name, Kind: &zoneKind, Nameservers: zone.Spec.Nameservers, - Catalog: &catalog, + Catalog: catalog, }) if err != nil { log.Error(err, "Failed to update zone") @@ -240,6 +246,12 @@ func (r *ZoneReconciler) createExternalResources(ctx context.Context, zone *dnsv zone.Spec.Nameservers[i] = makeCanonical(ns) } + // Make Catalog canonical + var catalog *string + if zone.Spec.Catalog != nil { + catalog = ptr.To(makeCanonical(ptr.Deref(zone.Spec.Catalog, ""))) + } + z := powerdns.Zone{ ID: &zone.Name, Name: &zone.Name, @@ -249,7 +261,7 @@ func (r *ZoneReconciler) createExternalResources(ctx context.Context, zone *dnsv // SOAEditAPI: &soaEditApi, // APIRectify: &apiRectify, Nameservers: zone.Spec.Nameservers, - Catalog: zone.Spec.Catalog, + Catalog: catalog, } _, err := r.PDNSClient.Zones.Add(ctx, &z)