Skip to content

Commit

Permalink
Remove webhooks old api (#127)
Browse files Browse the repository at this point in the history
* moving to new webhooks api
  • Loading branch information
OrNovo authored Aug 4, 2024
1 parent 182c972 commit 457d0de
Show file tree
Hide file tree
Showing 71 changed files with 87 additions and 17,108 deletions.
21 changes: 11 additions & 10 deletions apis/coralogix/v1alpha1/alert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package v1alpha1

import (
"context"
"encoding/json"
"fmt"
"reflect"
"strconv"
Expand All @@ -28,10 +27,12 @@ import (
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"
webhooks "github.com/coralogix/coralogix-operator/controllers/clientset/grpc/outbound-webhooks"
"google.golang.org/protobuf/types/known/wrapperspb"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -112,7 +113,7 @@ var (
}
msInHour = int(time.Hour.Milliseconds())
msInMinute = int(time.Minute.Milliseconds())
WebhooksClient clientset.WebhooksClientInterface
WebhooksClient clientset.OutboundWebhooksClientInterface
)

type ProtoTimeFrameAndRelativeTimeFrame struct {
Expand Down Expand Up @@ -1066,17 +1067,17 @@ func expandNotification(ctx context.Context, notification Notification) (*alerts
}

func searchIntegrationID(ctx context.Context, name string) (uint32, error) {
webhooksStr, err := WebhooksClient.GetWebhooks(ctx)
log := log.FromContext(ctx)
log.V(1).Info("Listing all outgoing webhooks")
listWebhooksResp, err := WebhooksClient.ListAllOutgoingWebhooks(ctx, &webhooks.ListAllOutgoingWebhooksRequest{})
if err != nil {
log.Error(err, "Failed to list all outgoing webhooks")
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

for _, webhook := range listWebhooksResp.GetDeployed() {
if webhook.GetName().GetValue() == name {
return webhook.GetExternalId().GetValue(), nil
}
}
return 0, fmt.Errorf("integration with name %s not found", name)
Expand Down
35 changes: 26 additions & 9 deletions controllers/alphacontrollers/alert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package alphacontrollers

import (
"context"
"encoding/json"
stdErr "errors"
"fmt"
"strconv"
"time"

webhooks "github.com/coralogix/coralogix-operator/controllers/clientset/grpc/outbound-webhooks"
"github.com/go-logr/logr"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -76,7 +76,7 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
"namespace", req.NamespacedName.Namespace,
)

coralogixv1alpha1.WebhooksClient = r.CoralogixClientSet.Webhooks()
coralogixv1alpha1.WebhooksClient = r.CoralogixClientSet.OutboundWebhooks()
alert := coralogixv1alpha1.NewAlert()

if err = r.Client.Get(ctx, req.NamespacedName, alert); err != nil {
Expand Down Expand Up @@ -159,6 +159,25 @@ func (r *AlertReconciler) update(ctx context.Context,
return err
}

remoteUpdatedAlert, err := r.CoralogixClientSet.Alerts().GetAlert(ctx, &alerts.GetAlertByUniqueIdRequest{
Id: wrapperspb.String(*alert.Status.ID),
})
if err != nil {
log.Error(err, "Error on getting updated alert")
return err
}
status, err = getStatus(ctx, remoteUpdatedAlert.GetAlert(), alert.Spec)
if err != nil {
log.Error(err, "Error on getting status")
return err
}

alert.Status = status
if err = r.Update(ctx, alert); err != nil {
log.Error(err, "Error on updating alert status")
return err
}

return nil
}

Expand Down Expand Up @@ -1043,17 +1062,15 @@ func flattenNotification(ctx context.Context, notification *alerts.AlertNotifica

switch integration := notification.GetIntegrationType().(type) {
case *alerts.AlertNotification_IntegrationId:
log := log.FromContext(ctx)
id := strconv.Itoa(int(integration.IntegrationId.GetValue()))
webhookStr, err := coralogixv1alpha1.WebhooksClient.GetWebhook(ctx, id)
log.V(1).Info("get webhook", "id", id)
webhook, err := coralogixv1alpha1.WebhooksClient.GetOutboundWebhook(ctx, &webhooks.GetOutgoingWebhookRequest{Id: wrapperspb.String(id)})
if err != nil {
log.Error(err, "error on get webhook")
return flattenedNotification, fmt.Errorf("error on get webhook - %w", err)
}
var m map[string]interface{}
if err = json.Unmarshal([]byte(webhookStr), &m); err != nil {
return flattenedNotification, fmt.Errorf("error on unmarshal webhook - %w", err)
}
flattenedNotification.IntegrationName = new(string)
*flattenedNotification.IntegrationName = m["alias"].(string)
flattenedNotification.IntegrationName = utils.WrapperspbStringToStringPointer(webhook.GetWebhook().GetName())
case *alerts.AlertNotification_Recipients:
flattenedNotification.EmailRecipients = utils.WrappedStringSliceToStringSlice(integration.Recipients.Emails)
}
Expand Down
20 changes: 10 additions & 10 deletions controllers/alphacontrollers/alert_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type PrepareParams struct {
ctx context.Context
clientSet *mock_clientset.MockClientSetInterface
alertsClient *mock_clientset.MockAlertsClientInterface
webhooksClient *mock_clientset.MockWebhooksClientInterface
webhooksClient *mock_clientset.MockOutboundWebhooksClientInterface
alert *coralogixv1alpha1.Alert
remoteAlert *alerts.Alert
}
Expand Down Expand Up @@ -194,11 +194,11 @@ func TestAlertCreation(t *testing.T) {
alertsClient := mock_clientset.NewMockAlertsClientInterface(controller)

// Creating webhooks client.
webhooksClient := mock_clientset.NewMockWebhooksClientInterface(controller)
webhooksClient := mock_clientset.NewMockOutboundWebhooksClientInterface(controller)

// Preparing common mocks.
clientSet.EXPECT().Alerts().MaxTimes(1).MinTimes(1).Return(alertsClient)
clientSet.EXPECT().Webhooks().Return(webhooksClient).AnyTimes()
clientSet.EXPECT().OutboundWebhooks().Return(webhooksClient).AnyTimes()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestAlertUpdate(t *testing.T) {
},
prepare: func(params PrepareParams) {
params.alertsClient.EXPECT().
GetAlert(params.alert.Namespace, coralogixv1alpha1.NewAlert()).
GetAlert(params.ctx, gomock.Any()).
Return(&alerts.GetAlertByUniqueIdResponse{Alert: params.remoteAlert}, nil).
MinTimes(1).MaxTimes(1)

Expand All @@ -368,7 +368,7 @@ func TestAlertUpdate(t *testing.T) {

params.alertsClient.EXPECT().GetAlert(params.ctx, gomock.Any()).
Return(&alerts.GetAlertByUniqueIdResponse{Alert: params.remoteAlert}, nil).
MinTimes(1).MaxTimes(1)
MinTimes(1).MaxTimes(2)
},
},
{
Expand Down Expand Up @@ -413,7 +413,7 @@ func TestAlertUpdate(t *testing.T) {
},
prepare: func(params PrepareParams) {
params.alertsClient.EXPECT().
GetAlert(params.alert.Namespace, coralogixv1alpha1.NewAlert()).
GetAlert(params.ctx, gomock.Any()).
Return(&alerts.GetAlertByUniqueIdResponse{Alert: params.remoteAlert}, nil).
MinTimes(1).MaxTimes(1)

Expand Down Expand Up @@ -444,11 +444,11 @@ func TestAlertUpdate(t *testing.T) {
alertsClient := mock_clientset.NewMockAlertsClientInterface(controller)

// Creating webhooks client.
webhooksClient := mock_clientset.NewMockWebhooksClientInterface(controller)
webhooksClient := mock_clientset.NewMockOutboundWebhooksClientInterface(controller)

// Preparing common mocks.
clientSet.EXPECT().Alerts().MaxTimes(1).MinTimes(1).Return(alertsClient)
clientSet.EXPECT().Webhooks().Return(webhooksClient).AnyTimes()
clientSet.EXPECT().OutboundWebhooks().Return(webhooksClient).AnyTimes()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -654,11 +654,11 @@ func TestAlertDelete(t *testing.T) {
alertsClient := mock_clientset.NewMockAlertsClientInterface(controller)

// Creating webhooks client.
webhooksClient := mock_clientset.NewMockWebhooksClientInterface(controller)
webhooksClient := mock_clientset.NewMockOutboundWebhooksClientInterface(controller)

// Preparing common mocks.
clientSet.EXPECT().Alerts().MaxTimes(1).MinTimes(1).Return(alertsClient)
clientSet.EXPECT().Webhooks().Return(webhooksClient).AnyTimes()
clientSet.EXPECT().OutboundWebhooks().Return(webhooksClient).AnyTimes()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
67 changes: 0 additions & 67 deletions controllers/clientset/actions-client.go

This file was deleted.

7 changes: 0 additions & 7 deletions controllers/clientset/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ type ClientSetInterface interface {
RuleGroups() RuleGroupsClientInterface
Alerts() AlertsClientInterface
RecordingRuleGroups() RecordingRulesGroupsClientInterface
Webhooks() WebhooksClientInterface
OutboundWebhooks() OutboundWebhooksClientInterface
}

type ClientSet struct {
ruleGroups *RuleGroupsClient
alerts *AlertsClient
recordingRuleGroups *RecordingRulesGroupsClient
webhooks *WebhooksClient
outboundWebhooks *OutboundWebhooksClient
}

Expand All @@ -25,10 +23,6 @@ func (c *ClientSet) Alerts() AlertsClientInterface {
return c.alerts
}

func (c *ClientSet) Webhooks() WebhooksClientInterface {
return c.webhooks
}

func (c *ClientSet) RecordingRuleGroups() RecordingRulesGroupsClientInterface {
return c.recordingRuleGroups
}
Expand All @@ -44,7 +38,6 @@ func NewClientSet(targetUrl, apiKey string) ClientSetInterface {
ruleGroups: NewRuleGroupsClient(apikeyCPC),
alerts: NewAlertsClient(apikeyCPC),
recordingRuleGroups: NewRecordingRuleGroupsClient(apikeyCPC),
webhooks: NewWebhooksClient(apikeyCPC),
outboundWebhooks: NewOutboundWebhooksClient(apikeyCPC),
}
}
67 changes: 0 additions & 67 deletions controllers/clientset/dashboards-client.go

This file was deleted.

Loading

0 comments on commit 457d0de

Please sign in to comment.