Skip to content

Commit

Permalink
KO-341: Added event flow for BackupService, Backup and Restore contro…
Browse files Browse the repository at this point in the history
…llers (#324)

* Added event flow and logs for BackupService, Backup and Restore controllers.
  • Loading branch information
jwalantmodi05 authored Jan 6, 2025
1 parent e945246 commit 50692f9
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 14 deletions.
9 changes: 9 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -56,6 +59,16 @@ 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")

aeroBackupService.Namespace = request.Namespace
aeroBackupService.Name = request.Name
r.Recorder.Eventf(
aeroBackupService, corev1.EventTypeNormal, "Deleted",
"Deleted AerospikeBackupService %s/%s", aeroBackupService.Namespace,
aeroBackupService.Name,
)

// Request object not found, could have been deleted after Reconcile request.
return reconcile.Result{}, nil
}
Expand All @@ -68,6 +81,7 @@ func (r *AerospikeBackupServiceReconciler) Reconcile(_ context.Context, request
Client: r.Client,
Log: log,
Scheme: r.Scheme,
Recorder: r.Recorder,
}

return cr.Reconcile()
Expand Down
61 changes: 56 additions & 5 deletions internal/controller/backup-service/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand All @@ -107,7 +137,7 @@ func (r *SingleBackupServiceReconciler) reconcileConfigMap() error {
return err
}

r.Log.Info("Create Backup Service ConfigMap",
r.Log.Info("Creating Backup Service ConfigMap",
"name", getBackupServiceName(r.aeroBackupService))

cm = &corev1.ConfigMap{
Expand Down Expand Up @@ -136,14 +166,16 @@ func (r *SingleBackupServiceReconciler) reconcileConfigMap() error {
)
}

r.Log.Info("Created new Backup Service ConfigMap",
r.Log.Info("Created Backup Service ConfigMap",
"name", getBackupServiceName(r.aeroBackupService))
r.Recorder.Eventf(r.aeroBackupService, corev1.EventTypeNormal, "ConfigMapCreated",
"Created Backup Service ConfigMap %s/%s", r.aeroBackupService.Namespace, r.aeroBackupService.Name)

return nil
}

r.Log.Info(
"ConfigMap already exist. Updating existing ConfigMap if required",
"Backup Service ConfigMap already exist. Updating existing ConfigMap if required",
"name", getBackupServiceName(r.aeroBackupService),
)

Expand Down Expand Up @@ -183,6 +215,8 @@ func (r *SingleBackupServiceReconciler) reconcileConfigMap() error {

r.Log.Info("Updated Backup Service ConfigMap",
"name", getBackupServiceName(r.aeroBackupService))
r.Recorder.Eventf(r.aeroBackupService, corev1.EventTypeNormal, "ConfigMapUpdated",
"Updated Backup Service ConfigMap %s/%s", r.aeroBackupService.Namespace, r.aeroBackupService.Name)

return nil
}
Expand All @@ -207,7 +241,7 @@ func (r *SingleBackupServiceReconciler) reconcileDeployment() error {
return err
}

r.Log.Info("Create Backup Service deployment",
r.Log.Info("Creating Backup Service deployment",
"name", getBackupServiceName(r.aeroBackupService))

deployment, err := r.getDeploymentObject()
Expand All @@ -228,6 +262,11 @@ func (r *SingleBackupServiceReconciler) reconcileDeployment() error {
return fmt.Errorf("failed to deploy Backup service deployment: %v", err)
}

r.Log.Info("Created Backup Service deployment",
"name", getBackupServiceName(r.aeroBackupService))
r.Recorder.Eventf(r.aeroBackupService, corev1.EventTypeNormal, "DeploymentCreated",
"Created Backup Service Deployment %s/%s", r.aeroBackupService.Namespace, r.aeroBackupService.Name)

return r.waitForDeploymentToBeReady()
}

Expand All @@ -249,6 +288,11 @@ func (r *SingleBackupServiceReconciler) reconcileDeployment() error {
return fmt.Errorf("failed to update Backup service deployment: %v", err)
}

r.Log.Info("Updated Backup Service deployment",
"name", getBackupServiceName(r.aeroBackupService))
r.Recorder.Eventf(r.aeroBackupService, corev1.EventTypeNormal, "DeploymentUpdated",
"Updated Backup Service Deployment %s/%s", r.aeroBackupService.Namespace, r.aeroBackupService.Name)

if oldResourceVersion != deploy.ResourceVersion {
r.Log.Info("Deployment spec is updated, will result in rolling restart")
return r.waitForDeploymentToBeReady()
Expand Down Expand Up @@ -457,7 +501,7 @@ func (r *SingleBackupServiceReconciler) reconcileService() error {
return err
}

r.Log.Info("Create Backup Service",
r.Log.Info("Creating Backup Service",
"name", getBackupServiceName(r.aeroBackupService))

svc, err := r.getServiceObject()
Expand All @@ -478,6 +522,11 @@ func (r *SingleBackupServiceReconciler) reconcileService() error {
return fmt.Errorf("failed to create Backup Service: %v", err)
}

r.Log.Info("Created Backup Service",
"name", getBackupServiceName(r.aeroBackupService))
r.Recorder.Eventf(r.aeroBackupService, corev1.EventTypeNormal, "ServiceCreated",
"Created Backup Service %s/%s", r.aeroBackupService.Namespace, r.aeroBackupService.Name)

return nil
}

Expand All @@ -498,6 +547,8 @@ func (r *SingleBackupServiceReconciler) reconcileService() error {
}

r.Log.Info("Updated Backup Service", "name", getBackupServiceName(r.aeroBackupService))
r.Recorder.Eventf(r.aeroBackupService, corev1.EventTypeNormal, "ServiceUpdated",
"Updated Backup Service %s/%s", r.aeroBackupService.Namespace, r.aeroBackupService.Name)

return nil
}
Expand Down
7 changes: 5 additions & 2 deletions internal/controller/backup/aerospikebackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
30 changes: 28 additions & 2 deletions internal/controller/backup/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ type SingleBackupReconciler struct {
func (r *SingleBackupReconciler) Reconcile() (result ctrl.Result, recErr error) {
// Check DeletionTimestamp to see if the backup is being deleted
if !r.aeroBackup.ObjectMeta.DeletionTimestamp.IsZero() {
r.Log.Info("Deleting AerospikeBackup")

if err := r.removeFinalizer(finalizerName); err != nil {
r.Log.Error(err, "Failed to remove finalizer")
return reconcile.Result{}, err
}

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
}
Expand All @@ -54,16 +61,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",
r.aeroBackup.Spec.BackupService.String())

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
}

Expand Down Expand Up @@ -198,6 +217,9 @@ func (r *SingleBackupReconciler) reconcileConfigMap() error {
r.Log.Info("Updated Backup Service ConfigMap for Backup",
"name", r.aeroBackup.Spec.BackupService.String(),
)
r.Recorder.Eventf(r.aeroBackup, corev1.EventTypeNormal, "ConfigMapUpdated",
"Updated Backup Service ConfigMap %s for Backup %s/%s", r.aeroBackup.Spec.BackupService.String(),
r.aeroBackup.Namespace, r.aeroBackup.Name)

return nil
}
Expand Down Expand Up @@ -292,7 +314,7 @@ func (r *SingleBackupReconciler) scheduleOnDemandBackup() error {
return nil
}

r.Log.Info("Schedule on-demand backup",
r.Log.Info("Scheduling on-demand backup",
"ID", r.aeroBackup.Spec.OnDemandBackups[0].ID, "routine", r.aeroBackup.Spec.OnDemandBackups[0].RoutineName)

backupServiceClient, err := backup_service.GetBackupServiceClient(r.Client, &r.aeroBackup.Spec.BackupService)
Expand All @@ -308,8 +330,10 @@ 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.Recorder.Eventf(r.aeroBackup, corev1.EventTypeNormal, "BackupScheduled",
"Scheduled on-demand backup %s/%s", r.aeroBackup.Namespace, r.aeroBackup.Name)

r.Log.Info("Reconciled scheduled backup")
r.Log.Info("Reconciled on-demand backup")

return nil
}
Expand Down Expand Up @@ -421,6 +445,8 @@ func (r *SingleBackupReconciler) reconcileScheduledBackup() error {
}

r.Log.Info("Reconciled scheduled backup")
r.Recorder.Eventf(r.aeroBackup, corev1.EventTypeNormal, "BackupReconciled",
"Reconciled scheduled backup %s/%s", r.aeroBackup.Namespace, r.aeroBackup.Name)

return nil
}
Expand Down
Loading

0 comments on commit 50692f9

Please sign in to comment.