Skip to content

Commit

Permalink
use context
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Linkhorst committed Mar 31, 2020
1 parent 406f4bc commit 5150e48
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
20 changes: 10 additions & 10 deletions chaoskube/chaoskube.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func New(client kubernetes.Interface, labels, annotations, namespaces, namespace
// described by channel next. It returns when the given context is canceled.
func (c *Chaoskube) Run(ctx context.Context, next <-chan time.Time) {
for {
if err := c.TerminateVictims(); err != nil {
if err := c.TerminateVictims(ctx); err != nil {
c.Logger.WithField("err", err).Error("failed to terminate victim")
metrics.ErrorsTotal.Inc()
}
Expand All @@ -142,7 +142,7 @@ func (c *Chaoskube) Run(ctx context.Context, next <-chan time.Time) {

// TerminateVictims picks and deletes a victim.
// It respects the configured excluded weekdays, times of day and days of a year filters.
func (c *Chaoskube) TerminateVictims() error {
func (c *Chaoskube) TerminateVictims(ctx context.Context) error {
now := c.Now().In(c.Timezone)

for _, wd := range c.ExcludedWeekdays {
Expand All @@ -166,7 +166,7 @@ func (c *Chaoskube) TerminateVictims() error {
}
}

victims, err := c.Victims()
victims, err := c.Victims(ctx)
if err == errPodNotFound {
c.Logger.Debug(msgVictimNotFound)
return nil
Expand All @@ -177,16 +177,16 @@ func (c *Chaoskube) TerminateVictims() error {

var result *multierror.Error
for _, victim := range victims {
err = c.DeletePod(victim)
err = c.DeletePod(ctx, victim)
result = multierror.Append(result, err)
}

return result.ErrorOrNil()
}

// Victims returns up to N pods as configured by MaxKill flag
func (c *Chaoskube) Victims() ([]v1.Pod, error) {
pods, err := c.Candidates()
func (c *Chaoskube) Victims(ctx context.Context) ([]v1.Pod, error) {
pods, err := c.Candidates(ctx)
if err != nil {
return []v1.Pod{}, err
}
Expand All @@ -203,10 +203,10 @@ func (c *Chaoskube) Victims() ([]v1.Pod, error) {

// Candidates returns the list of pods that are available for termination.
// It returns all pods that match the configured label, annotation and namespace selectors.
func (c *Chaoskube) Candidates() ([]v1.Pod, error) {
func (c *Chaoskube) Candidates(ctx context.Context) ([]v1.Pod, error) {
listOptions := metav1.ListOptions{LabelSelector: c.Labels.String()}

podList, err := c.Client.CoreV1().Pods(v1.NamespaceAll).List(context.TODO(), listOptions)
podList, err := c.Client.CoreV1().Pods(v1.NamespaceAll).List(ctx, listOptions)
if err != nil {
return nil, err
}
Expand All @@ -233,7 +233,7 @@ func (c *Chaoskube) Candidates() ([]v1.Pod, error) {

// DeletePod deletes the given pod with the selected terminator.
// It will not delete the pod if dry-run mode is enabled.
func (c *Chaoskube) DeletePod(victim v1.Pod) error {
func (c *Chaoskube) DeletePod(ctx context.Context, victim v1.Pod) error {
c.Logger.WithFields(log.Fields{
"namespace": victim.Namespace,
"name": victim.Name,
Expand All @@ -245,7 +245,7 @@ func (c *Chaoskube) DeletePod(victim v1.Pod) error {
}

start := time.Now()
err := c.Terminator.Terminate(victim)
err := c.Terminator.Terminate(ctx, victim)
metrics.TerminationDurationSeconds.Observe(time.Since(start).Seconds())
if err != nil {
return err
Expand Down
20 changes: 10 additions & 10 deletions chaoskube/chaoskube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (suite *Suite) TestNoVictimReturnsError() {
1,
)

_, err := chaoskube.Victims()
_, err := chaoskube.Victims(context.TODO())
suite.Equal(err, errPodNotFound)
suite.EqualError(err, "pod not found")
}
Expand Down Expand Up @@ -412,7 +412,7 @@ func (suite *Suite) TestDeletePod() {

victim := util.NewPod("default", "foo", v1.PodRunning)

err := chaoskube.DeletePod(victim)
err := chaoskube.DeletePod(context.TODO(), victim)
suite.Require().NoError(err)

suite.AssertLog(logOutput, log.InfoLevel, "terminating pod", log.Fields{"namespace": "default", "name": "foo"})
Expand Down Expand Up @@ -441,7 +441,7 @@ func (suite *Suite) TestDeletePodNotFound() {

victim := util.NewPod("default", "foo", v1.PodRunning)

err := chaoskube.DeletePod(victim)
err := chaoskube.DeletePod(context.TODO(), victim)
suite.EqualError(err, `pods "foo" not found`)
}

Expand Down Expand Up @@ -670,10 +670,10 @@ func (suite *Suite) TestTerminateVictim() {
)
chaoskube.Now = tt.now

err := chaoskube.TerminateVictims()
err := chaoskube.TerminateVictims(context.TODO())
suite.Require().NoError(err)

pods, err := chaoskube.Candidates()
pods, err := chaoskube.Candidates(context.TODO())
suite.Require().NoError(err)

suite.Len(pods, tt.remainingPodCount)
Expand All @@ -699,7 +699,7 @@ func (suite *Suite) TestTerminateNoVictimLogsInfo() {
1,
)

err := chaoskube.TerminateVictims()
err := chaoskube.TerminateVictims(context.TODO())
suite.Require().NoError(err)

suite.AssertLog(logOutput, log.DebugLevel, msgVictimNotFound, log.Fields{})
Expand All @@ -708,14 +708,14 @@ func (suite *Suite) TestTerminateNoVictimLogsInfo() {
// helper functions

func (suite *Suite) assertCandidates(chaoskube *Chaoskube, expected []map[string]string) {
pods, err := chaoskube.Candidates()
pods, err := chaoskube.Candidates(context.TODO())
suite.Require().NoError(err)

suite.AssertPods(pods, expected)
}

func (suite *Suite) assertVictims(chaoskube *Chaoskube, expected []map[string]string) {
victims, err := chaoskube.Victims()
victims, err := chaoskube.Victims(context.TODO())
suite.Require().NoError(err)

for i, victim := range victims {
Expand Down Expand Up @@ -920,7 +920,7 @@ func (suite *Suite) TestMinimumAge() {
suite.Require().NoError(err)
}

pods, err := chaoskube.Candidates()
pods, err := chaoskube.Candidates(context.TODO())
suite.Require().NoError(err)

suite.Len(pods, tt.candidates)
Expand Down Expand Up @@ -999,7 +999,7 @@ func (suite *Suite) TestNotifierCall() {
)

victim := util.NewPod("default", "foo", v1.PodRunning)
err := chaoskube.DeletePod(victim)
err := chaoskube.DeletePod(context.TODO(), victim)

suite.Require().NoError(err)
suite.assertNotified(testNotifier)
Expand Down
4 changes: 2 additions & 2 deletions terminator/delete_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func NewDeletePodTerminator(client kubernetes.Interface, logger log.FieldLogger,
}

// Terminate sends a request to Kubernetes to delete the pod.
func (t *DeletePodTerminator) Terminate(victim v1.Pod) error {
func (t *DeletePodTerminator) Terminate(ctx context.Context, victim v1.Pod) error {
t.logger.WithFields(log.Fields{
"namespace": victim.Namespace,
"name": victim.Name,
}).Debug("calling deletePod endpoint")

return t.client.CoreV1().Pods(victim.Namespace).Delete(context.TODO(), victim.Name, deleteOptions(t.gracePeriod))
return t.client.CoreV1().Pods(victim.Namespace).Delete(ctx, victim.Name, deleteOptions(t.gracePeriod))
}

func deleteOptions(gracePeriod time.Duration) metav1.DeleteOptions {
Expand Down
2 changes: 1 addition & 1 deletion terminator/delete_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (suite *DeletePodTerminatorSuite) TestTerminate() {

victim := util.NewPod("default", "foo", v1.PodRunning)

err := terminator.Terminate(victim)
err := terminator.Terminate(context.TODO(), victim)
suite.Require().NoError(err)

suite.AssertLog(logOutput, log.DebugLevel, "calling deletePod endpoint", log.Fields{"namespace": "default", "name": "foo"})
Expand Down
6 changes: 4 additions & 2 deletions terminator/terminator.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package terminator

import (
"k8s.io/api/core/v1"
"context"

v1 "k8s.io/api/core/v1"
)

// Terminator is the interface for implementations of pod terminators.
type Terminator interface {
// Terminate terminates the given pod.
Terminate(victim v1.Pod) error
Terminate(ctx context.Context, victim v1.Pod) error
}

0 comments on commit 5150e48

Please sign in to comment.