Skip to content

Commit

Permalink
Issue warning events on reconciliation errors
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Nov 8, 2022
1 parent 1896ae3 commit c3f711a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
6 changes: 6 additions & 0 deletions controllers/alert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -116,6 +117,11 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resu
r.Metrics.RecordDuration(ctx, obj, reconcileStart)
r.Metrics.RecordSuspend(ctx, obj, obj.Spec.Suspend)

// Issue warning event if the reconciliation failed.
if retErr != nil {
r.Event(obj, corev1.EventTypeWarning, meta.FailedReason, retErr.Error())
}

// Patch finalizers, status and conditions.
retErr = r.patch(ctx, obj, patcher)
}()
Expand Down
7 changes: 7 additions & 0 deletions controllers/provider_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
kuberecorder "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 @@ -49,6 +50,7 @@ import (
type ProviderReconciler struct {
client.Client
helper.Metrics
kuberecorder.EventRecorder

ControllerName string
}
Expand Down Expand Up @@ -97,6 +99,11 @@ func (r *ProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r
r.Metrics.RecordDuration(ctx, obj, reconcileStart)
r.Metrics.RecordSuspend(ctx, obj, obj.Spec.Suspend)

// Issue warning event if the reconciliation failed.
if retErr != nil {
r.Event(obj, corev1.EventTypeWarning, meta.FailedReason, retErr.Error())
}

// Patch finalizers, status and conditions.
retErr = r.patch(ctx, obj, patcher)
}()
Expand Down
7 changes: 7 additions & 0 deletions controllers/receiver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
kuberecorder "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 @@ -46,6 +47,7 @@ import (
type ReceiverReconciler struct {
client.Client
helper.Metrics
kuberecorder.EventRecorder

ControllerName string
}
Expand Down Expand Up @@ -104,6 +106,11 @@ func (r *ReceiverReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r
r.Metrics.RecordDuration(ctx, obj, reconcileStart)
r.Metrics.RecordSuspend(ctx, obj, obj.Spec.Suspend)

// Issue warning event if the reconciliation failed.
if retErr != nil {
r.Event(obj, corev1.EventTypeWarning, meta.FailedReason, retErr.Error())
}

// Patch finalizers, status and conditions.
retErr = r.patch(ctx, obj, patcher)
}()
Expand Down
8 changes: 5 additions & 3 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,20 @@ func TestMain(m *testing.M) {
controllerName := "notification-controller"
testMetricsH := controller.MustMakeMetrics(testEnv)

reconciler := AlertReconciler{
if err := (&AlertReconciler{
Client: testEnv,
Metrics: testMetricsH,
ControllerName: controllerName,
}
if err := (reconciler).SetupWithManager(testEnv); err != nil {
EventRecorder: testEnv.GetEventRecorderFor(controllerName),
}).SetupWithManager(testEnv); err != nil {
panic(fmt.Sprintf("Failed to start AlerReconciler: %v", err))
}

if err := (&ProviderReconciler{
Client: testEnv,
Metrics: testMetricsH,
ControllerName: controllerName,
EventRecorder: testEnv.GetEventRecorderFor(controllerName),
}).SetupWithManager(testEnv); err != nil {
panic(fmt.Sprintf("Failed to start ProviderReconciler: %v", err))
}
Expand All @@ -88,6 +89,7 @@ func TestMain(m *testing.M) {
Client: testEnv,
Metrics: testMetricsH,
ControllerName: controllerName,
EventRecorder: testEnv.GetEventRecorderFor(controllerName),
}).SetupWithManager(testEnv); err != nil {
panic(fmt.Sprintf("Failed to start ReceiverReconciler: %v", err))
}
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func main() {
Client: mgr.GetClient(),
ControllerName: controllerName,
Metrics: metricsH,
EventRecorder: mgr.GetEventRecorderFor(controllerName),
}).SetupWithManagerAndOptions(mgr, controllers.ProviderReconcilerOptions{
MaxConcurrentReconciles: concurrent,
RateLimiter: helper.GetRateLimiter(rateLimiterOptions),
Expand All @@ -138,6 +139,7 @@ func main() {
Client: mgr.GetClient(),
ControllerName: controllerName,
Metrics: metricsH,
EventRecorder: mgr.GetEventRecorderFor(controllerName),
}).SetupWithManagerAndOptions(mgr, controllers.AlertReconcilerOptions{
MaxConcurrentReconciles: concurrent,
RateLimiter: helper.GetRateLimiter(rateLimiterOptions),
Expand All @@ -149,6 +151,7 @@ func main() {
Client: mgr.GetClient(),
ControllerName: controllerName,
Metrics: metricsH,
EventRecorder: mgr.GetEventRecorderFor(controllerName),
}).SetupWithManagerAndOptions(mgr, controllers.ReceiverReconcilerOptions{
MaxConcurrentReconciles: concurrent,
RateLimiter: helper.GetRateLimiter(rateLimiterOptions),
Expand Down

0 comments on commit c3f711a

Please sign in to comment.