From 694f46740405354cc64ab413008452d28a0cd8f7 Mon Sep 17 00:00:00 2001 From: Jwalant Modi Date: Tue, 12 Nov 2024 17:35:12 +0530 Subject: [PATCH] Added event flow and logs for BackupService, Backup and Restore controllers. --- cmd/main.go | 9 ++++++ .../aerospikebackupservice_controller.go | 19 +++++++++++- .../controller/backup-service/reconciler.go | 30 +++++++++++++++++++ .../backup/aerospikebackup_controller.go | 7 +++-- internal/controller/backup/reconciler.go | 20 ++++++++++++- .../restore/aerospikerestore_controller.go | 25 +++++++++++++--- internal/controller/restore/reconciler.go | 16 ++++++++++ 7 files changed, 118 insertions(+), 8 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 344d936e..1ce55a98 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -167,6 +167,9 @@ func main() { Client: client, Scheme: mgr.GetScheme(), Log: ctrl.Log.WithName("controller").WithName("AerospikeBackupService"), + Recorder: eventBroadcaster.NewRecorder( + mgr.GetScheme(), v1.EventSource{Component: "aerospikeBackupService-controller"}, + ), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "AerospikeBackupService") os.Exit(1) @@ -181,6 +184,9 @@ func main() { Client: client, Scheme: mgr.GetScheme(), Log: ctrl.Log.WithName("controller").WithName("AerospikeBackup"), + Recorder: eventBroadcaster.NewRecorder( + mgr.GetScheme(), v1.EventSource{Component: "aerospikeBackup-controller"}, + ), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "AerospikeBackup") os.Exit(1) @@ -195,6 +201,9 @@ func main() { Client: client, Scheme: mgr.GetScheme(), Log: ctrl.Log.WithName("controller").WithName("AerospikeRestore"), + Recorder: eventBroadcaster.NewRecorder( + mgr.GetScheme(), v1.EventSource{Component: "aerospikeRestore-controller"}, + ), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "AerospikeRestore") os.Exit(1) diff --git a/internal/controller/backup-service/aerospikebackupservice_controller.go b/internal/controller/backup-service/aerospikebackupservice_controller.go index e9965e4a..9b50f8c5 100644 --- a/internal/controller/backup-service/aerospikebackupservice_controller.go +++ b/internal/controller/backup-service/aerospikebackupservice_controller.go @@ -20,8 +20,10 @@ import ( "context" "github.com/go-logr/logr" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" k8sruntime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" @@ -36,7 +38,8 @@ import ( type AerospikeBackupServiceReconciler struct { Scheme *k8sruntime.Scheme client.Client - Log logr.Logger + Recorder record.EventRecorder + Log logr.Logger } //nolint:lll // for readability @@ -56,6 +59,19 @@ func (r *AerospikeBackupServiceReconciler) Reconcile(_ context.Context, request aeroBackupService := &asdbv1beta1.AerospikeBackupService{} if err := r.Client.Get(context.TODO(), request.NamespacedName, aeroBackupService); err != nil { if errors.IsNotFound(err) { + log.Info("Deleted AerospikeBackupService") + + objectRef := &corev1.ObjectReference{ + Kind: "AerospikeBackupService", + Namespace: request.Namespace, + Name: request.Name, + } + r.Recorder.Eventf( + objectRef, corev1.EventTypeNormal, "Deleted", + "Deleted AerospikeBackupService %s/%s", objectRef.Namespace, + objectRef.Name, + ) + // Request object not found, could have been deleted after Reconcile request. return reconcile.Result{}, nil } @@ -68,6 +84,7 @@ func (r *AerospikeBackupServiceReconciler) Reconcile(_ context.Context, request Client: r.Client, Log: log, Scheme: r.Scheme, + Recorder: r.Recorder, } return cr.Reconcile() diff --git a/internal/controller/backup-service/reconciler.go b/internal/controller/backup-service/reconciler.go index 258ad2ec..bfcf4ad6 100644 --- a/internal/controller/backup-service/reconciler.go +++ b/internal/controller/backup-service/reconciler.go @@ -63,6 +63,13 @@ func (r *SingleBackupServiceReconciler) Reconcile() (result ctrl.Result, recErr }() if !r.aeroBackupService.ObjectMeta.DeletionTimestamp.IsZero() { + r.Log.Info("Deleted AerospikeBackupService") + r.Recorder.Eventf( + r.aeroBackupService, corev1.EventTypeNormal, "Deleted", + "Deleted AerospikeBackupService %s/%s", r.aeroBackupService.Namespace, + r.aeroBackupService.Name, + ) + // Stop reconciliation as the Aerospike Backup service is being deleted return reconcile.Result{}, nil } @@ -73,21 +80,44 @@ func (r *SingleBackupServiceReconciler) Reconcile() (result ctrl.Result, recErr } if err := r.reconcileConfigMap(); err != nil { + r.Log.Error(err, "Failed to reconcile config map") + r.Recorder.Eventf(r.aeroBackupService, corev1.EventTypeWarning, + "ConfigMapReconcileFailed", "Failed to reconcile config map %s/%s", + r.aeroBackupService.Namespace, r.aeroBackupService.Name) + recErr = err + return ctrl.Result{}, err } if err := r.reconcileDeployment(); err != nil { + r.Log.Error(err, "Failed to reconcile deployment") + r.Recorder.Eventf(r.aeroBackupService, corev1.EventTypeWarning, + "DeploymentReconcileFailed", "Failed to reconcile deployment %s/%s", + r.aeroBackupService.Namespace, r.aeroBackupService.Name) + recErr = err + return ctrl.Result{}, err } if err := r.reconcileService(); err != nil { + r.Log.Error(err, "Failed to reconcile service") + r.Recorder.Eventf(r.aeroBackupService, corev1.EventTypeWarning, + "ServiceReconcileFailed", "Failed to reconcile service %s/%s", + r.aeroBackupService.Namespace, r.aeroBackupService.Name) + recErr = err + return ctrl.Result{}, err } if err := r.updateStatus(); err != nil { + r.Log.Error(err, "Failed to update status") + r.Recorder.Eventf(r.aeroBackupService, corev1.EventTypeWarning, + "StatusUpdateFailed", "Failed to update AerospikeBackupService status %s/%s", + r.aeroBackupService.Namespace, r.aeroBackupService.Name) + return ctrl.Result{}, err } diff --git a/internal/controller/backup/aerospikebackup_controller.go b/internal/controller/backup/aerospikebackup_controller.go index 9310a3a4..658a238a 100644 --- a/internal/controller/backup/aerospikebackup_controller.go +++ b/internal/controller/backup/aerospikebackup_controller.go @@ -22,6 +22,7 @@ import ( "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/api/errors" k8sruntime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" @@ -37,8 +38,9 @@ const finalizerName = "asdb.aerospike.com/backup-finalizer" // AerospikeBackupReconciler reconciles a AerospikeBackup object type AerospikeBackupReconciler struct { client.Client - Scheme *k8sruntime.Scheme - Log logr.Logger + Scheme *k8sruntime.Scheme + Recorder record.EventRecorder + Log logr.Logger } //+kubebuilder:rbac:groups=asdb.aerospike.com,resources=aerospikebackups,verbs=get;list;watch;create;update;patch;delete @@ -68,6 +70,7 @@ func (r *AerospikeBackupReconciler) Reconcile(_ context.Context, request ctrl.Re Client: r.Client, Log: log, Scheme: r.Scheme, + Recorder: r.Recorder, } return cr.Reconcile() diff --git a/internal/controller/backup/reconciler.go b/internal/controller/backup/reconciler.go index fbe48454..fec03c29 100644 --- a/internal/controller/backup/reconciler.go +++ b/internal/controller/backup/reconciler.go @@ -42,6 +42,12 @@ func (r *SingleBackupReconciler) Reconcile() (result ctrl.Result, recErr error) return reconcile.Result{}, err } + r.Log.Info("Deleted AerospikeBackup") + r.Recorder.Eventf( + r.aeroBackup, corev1.EventTypeNormal, "Deleted", + "Deleted AerospikeBackup %s/%s", r.aeroBackup.Namespace, + r.aeroBackup.Name, + ) // Stop reconciliation as the backup is being deleted return reconcile.Result{}, nil } @@ -54,16 +60,28 @@ func (r *SingleBackupReconciler) Reconcile() (result ctrl.Result, recErr error) if err := r.reconcileConfigMap(); err != nil { r.Log.Error(err, "Failed to reconcile config map") + r.Recorder.Eventf(r.aeroBackup, corev1.EventTypeWarning, + "ConfigMapReconcileFailed", "Failed to reconcile config map %s/%s", + r.aeroBackup.Namespace, r.aeroBackup.Name) + return reconcile.Result{}, err } if err := r.reconcileBackup(); err != nil { r.Log.Error(err, "Failed to reconcile backup") + r.Recorder.Eventf(r.aeroBackup, corev1.EventTypeWarning, + "BackupReconcileFailed", "Failed to reconcile backup %s/%s", + r.aeroBackup.Namespace, r.aeroBackup.Name) + return reconcile.Result{}, err } if err := r.updateStatus(); err != nil { r.Log.Error(err, "Failed to update status") + r.Recorder.Eventf(r.aeroBackup, corev1.EventTypeWarning, + "StatusUpdateFailed", "Failed to update AerospikeBackup status %s/%s", + r.aeroBackup.Namespace, r.aeroBackup.Name) + return reconcile.Result{}, err } @@ -309,7 +327,7 @@ func (r *SingleBackupReconciler) scheduleOnDemandBackup() error { r.Log.Info("Scheduled on-demand backup", "ID", r.aeroBackup.Spec.OnDemandBackups[0].ID, "routine", r.aeroBackup.Spec.OnDemandBackups[0].RoutineName) - r.Log.Info("Reconciled scheduled backup") + r.Log.Info("Reconciled on-demand backup") return nil } diff --git a/internal/controller/restore/aerospikerestore_controller.go b/internal/controller/restore/aerospikerestore_controller.go index b622a683..7c6c2d68 100644 --- a/internal/controller/restore/aerospikerestore_controller.go +++ b/internal/controller/restore/aerospikerestore_controller.go @@ -20,23 +20,26 @@ import ( "context" "github.com/go-logr/logr" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" k8sRuntime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/record" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" asdbv1beta1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1beta1" "github.com/aerospike/aerospike-kubernetes-operator/internal/controller/common" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" ) // AerospikeRestoreReconciler reconciles a AerospikeRestore object type AerospikeRestoreReconciler struct { client.Client - Scheme *k8sRuntime.Scheme - Log logr.Logger + Scheme *k8sRuntime.Scheme + Recorder record.EventRecorder + Log logr.Logger } //nolint:lll // for readability @@ -55,6 +58,19 @@ func (r *AerospikeRestoreReconciler) Reconcile(_ context.Context, request ctrl.R aeroRestore := &asdbv1beta1.AerospikeRestore{} if err := r.Client.Get(context.TODO(), request.NamespacedName, aeroRestore); err != nil { if errors.IsNotFound(err) { + log.Info("Deleted AerospikeRestore") + + objectRef := &corev1.ObjectReference{ + Kind: "AerospikeRestore", + Namespace: request.Namespace, + Name: request.Name, + } + r.Recorder.Eventf( + objectRef, corev1.EventTypeNormal, "Deleted", + "Deleted AerospikeRestore %s/%s", objectRef.Namespace, + objectRef.Name, + ) + // Request object not found, could have been deleted after Reconcile request. return reconcile.Result{}, nil } @@ -67,6 +83,7 @@ func (r *AerospikeRestoreReconciler) Reconcile(_ context.Context, request ctrl.R Client: r.Client, Log: log, Scheme: r.Scheme, + Recorder: r.Recorder, } return cr.Reconcile() diff --git a/internal/controller/restore/reconciler.go b/internal/controller/restore/reconciler.go index 435cd994..23858853 100644 --- a/internal/controller/restore/reconciler.go +++ b/internal/controller/restore/reconciler.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/go-logr/logr" + corev1 "k8s.io/api/core/v1" k8sRuntime "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" @@ -31,6 +32,13 @@ type SingleRestoreReconciler struct { func (r *SingleRestoreReconciler) Reconcile() (result ctrl.Result, recErr error) { if !r.aeroRestore.ObjectMeta.DeletionTimestamp.IsZero() { + r.Log.Info("Deleted AerospikeRestore") + r.Recorder.Eventf( + r.aeroRestore, corev1.EventTypeNormal, "Deleted", + "Deleted AerospikeRestore %s/%s", r.aeroRestore.Namespace, + r.aeroRestore.Name, + ) + // Stop reconciliation as the Aerospike restore is being deleted return reconcile.Result{}, nil } @@ -41,6 +49,10 @@ func (r *SingleRestoreReconciler) Reconcile() (result ctrl.Result, recErr error) if res := r.reconcileRestore(); !res.IsSuccess { if res.Err != nil { + r.Log.Error(res.Err, "Failed to reconcile restore") + r.Recorder.Eventf(r.aeroRestore, corev1.EventTypeWarning, "RestoreReconcileFailed", + "Failed to reconcile restore %s/%s", r.aeroRestore.Namespace, r.aeroRestore.Name) + return res.Result, res.Err } @@ -48,6 +60,10 @@ func (r *SingleRestoreReconciler) Reconcile() (result ctrl.Result, recErr error) } if err := r.checkRestoreStatus(); err != nil { + r.Log.Error(err, "Failed to check restore status") + r.Recorder.Eventf(r.aeroRestore, corev1.EventTypeWarning, "CheckStatusFailed", + "Failed to check restore status %s/%s", r.aeroRestore.Namespace, r.aeroRestore.Name) + return ctrl.Result{}, err }