Skip to content

Commit

Permalink
issue-120, k8s resource events for Maintenance events resource is imp…
Browse files Browse the repository at this point in the history
…lemented
  • Loading branch information
RostislavPorohnya committed Mar 15, 2023
1 parent bd360b0 commit 2e82538
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 14 deletions.
1 change: 0 additions & 1 deletion apis/clusterresources/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion apis/clusters/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ kind: Kustomization
images:
- name: controller
newName: rostislavporohnya/instaclustr-operator
newTag: icop372
newTag: icop380
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 @@ -294,10 +294,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

0 comments on commit 2e82538

Please sign in to comment.