diff --git a/internal/controller/pdns_helper.go b/internal/controller/pdns_helper.go index 38b2126..98db711 100644 --- a/internal/controller/pdns_helper.go +++ b/internal/controller/pdns_helper.go @@ -18,6 +18,7 @@ import ( "github.com/joeig/go-powerdns/v3" dnsv1alpha1 "github.com/orange-opensource/powerdns-operator/api/v1alpha1" + "k8s.io/utils/ptr" ) type pdnsRecordsClienter interface { @@ -41,13 +42,8 @@ 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) { - var zoneCatalog, externalZoneCatalog string - if zone.Spec.Catalog != nil { - zoneCatalog = *zone.Spec.Catalog - } - if externalZone.Catalog != nil { - externalZoneCatalog = *externalZone.Catalog - } + zoneCatalog := 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) } @@ -76,18 +72,3 @@ func rrsetIsIdenticalToExternalRRset(rrset *dnsv1alpha1.RRset, externalRecord po func makeCanonical(in string) string { return fmt.Sprintf("%s.", strings.TrimSuffix(in, ".")) } - -// Bool is a helper function that allocates a new bool value to store v and returns a pointer to it. -func Bool(v bool) *bool { - return &v -} - -// RRType is a helper function that allocates a new RRType value to store t and returns a pointer to it. -func RRType(t powerdns.RRType) *powerdns.RRType { - return &t -} - -// Uint32 is a helper function that allocates a new uint32 value to store i and returns a pointer to it. -func Uint32(i uint32) *uint32 { - return &i -} diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index e0d946a..850b914 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -29,6 +29,7 @@ import ( "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" + "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" @@ -223,13 +224,13 @@ func (m mockZonesClient) Add(ctx context.Context, zone *powerdns.Zone) (*powerdn zoneCanonicalName := makeCanonical(*zone.Name) rrset := powerdns.RRset{ Name: &zoneCanonicalName, - TTL: Uint32(DEFAULT_TTL_FOR_NS_RECORDS), - Type: RRType(powerdns.RRTypeNS), + TTL: ptr.To(DEFAULT_TTL_FOR_NS_RECORDS), + Type: ptr.To(powerdns.RRTypeNS), Records: []powerdns.Record{}, } for _, ns := range zone.Nameservers { nsName := ns - rrset.Records = append(rrset.Records, powerdns.Record{Content: &nsName, Disabled: Bool(false), SetPTR: Bool(false)}) + rrset.Records = append(rrset.Records, powerdns.Record{Content: &nsName, Disabled: ptr.To(false), SetPTR: ptr.To(false)}) } writeToRecordsMap(zoneCanonicalName, &rrset) writeToZonesMap(zoneCanonicalName, zone) @@ -259,7 +260,7 @@ func (m mockZonesClient) Change(ctx context.Context, domain string, zone *powerd } serial := localZone.Serial if *zone.Kind != *localZone.Kind || *zone.Catalog != *localZone.Catalog { - serial = Uint32(*localZone.Serial + uint32(1)) + serial = ptr.To(*localZone.Serial + uint32(1)) } zone.Serial = serial @@ -323,14 +324,14 @@ func (m mockRecordsClient) Change(ctx context.Context, domain string, name strin for _, c := range content { localContent := c - r := powerdns.Record{Content: &localContent, Disabled: Bool(false), SetPTR: Bool(false)} + r := powerdns.Record{Content: &localContent, Disabled: ptr.To(false), SetPTR: ptr.To(false)} rrset.Records = append(rrset.Records, r) } writeToRecordsMap(makeCanonical(name), rrset) if !isRRsetIdentical || isNewRRset { if zone, ok := readFromZonesMap(makeCanonical(domain)); ok { - zone.Serial = Uint32(*zone.Serial + uint32(1)) + zone.Serial = ptr.To(*zone.Serial + uint32(1)) writeToZonesMap(makeCanonical(domain), zone) } } @@ -354,9 +355,7 @@ func getMockedNameservers(zoneName string) (result []string) { func getMockedKind(zoneName string) (result string) { zone, _ := readFromZonesMap(makeCanonical(zoneName)) - if zone.Kind != nil { - result = string(*zone.Kind) - } + result = string(ptr.Deref(zone.Kind, "")) return } @@ -393,8 +392,6 @@ func getMockedComment(rrsetName, rrsetType string) (result string) { //nolint:unparam func getMockedCatalog(zoneName string) (result string) { zone, _ := readFromZonesMap(makeCanonical(zoneName)) - if zone.Catalog != nil { - result = *zone.Catalog - } + result = ptr.Deref(zone.Catalog, "") return } diff --git a/internal/controller/zone_controller.go b/internal/controller/zone_controller.go index 09d9c10..54bc62a 100644 --- a/internal/controller/zone_controller.go +++ b/internal/controller/zone_controller.go @@ -19,6 +19,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -29,7 +30,7 @@ import ( const ( FINALIZER_NAME = "dns.cav.enablers.ob/finalizer" - DEFAULT_TTL_FOR_NS_RECORDS = 1500 + DEFAULT_TTL_FOR_NS_RECORDS = uint32(1500) ZONE_NOT_FOUND_MSG = "Not Found" ZONE_NOT_FOUND_CODE = 404 @@ -106,7 +107,7 @@ func (r *ZoneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. } } else { // If Zone exists, compare Type and Nameservers and update it if necessary - ns, err := r.PDNSClient.Records.Get(ctx, zone.ObjectMeta.Name, zone.ObjectMeta.Name, RRType(powerdns.RRTypeNS)) + ns, err := r.PDNSClient.Records.Get(ctx, zone.ObjectMeta.Name, zone.ObjectMeta.Name, ptr.To(powerdns.RRTypeNS)) if err != nil { return ctrl.Result{}, err } @@ -132,7 +133,7 @@ func (r *ZoneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. // Nameservers changes if !nsIdentical { - ttl := Uint32(DEFAULT_TTL_FOR_NS_RECORDS) + ttl := ptr.To(DEFAULT_TTL_FOR_NS_RECORDS) if filteredRRset.TTL != nil { ttl = filteredRRset.TTL } @@ -202,10 +203,7 @@ 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 := "" - if zone.Spec.Catalog != nil { - catalog = *zone.Spec.Catalog - } + catalog := ptr.Deref(zone.Spec.Catalog, "") err := r.PDNSClient.Zones.Change(ctx, zone.ObjectMeta.Name, &powerdns.Zone{ Name: &zone.ObjectMeta.Name, Kind: &zoneKind, @@ -246,7 +244,7 @@ func (r *ZoneReconciler) createExternalResources(ctx context.Context, zone *dnsv ID: &zone.Name, Name: &zone.Name, Kind: powerdns.ZoneKindPtr(powerdns.ZoneKind(zone.Spec.Kind)), - DNSsec: Bool(false), + DNSsec: ptr.To(false), // SOAEdit: &soaEdit, // SOAEditAPI: &soaEditApi, // APIRectify: &apiRectify, @@ -266,10 +264,7 @@ func (r *ZoneReconciler) createExternalResources(ctx context.Context, zone *dnsv func (r *ZoneReconciler) patchStatus(ctx context.Context, zone *dnsv1alpha1.Zone, zoneRes *powerdns.Zone) error { original := zone.DeepCopy() - var kind string - if zoneRes.Kind != nil { - kind = string(*zoneRes.Kind) - } + kind := string(ptr.Deref(zoneRes.Kind, "")) zone.Status = dnsv1alpha1.ZoneStatus{ ID: zoneRes.ID, Name: zoneRes.Name,