From 436d7867f3921722b8554d4b1bdc3d0f3f9aabb4 Mon Sep 17 00:00:00 2001 From: Abdelrahman Ahmed <16365652+abahmed@users.noreply.github.com> Date: Fri, 31 May 2024 19:56:12 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20send=20alerts=20to=20channels=20?= =?UTF-8?q?(#306)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 send alerts to channels --- alertmanager/dingtalk/dingtalk_test.go | 10 ++++---- alertmanager/discord/discord.go | 4 +-- alertmanager/discord/discord_test.go | 8 +++--- alertmanager/email/email.go | 6 ++--- alertmanager/email/email_test.go | 10 ++++---- alertmanager/feishu/feishu_test.go | 22 ++++++++-------- alertmanager/googlechat/googlechat_test.go | 10 ++++---- alertmanager/matrix/matrix_test.go | 10 ++++---- alertmanager/mattermost/mattermost.go | 4 +-- alertmanager/mattermost/mattermost_test.go | 10 ++++---- alertmanager/opsgenie/opsgenie.go | 8 +++--- alertmanager/opsgenie/opsgenie_test.go | 30 +++++++++++----------- alertmanager/pagerduty/pagerduty.go | 8 +++--- alertmanager/pagerduty/pagerduty_test.go | 30 +++++++++++----------- alertmanager/rocketchat/rocketchat_test.go | 10 ++++---- alertmanager/slack/slack.go | 4 +-- alertmanager/slack/slack_test.go | 8 +++--- alertmanager/teams/teams_test.go | 10 ++++---- alertmanager/telegram/telegram.go | 4 +-- alertmanager/telegram/telegram_test.go | 10 ++++---- alertmanager/webhook/webhook.go | 4 +-- alertmanager/webhook/webhook_test.go | 30 +++++++++++----------- alertmanager/zenduty/zenduty.go | 6 ++--- alertmanager/zenduty/zenduty_test.go | 30 +++++++++++----------- event/event.go | 14 +++++----- event/format.go | 12 ++++----- handler/executeContainersFilters.go | 12 +++++++++ handler/executePodFilters.go | 12 +++++++++ util/util.go | 14 ++-------- util/util_test.go | 27 +------------------ 30 files changed, 183 insertions(+), 194 deletions(-) diff --git a/alertmanager/dingtalk/dingtalk_test.go b/alertmanager/dingtalk/dingtalk_test.go index 5e60fe90..c271a46f 100644 --- a/alertmanager/dingtalk/dingtalk_test.go +++ b/alertmanager/dingtalk/dingtalk_test.go @@ -128,11 +128,11 @@ func TestSendEvent(t *testing.T) { c.url = s.URL + "/send?accessToken=%s" ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/discord/discord.go b/alertmanager/discord/discord.go index bde4f5b8..dbe59409 100644 --- a/alertmanager/discord/discord.go +++ b/alertmanager/discord/discord.go @@ -77,12 +77,12 @@ func (s *Discord) SendEvent(ev *event.Event) error { }, { Name: "Name", - Value: ev.Name, + Value: ev.PodName, Inline: true, }, { Name: "Container", - Value: ev.Container, + Value: ev.ContainerName, Inline: true, }, { diff --git a/alertmanager/discord/discord_test.go b/alertmanager/discord/discord_test.go index 0e5ea176..14537b4d 100644 --- a/alertmanager/discord/discord_test.go +++ b/alertmanager/discord/discord_test.go @@ -72,10 +72,10 @@ func TestSendEvent(t *testing.T) { c.send = mockedSend ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", Logs: "Nam quis nulla. Integer malesuada. In in enim a arcu " + "imperdiet malesuada. Sed vel lectus. Donec odio urna, tempus " + "molestie, porttitor ut, iaculis quis, sem. Phasellus rhoncus.\n" + diff --git a/alertmanager/email/email.go b/alertmanager/email/email.go index 2ec2ecc6..3a7bf750 100644 --- a/alertmanager/email/email.go +++ b/alertmanager/email/email.go @@ -114,7 +114,7 @@ func (e *Email) buildMessageSubjectAndBody( logsText = util.JsonEscape(ev.Logs) } - subject := fmt.Sprintf("⛑ Kwatch detected a crash in pod %s ", ev.Container) + subject := fmt.Sprintf("⛑ Kwatch detected a crash in pod %s ", ev.ContainerName) body := fmt.Sprintf( "An alert for cluster: *%s* Name: *%s* Container: *%s* "+ "Namespace: *%s* "+ @@ -122,8 +122,8 @@ func (e *Email) buildMessageSubjectAndBody( "Logs: *%s* \\n "+ "Events: *%s* ", e.appCfg.ClusterName, - ev.Name, - ev.Container, + ev.PodName, + ev.ContainerName, ev.Namespace, logsText, eventsText, diff --git a/alertmanager/email/email_test.go b/alertmanager/email/email_test.go index c79b9419..86be012a 100644 --- a/alertmanager/email/email_test.go +++ b/alertmanager/email/email_test.go @@ -122,11 +122,11 @@ func TestSendEvent(t *testing.T) { c.send = mockedSend ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/feishu/feishu_test.go b/alertmanager/feishu/feishu_test.go index 3d98a61d..ac179340 100644 --- a/alertmanager/feishu/feishu_test.go +++ b/alertmanager/feishu/feishu_test.go @@ -44,12 +44,12 @@ func TestBuildRequestBodyFeiShu(t *testing.T) { c := NewFeiShu(configMap, &config.App{ClusterName: "dev"}) assertions.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", - Events: "test", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", + Events: "test", } formattedMsg := ev.FormatMarkdown(c.appCfg.ClusterName, "", "") @@ -113,11 +113,11 @@ func TestSendEvent(t *testing.T) { assertions.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/googlechat/googlechat_test.go b/alertmanager/googlechat/googlechat_test.go index 0c50e6bb..8309e2ab 100644 --- a/alertmanager/googlechat/googlechat_test.go +++ b/alertmanager/googlechat/googlechat_test.go @@ -84,11 +84,11 @@ func TestSendEvent(t *testing.T) { assert.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/matrix/matrix_test.go b/alertmanager/matrix/matrix_test.go index 26e9c935..d3766299 100644 --- a/alertmanager/matrix/matrix_test.go +++ b/alertmanager/matrix/matrix_test.go @@ -118,11 +118,11 @@ func TestSendEvent(t *testing.T) { assert.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/mattermost/mattermost.go b/alertmanager/mattermost/mattermost.go index 5b35d6f2..160dd564 100644 --- a/alertmanager/mattermost/mattermost.go +++ b/alertmanager/mattermost/mattermost.go @@ -149,12 +149,12 @@ func (m *Mattermost) buildMessage(e *event.Event, msg *string) []byte { }, { Title: "Name", - Value: e.Name, + Value: e.PodName, Short: true, }, { Title: "Container", - Value: e.Container, + Value: e.ContainerName, Short: true, }, { diff --git a/alertmanager/mattermost/mattermost_test.go b/alertmanager/mattermost/mattermost_test.go index c3fb1e92..7a6c309d 100644 --- a/alertmanager/mattermost/mattermost_test.go +++ b/alertmanager/mattermost/mattermost_test.go @@ -84,11 +84,11 @@ func TestSendEvent(t *testing.T) { assert.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/opsgenie/opsgenie.go b/alertmanager/opsgenie/opsgenie.go index 720535ca..30051a13 100644 --- a/alertmanager/opsgenie/opsgenie.go +++ b/alertmanager/opsgenie/opsgenie.go @@ -121,21 +121,21 @@ func (m *Opsgenie) buildMessage(e *event.Event) []byte { // use custom title if it's provided, otherwise use default title := m.title if len(title) == 0 { - title = fmt.Sprintf(defaultOpsgenieTitle, e.Name) + title = fmt.Sprintf(defaultOpsgenieTitle, e.PodName) } payload.Message = title // use custom text if it's provided, otherwise use default text := m.text if len(text) == 0 { - text = fmt.Sprintf(defaultOpsgenieText, e.Container, e.Name) + text = fmt.Sprintf(defaultOpsgenieText, e.ContainerName, e.PodName) } payload.Description = text payload.Details = map[string]string{ "Cluster": m.appCfg.ClusterName, - "Name": e.Name, - "Container": e.Container, + "Name": e.PodName, + "Container": e.ContainerName, "Namespace": e.Namespace, "Reason": e.Reason, "Events": events, diff --git a/alertmanager/opsgenie/opsgenie_test.go b/alertmanager/opsgenie/opsgenie_test.go index 259ecf52..077ff0cc 100644 --- a/alertmanager/opsgenie/opsgenie_test.go +++ b/alertmanager/opsgenie/opsgenie_test.go @@ -61,11 +61,11 @@ func TestSendEvent(t *testing.T) { c.url = s.URL ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } @@ -91,11 +91,11 @@ func TestSendEventError(t *testing.T) { c.url = s.URL ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } @@ -113,11 +113,11 @@ func TestInvaildHttpRequest(t *testing.T) { c.url = "h ttp://localhost" ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/pagerduty/pagerduty.go b/alertmanager/pagerduty/pagerduty.go index 9af6d080..9f9a5738 100644 --- a/alertmanager/pagerduty/pagerduty.go +++ b/alertmanager/pagerduty/pagerduty.go @@ -118,11 +118,11 @@ func (s *Pagerduty) buildRequestBodyPagerDuty( } }`, key, - fmt.Sprintf(defaultEventTitle, ev.Container), - ev.Container, + fmt.Sprintf(defaultEventTitle, ev.ContainerName), + ev.ContainerName, s.appCfg.ClusterName, - ev.Name, - ev.Container, + ev.PodName, + ev.ContainerName, ev.Namespace, ev.Reason, eventsText, diff --git a/alertmanager/pagerduty/pagerduty_test.go b/alertmanager/pagerduty/pagerduty_test.go index ebba3c29..c302c1ba 100644 --- a/alertmanager/pagerduty/pagerduty_test.go +++ b/alertmanager/pagerduty/pagerduty_test.go @@ -59,11 +59,11 @@ func TestSendEvent(t *testing.T) { assert.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } @@ -88,11 +88,11 @@ func TestSendEventError(t *testing.T) { c.url = s.URL ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } @@ -110,11 +110,11 @@ func TestInvaildHttpRequest(t *testing.T) { c.url = "h ttp://localhost" ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/rocketchat/rocketchat_test.go b/alertmanager/rocketchat/rocketchat_test.go index 68ebdcad..689992b9 100644 --- a/alertmanager/rocketchat/rocketchat_test.go +++ b/alertmanager/rocketchat/rocketchat_test.go @@ -84,11 +84,11 @@ func TestSendEvent(t *testing.T) { assert.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/slack/slack.go b/alertmanager/slack/slack.go index f86c1682..f281907b 100644 --- a/alertmanager/slack/slack.go +++ b/alertmanager/slack/slack.go @@ -83,8 +83,8 @@ func (s *Slack) SendEvent(ev *event.Event) error { Type: "section", Fields: []*slackClient.TextBlockObject{ markdownF("*Cluster*\n%s", s.appCfg.ClusterName), - markdownF("*Name*\n%s", ev.Name), - markdownF("*Container*\n%s", ev.Container), + markdownF("*Name*\n%s", ev.PodName), + markdownF("*Container*\n%s", ev.ContainerName), markdownF("*Namespace*\n%s", ev.Namespace), markdownF("*Reason*\n%s", ev.Reason), }, diff --git a/alertmanager/slack/slack_test.go b/alertmanager/slack/slack_test.go index e4934c8d..7f7cbc1b 100644 --- a/alertmanager/slack/slack_test.go +++ b/alertmanager/slack/slack_test.go @@ -55,10 +55,10 @@ func TestSendEvent(t *testing.T) { s.send = mockedSend ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", Logs: "Nam quis nulla. Integer malesuada. In in enim a arcu " + "imperdiet malesuada. Sed vel lectus. Donec odio urna, tempus " + "molestie, porttitor ut, iaculis quis, sem. Phasellus rhoncus.\n" + diff --git a/alertmanager/teams/teams_test.go b/alertmanager/teams/teams_test.go index 4550a7f2..5d9f031c 100644 --- a/alertmanager/teams/teams_test.go +++ b/alertmanager/teams/teams_test.go @@ -84,11 +84,11 @@ func TestSendEvent(t *testing.T) { assert.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/telegram/telegram.go b/alertmanager/telegram/telegram.go index 70086b51..c9e671b5 100644 --- a/alertmanager/telegram/telegram.go +++ b/alertmanager/telegram/telegram.go @@ -102,8 +102,8 @@ func (t *Telegram) buildRequestBodyTelegram( "Logs: *%s* \\n "+ "Events: *%s* ", t.appCfg.ClusterName, - e.Name, - e.Container, + e.PodName, + e.ContainerName, e.Namespace, logsText, eventsText, diff --git a/alertmanager/telegram/telegram_test.go b/alertmanager/telegram/telegram_test.go index 0cd22c64..52efd920 100644 --- a/alertmanager/telegram/telegram_test.go +++ b/alertmanager/telegram/telegram_test.go @@ -107,11 +107,11 @@ func TestSendEvent(t *testing.T) { assert.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/webhook/webhook.go b/alertmanager/webhook/webhook.go index db2e6ead..38977cd5 100644 --- a/alertmanager/webhook/webhook.go +++ b/alertmanager/webhook/webhook.go @@ -132,8 +132,8 @@ func (w *Webhook) buildRequestBody( postBody, _ := json.Marshal(map[string]interface{}{ "Cluster": w.appCfg.ClusterName, - "Name": ev.Name, - "Container": ev.Container, + "Name": ev.PodName, + "Container": ev.ContainerName, "Namespace": ev.Namespace, "Reason": ev.Reason, "Events": eventsText, diff --git a/alertmanager/webhook/webhook_test.go b/alertmanager/webhook/webhook_test.go index 0f3ac19e..c3900272 100644 --- a/alertmanager/webhook/webhook_test.go +++ b/alertmanager/webhook/webhook_test.go @@ -96,11 +96,11 @@ func TestSendEvent(t *testing.T) { assert.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } @@ -134,11 +134,11 @@ func TestSendEventError(t *testing.T) { assert.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } @@ -155,11 +155,11 @@ func TestInvaildHttpRequest(t *testing.T) { assert.NotNil(c) ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/alertmanager/zenduty/zenduty.go b/alertmanager/zenduty/zenduty.go index c25fc56d..89417e68 100644 --- a/alertmanager/zenduty/zenduty.go +++ b/alertmanager/zenduty/zenduty.go @@ -126,7 +126,7 @@ func (m *Zenduty) buildMessage(e *event.Event) []byte { events = (e.Events) } - payload.Message = fmt.Sprintf(defaultZendutyTitle, e.Name) + payload.Message = fmt.Sprintf(defaultZendutyTitle, e.PodName) payload.Summary = fmt.Sprintf( "An alert has been triggered for\n\n"+ "cluster: %s\n"+ @@ -137,8 +137,8 @@ func (m *Zenduty) buildMessage(e *event.Event) []byte { "Events:\n%s\n\n"+ "Logs:\n%s\n\n", m.appCfg.ClusterName, - e.Name, - e.Container, + e.PodName, + e.ContainerName, e.Namespace, e.Reason, events, diff --git a/alertmanager/zenduty/zenduty_test.go b/alertmanager/zenduty/zenduty_test.go index 9ba1b57b..4e458c85 100644 --- a/alertmanager/zenduty/zenduty_test.go +++ b/alertmanager/zenduty/zenduty_test.go @@ -61,11 +61,11 @@ func TestSendEvent(t *testing.T) { c.url = s.URL ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } @@ -91,11 +91,11 @@ func TestSendEventError(t *testing.T) { c.url = s.URL ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } @@ -113,11 +113,11 @@ func TestInvaildHttpRequest(t *testing.T) { c.url = "h ttp://localhost" ev := event.Event{ - Name: "test-pod", - Container: "test-container", - Namespace: "default", - Reason: "OOMKILLED", - Logs: "test\ntestlogs", + PodName: "test-pod", + ContainerName: "test-container", + Namespace: "default", + Reason: "OOMKILLED", + Logs: "test\ntestlogs", Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + "event3\nevent5\nevent6-event8-event11-event12", } diff --git a/event/event.go b/event/event.go index 486259fb..8d68156e 100644 --- a/event/event.go +++ b/event/event.go @@ -2,11 +2,11 @@ package event // Event used to represent info needed by providers to send messages type Event struct { - Name string - Container string - Namespace string - Reason string - Events string - Logs string - Labels map[string]string + PodName string + ContainerName string + Namespace string + Reason string + Events string + Logs string + Labels map[string]string } diff --git a/event/format.go b/event/format.go index c7eb10a3..37eb98a7 100644 --- a/event/format.go +++ b/event/format.go @@ -41,8 +41,8 @@ func (e *Event) FormatMarkdown(clusterName, text, delimiter string) string { "**Events:**\n```\n%s\n```"+delimiter+ "**Logs:**\n```\n%s\n```", text, - clusterName, e.Name, - e.Container, + clusterName, e.PodName, + e.ContainerName, e.Namespace, e.Reason, eventsText, @@ -84,8 +84,8 @@ func (e *Event) FormatHtml(clusterName, text string) string { "Logs:
%s
", text, clusterName, - e.Name, - e.Container, + e.PodName, + e.ContainerName, e.Namespace, e.Reason, strings.ReplaceAll(eventsText, "\n", "
"), @@ -126,8 +126,8 @@ func (e *Event) FormatText(clusterName, text string) string { "Events:\n%s\n\n"+ "Logs:\n%s\n\n", clusterName, - e.Name, - e.Container, + e.PodName, + e.ContainerName, e.Namespace, e.Reason, eventsText, diff --git a/handler/executeContainersFilters.go b/handler/executeContainersFilters.go index e9d08c0e..dbafd439 100644 --- a/handler/executeContainersFilters.go +++ b/handler/executeContainersFilters.go @@ -3,8 +3,10 @@ package handler import ( "time" + "github.com/abahmed/kwatch/event" "github.com/abahmed/kwatch/filter" "github.com/abahmed/kwatch/storage" + "github.com/abahmed/kwatch/util" "github.com/sirupsen/logrus" ) @@ -51,6 +53,16 @@ func (h *handler) executeContainersFilters(ctx *filter.Context) { ctx.Container.Reason, ctx.Container.Msg, ctx.Container.ExitCode) + + h.alertManager.NotifyEvent(event.Event{ + PodName: ctx.Pod.Name, + ContainerName: ctx.Container.Container.Name, + Namespace: ctx.Pod.Namespace, + Reason: ctx.Container.Reason, + Events: util.GetPodEventsStr(ctx.Events), + Logs: ctx.Container.Logs, + Labels: ctx.Pod.Labels, + }) } } } diff --git a/handler/executePodFilters.go b/handler/executePodFilters.go index 9d0afadc..05139bfa 100644 --- a/handler/executePodFilters.go +++ b/handler/executePodFilters.go @@ -1,8 +1,10 @@ package handler import ( + "github.com/abahmed/kwatch/event" "github.com/abahmed/kwatch/filter" "github.com/abahmed/kwatch/storage" + "github.com/abahmed/kwatch/util" "github.com/sirupsen/logrus" ) @@ -38,4 +40,14 @@ func (h *handler) executePodFilters(ctx *filter.Context) { ) logrus.Printf("pod only issue %s %s %s %s", ctx.Pod.Name, ownerName, ctx.PodReason, ctx.PodMsg) + + h.alertManager.NotifyEvent(event.Event{ + PodName: ctx.Pod.Name, + ContainerName: "", + Namespace: ctx.Pod.Namespace, + Reason: ctx.PodReason, + Events: util.GetPodEventsStr(ctx.Events), + Logs: "", + Labels: ctx.Pod.Labels, + }) } diff --git a/util/util.go b/util/util.go index 2355c86c..1b276dd7 100644 --- a/util/util.go +++ b/util/util.go @@ -17,20 +17,10 @@ import ( ) // GetPodEventsStr returns formatted events as a string for specified pod -func GetPodEventsStr(c kubernetes.Interface, name, namespace string) string { - events, err := GetPodEvents(c, name, namespace) - +func GetPodEventsStr(events *[]v1.Event) string { eventsString := "" - if err != nil { - logrus.Warnf( - "failed to get events for %s@%s: %s", - name, - namespace, - err.Error()) - return eventsString - } - for _, ev := range events.Items { + for _, ev := range *events { eventsString += fmt.Sprintf( "[%s] %s %s\n", diff --git a/util/util_test.go b/util/util_test.go index 1e62b5af..2ba4841a 100644 --- a/util/util_test.go +++ b/util/util_test.go @@ -63,44 +63,19 @@ func TestJsonEscape(t *testing.T) { func TestGetPodEventsStr(t *testing.T) { assert := assert.New(t) - cli := fake.NewSimpleClientset() event := v1.Event{ Reason: "test reason", Message: "test message", LastTimestamp: metav1.Now(), } - cli.PrependReactor( - "list", - "events", - func(action k8stesting.Action) (bool, runtime.Object, error) { - return true, &v1.EventList{ - Items: []v1.Event{event}, - }, nil - }) - result := GetPodEventsStr(cli, "dummy-app-579f7cd745-t6fdg", "test") + result := GetPodEventsStr(&[]v1.Event{event}) expectedOutput := "[" + event.LastTimestamp.String() + "] " + event.Reason + " " + event.Message assert.Equal(result, expectedOutput) } -func TestGetPodEventsStrError(t *testing.T) { - assert := assert.New(t) - - cli := fake.NewSimpleClientset() - - cli.PrependReactor( - "list", - "events", - func(action k8stesting.Action) (bool, runtime.Object, error) { - return true, nil, errors.New("ssss") - }) - - result := GetPodEventsStr(cli, "dummy-app-579f7cd745-t6fdg", "test") - assert.Equal(result, "") -} - func TestContainsKillingStoppingContainerEvents(t *testing.T) { assert := assert.New(t)