Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add logging visibility #7

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/controller/rrset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (r *RRsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
if controllerutil.ContainsFinalizer(rrset, FINALIZER_NAME) {
controllerutil.RemoveFinalizer(rrset, FINALIZER_NAME)
if err := r.Update(ctx, rrset); err != nil {
log.Error(err, "Failed to remove finalizer")
return ctrl.Result{}, err
}
}
Expand All @@ -71,6 +72,7 @@ func (r *RRsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
// Requeue after few seconds
return ctrl.Result{RequeueAfter: 2 * time.Second}, nil
} else {
log.Error(err, "Failed to get zone")
return ctrl.Result{}, err
}
}
Expand All @@ -84,6 +86,7 @@ func (r *RRsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
controllerutil.AddFinalizer(rrset, FINALIZER_NAME)
lastUpdateTime = &metav1.Time{Time: time.Now().UTC()}
if err := r.Update(ctx, rrset); err != nil {
log.Error(err, "Failed to add finalizer")
return ctrl.Result{}, err
}
}
Expand All @@ -94,12 +97,14 @@ func (r *RRsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
if err := r.deleteExternalResources(ctx, zone, rrset); err != nil {
// if fail to delete the external resource, return with error
// so that it can be retried
log.Error(err, "Failed to delete external resources")
return ctrl.Result{}, err
}

// remove our finalizer from the list and update it.
controllerutil.RemoveFinalizer(rrset, FINALIZER_NAME)
if err := r.Update(ctx, rrset); err != nil {
log.Error(err, "Failed to remove finalizer")
return ctrl.Result{}, err
}
lastUpdateTime = &metav1.Time{Time: time.Now().UTC()}
Expand All @@ -112,6 +117,7 @@ func (r *RRsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
// Create or Update
changed, err := r.createOrUpdateExternalResources(ctx, zone, rrset)
if err != nil {
log.Error(err, "Failed to create or update external resources")
return ctrl.Result{}, err
}
if changed {
Expand All @@ -121,8 +127,10 @@ func (r *RRsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
// Set OwnerReference
if err := r.ownObject(ctx, zone, rrset); err != nil {
if errors.IsConflict(err) {
log.Info("Conflict on RRSet owner reference, retrying")
return ctrl.Result{Requeue: true}, nil
}
log.Error(err, "Failed to set owner reference")
return ctrl.Result{}, err
}

Expand Down Expand Up @@ -203,8 +211,11 @@ func (r *RRsetReconciler) createOrUpdateExternalResources(ctx context.Context, z

// ownObject set the owner reference on RRset
func (r *RRsetReconciler) ownObject(ctx context.Context, zone *dnsv1alpha1.Zone, rrset *dnsv1alpha1.RRset) error {
log := log.FromContext(ctx)

err := ctrl.SetControllerReference(zone, rrset, r.Scheme)
if err != nil {
log.Error(err, "Failed to set owner reference. Is there already a controller managing this object?")
return err
}
return r.Update(ctx, rrset)
Expand Down
7 changes: 1 addition & 6 deletions internal/controller/zone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (r *ZoneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
// If Zone does not exist, create it
err := r.createExternalResources(ctx, zone)
if err != nil {
log.Error(err, "Failed to create external resources")
return ctrl.Result{}, err
}
} else {
Expand Down Expand Up @@ -184,26 +185,22 @@ func (r *ZoneReconciler) getExternalResources(ctx context.Context, domain string
return nil, err
}
}

return zoneRes, nil
}

func (r *ZoneReconciler) deleteExternalResources(ctx context.Context, zone *dnsv1alpha1.Zone) error {
log := log.FromContext(ctx)

err := r.PDNSClient.Zones.Delete(ctx, zone.ObjectMeta.Name)
// Zone may have already been deleted and it is not an error
if err != nil && err.Error() != ZONE_NOT_FOUND_MSG {
log.Error(err, "Failed to delete zone")
return err
}

return nil
}

func (r *ZoneReconciler) updateExternalResources(ctx context.Context, zone *dnsv1alpha1.Zone) error {
log := log.FromContext(ctx)

zoneKind := powerdns.ZoneKind(zone.Spec.Kind)
err := r.PDNSClient.Zones.Change(ctx, zone.ObjectMeta.Name, &powerdns.Zone{
Name: &zone.ObjectMeta.Name,
Expand All @@ -214,13 +211,11 @@ func (r *ZoneReconciler) updateExternalResources(ctx context.Context, zone *dnsv
log.Error(err, "Failed to update zone")
return err
}

return nil
}

func (r *ZoneReconciler) updateNsOnExternalResources(ctx context.Context, zone *dnsv1alpha1.Zone, ttl uint32) error {
log := log.FromContext(ctx)

nameserversCanonical := []string{}
for _, n := range zone.Spec.Nameservers {
nameserversCanonical = append(nameserversCanonical, fmt.Sprintf("%s.", n))
Expand Down