Skip to content

Commit

Permalink
batch - support integration-name instead of integration-ID (#32)
Browse files Browse the repository at this point in the history
* batch - support integration-name instead of integration-ID

* updating docs

* getting the context from the caller

* rename func name

* adding error handling for flatten alert
  • Loading branch information
OrNovo authored Jul 19, 2023
1 parent 7bd5bc9 commit 0e5b0d3
Show file tree
Hide file tree
Showing 32 changed files with 160 additions and 103 deletions.
67 changes: 46 additions & 21 deletions apis/coralogix/v1alpha1/alert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ limitations under the License.
package v1alpha1

import (
"context"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"

utils "github.com/coralogix/coralogix-operator/apis"
"github.com/coralogix/coralogix-operator/controllers/clientset"
alerts "github.com/coralogix/coralogix-operator/controllers/clientset/grpc/alerts/v2"
"google.golang.org/protobuf/types/known/wrapperspb"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -106,8 +109,9 @@ var (
NotifyOnTriggeredOnly: alerts.NotifyOn_TRIGGERED_ONLY,
NotifyOnTriggeredAndResolved: alerts.NotifyOn_TRIGGERED_AND_RESOLVED,
}
msInHour = int(time.Hour.Milliseconds())
msInMinute = int(time.Minute.Milliseconds())
msInHour = int(time.Hour.Milliseconds())
msInMinute = int(time.Minute.Milliseconds())
WebhooksClient *clientset.WebhooksClient
)

type ProtoTimeFrameAndRelativeTimeFrame struct {
Expand Down Expand Up @@ -149,15 +153,15 @@ type AlertSpec struct {
AlertType AlertType `json:"alertType"`
}

func (in *AlertSpec) ExtractCreateAlertRequest() (*alerts.CreateAlertRequest, error) {
func (in *AlertSpec) ExtractCreateAlertRequest(ctx context.Context) (*alerts.CreateAlertRequest, error) {
enabled := wrapperspb.Bool(in.Active)
name := wrapperspb.String(in.Name)
description := wrapperspb.String(in.Description)
severity := AlertSchemaSeverityToProtoSeverity[in.Severity]
metaLabels := expandMetaLabels(in.Labels)
expirationDate := expandExpirationDate(in.ExpirationDate)
showInInsight := expandShowInInsight(in.ShowInInsight)
notificationGroups, err := expandNotificationGroups(in.NotificationGroups)
notificationGroups, err := expandNotificationGroups(ctx, in.NotificationGroups)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -983,10 +987,10 @@ func expandShowInInsight(showInInsight *ShowInInsight) *alerts.ShowInInsight {
}
}

func expandNotificationGroups(notificationGroups []NotificationGroup) ([]*alerts.AlertNotificationGroups, error) {
func expandNotificationGroups(ctx context.Context, notificationGroups []NotificationGroup) ([]*alerts.AlertNotificationGroups, error) {
result := make([]*alerts.AlertNotificationGroups, 0, len(notificationGroups))
for i, ng := range notificationGroups {
notificationGroup, err := expandNotificationGroup(ng)
notificationGroup, err := expandNotificationGroup(ctx, ng)
if err != nil {
return nil, fmt.Errorf("error on notificationGroups[%d] - %s", i, err.Error())
}
Expand All @@ -995,9 +999,9 @@ func expandNotificationGroups(notificationGroups []NotificationGroup) ([]*alerts
return result, nil
}

func expandNotificationGroup(notificationGroup NotificationGroup) (*alerts.AlertNotificationGroups, error) {
func expandNotificationGroup(ctx context.Context, notificationGroup NotificationGroup) (*alerts.AlertNotificationGroups, error) {
groupFields := utils.StringSliceToWrappedStringSlice(notificationGroup.GroupByFields)
notifications, err := expandNotifications(notificationGroup.Notifications)
notifications, err := expandNotifications(ctx, notificationGroup.Notifications)
if err != nil {
return nil, err
}
Expand All @@ -1008,10 +1012,10 @@ func expandNotificationGroup(notificationGroup NotificationGroup) (*alerts.Alert
}, nil
}

func expandNotifications(notifications []Notification) ([]*alerts.AlertNotification, error) {
func expandNotifications(ctx context.Context, notifications []Notification) ([]*alerts.AlertNotification, error) {
result := make([]*alerts.AlertNotification, 0, len(notifications))
for i, n := range notifications {
notification, err := expandNotification(n)
notification, err := expandNotification(ctx, n)
if err != nil {
return nil, fmt.Errorf("error on notifications[%d] - %s", i, err.Error())
}
Expand All @@ -1020,7 +1024,7 @@ func expandNotifications(notifications []Notification) ([]*alerts.AlertNotificat
return result, nil
}

func expandNotification(notification Notification) (*alerts.AlertNotification, error) {
func expandNotification(ctx context.Context, notification Notification) (*alerts.AlertNotification, error) {
retriggeringPeriodSeconds := wrapperspb.UInt32(uint32(60 * notification.RetriggeringPeriodMinutes))
notifyOn := AlertSchemaNotifyOnToProtoNotifyOn[notification.NotifyOn]

Expand All @@ -1029,16 +1033,20 @@ func expandNotification(notification Notification) (*alerts.AlertNotification, e
NotifyOn: &notifyOn,
}

if integrationID := notification.IntegrationID; integrationID != nil {
if integrationName := notification.IntegrationName; integrationName != nil {
integrationID, err := searchIntegrationID(ctx, *integrationName)
if err != nil {
return nil, err
}
result.IntegrationType = &alerts.AlertNotification_IntegrationId{
IntegrationId: wrapperspb.UInt32(uint32(*integrationID)),
IntegrationId: wrapperspb.UInt32(integrationID),
}
}

emails := notification.EmailRecipients
{
if result.IntegrationType != nil && len(emails) != 0 {
return nil, fmt.Errorf("required exactly on of 'integrationID' or 'emailRecipients'")
return nil, fmt.Errorf("required exactly on of 'integrationName' or 'emailRecipients'")
}

if result.IntegrationType == nil {
Expand All @@ -1053,6 +1061,23 @@ func expandNotification(notification Notification) (*alerts.AlertNotification, e
return result, nil
}

func searchIntegrationID(ctx context.Context, name string) (uint32, error) {
webhooksStr, err := WebhooksClient.GetWebhooks(ctx)
if err != nil {
return 0, err
}
var maps []map[string]interface{}
if err = json.Unmarshal([]byte(webhooksStr), &maps); err != nil {
return 0, err
}
for _, m := range maps {
if m["alias"] == name {
return uint32(m["id"].(float64)), nil
}
}
return 0, fmt.Errorf("integration with name %s not found", name)
}

func (in *AlertSpec) DeepEqual(actualAlert *AlertStatus) (bool, utils.Diff) {
if actualName := actualAlert.Name; actualName != in.Name {
return false, utils.Diff{
Expand Down Expand Up @@ -1188,7 +1213,7 @@ func DeepEqualNotificationGroups(notificationGroups []NotificationGroup, actualN
return false, utils.Diff{}
}

func (in *AlertSpec) ExtractUpdateAlertRequest(id string) (*alerts.UpdateAlertByUniqueIdRequest, error) {
func (in *AlertSpec) ExtractUpdateAlertRequest(ctx context.Context, id string) (*alerts.UpdateAlertByUniqueIdRequest, error) {
uniqueIdentifier := wrapperspb.String(id)
enabled := wrapperspb.Bool(in.Active)
name := wrapperspb.String(in.Name)
Expand All @@ -1197,7 +1222,7 @@ func (in *AlertSpec) ExtractUpdateAlertRequest(id string) (*alerts.UpdateAlertBy
metaLabels := expandMetaLabels(in.Labels)
expirationDate := expandExpirationDate(in.ExpirationDate)
showInInsight := expandShowInInsight(in.ShowInInsight)
notificationGroups, err := expandNotificationGroups(in.NotificationGroups)
notificationGroups, err := expandNotificationGroups(ctx, in.NotificationGroups)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1303,7 +1328,7 @@ type Notification struct {
NotifyOn NotifyOn `json:"notifyOn,omitempty"`

// +optional
IntegrationID *int32 `json:"integrationID,omitempty"`
IntegrationName *string `json:"integrationName,omitempty"`

// +optional
EmailRecipients []string `json:"emailRecipients,omitempty"`
Expand All @@ -1318,11 +1343,11 @@ func (in *Notification) DeepEqual(actualNotification Notification) (bool, utils.
}
}

if !reflect.DeepEqual(in.IntegrationID, actualNotification.IntegrationID) {
if !reflect.DeepEqual(in.IntegrationName, actualNotification.IntegrationName) {
return false, utils.Diff{
Name: "IntegrationID",
Desired: in.IntegrationID,
Actual: actualNotification.IntegrationID,
Name: "IntegrationName",
Desired: in.IntegrationName,
Actual: actualNotification.IntegrationName,
}
}

Expand Down
6 changes: 3 additions & 3 deletions apis/coralogix/v1alpha1/zz_generated.deepcopy.go

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

10 changes: 4 additions & 6 deletions config/crd/bases/coralogix.com_alerts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -868,9 +868,8 @@ spec:
items:
type: string
type: array
integrationID:
format: int32
type: integer
integrationName:
type: string
notifyOn:
type: string
retriggeringPeriodMinutes:
Expand Down Expand Up @@ -1767,9 +1766,8 @@ spec:
items:
type: string
type: array
integrationID:
format: int32
type: integer
integrationName:
type: string
notifyOn:
type: string
retriggeringPeriodMinutes:
Expand Down
2 changes: 1 addition & 1 deletion config/samples/alerts/flow_alert_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
- groupByFields: [ "coralogix.metadata.sdkId" ]
notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: WebhookAlerts
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example@coralogix.com" ]
Expand Down
2 changes: 1 addition & 1 deletion config/samples/alerts/lucene_alert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
- groupByFields: [ "coralogix.metadata.sdkId" ]
notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: WebhookAlerts
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example@coralogix.com" ]
Expand Down
2 changes: 1 addition & 1 deletion config/samples/alerts/new_value_alert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
- groupByFields: [ "remote_addr_geoip.country_name" ]
notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: WebhookAlerts
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example@coralogix.com" ]
Expand Down
4 changes: 2 additions & 2 deletions config/samples/alerts/promql_alert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ spec:
notificationGroups:
- notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: slack-webhook
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example@coralogix.com" ]
retriggeringPeriodMinutes: 1440
- groupByFields: [ "coralogix.metadata.sdkId" ]
notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: WebhookAlerts
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example@coralogix.com" ]
Expand Down
4 changes: 2 additions & 2 deletions config/samples/alerts/ratio_alert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ spec:
notificationGroups:
- notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: WebhookAlerts
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example@coralogix.com" ]
retriggeringPeriodMinutes: 1440
- groupByFields: [ "coralogix.metadata.sdkId" ]
notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: WebhookAlerts
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example2@coralogix.com" ]
Expand Down
4 changes: 2 additions & 2 deletions config/samples/alerts/standard_alert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ spec:
notificationGroups:
- notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: slack-webhook
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example@coralogix.com" ]
retriggeringPeriodMinutes: 1440
- groupByFields: [ "coralogix.metadata.sdkId" ]
notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: WebhookAlerts
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example2@coralogix.com" ]
Expand Down
4 changes: 2 additions & 2 deletions config/samples/alerts/time_relative_alert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ spec:
notificationGroups:
- notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: WebhookAlerts
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example@coralogix.com" ]
retriggeringPeriodMinutes: 1440
- groupByFields: [ "coralogix.metadata.sdkId" ]
notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: WebhookAlerts
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example2@coralogix.com" ]
Expand Down
2 changes: 1 addition & 1 deletion config/samples/alerts/tracing_alert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spec:
notificationGroups:
- notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: slack-webhook
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example2@coralogix.com" ]
Expand Down
2 changes: 1 addition & 1 deletion config/samples/alerts/unique_count_alert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
- groupByFields: [ "EventType" ]
notifications:
- notifyOn: TriggeredOnly
integrationID: 2235
integrationName: WebhookAlerts
retriggeringPeriodMinutes: 1
- notifyOn: TriggeredAndResolved
emailRecipients: [ "example2@coralogix.com" ]
Expand Down
Loading

0 comments on commit 0e5b0d3

Please sign in to comment.