-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from GaruGaru/master
Slack notification integration
- Loading branch information
Showing
9 changed files
with
364 additions
and
2 deletions.
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,16 @@ | ||
package notifier | ||
|
||
import ( | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const NotifierNoop = "noop" | ||
|
||
type Noop struct { | ||
Calls int | ||
} | ||
|
||
func (t *Noop) NotifyPodTermination(pod v1.Pod) error { | ||
t.Calls++ | ||
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,32 @@ | ||
package notifier | ||
|
||
import ( | ||
multierror "github.com/hashicorp/go-multierror" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
type Notifier interface { | ||
NotifyPodTermination(pod v1.Pod) error | ||
} | ||
|
||
type Notifiers struct { | ||
notifiers []Notifier | ||
} | ||
|
||
func New() *Notifiers { | ||
return &Notifiers{notifiers: make([]Notifier, 0)} | ||
} | ||
|
||
func (m *Notifiers) NotifyPodTermination(pod v1.Pod) error { | ||
var result error | ||
for _, n := range m.notifiers { | ||
if err := n.NotifyPodTermination(pod); err != nil { | ||
result = multierror.Append(result, err) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func (m *Notifiers) Add(notifier Notifier) { | ||
m.notifiers = append(m.notifiers, notifier) | ||
} |
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,87 @@ | ||
package notifier | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/go-multierror" | ||
"testing" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
|
||
"github.com/linki/chaoskube/internal/testutil" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type NotifierSuite struct { | ||
testutil.TestSuite | ||
} | ||
|
||
type FailingNotifier struct{} | ||
|
||
func (f FailingNotifier) NotifyPodTermination(pod v1.Pod) error { | ||
return fmt.Errorf("notify error") | ||
} | ||
|
||
func (suite *NotifierSuite) TestMultiNotifierWithoutNotifiers() { | ||
manager := New() | ||
err := manager.NotifyPodTermination(v1.Pod{}) | ||
suite.NoError(err) | ||
} | ||
|
||
func (suite *NotifierSuite) TestMultiNotifierWithNotifier() { | ||
manager := New() | ||
n := Noop{} | ||
manager.Add(&n) | ||
err := manager.NotifyPodTermination(v1.Pod{}) | ||
suite.Require().NoError(err) | ||
|
||
suite.Equal(1, n.Calls) | ||
} | ||
|
||
func (suite *NotifierSuite) TestMultiNotifierWithMultipleNotifier() { | ||
manager := New() | ||
n1 := Noop{} | ||
n2 := Noop{} | ||
manager.Add(&n1) | ||
manager.Add(&n2) | ||
|
||
err := manager.NotifyPodTermination(v1.Pod{}) | ||
suite.Require().NoError(err) | ||
|
||
suite.Equal(1, n1.Calls) | ||
suite.Equal(1, n2.Calls) | ||
} | ||
|
||
func (suite *NotifierSuite) TestMultiNotifierWithNotifierError() { | ||
manager := New() | ||
f := FailingNotifier{} | ||
manager.Add(&f) | ||
err := manager.NotifyPodTermination(v1.Pod{}) | ||
suite.Require().Error(err) | ||
} | ||
|
||
func (suite *NotifierSuite) TestMultiNotifierWithNotifierMultipleError() { | ||
manager := New() | ||
f0 := FailingNotifier{} | ||
f1 := FailingNotifier{} | ||
manager.Add(&f0) | ||
manager.Add(&f1) | ||
err := manager.NotifyPodTermination(v1.Pod{}).(*multierror.Error) | ||
suite.Require().Error(err) | ||
suite.Require().Len(err.Errors, 2) | ||
} | ||
|
||
func (suite *NotifierSuite) TestMultiNotifierWithOneFailingNotifier() { | ||
manager := New() | ||
f := FailingNotifier{} | ||
n := Noop{} | ||
manager.Add(&n) | ||
manager.Add(&f) | ||
err := manager.NotifyPodTermination(v1.Pod{}).(*multierror.Error) | ||
suite.Require().Error(err) | ||
suite.Require().Len(err.Errors, 1) | ||
} | ||
|
||
func TestNotifierSuite(t *testing.T) { | ||
suite.Run(t, new(NotifierSuite)) | ||
} |
Oops, something went wrong.