-
Notifications
You must be signed in to change notification settings - Fork 727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Google Chat alerts #953
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f95af1f
add gchat alerting support
afa6ac3
add gchat alerting support
51a55e0
tests ok
e439bb1
rollback wrong removed
4f81534
Remove wrong dash on crd file.
ab72394
add gchat option to Notifier Factory
7825086
add gcht to crd file and make crd
ce4cd61
Fix func name
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add this to
artifacts/flagger/crd.yaml
then runmake crd
and commit all changes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done