Skip to content

Commit

Permalink
Merge pull request #115 from linki/event-recorder
Browse files Browse the repository at this point in the history
Replace event related code with Kube's EventRecorder
  • Loading branch information
linki authored Nov 27, 2018
2 parents 27e59bd + 8169dae commit e26dbd6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 77 deletions.
13 changes: 12 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 14 additions & 33 deletions chaoskube/chaoskube.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/tools/reference"

"github.com/linki/chaoskube/util"
Expand Down Expand Up @@ -44,10 +46,10 @@ type Chaoskube struct {
Logger log.FieldLogger
// dry run will not allow any pod terminations
DryRun bool
// create event with deletion message in victims namespace
CreateEvent bool
// grace period to terminate the pods
GracePeriod time.Duration
// event recorder allows to publish events to Kubernetes
EventRecorder record.EventRecorder
// a function to retrieve the current time
Now func() time.Time
}
Expand All @@ -72,8 +74,11 @@ var (
// * a time zone to apply to the aforementioned time-based filters
// * a logger implementing logrus.FieldLogger to send log output to
// * whether to enable/disable dry-run mode
// * whether to enable/disable event creation
func New(client kubernetes.Interface, labels, annotations, namespaces labels.Selector, excludedWeekdays []time.Weekday, excludedTimesOfDay []util.TimePeriod, excludedDaysOfYear []time.Time, timezone *time.Location, minimumAge time.Duration, logger log.FieldLogger, dryRun bool, createEvent bool, gracePeriod time.Duration) *Chaoskube {
func New(client kubernetes.Interface, labels, annotations, namespaces labels.Selector, excludedWeekdays []time.Weekday, excludedTimesOfDay []util.TimePeriod, excludedDaysOfYear []time.Time, timezone *time.Location, minimumAge time.Duration, logger log.FieldLogger, dryRun bool, gracePeriod time.Duration) *Chaoskube {
broadcaster := record.NewBroadcaster()
broadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: client.CoreV1().Events(v1.NamespaceAll)})
recorder := broadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "chaoskube"})

return &Chaoskube{
Client: client,
Labels: labels,
Expand All @@ -86,8 +91,8 @@ func New(client kubernetes.Interface, labels, annotations, namespaces labels.Sel
MinimumAge: minimumAge,
Logger: logger,
DryRun: dryRun,
CreateEvent: createEvent,
GracePeriod: gracePeriod,
EventRecorder: recorder,
Now: time.Now,
}
}
Expand Down Expand Up @@ -205,38 +210,14 @@ func (c *Chaoskube) DeletePod(victim v1.Pod) error {
return err
}

err = c.CreateDeleteEvent(victim)
if err != nil {
c.Logger.WithField("err", err).Error("failed to create deletion event")
}

return nil
}

// CreateDeleteEvent creates an event in victims namespace with an deletion message.
func (c *Chaoskube) CreateDeleteEvent(victim v1.Pod) error {
ref, err := reference.GetReference(scheme.Scheme, victim.DeepCopyObject())
ref, err := reference.GetReference(scheme.Scheme, &victim)
if err != nil {
return err
}

t := metav1.Time{Time: c.Now()}
_, err = c.Client.CoreV1().Events(victim.Namespace).Create(&v1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s.chaos.%x", victim.Name, t.UnixNano()),
Namespace: victim.Namespace,
},
InvolvedObject: *ref,
Reason: "Chaos",
Message: fmt.Sprintf("Deleted pod %s", victim.Name),
FirstTimestamp: t,
LastTimestamp: t,
Count: 1,
Action: "Deleted",
Type: v1.EventTypeNormal,
})

return err
c.EventRecorder.Event(ref, v1.EventTypeNormal, "Killing", "Pod was deleted by chaoskube to introduce chaos.")

return nil
}

// filterByNamespaces filters a list of pods by a given namespace selector.
Expand Down
44 changes: 2 additions & 42 deletions chaoskube/chaoskube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (suite *Suite) TestNew() {
minimumAge,
logger,
false,
true,
gracePeriod,
)
suite.Require().NotNil(chaoskube)
Expand Down Expand Up @@ -89,7 +88,6 @@ func (suite *Suite) TestRunContextCanceled() {
time.UTC,
time.Duration(0),
false,
true,
10,
)

Expand Down Expand Up @@ -140,7 +138,6 @@ func (suite *Suite) TestCandidates() {
time.UTC,
time.Duration(0),
false,
true,
10,
)

Expand Down Expand Up @@ -176,7 +173,6 @@ func (suite *Suite) TestVictim() {
time.UTC,
time.Duration(0),
false,
true,
10,
)

Expand All @@ -196,7 +192,6 @@ func (suite *Suite) TestNoVictimReturnsError() {
time.UTC,
time.Duration(0),
false,
true,
10,
)

Expand Down Expand Up @@ -226,7 +221,6 @@ func (suite *Suite) TestDeletePod() {
time.UTC,
time.Duration(0),
tt.dryRun,
true,
10,
)

Expand Down Expand Up @@ -458,7 +452,6 @@ func (suite *Suite) TestTerminateVictim() {
tt.timezone,
time.Duration(0),
false,
true,
10,
)
chaoskube.Now = tt.now
Expand All @@ -473,35 +466,6 @@ func (suite *Suite) TestTerminateVictim() {
}
}

func (suite *Suite) TestTerminateVictimCreatesEvent() {
chaoskube := suite.setupWithPods(
labels.Everything(),
labels.Everything(),
labels.Everything(),
[]time.Weekday{},
[]util.TimePeriod{},
[]time.Time{},
time.UTC,
time.Duration(0),
false,
true,
10,
)
chaoskube.Now = ThankGodItsFriday{}.Now

err := chaoskube.TerminateVictim()
suite.Require().NoError(err)

events, err := chaoskube.Client.CoreV1().Events(v1.NamespaceAll).List(metav1.ListOptions{})
suite.Require().NoError(err)

suite.Require().Len(events.Items, 1)
event := events.Items[0]

suite.Equal("foo.chaos.-2be96689beac4e00", event.Name)
suite.Equal("Deleted pod foo", event.Message)
}

// TestTerminateNoVictimLogsInfo tests that missing victim prints a log message
func (suite *Suite) TestTerminateNoVictimLogsInfo() {
chaoskube := suite.setup(
Expand All @@ -514,7 +478,6 @@ func (suite *Suite) TestTerminateNoVictimLogsInfo() {
time.UTC,
time.Duration(0),
false,
true,
10,
)

Expand Down Expand Up @@ -564,7 +527,7 @@ func (suite *Suite) assertLog(level log.Level, msg string, fields log.Fields) {
}
}

func (suite *Suite) setupWithPods(labelSelector labels.Selector, annotations labels.Selector, namespaces labels.Selector, excludedWeekdays []time.Weekday, excludedTimesOfDay []util.TimePeriod, excludedDaysOfYear []time.Time, timezone *time.Location, minimumAge time.Duration, dryRun bool, createEvent bool, gracePeriod time.Duration) *Chaoskube {
func (suite *Suite) setupWithPods(labelSelector labels.Selector, annotations labels.Selector, namespaces labels.Selector, excludedWeekdays []time.Weekday, excludedTimesOfDay []util.TimePeriod, excludedDaysOfYear []time.Time, timezone *time.Location, minimumAge time.Duration, dryRun bool, gracePeriod time.Duration) *Chaoskube {
chaoskube := suite.setup(
labelSelector,
annotations,
Expand All @@ -575,7 +538,6 @@ func (suite *Suite) setupWithPods(labelSelector labels.Selector, annotations lab
timezone,
minimumAge,
dryRun,
createEvent,
gracePeriod,
)

Expand All @@ -593,7 +555,7 @@ func (suite *Suite) setupWithPods(labelSelector labels.Selector, annotations lab
return chaoskube
}

func (suite *Suite) setup(labelSelector labels.Selector, annotations labels.Selector, namespaces labels.Selector, excludedWeekdays []time.Weekday, excludedTimesOfDay []util.TimePeriod, excludedDaysOfYear []time.Time, timezone *time.Location, minimumAge time.Duration, dryRun bool, createEvent bool, gracePeriod time.Duration) *Chaoskube {
func (suite *Suite) setup(labelSelector labels.Selector, annotations labels.Selector, namespaces labels.Selector, excludedWeekdays []time.Weekday, excludedTimesOfDay []util.TimePeriod, excludedDaysOfYear []time.Time, timezone *time.Location, minimumAge time.Duration, dryRun bool, gracePeriod time.Duration) *Chaoskube {
logOutput.Reset()

return New(
Expand All @@ -608,7 +570,6 @@ func (suite *Suite) setup(labelSelector labels.Selector, annotations labels.Sele
minimumAge,
logger,
dryRun,
createEvent,
gracePeriod,
)
}
Expand Down Expand Up @@ -709,7 +670,6 @@ func (suite *Suite) TestMinimumAge() {
time.UTC,
tt.minimumAge,
false,
true,
10,
)
chaoskube.Now = tt.now
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ func main() {
minimumAge,
log.StandardLogger(),
dryRun,
createEvent,
gracePeriod,
)

Expand Down
4 changes: 4 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func TimeOfDay(pointInTime time.Time) time.Time {
// NewPod returns a new pod instance for testing purposes.
func NewPod(namespace, name string, phase v1.PodPhase) v1.Pod {
return v1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
Expand Down

0 comments on commit e26dbd6

Please sign in to comment.