55 "encoding/json"
66 "fmt"
77 "net/http"
8+ "time"
89
910 "github.com/openshift-online/ocm-cli/pkg/arguments"
1011 sdk "github.com/openshift-online/ocm-sdk-go"
@@ -15,6 +16,8 @@ import (
1516 oav1alpha1 "github.com/openshift/ocm-agent-operator/pkg/apis/ocmagent/v1alpha1"
1617 "github.com/openshift/ocm-agent/pkg/config"
1718 "github.com/openshift/ocm-agent/pkg/ocm"
19+ corev1 "k8s.io/api/core/v1"
20+ v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1821
1922 "k8s.io/client-go/util/retry"
2023 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -149,7 +152,7 @@ func (h *WebhookReceiverHandler) processAlert(alert template.Alert, mnl *oav1alp
149152 }
150153
151154 // Update the notification status to indicate a servicelog has been sent
152- err = h .updateNotificationStatus (notification , managedNotifications )
155+ err = h .updateNotificationStatus (notification , managedNotifications , true )
153156 if err != nil {
154157 log .WithFields (log.Fields {LogFieldNotificationName : notification .Name , LogFieldManagedNotification : managedNotifications .Name }).WithError (err ).Error ("unable to update notification status" )
155158 return err
@@ -236,7 +239,7 @@ func (h *WebhookReceiverHandler) sendServiceLog(n *oav1alpha1.Notification, firi
236239 return nil
237240}
238241
239- func (h * WebhookReceiverHandler ) updateNotificationStatus (n * oav1alpha1.Notification , mn * oav1alpha1.ManagedNotification ) error {
242+ func (h * WebhookReceiverHandler ) updateNotificationStatus (n * oav1alpha1.Notification , mn * oav1alpha1.ManagedNotification , firing bool ) error {
240243
241244 // Update lastSent timestamp
242245 err := retry .RetryOnConflict (retry .DefaultRetry , func () error {
@@ -250,18 +253,52 @@ func (h *WebhookReceiverHandler) updateNotificationStatus(n *oav1alpha1.Notifica
250253 return err
251254 }
252255
256+ timeNow := & v1.Time {Time : time .Now ()}
253257 status , err := m .Status .GetNotificationRecord (n .Name )
254258 if err != nil {
255259 // Status does not exist, create it
260+ // This will happen only when the alert is firing first time
256261 status = & oav1alpha1.NotificationRecord {
257262 Name : n .Name ,
258263 ServiceLogSentCount : 0 ,
259264 }
260- status .SetStatus (oav1alpha1 .ConditionServiceLogSent , "Service log sent" )
265+ status .SetStatus (oav1alpha1 .ConditionAlertFiring , "Alert is firing" , corev1 .ConditionTrue , timeNow )
266+ status .SetStatus (oav1alpha1 .ConditionAlertResolved , "Alert resolved" , corev1 .ConditionFalse , timeNow )
267+ status .SetStatus (oav1alpha1 .ConditionServiceLogSent , "Service log sent" , corev1 .ConditionTrue , timeNow )
261268 } else {
262269 // Status exists, update it
263- status .SetStatus (oav1alpha1 .ConditionServiceLogSent , "Service log sent" )
270+ // When the alert is already firing
271+ firingCondition := status .Conditions .GetCondition (oav1alpha1 .ConditionAlertFiring ).Status
272+ if firingCondition == corev1 .ConditionTrue {
273+ firedConditionTime := status .Conditions .GetCondition (oav1alpha1 .ConditionAlertFiring ).LastTransitionTime
274+ resolvedConditionTime := status .Conditions .GetCondition (oav1alpha1 .ConditionAlertResolved ).LastTransitionTime
275+ if firing {
276+ // Status transition is Firing to Firing
277+ // Do not update the condition for AlertFiring and AlertResolved
278+ // Only update the timestamp for the ServiceLogSent
279+ status .SetStatus (oav1alpha1 .ConditionAlertFiring , "Alert is firing" , corev1 .ConditionTrue , firedConditionTime )
280+ status .SetStatus (oav1alpha1 .ConditionAlertResolved , "Alert resolved" , corev1 .ConditionFalse , resolvedConditionTime )
281+ status .SetStatus (oav1alpha1 .ConditionServiceLogSent , "Service log sent" , corev1 .ConditionTrue , timeNow )
282+ } else {
283+ // Status transition is Firing to Resolved
284+ // Update the condition status and timestamp for AlertFiring
285+ // Update the condition status and timestamp for AlertResolved
286+ // Update the timestamp for the ServiceLogSent
287+ status .SetStatus (oav1alpha1 .ConditionAlertFiring , "Alert is firing" , corev1 .ConditionFalse , timeNow )
288+ status .SetStatus (oav1alpha1 .ConditionAlertResolved , "Alert resolved" , corev1 .ConditionTrue , timeNow )
289+ status .SetStatus (oav1alpha1 .ConditionServiceLogSent , "Service log sent" , corev1 .ConditionTrue , timeNow )
290+ }
291+ } else {
292+ // Status transition is Resolved to Firing
293+ // Update the condition status and timestamp for AlertFiring
294+ // Update the condition status and timestamp for AlertResolved
295+ // Update the timestamp for the ServiceLogSent
296+ status .SetStatus (oav1alpha1 .ConditionAlertFiring , "Alert is firing" , corev1 .ConditionTrue , timeNow )
297+ status .SetStatus (oav1alpha1 .ConditionAlertResolved , "Alert resolved" , corev1 .ConditionFalse , timeNow )
298+ status .SetStatus (oav1alpha1 .ConditionServiceLogSent , "Service log sent" , corev1 .ConditionTrue , timeNow )
299+ }
264300 }
301+
265302 m .Status .Notifications .SetNotificationRecord (* status )
266303
267304 err = h .c .Status ().Update (context .TODO (), m )
0 commit comments