-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
- Loading branch information
1 parent
9ecfb37
commit 61103fc
Showing
6 changed files
with
162 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package notifier | ||
|
||
import ( | ||
"crypto/sha1" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/fluxcd/pkg/runtime/events" | ||
"github.com/hashicorp/go-retryablehttp" | ||
) | ||
|
||
type Matrix struct { | ||
Token string | ||
URL string | ||
RoomId string | ||
} | ||
|
||
type MatrixPayload struct { | ||
Body string `json:"body"` | ||
MsgType string `json:"msgtype"` | ||
} | ||
|
||
func NewMatrix(serverURL, token, roomId string) (*Matrix, error) { | ||
_, err := url.ParseRequestURI(serverURL) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid Matrix homeserver URL %s", serverURL) | ||
} | ||
|
||
return &Matrix{ | ||
URL: serverURL, | ||
RoomId: roomId, | ||
Token: token, | ||
}, nil | ||
} | ||
|
||
func (m *Matrix) Post(event events.Event) error { | ||
txId, err := sha1sum(event) | ||
if err != nil { | ||
return fmt.Errorf("unable to generate unique tx id: %s", err) | ||
} | ||
fullURL := fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message/%s", | ||
m.URL, m.RoomId, txId) | ||
|
||
emoji := "💫" | ||
if event.Severity == events.EventSeverityError { | ||
emoji = "🚨" | ||
} | ||
var metadata string | ||
for k, v := range event.Metadata { | ||
metadata = metadata + fmt.Sprintf("- %s: %s\n", k, v) | ||
} | ||
heading := fmt.Sprintf("%s %s/%s.%s", emoji, strings.ToLower(event.InvolvedObject.Kind), | ||
event.InvolvedObject.Name, event.InvolvedObject.Namespace) | ||
msg := fmt.Sprintf("%s\n%s\n%s", heading, event.Message, metadata) | ||
|
||
payload := MatrixPayload{ | ||
Body: msg, | ||
MsgType: "m.text", | ||
} | ||
|
||
err = postMessage(fullURL, "", nil, payload, func(request *retryablehttp.Request) { | ||
request.Method = http.MethodPut | ||
request.Header.Add("Authorization", "Bearer "+m.Token) | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("postMessage failed: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func sha1sum(event events.Event) (string, error) { | ||
val, err := json.Marshal(event) | ||
if err != nil { | ||
return "", err | ||
} | ||
digest := sha1.Sum(val) | ||
return fmt.Sprintf("%x", digest), 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,50 @@ | ||
package notifier | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/fluxcd/pkg/runtime/events" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestSha1Sum(t *testing.T) { | ||
timestamp, err := time.Parse("Jan 2, 2006 at 3:04pm (WAT)", "Aug 24, 2021 at 4:18pm (WAT)") | ||
if err != nil { | ||
t.Fatalf("unexpected error getting timestamp: %s", err) | ||
} | ||
|
||
tests := []struct { | ||
event events.Event | ||
sha1 string | ||
}{ | ||
{ | ||
event: events.Event{ | ||
InvolvedObject: corev1.ObjectReference{}, | ||
Severity: events.EventSeverityInfo, | ||
Timestamp: metav1.Time{ | ||
Time: timestamp, | ||
}, | ||
Message: "update successful", | ||
Reason: "update sucesful", | ||
Metadata: nil, | ||
ReportingController: "", | ||
ReportingInstance: "", | ||
}, | ||
sha1: "37d91b4f6a1e44c6a38273b0a0fd408fade7b0f5", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
hash, err := sha1sum(tt.event) | ||
if err != nil { | ||
t.Fatalf("unexpected err: %s", err) | ||
} | ||
|
||
if tt.sha1 != hash { | ||
t.Errorf("wrong sha1 sum from event %v. expected %q got %q", | ||
tt.event, tt.sha1, hash) | ||
} | ||
} | ||
} |