Skip to content

Commit

Permalink
Added event flow and logs for BackupService, Backup and Restore contr…
Browse files Browse the repository at this point in the history
…ollers.
  • Loading branch information
jwalantmodi05 committed Nov 12, 2024
1 parent 12c90d4 commit 694f467
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 8 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,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
}
Expand All @@ -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()
Expand Down
30 changes: 30 additions & 0 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 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
20 changes: 19 additions & 1 deletion internal/controller/backup/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 21 additions & 4 deletions internal/controller/restore/aerospikerestore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions internal/controller/restore/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -41,13 +49,21 @@ 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
}

return res.Result, nil
}

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
}

Expand Down

0 comments on commit 694f467

Please sign in to comment.