channel overrides for notifications

feature/custom-chan-for-approvals
Karolis Rusenas 2018-02-25 19:00:08 +00:00
parent 1915eb170d
commit d51f301fbb
2 changed files with 65 additions and 0 deletions

View File

@ -33,6 +33,10 @@ const KeelPollDefaultSchedule = "@every 1m"
// KeelDigestAnnotation - digest annotation
const KeelDigestAnnotation = "keel.sh/digest"
// KeelNotificationChanAnnotation - optional notification to override
// default notification channel(-s) per deployment/chart
const KeelNotificationChanAnnotation = "keel.sh/notify"
// KeelMinimumApprovalsLabel - min approvals
const KeelMinimumApprovalsLabel = "keel.sh/approvals"
@ -174,6 +178,27 @@ type EventNotification struct {
CreatedAt time.Time `json:"createdAt"`
Type Notification `json:"type"`
Level Level `json:"level"`
// Channels is an optional variable to override
// default channel(-s) when performing an update
Channels []string `json:"-"`
}
// ParseEventNotificationChannels - parses deployment annotations or chart config
// to get channel overrides
func ParseEventNotificationChannels(annotations map[string]string) []string {
channels := []string{}
if annotations == nil {
return channels
}
chanStr, ok := annotations[KeelNotificationChanAnnotation]
if ok {
chans := strings.Split(chanStr, ",")
for _, c := range chans {
channels = append(channels, strings.TrimSpace(c))
}
}
return channels
}
// Notification - notification types used by notifier

View File

@ -1,6 +1,7 @@
package types
import (
"reflect"
"testing"
"time"
)
@ -126,3 +127,42 @@ func TestNotExpired(t *testing.T) {
}
}
func TestParseEventNotificationChannels(t *testing.T) {
type args struct {
annotations map[string]string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "no chans",
args: args{map[string]string{"foo": "bar"}},
want: []string{},
},
{
name: "one chan",
args: args{map[string]string{KeelNotificationChanAnnotation: "verychan"}},
want: []string{"verychan"},
},
{
name: "two chans with space",
args: args{map[string]string{KeelNotificationChanAnnotation: "verychan, corp"}},
want: []string{"verychan", "corp"},
},
{
name: "two chans no space",
args: args{map[string]string{KeelNotificationChanAnnotation: "verychan,corp"}},
want: []string{"verychan", "corp"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ParseEventNotificationChannels(tt.args.annotations); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseEventNotificationChannels() = %v, want %v", got, tt.want)
}
})
}
}