Skip to content

Commit

Permalink
fix: catalog have to be canonical (#48)
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony TREUILLIER <[email protected]>
  • Loading branch information
antrema authored Nov 6, 2024
1 parent 5360408 commit 8ed036f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 6 additions & 2 deletions internal/controller/pdns_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 15 additions & 3 deletions internal/controller/zone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 8ed036f

Please sign in to comment.