|
| 1 | +/* |
| 2 | +Copyright 2018 Bitnami |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package lark |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "github.com/sirupsen/logrus" |
| 22 | + "os" |
| 23 | + |
| 24 | + "bytes" |
| 25 | + "encoding/json" |
| 26 | + "net/http" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/bitnami-labs/kubewatch/config" |
| 30 | + "github.com/bitnami-labs/kubewatch/pkg/event" |
| 31 | +) |
| 32 | + |
| 33 | +var webhookErrMsg = ` |
| 34 | +%s |
| 35 | +
|
| 36 | +You need to set Lark Webhook url |
| 37 | +using "--url/-u", or using environment variables: |
| 38 | +
|
| 39 | +export KW_LARK_WEBHOOK_URL=webhook_url |
| 40 | +
|
| 41 | +Command line flags will override environment variables |
| 42 | +
|
| 43 | +` |
| 44 | + |
| 45 | +// Webhook handler implements handler.Handler interface, |
| 46 | +// Notify event to Webhook channel |
| 47 | +type Webhook struct { |
| 48 | + Url string |
| 49 | +} |
| 50 | + |
| 51 | +// TextMessage for messages |
| 52 | +type TextMessage struct { |
| 53 | + MsgType string `json:"msg_type"` |
| 54 | + Content *TextContent `json:"content"` |
| 55 | +} |
| 56 | + |
| 57 | +type TextContent struct { |
| 58 | + Text string `json:"text"` |
| 59 | +} |
| 60 | + |
| 61 | +// Init prepares Webhook configuration |
| 62 | +func (m *Webhook) Init(c *config.Config) error { |
| 63 | + url := c.Handler.Lark.WebhookURL |
| 64 | + if url == "" { |
| 65 | + url = os.Getenv("KW_LARK_WEBHOOK_URL") |
| 66 | + } |
| 67 | + m.Url = url |
| 68 | + return checkMissingWebhookVars(m) |
| 69 | +} |
| 70 | + |
| 71 | +// Handle handles an event. |
| 72 | +func (m *Webhook) Handle(e event.Event) { |
| 73 | + webhookMessage := prepareWebhookMessage(e, m) |
| 74 | + |
| 75 | + err := postMessage(m.Url, webhookMessage) |
| 76 | + if err != nil { |
| 77 | + logrus.Printf("%s\n", err) |
| 78 | + return |
| 79 | + } |
| 80 | + logrus.Printf("Message successfully sent to lark webhook: %s at %s ", m.Url, time.Now()) |
| 81 | +} |
| 82 | + |
| 83 | +func checkMissingWebhookVars(s *Webhook) error { |
| 84 | + if s.Url == "" { |
| 85 | + return fmt.Errorf(webhookErrMsg, "Missing Webhook url") |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func prepareWebhookMessage(e event.Event, m *Webhook) *TextMessage { |
| 91 | + return &TextMessage{ |
| 92 | + MsgType: "text", |
| 93 | + Content: &TextContent{Text: e.Message()}, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func postMessage(url string, textMessage *TextMessage) error { |
| 98 | + message, err := json.Marshal(textMessage) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + req, err := http.NewRequest("POST", url, bytes.NewBuffer(message)) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + req.Header.Add("Content-Type", "application/json") |
| 107 | + |
| 108 | + client := &http.Client{} |
| 109 | + _, err = client.Do(req) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
0 commit comments