-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Google Chat alerts (#953)
Add gchat alerting support Signed-off-by: fpinna <fplkid@gmail.com>
- Loading branch information
Showing
8 changed files
with
180 additions
and
1 deletion.
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
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,120 @@ | ||
/* | ||
Copyright 2020 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package notifier | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
// Google Chat holds the incoming webhook URL | ||
type GChat struct { | ||
URL string | ||
ProxyURL string | ||
} | ||
|
||
// GChatPayload holds the message | ||
type GChatPayload struct { | ||
Cards []GChatCards `json:"cards"` | ||
} | ||
|
||
// Start - GChatCards holds the canary analysis result | ||
type GChatCards struct { | ||
Header GChatHeader `json:"header"` | ||
Sections []*GChatSections `json:"sections"` | ||
} | ||
|
||
type GChatHeader struct { | ||
Title string `json:"title"` | ||
SubTitle string `json:"subtitle"` | ||
ImageUrl string `json:"imageUrl"` | ||
} | ||
|
||
type GChatSections struct { | ||
Widgets []GChatWidgets `json:"widgets"` | ||
} | ||
|
||
type GChatWidgets struct { | ||
TextParagraph GChatText `json:"textParagraph"` | ||
} | ||
|
||
type GChatField struct { | ||
Name string `json:"name"` | ||
Value string `json:"value"` | ||
} | ||
|
||
type GChatText struct { | ||
Text string `json:"text"` | ||
} | ||
|
||
// NewGChat validates the GChat URL and returns a GChat object | ||
func NewGChat(hookURL string, proxyURL string) (*GChat, error) { | ||
_, err := url.ParseRequestURI(hookURL) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid Google Chat webhook URL %s", hookURL) | ||
} | ||
|
||
return &GChat{ | ||
URL: hookURL, | ||
ProxyURL: proxyURL, | ||
}, nil | ||
} | ||
|
||
// Post Google Chat message | ||
func (s *GChat) Post(workload string, namespace string, message string, fields []Field, severity string) error { | ||
facts := make([]*GChatSections, 0, len(fields)) | ||
facts = append(facts, &GChatSections{ | ||
Widgets: []GChatWidgets{ | ||
{ | ||
TextParagraph: GChatText{ | ||
Text: "<font color=#848484><strong>" + message + "</strong></font>", | ||
}, | ||
}, | ||
}, | ||
}) | ||
for _, f := range fields { | ||
facts = append(facts, &GChatSections{ | ||
Widgets: []GChatWidgets{ | ||
{ | ||
TextParagraph: GChatText{ | ||
Text: f.Name + "<br><font color=#848484><strong>" + f.Value + "</strong></font>", | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
payload := GChatPayload{ | ||
Cards: []GChatCards{ | ||
{ | ||
Header: GChatHeader{ | ||
Title: "Flagger", | ||
SubTitle: fmt.Sprintf("%s.%s", workload, namespace), | ||
ImageUrl: "https://flagger.app/favicon.png", | ||
}, | ||
Sections: facts, | ||
}, | ||
}, | ||
} | ||
|
||
err := postMessage(s.URL, s.ProxyURL, payload) | ||
if err != nil { | ||
return fmt.Errorf("postMessage failed: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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,53 @@ | ||
/* | ||
Copyright 2020 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package notifier | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGChat_Post(t *testing.T) { | ||
|
||
fields := []Field{ | ||
{Name: "name1", Value: "value1"}, | ||
{Name: "name2", Value: "value2"}, | ||
} | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
b, err := ioutil.ReadAll(r.Body) | ||
require.NoError(t, err) | ||
|
||
var payload = GChatPayload{} | ||
err = json.Unmarshal(b, &payload) | ||
require.NoError(t, err) | ||
require.Equal(t, "podinfo.test", payload.Cards[0].Header.SubTitle) | ||
require.Equal(t, len(fields), len(payload.Cards[0].Sections)-1) | ||
})) | ||
defer ts.Close() | ||
|
||
GChat, err := NewGChat(ts.URL, "") | ||
require.NoError(t, err) | ||
|
||
err = GChat.Post("podinfo", "test", "test", fields, "info") | ||
require.NoError(t, err) | ||
} |