Skip to content

Commit c19db05

Browse files
author
Felix Glaser
committed
Adds percentage
1 parent 9420025 commit c19db05

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
labelString string
2828
master string
2929
nsString string
30+
percentage float64
3031
runFrom string
3132
runUntil string
3233
version string
@@ -37,11 +38,12 @@ func init() {
3738
kingpin.Flag("debug", "Enable debug logging.").BoolVar(&debug)
3839
kingpin.Flag("dry-run", "If true, don't actually do anything.").Default("true").BoolVar(&dryRun)
3940
kingpin.Flag("excludeWeekends", "Do not run on weekends").BoolVar(&excludeWeekends)
40-
kingpin.Flag("interval", "Interval between Pod terminations").DurationVar(&interval)
41+
kingpin.Flag("interval", "Interval between Pod terminations").Default("1m").DurationVar(&interval)
4142
kingpin.Flag("kubeconfig", "Path to a kubeconfig file").StringVar(&kubeconfig)
4243
kingpin.Flag("labels", "A set of labels to restrict the list of affected pods. Defaults to everything.").StringVar(&labelString)
4344
kingpin.Flag("master", "The address of the Kubernetes cluster to target").StringVar(&master)
4445
kingpin.Flag("namespaces", "A set of namespaces to restrict the list of affected pods. Defaults to everything.").StringVar(&nsString)
46+
kingpin.Flag("percentage", "How likely should a pod be killed every single run").Default("0.0").Float64Var(&percentage)
4547
kingpin.Flag("run-from", "Start chaoskube daily at hours:minutes, e.g. 9:00").Default("0:00").StringVar(&runFrom)
4648
kingpin.Flag("run-until", "Stop chaoskube daily at hours:minutes, e.g. 17:00").Default("0:00").StringVar(&runUntil)
4749
}
@@ -110,7 +112,7 @@ func main() {
110112
log.Fatal(err)
111113
}
112114
for _, candidate := range candidates {
113-
if util.PodShouldDie(candidate, interval) {
115+
if util.PodShouldDie(candidate, interval, percentage) {
114116
chaoskube.DeletePod(candidate)
115117
}
116118
}

util/util.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package util
22

33
import (
4-
"fmt"
54
"math/rand"
65
"strconv"
76
"strings"
@@ -14,6 +13,10 @@ import (
1413

1514
var timeNow = timeNowFunc
1615

16+
func init() {
17+
rand.Seed(timeNow().Unix())
18+
}
19+
1720
func timeNowFunc() time.Time {
1821
return time.Now()
1922
}
@@ -91,7 +94,7 @@ func ShouldRunNow(excludeWeekends bool, runFrom string, runUntil string) bool {
9194
}
9295

9396
func parseLabel(label string) (rate int, span int, err error) {
94-
split := strings.Split(label, "/")
97+
split := strings.Split(label, ".")
9598
if len(split) != 2 {
9699
return 0, 0, err
97100
}
@@ -111,19 +114,21 @@ func parseLabel(label string) (rate int, span int, err error) {
111114
return
112115
}
113116

114-
func getOdds(p v1.Pod, interval time.Duration) float64 {
117+
func getOdds(p v1.Pod, interval time.Duration, percentage float64) float64 {
115118
labels := p.GetLabels()
119+
if labels["chaos.schedule"] == "" {
120+
return percentage
121+
}
116122
rate, span, err := parseLabel(labels["chaos.schedule"])
117123
if err != nil {
124+
log.Errorf("Error: %v from parsing %v's chaos.schedule, which is %s", err, p.Name, labels["chaos.schedule"])
118125
return 0.0
119126
}
120-
// make intervall play well with the other ints
121-
fmt.Println(rate, interval, span)
122127
return (float64(rate) * interval.Minutes()) / float64(span)
123128
}
124129

125-
func PodShouldDie(p v1.Pod, interval time.Duration) bool {
126-
rand.Seed(timeNow().Unix())
127-
odds := getOdds(p, interval)
128-
return (rand.Float64() <= odds)
130+
func PodShouldDie(p v1.Pod, interval time.Duration, percentage float64) bool {
131+
odds := getOdds(p, interval, percentage)
132+
random := rand.Float64()
133+
return (random <= odds)
129134
}

util/util_test.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ func TestShouldRunNow(t *testing.T) {
8484

8585
func TestParseLabel(t *testing.T) {
8686
labels := map[string]map[string]int{
87-
"1/hour": {"rate": 1, "span": 60},
88-
"2/day": {"rate": 2, "span": 1440},
89-
"3/week": {"rate": 3, "span": 10080},
87+
"1.hour": {"rate": 1, "span": 60},
88+
"2.day": {"rate": 2, "span": 1440},
89+
"3.week": {"rate": 3, "span": 10080},
9090
}
9191
for k, v := range labels {
9292
rate, span, err := parseLabel(k)
@@ -103,17 +103,26 @@ func TestParseLabel(t *testing.T) {
103103
}
104104

105105
func TestGetOdds(t *testing.T) {
106-
schedules := map[string]float64{"1/hour": 1.0 / float64(60), "2/day": 2.0 / float64(60*24), "3/week": 3.0 / float64(60*24*7)}
106+
schedules := map[string]float64{"1.hour": 1.0 / float64(60), "2.day": 2.0 / float64(60*24), "3.week": 3.0 / float64(60*24*7)}
107+
percentage := 0.5
107108
for schedule, initial_odd := range schedules {
108109
p := NewPod("default", "foo", schedule)
109110
intervalls := []time.Duration{time.Minute * 1, time.Minute * 5, time.Minute * 10, time.Minute * 60}
110111
for _, interval := range intervalls {
111-
odd := getOdds(p, interval)
112+
odd := getOdds(p, interval, 0.5)
112113
target_odd := int(initial_odd * interval.Minutes() * 100)
113114
conv_odd := int(odd * 100)
114115
if conv_odd != target_odd {
115-
t.Fatalf("getOdds returned wrong odd want: %v got: %v, schedule: %v, interval: %v", target_odd, odd, schedule, interval)
116+
t.Fatalf("getOdds returned wrong odd want: %v got: %v, schedule: %v, interval: %v, percentage: %v",
117+
target_odd, odd, schedule, interval, percentage)
116118
}
117119
}
118120
}
121+
p := NewPod("default", "foo")
122+
interval := 10 * time.Minute
123+
odd := getOdds(p, interval, 0.5)
124+
if odd != 0.5 {
125+
t.Fatalf("getOdds returned wrong odd want: %v got: %v, schedule: %v, interval: %v, percentage: %v",
126+
percentage, odd, "", interval, percentage)
127+
}
119128
}

0 commit comments

Comments
 (0)