Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle signals for graceful shutdown #95

Merged
merged 4 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions chaoskube/chaoskube.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chaoskube

import (
"context"
"errors"
"fmt"
"math/rand"
Expand Down Expand Up @@ -82,6 +83,23 @@ func New(client kubernetes.Interface, labels, annotations, namespaces labels.Sel
}
}

// Run continuously picks and terminates a victim pod at a given interval
// 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.TerminateVictim(); err != nil {
c.Logger.WithField("err", err).Error("failed to terminate victim")
}

c.Logger.Debug("sleeping...")
select {
case <-next:
case <-ctx.Done():
return
}
}
}

// TerminateVictim picks and deletes a victim.
// It respects the configured excluded weekdays, times of day and days of a year filters.
func (c *Chaoskube) TerminateVictim() error {
Expand Down
21 changes: 21 additions & 0 deletions chaoskube/chaoskube_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chaoskube

import (
"context"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -72,6 +73,26 @@ func (suite *Suite) TestNew() {
suite.Equal(false, chaoskube.DryRun)
}

// TestRunContextCanceled tests that a canceled context will exit the Run function.
func (suite *Suite) TestRunContextCanceled() {
chaoskube := suite.setup(
labels.Everything(),
labels.Everything(),
labels.Everything(),
[]time.Weekday{},
[]util.TimePeriod{},
[]time.Time{},
time.UTC,
time.Duration(0),
false,
)

ctx, cancel := context.WithCancel(context.Background())
cancel()

chaoskube.Run(ctx, nil)
}

func (suite *Suite) TestCandidates() {
foo := map[string]string{"namespace": "default", "name": "foo"}
bar := map[string]string{"namespace": "testing", "name": "bar"}
Expand Down
24 changes: 17 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package main

import (
"context"
"fmt"
"math/rand"
"net/http"
"os"
"os/signal"
"syscall"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -171,14 +174,21 @@ func main() {
}()
}

for {
if err := chaoskube.TerminateVictim(); err != nil {
log.WithField("err", err).Error("failed to terminate victim")
}
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)

log.WithField("duration", interval).Debug("sleeping")
time.Sleep(interval)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
<-done
cancel()
}()

ticker := time.NewTicker(interval)
defer ticker.Stop()

chaoskube.Run(ctx, ticker.C)
}

func newClient() (*kubernetes.Clientset, error) {
Expand Down