Skip to content

Commit

Permalink
Don't log unnecessary object names
Browse files Browse the repository at this point in the history
controller-runtime sets a target object name into logger in default, it
is unenecessary.

Signed-off-by: Toshikuni Fukaya <toshikuni-fukaya@cybozu.co.jp>
  • Loading branch information
toshipp committed Oct 9, 2024
1 parent 085d90a commit 5311316
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
8 changes: 3 additions & 5 deletions internal/controller/mantlebackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (r *MantleBackupReconciler) checkPVCInManagedCluster(ctx context.Context, b
return err
}
if clusterID != r.managedCephClusterID {
logger.Info("clusterID not matched", "namespace", backup.Namespace, "backup", backup.Name, "pvc", pvc.Name, "clusterID", clusterID, "managedCephClusterID", r.managedCephClusterID)
logger.Info("clusterID not matched", "pvc", pvc.Name, "clusterID", clusterID, "managedCephClusterID", r.managedCephClusterID)
return errSkipProcessing
}

Expand Down Expand Up @@ -291,19 +291,17 @@ func (r *MantleBackupReconciler) Reconcile(ctx context.Context, req ctrl.Request

err := r.Get(ctx, req.NamespacedName, &backup)
if errors.IsNotFound(err) {
logger.Info("MantleBackup is not found", "name", backup.Name, "error", err)
logger.Info("MantleBackup is not found", "error", err)
return ctrl.Result{}, nil
}
if err != nil {
logger.Error(err, "failed to get MantleBackup", "name", req.NamespacedName)
logger.Error(err, "failed to get MantleBackup")
return ctrl.Result{}, err
}

if isCreatedWhenMantleControllerWasSecondary(&backup) {
logger.Info(
"skipping to reconcile the MantleBackup created by a remote mantle-controller to prevent accidental data loss",
"name", backup.GetName(),
"namespace", backup.GetNamespace(),
)
return ctrl.Result{}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/mantlebackupconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (r *MantleBackupConfigReconciler) Reconcile(ctx context.Context, req ctrl.R
var mbc mantlev1.MantleBackupConfig
if err := r.Client.Get(ctx, req.NamespacedName, &mbc); err != nil {
if errors.IsNotFound(err) {
logger.Info("MantleBackupConfig not found", "name", req.Name, "namespace", req.Namespace, "error", err)
logger.Info("MantleBackupConfig not found", "error", err)
return ctrl.Result{}, nil
}
return ctrl.Result{}, fmt.Errorf("failed to get MantleBackupConfig: %w", err)
Expand All @@ -91,7 +91,7 @@ func (r *MantleBackupConfigReconciler) Reconcile(ctx context.Context, req ctrl.R
if !mbc.ObjectMeta.DeletionTimestamp.IsZero() {
if controllerutil.ContainsFinalizer(&mbc, MantleBackupConfigFinalizerName) {
// Delete the CronJob. If we failed to delete it because it's not found, ignore the error.
logger.Info("start deleting cronjobs", "name", mbc.Name, "namespace", mbc.Namespace)
logger.Info("start deleting cronjobs")
if err := r.deleteCronJob(ctx, &mbc, cronJobInfo.namespace); err != nil && !errors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("failed to delete cronjob: %w", err)
}
Expand All @@ -114,7 +114,7 @@ func (r *MantleBackupConfigReconciler) Reconcile(ctx context.Context, req ctrl.R
return ctrl.Result{}, fmt.Errorf("failed to get Ceph cluster ID: %s: %s: %w", mbc.Namespace, mbc.Spec.PVC, err)
}
if clusterID != r.managedCephClusterID {
logger.Info("the target pvc is not managed by this controller", "name", req.Name, "namespace", req.Namespace, "error", err)
logger.Info("the target pvc is not managed by this controller", "error", err)
return ctrl.Result{}, nil
}

Expand Down
24 changes: 12 additions & 12 deletions internal/controller/mantlerestore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func (r *MantleRestoreReconciler) Reconcile(ctx context.Context, req ctrl.Reques
var restore mantlev1.MantleRestore
err := r.client.Get(ctx, req.NamespacedName, &restore)
if errors.IsNotFound(err) {
logger.Info("MantleRestore resource not found", "name", req.Name, "error", err)
logger.Info("MantleRestore resource not found", "error", err)
return ctrl.Result{}, nil
}
if err != nil {
logger.Error(err, "failed to get MantleRestore", "name", req.NamespacedName)
logger.Error(err, "failed to get MantleRestore")
return ctrl.Result{}, err
}

Expand All @@ -86,7 +86,7 @@ func (r *MantleRestoreReconciler) Reconcile(ctx context.Context, req ctrl.Reques

func (r *MantleRestoreReconciler) restore(ctx context.Context, restore *mantlev1.MantleRestore) (ctrl.Result, error) {
logger := log.FromContext(ctx)
logger.Info("restoring", "name", restore.Name, "namespace", restore.Namespace, "backup", restore.Spec.Backup)
logger.Info("restoring", "backup", restore.Spec.Backup)

// skip if already ReadyToUse
if meta.IsStatusConditionTrue(restore.Status.Conditions, mantlev1.RestoreConditionReadyToUse) {
Expand Down Expand Up @@ -131,7 +131,7 @@ func (r *MantleRestoreReconciler) restore(ctx context.Context, restore *mantlev1
controllerutil.AddFinalizer(restore, MantleRestoreFinalizerName)
err = r.client.Update(ctx, restore)
if err != nil {
logger.Error(err, "failed to add finalizer", "restore", restore.Name, "namespace", restore.Namespace)
logger.Error(err, "failed to add finalizer")
return ctrl.Result{}, err
}

Expand Down Expand Up @@ -167,13 +167,13 @@ func (r *MantleRestoreReconciler) restore(ctx context.Context, restore *mantlev1

// create a restore PV with the clone image
if err := r.createRestoringPV(ctx, restore, &backup); err != nil {
logger.Error(err, "failed to create PV", "restore", restore.Name, "namespace", restore.Namespace)
logger.Error(err, "failed to create PV")
return ctrl.Result{}, err
}

// create a restore PVC with the restore PV
if err := r.createRestoringPVC(ctx, restore, &backup); err != nil {
logger.Error(err, "failed to create PVC", "restore", restore.Name, "namespace", restore.Namespace)
logger.Error(err, "failed to create PVC")
return ctrl.Result{}, err
}

Expand Down Expand Up @@ -340,32 +340,32 @@ func (r *MantleRestoreReconciler) cleanup(ctx context.Context, restore *mantlev1
return ctrl.Result{}, nil
}

logger.Info("deleting", "name", restore.Name, "namespace", restore.Namespace)
logger.Info("deleting")

// delete the PVC
if err := r.deleteRestoringPVC(ctx, restore); err != nil {
logger.Error(err, "failed to delete PVC", "restore", restore.Name, "namespace", restore.Namespace)
logger.Error(err, "failed to delete PVC")
return ctrl.Result{}, err
}

// delete the PV
err := r.deleteRestoringPV(ctx, restore)
if err != nil {
logger.Error(err, "failed to get PV", "restore", restore.Name, "namespace", restore.Namespace)
logger.Error(err, "failed to get PV")
return ctrl.Result{}, err
}

// delete the clone image
if err := r.removeRBDImage(ctx, restore); err != nil {
logger.Error(err, "failed to remove image", "restore", restore.Name, "namespace", restore.Namespace)
logger.Error(err, "failed to remove image")
return ctrl.Result{}, err
}

// remove the finalizer
controllerutil.RemoveFinalizer(restore, MantleRestoreFinalizerName)
err = r.client.Update(ctx, restore)
if err != nil {
logger.Error(err, "failed to remove finalizer", "restore", restore.Name, "namespace", restore.Namespace)
logger.Error(err, "failed to remove finalizer")
return ctrl.Result{}, err
}

Expand Down Expand Up @@ -438,7 +438,7 @@ func (r *MantleRestoreReconciler) removeRBDImage(ctx context.Context, restore *m
logger := log.FromContext(ctx)
image := r.restoringRBDImageName(restore)
pool := restore.Status.Pool
logger.Info("removing image", "restore", restore.Name, "namespace", restore.Namespace, "pool", pool, "image", image)
logger.Info("removing image", "pool", pool, "image", image)
images, err := r.ceph.RBDLs(pool)
if err != nil {
return fmt.Errorf("failed to list RBD images: %v", err)
Expand Down

0 comments on commit 5311316

Please sign in to comment.