Skip to content

Commit cb5ef10

Browse files
committed
update the status for managed notification object
1 parent d78c8de commit cb5ef10

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ require (
99
github.com/onsi/gomega v1.18.1
1010
github.com/openshift-online/ocm-cli v0.1.62
1111
github.com/openshift-online/ocm-sdk-go v0.1.242
12-
github.com/openshift/ocm-agent-operator v0.0.0-20220315043808-a190995e3fda
12+
github.com/openshift/ocm-agent-operator v0.0.0-20220316071606-afd26cc5ca68
1313
github.com/prometheus/alertmanager v0.23.0
1414
github.com/prometheus/client_golang v1.11.0
1515
github.com/sirupsen/logrus v1.8.1
1616
github.com/spf13/cobra v1.3.0
1717
github.com/spf13/pflag v1.0.5
1818
github.com/spf13/viper v1.10.1
19+
k8s.io/api v0.22.1
1920
k8s.io/apimachinery v0.23.3
2021
k8s.io/client-go v12.0.0+incompatible
2122
k8s.io/kubectl v0.23.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,8 +1124,8 @@ github.com/openshift-online/ocm-sdk-go v0.1.242 h1:fesCAv8tg2gcVAV1aKwCoQrkRmDq+
11241124
github.com/openshift-online/ocm-sdk-go v0.1.242/go.mod h1:nHYjrmvR9L7KSRZukysau62iEHKJOQYFhlceqaVAV1Y=
11251125
github.com/openshift/api v0.0.0-20210910062324-a41d3573a3ba/go.mod h1:izBmoXbUu3z5kUa4FjZhvekTsyzIWiOoaIgJiZBBMQs=
11261126
github.com/openshift/build-machinery-go v0.0.0-20210423112049-9415d7ebd33e/go.mod h1:b1BuldmJlbA/xYtdZvKi+7j5YGB44qJUJDZ9zwiNCfE=
1127-
github.com/openshift/ocm-agent-operator v0.0.0-20220315043808-a190995e3fda h1:2nXscsh7v2S1RxeBJydANFWoevRGOS4ZDg8aejAxyHs=
1128-
github.com/openshift/ocm-agent-operator v0.0.0-20220315043808-a190995e3fda/go.mod h1:ZwZI7wGFEmXT6bkoHfdNivUn8r5nG86g9+GeBqjIjz0=
1127+
github.com/openshift/ocm-agent-operator v0.0.0-20220316071606-afd26cc5ca68 h1:FqYpwnYsrk0MPBw4gxgfA8CS/0T03y4zNVkpvotmIr8=
1128+
github.com/openshift/ocm-agent-operator v0.0.0-20220316071606-afd26cc5ca68/go.mod h1:ZwZI7wGFEmXT6bkoHfdNivUn8r5nG86g9+GeBqjIjz0=
11291129
github.com/openshift/origin v0.0.0-20160503220234-8f127d736703/go.mod h1:0Rox5r9C8aQn6j1oAOQ0c1uC86mYbUFObzjBRvUKHII=
11301130
github.com/openshift/prom-label-proxy v0.1.1-0.20191016113035-b8153a7f39f1/go.mod h1:p5MuxzsYP1JPsNGwtjtcgRHHlGziCJJfztff91nNixw=
11311131
github.com/openshift/rosa v1.1.7/go.mod h1:Ap4vfflxHXfzY0MIt1l9dckwG+DkPUDc/vv1mk1cSCQ=

pkg/handlers/webhookreceiver.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
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

Comments
 (0)