Skip to content

Commit

Permalink
refactor: use k8s.io/utils instead of specific functions (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
antrema authored Sep 12, 2024
1 parent 8fe6410 commit d096950
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 46 deletions.
25 changes: 3 additions & 22 deletions internal/controller/pdns_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
}
21 changes: 9 additions & 12 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
19 changes: 7 additions & 12 deletions internal/controller/zone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit d096950

Please sign in to comment.