Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-120, k8s resource events for Maintenance events resource is implemented #372

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ kind: MaintenanceEvents
metadata:
name: maintenanceevents-sample
spec:
clusterId: "2217f85d-f4a8-4da4-b7d7-1b39c5fc625e"
clusterId: "917062a6-a449-4527-a3aa-e3904b04b39f"
exclusionWindows:
- dayOfWeek: "MONDAY"
startHour: 1
durationInHours: 1
maintenanceEventsReschedule:
- scheduledStartTime: "2023-03-08T00:00:00Z"
scheduleId: "0585598f-e672-4bd2-806a-673ba521665c"
# maintenanceEventsReschedule:
# - scheduledStartTime: "2023-03-08T00:00:00Z"
# scheduleId: "0585598f-e672-4bd2-806a-673ba521665c"
53 changes: 50 additions & 3 deletions controllers/clusterresources/maintenanceevents_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -39,9 +40,10 @@ import (
// MaintenanceEventsReconciler reconciles a MaintenanceEvents object
type MaintenanceEventsReconciler struct {
client.Client
Scheme *runtime.Scheme
API instaclustr.API
Scheduler scheduler.Interface
Scheme *runtime.Scheme
API instaclustr.API
Scheduler scheduler.Interface
EventRecorder record.EventRecorder
}

//+kubebuilder:rbac:groups=clusterresources.instaclustr.com,resources=maintenanceevents,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -86,9 +88,19 @@ func (r *MaintenanceEventsReconciler) Reconcile(ctx context.Context, req ctrl.Re
"resource", me,
)

r.EventRecorder.Eventf(
me, models.Warning, models.DeletionFailed,
"Exclusion windows deletion on the Instaclustr is failed. Reason: %v",
err,
)
return models.ReconcileRequeue, nil
}

r.EventRecorder.Eventf(
me, models.Normal, models.DeletionStarted,
"Exclusion windows deletion request is sent to the Instaclustr API.",
)

r.Scheduler.RemoveJob(me.GetJobID(scheduler.StatusChecker))
controllerutil.RemoveFinalizer(me, models.DeletionFinalizer)
err = r.Patch(ctx, me, patch)
Expand All @@ -97,6 +109,11 @@ func (r *MaintenanceEventsReconciler) Reconcile(ctx context.Context, req ctrl.Re
"resource", me,
)

r.EventRecorder.Eventf(
me, models.Warning, models.PatchFailed,
"Resource patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue, nil
}

Expand All @@ -109,6 +126,11 @@ func (r *MaintenanceEventsReconciler) Reconcile(ctx context.Context, req ctrl.Re
"spec", me.Spec,
)

r.EventRecorder.Eventf(
me, models.Warning, models.UpdateFailed,
"Maintenance events update is failed. Reason: %v",
err,
)
return models.ReconcileRequeue, nil
}

Expand All @@ -118,6 +140,11 @@ func (r *MaintenanceEventsReconciler) Reconcile(ctx context.Context, req ctrl.Re
"spec", me.Spec,
)

r.EventRecorder.Eventf(
me, models.Warning, models.UpdateFailed,
"Exclusion windows update is failed. Reason: %v",
err,
)
return models.ReconcileRequeue, nil
}

Expand All @@ -127,6 +154,11 @@ func (r *MaintenanceEventsReconciler) Reconcile(ctx context.Context, req ctrl.Re
"status", me.Status,
)

r.EventRecorder.Eventf(
me, models.Warning, models.PatchFailed,
"Resource status patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue, nil
}

Expand All @@ -137,6 +169,11 @@ func (r *MaintenanceEventsReconciler) Reconcile(ctx context.Context, req ctrl.Re
"status", me.Status,
)

r.EventRecorder.Eventf(
me, models.Warning, models.PatchFailed,
"Resource patch is failed. Reason: %v",
err,
)
return models.ReconcileRequeue, nil
}

Expand All @@ -146,9 +183,19 @@ func (r *MaintenanceEventsReconciler) Reconcile(ctx context.Context, req ctrl.Re
"spec", me.Spec,
)

r.EventRecorder.Eventf(
me, models.Warning, models.CreationFailed,
"Resource status check job is failed. Reason: %v",
err,
)
return models.ReconcileRequeue, nil
}

r.EventRecorder.Eventf(
me, models.Normal, models.Created,
"Cluster backups check job is started",
)

l.Info("Maintenance Event resource was reconciled",
"spec", me.Spec,
"status", me.Status,
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,11 @@ func main() {
os.Exit(1)
}
if err = (&clusterresourcescontrollers.MaintenanceEventsReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
API: instaClient,
Scheduler: s,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
API: instaClient,
Scheduler: s,
EventRecorder: eventRecorder,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MaintenanceEvents")
os.Exit(1)
Expand Down