-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package googlechat | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/abahmed/kwatch/config" | ||
"github.com/abahmed/kwatch/event" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type GoogleChat struct { | ||
webhook string | ||
text string | ||
|
||
// reference for general app configuration | ||
appCfg *config.App | ||
} | ||
|
||
type payload struct { | ||
Text string `json:"text"` | ||
} | ||
|
||
// NewGoogleChat returns new google chat instance | ||
func NewGoogleChat(config map[string]interface{}, appCfg *config.App) *GoogleChat { | ||
webhook, ok := config["webhook"].(string) | ||
if !ok || len(webhook) == 0 { | ||
logrus.Warnf("initializing Google Chat with empty webhook url") | ||
return nil | ||
} | ||
|
||
logrus.Infof("initializing Google Chat with webhook url: %s", webhook) | ||
|
||
text, _ := config["text"].(string) | ||
|
||
return &GoogleChat{ | ||
webhook: webhook, | ||
text: text, | ||
appCfg: appCfg, | ||
} | ||
} | ||
|
||
// Name returns name of the provider | ||
func (r *GoogleChat) Name() string { | ||
return "Google Chat" | ||
} | ||
|
||
// SendEvent sends event to the provider | ||
func (r *GoogleChat) SendEvent(e *event.Event) error { | ||
formattedMsg := e.FormatText(r.appCfg.ClusterName, r.text) | ||
return r.sendAPI(r.buildRequestBody(formattedMsg)) | ||
} | ||
|
||
func (r *GoogleChat) sendAPI(reqBody string) error { | ||
client := &http.Client{} | ||
buffer := bytes.NewBuffer([]byte(reqBody)) | ||
request, err := http.NewRequest(http.MethodPost, r.webhook, buffer) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
request.Header.Set("Content-Type", "application/json") | ||
|
||
response, err := client.Do(request) | ||
if err != nil { | ||
return err | ||
} | ||
defer response.Body.Close() | ||
|
||
if response.StatusCode != 200 { | ||
body, _ := io.ReadAll(response.Body) | ||
return fmt.Errorf( | ||
"call to google chat alert returned status code %d: %s", | ||
response.StatusCode, | ||
string(body)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// SendMessage sends text message to the provider | ||
func (r *GoogleChat) SendMessage(msg string) error { | ||
return r.sendAPI(r.buildRequestBody(msg)) | ||
} | ||
|
||
func (r *GoogleChat) buildRequestBody(text string) string { | ||
msgPayload := &payload{ | ||
Text: text, | ||
} | ||
|
||
jsonBytes, _ := json.Marshal(msgPayload) | ||
return string(jsonBytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package googlechat | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/abahmed/kwatch/config" | ||
"github.com/abahmed/kwatch/event" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEmptyConfig(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
c := NewGoogleChat(map[string]interface{}{}, &config.App{ClusterName: "dev"}) | ||
assert.Nil(c) | ||
} | ||
|
||
func TestGoogleChat(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
configMap := map[string]interface{}{ | ||
"webhook": "testtest", | ||
} | ||
c := NewGoogleChat(configMap, &config.App{ClusterName: "dev"}) | ||
assert.NotNil(c) | ||
|
||
assert.Equal(c.Name(), "Google Chat") | ||
} | ||
|
||
func TestSendMessage(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
s := httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(`{"isOk": true}`)) | ||
})) | ||
|
||
defer s.Close() | ||
|
||
configMap := map[string]interface{}{ | ||
"webhook": s.URL, | ||
} | ||
c := NewGoogleChat(configMap, &config.App{ClusterName: "dev"}) | ||
assert.NotNil(c) | ||
|
||
assert.Nil(c.SendMessage("test")) | ||
} | ||
|
||
func TestSendMessageError(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
s := httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusBadGateway) | ||
})) | ||
|
||
defer s.Close() | ||
|
||
configMap := map[string]interface{}{ | ||
"webhook": s.URL, | ||
} | ||
c := NewGoogleChat(configMap, &config.App{ClusterName: "dev"}) | ||
assert.NotNil(c) | ||
|
||
assert.NotNil(c.SendMessage("test")) | ||
} | ||
|
||
func TestSendEvent(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
s := httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(`{"isOk": true}`)) | ||
})) | ||
|
||
defer s.Close() | ||
|
||
configMap := map[string]interface{}{ | ||
"webhook": s.URL, | ||
} | ||
c := NewGoogleChat(configMap, &config.App{ClusterName: "dev"}) | ||
assert.NotNil(c) | ||
|
||
ev := event.Event{ | ||
Name: "test-pod", | ||
Container: "test-container", | ||
Namespace: "default", | ||
Reason: "OOMKILLED", | ||
Logs: "test\ntestlogs", | ||
Events: "event1-event2-event3-event1-event2-event3-event1-event2-" + | ||
"event3\nevent5\nevent6-event8-event11-event12", | ||
} | ||
assert.Nil(c.SendEvent(&ev)) | ||
} | ||
|
||
func TestInvaildHttpRequest(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
configMap := map[string]interface{}{ | ||
"webhook": "h ttp://localhost", | ||
} | ||
c := NewGoogleChat(configMap, &config.App{ClusterName: "dev"}) | ||
assert.NotNil(c) | ||
|
||
assert.NotNil(c.SendMessage("test")) | ||
|
||
configMap = map[string]interface{}{ | ||
"webhook": "http://localhost:132323", | ||
} | ||
c = NewGoogleChat(configMap, &config.App{ClusterName: "dev"}) | ||
assert.NotNil(c) | ||
|
||
assert.NotNil(c.SendMessage("test")) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters