Skip to content

Commit

Permalink
more time period
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Linkhorst committed Feb 1, 2018
1 parent c4b3d2d commit f7409d8
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 41 deletions.
2 changes: 1 addition & 1 deletion chaoskube/chaoskube.go
Expand Up @@ -52,7 +52,7 @@ var msgVictimNotFound = "No victim could be found. If that's surprising double-c
var msgWeekdayExcluded = "This day of the week is excluded from chaos."

// msgTimePeriodExcluded is the log message when termination is suspended due to the daily time period filter
var msgTimePeriodExcluded = "This time period of the day is excluded from chaos."
var msgTimePeriodExcluded = "This time of day is excluded from chaos."

// New returns a new instance of Chaoskube. It expects a kubernetes client, a
// label, annotation and/or namespace selector to reduce the amount of affected
Expand Down
41 changes: 5 additions & 36 deletions main.go
Expand Up @@ -2,10 +2,8 @@ package main

import (
"os"
"strings"
"time"

"github.com/davecgh/go-spew/spew"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"

Expand Down Expand Up @@ -104,20 +102,21 @@ func main() {
log.Infof("Excluding weekdays: %s", parsedWeekdays)
}

parsedHours := parseHours(excludedHours)
parsedHours, err := util.ParseHours(excludedHours)
if err != nil {
log.Fatal(err)
}
if len(parsedHours) > 0 {
log.Infof("Excluding hours: %s", parsedHours)
}

spew.Dump(parsedHours)

chaoskube := chaoskube.New(
client,
labelSelector,
annotations,
namespaces,
parsedWeekdays,
[]chaoskube.TimePeriod{},
parsedHours,
parsedTimezone,
log.StandardLogger(),
dryRun,
Expand All @@ -134,36 +133,6 @@ func main() {
}
}

func parseHours(hours string) []chaoskube.TimePeriod {
parsedHours := []chaoskube.TimePeriod{}
for _, tp := range strings.Split(hours, ",") {
t := strings.Split(tp, "-")
if len(t) != 2 {
log.Fatal("wrong format")
}

t1, err := time.ParseInLocation(time.Kitchen, t[0], time.Local)
if err != nil {
log.Fatal(err)
}

t2, err := time.ParseInLocation(time.Kitchen, t[1], time.Local)
if err != nil {
log.Fatal(err)
}

year, month, day := time.Now().Date()
t1 = t1.AddDate(year, int(month), day).Local()
t2 = t2.AddDate(year, int(month), day).Local()

parsedHours = append(parsedHours, chaoskube.TimePeriod{
From: t1,
To: t2,
})
}
return parsedHours
}

func newClient() (*kubernetes.Clientset, error) {
if kubeconfig == "" {
if _, err := os.Stat(clientcmd.RecommendedHomeFile); err == nil {
Expand Down
32 changes: 31 additions & 1 deletion util/util.go
@@ -1,6 +1,7 @@
package util

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -30,8 +31,12 @@ func (tp TimePeriod) Includes(pointInTime time.Time) bool {
return TimeOfDay(pointInTime).Equal(tp.From)
}

func (tp TimePeriod) String() string {
return fmt.Sprintf("%s-%s", tp.From.Format(time.Kitchen), tp.To.Format(time.Kitchen))
}

func TimeOfDay(pointInTime time.Time) time.Time {
return time.Date(0, 0, 0, pointInTime.Hour(), pointInTime.Minute(), pointInTime.Second(), pointInTime.Nanosecond(), pointInTime.Location())
return time.Date(0, 0, 0, pointInTime.Hour(), pointInTime.Minute(), pointInTime.Second(), pointInTime.Nanosecond(), time.UTC)
}

// NewPod returns a new pod instance for testing purposes.
Expand Down Expand Up @@ -71,3 +76,28 @@ func ParseWeekdays(weekdays string) []time.Weekday {
}
return parsedWeekdays
}

func ParseHours(hours string) ([]TimePeriod, error) {
parsedHours := []TimePeriod{}

for _, tp := range strings.Split(hours, ",") {
parts := strings.Split(tp, "-")
if len(parts) != 2 {
continue
}

begin, err := time.ParseInLocation(time.Kitchen, strings.TrimSpace(parts[0]), time.Local)
if err != nil {
return nil, err
}

end, err := time.ParseInLocation(time.Kitchen, strings.TrimSpace(parts[1]), time.Local)
if err != nil {
return nil, err
}

parsedHours = append(parsedHours, NewTimePeriod(begin, end))
}

return parsedHours, nil
}
104 changes: 101 additions & 3 deletions util/util_test.go
@@ -1,6 +1,7 @@
package util

import (
"fmt"
"testing"
"time"
)
Expand Down Expand Up @@ -39,8 +40,8 @@ func TestNewTimePeriod(t *testing.T) {
time.Date(1869, 9, 24, 15, 04, 05, 06, timezone),
time.Date(1869, 9, 24, 16, 04, 05, 06, timezone),
TimePeriod{
From: time.Date(0, 0, 0, 15, 04, 05, 06, timezone),
To: time.Date(0, 0, 0, 16, 04, 05, 06, timezone),
From: time.Date(0, 0, 0, 15, 04, 05, 06, time.UTC),
To: time.Date(0, 0, 0, 16, 04, 05, 06, time.UTC),
},
},
} {
Expand Down Expand Up @@ -150,6 +151,40 @@ func TestTimePeriodIncludes(t *testing.T) {
}
}

func TestTimePeriodString(t *testing.T) {
for _, tc := range []struct {
given TimePeriod
expected string
}{
// empty time period
{
TimePeriod{},
"12:00AM-12:00AM",
},
// simple time period
{
TimePeriod{
From: time.Date(0, 0, 0, 8, 0, 0, 0, time.UTC),
To: time.Date(0, 0, 0, 16, 0, 0, 0, time.UTC),
},
"8:00AM-4:00PM",
},
// time period across days
{
TimePeriod{
From: time.Date(0, 0, 0, 16, 0, 0, 0, time.UTC),
To: time.Date(0, 0, 0, 8, 0, 0, 0, time.UTC),
},
"4:00PM-8:00AM",
},
} {
got := fmt.Sprintf("%s", tc.given)
if tc.expected != got {
t.Fatalf("expected %s, got %s", tc.expected, got)
}
}
}

func TestTimeOfDay(t *testing.T) {
timezone, err := time.LoadLocation("Australia/Brisbane")
if err != nil {
Expand All @@ -166,7 +201,7 @@ func TestTimeOfDay(t *testing.T) {
},
{
time.Date(1869, 9, 24, 15, 04, 05, 06, timezone),
time.Date(0, 0, 0, 15, 04, 05, 06, timezone),
time.Date(0, 0, 0, 15, 04, 05, 06, time.UTC),
},
} {
got := TimeOfDay(tc.pointInTime)
Expand Down Expand Up @@ -230,3 +265,66 @@ func TestParseWeekdays(t *testing.T) {
}
}
}

func TestParseHours(t *testing.T) {
for _, tc := range []struct {
given string
expected []TimePeriod
}{
// empty time period string
{
"",
[]TimePeriod{},
},
// single range string
{
"08:00AM-04:00PM",
[]TimePeriod{
{
From: time.Date(0, 0, 0, 8, 0, 0, 0, time.UTC),
To: time.Date(0, 0, 0, 16, 0, 0, 0, time.UTC),
},
},
},
// multiple ranges string
{
"08:00AM-04:00PM,08:00PM-10:00PM",
[]TimePeriod{
{
From: time.Date(0, 0, 0, 8, 0, 0, 0, time.UTC),
To: time.Date(0, 0, 0, 16, 0, 0, 0, time.UTC),
},
{
From: time.Date(0, 0, 0, 20, 0, 0, 0, time.UTC),
To: time.Date(0, 0, 0, 22, 0, 0, 0, time.UTC),
},
},
},
// string containing whitespace
{
" 08:00AM - 04:00PM ,, , 08:00PM - 10:00PM ",
[]TimePeriod{
{
From: time.Date(0, 0, 0, 8, 0, 0, 0, time.UTC),
To: time.Date(0, 0, 0, 16, 0, 0, 0, time.UTC),
},
{
From: time.Date(0, 0, 0, 20, 0, 0, 0, time.UTC),
To: time.Date(0, 0, 0, 22, 0, 0, 0, time.UTC),
},
},
},
} {
got, _ := ParseHours(tc.given)

if len(tc.expected) != len(got) {
t.Fatalf("expected %d, got %d", len(tc.expected), len(got))
}

for i := range tc.expected {
if tc.expected[i] != got[i] {
t.Errorf("expected %v, got %v", tc.expected[i], got[i])
}
}
}
}

0 comments on commit f7409d8

Please sign in to comment.