Skip to content

Commit

Permalink
Optimize logging for repo controller (#438)
Browse files Browse the repository at this point in the history
Log from context directly.

---------

Signed-off-by: efiacor <[email protected]>
  • Loading branch information
efiacor authored Nov 24, 2023
1 parent 9ec4452 commit 6068346
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions controllers/pkg/reconcilers/repository/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"reflect"

"code.gitea.io/sdk/gitea"
"github.com/go-logr/logr"
commonv1alpha1 "github.com/nephio-project/api/common/v1alpha1"
infrav1alpha1 "github.com/nephio-project/api/infra/v1alpha1"
"github.com/nephio-project/nephio/controllers/pkg/giteaclient"
Expand All @@ -42,9 +41,7 @@ func init() {
}

const (
finalizer = "infra.nephio.org/finalizer"
// errors
errGetCr = "cannot get cr"
finalizer = "infra.nephio.org/finalizer"
errUpdateStatus = "cannot update status"
)

Expand Down Expand Up @@ -86,19 +83,17 @@ type reconciler struct {
resource.APIPatchingApplicator
giteaClient giteaclient.GiteaClient
finalizer *resource.APIFinalizer

l logr.Logger
}

func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.l = log.FromContext(ctx)
r.l.Info("reconcile", "req", req)
log := log.FromContext(ctx)
log.Info("reconcile", "req", req)

cr := &infrav1alpha1.Repository{}
if err := r.Get(ctx, req.NamespacedName, cr); err != nil {
// if the resource no longer exists the reconcile loop is done
if resource.IgnoreNotFound(err) != nil {
r.l.Error(err, "cannot get resource")
log.Error(err, "cannot get resource")
return ctrl.Result{}, errors.Wrap(resource.IgnoreNotFound(err), "cannot get resource")
}
return ctrl.Result{}, nil
Expand All @@ -108,7 +103,7 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
giteaClient := r.giteaClient.Get()
if giteaClient == nil {
err := fmt.Errorf("gitea server unreachable")
r.l.Error(err, "cannot connect to git server")
log.Error(err, "cannot connect to git server")
cr.SetConditions(infrav1alpha1.Failed(err.Error()))
return ctrl.Result{Requeue: true}, errors.Wrap(r.Status().Update(ctx, cr), errUpdateStatus)
}
Expand All @@ -120,24 +115,24 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
// when successful remove the finalizer
if cr.Spec.Lifecycle.DeletionPolicy == commonv1alpha1.DeletionDelete {
if err := r.deleteRepo(ctx, r.giteaClient, cr); err != nil {
r.l.Error(err, "cannot delete repo in git server")
log.Error(err, "cannot delete repo in git server")
return ctrl.Result{Requeue: true}, errors.Wrap(r.Status().Update(ctx, cr), errUpdateStatus)
}
}

if err := r.finalizer.RemoveFinalizer(ctx, cr); err != nil {
r.l.Error(err, "cannot remove finalizer")
log.Error(err, "cannot remove finalizer")
cr.SetConditions(infrav1alpha1.Failed(err.Error()))
return ctrl.Result{Requeue: true}, errors.Wrap(r.Status().Update(ctx, cr), errUpdateStatus)
}

r.l.Info("Successfully deleted resource")
log.Info("Successfully deleted resource")
return ctrl.Result{Requeue: false}, nil
}

// add finalizer to avoid deleting the repo w/o it being deleted from the git server
if err := r.finalizer.AddFinalizer(ctx, cr); err != nil {
r.l.Error(err, "cannot add finalizer")
log.Error(err, "cannot add finalizer")
cr.SetConditions(infrav1alpha1.Failed(err.Error()))
return ctrl.Result{Requeue: true}, errors.Wrap(r.Status().Update(ctx, cr), errUpdateStatus)
}
Expand All @@ -151,9 +146,10 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
}

func (r *reconciler) upsertRepo(ctx context.Context, giteaClient giteaclient.GiteaClient, cr *infrav1alpha1.Repository) error {
log := log.FromContext(ctx)
u, _, err := giteaClient.GetMyUserInfo()
if err != nil {
r.l.Error(err, "cannot get user info")
log.Error(err, "cannot get user info")
cr.SetConditions(infrav1alpha1.Failed(err.Error()))
return err
}
Expand Down Expand Up @@ -187,17 +183,17 @@ func (r *reconciler) upsertRepo(ctx context.Context, giteaClient giteaclient.Git
createRepo.TrustModel = gitea.TrustModel(*cr.Spec.TrustModel)
}
createRepo.AutoInit = true
r.l.Info("repository", "config", createRepo)
log.Info("repository", "config", createRepo)

repo, _, err := giteaClient.CreateRepo(createRepo)
if err != nil {
r.l.Error(err, "cannot create repo")
log.Error(err, "cannot create repo")
// Here we don't provide the full error since the message change every time and this will re-trigger
// a new reconcile loop
cr.SetConditions(infrav1alpha1.Failed("cannot create repo"))
return err
}
r.l.Info("repo created", "name", cr.GetName())
log.Info("repo created", "name", cr.GetName())
cr.Status.URL = &repo.CloneURL
return nil
}
Expand All @@ -214,32 +210,33 @@ func (r *reconciler) upsertRepo(ctx context.Context, giteaClient giteaclient.Git
}
repo, _, err := giteaClient.EditRepo(u.UserName, cr.GetName(), editRepo)
if err != nil {
r.l.Error(err, "cannot update repo")
log.Error(err, "cannot update repo")
// Here we don't provide the full error since the message change every time and this will re-trigger
// a new reconcile loop
cr.SetConditions(infrav1alpha1.Failed("cannot update repo"))
return err
}
r.l.Info("repo updated", "name", cr.GetName())
log.Info("repo updated", "name", cr.GetName())
cr.Status.URL = &repo.CloneURL

return nil
}

func (r *reconciler) deleteRepo(ctx context.Context, giteaClient giteaclient.GiteaClient, cr *infrav1alpha1.Repository) error {
log := log.FromContext(ctx)
u, _, err := giteaClient.GetMyUserInfo()
if err != nil {
r.l.Error(err, "cannot get user info")
log.Error(err, "cannot get user info")
cr.SetConditions(infrav1alpha1.Failed(err.Error()))
return err
}

_, err = giteaClient.DeleteRepo(u.UserName, cr.GetName())
if err != nil {
r.l.Error(err, "cannot delete repo")
log.Error(err, "cannot delete repo")
cr.SetConditions(infrav1alpha1.Failed(err.Error()))
return err
}
r.l.Info("repo deleted", "name", cr.GetName())
log.Info("repo deleted", "name", cr.GetName())
return nil
}

0 comments on commit 6068346

Please sign in to comment.