forked from linki/chaoskube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (121 loc) · 3.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"os"
"time"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
"github.com/linki/chaoskube/chaoskube"
"github.com/linki/chaoskube/util"
)
var (
annString string
debug bool
dryRun bool
excludeWeekends bool
inCluster bool
interval time.Duration
kubeconfig string
labelString string
master string
nsString string
percentage float64
runFrom string
runUntil string
version string
)
func init() {
kingpin.Flag("annotations", "A set of annotations to restrict the list of affected pods. Defaults to everything.").StringVar(&annString)
kingpin.Flag("debug", "Enable debug logging.").BoolVar(&debug)
kingpin.Flag("dry-run", "If true, don't actually do anything.").Default("true").BoolVar(&dryRun)
kingpin.Flag("excludeWeekends", "Do not run on weekends").BoolVar(&excludeWeekends)
kingpin.Flag("interval", "Interval between Pod terminations").Default("1m").DurationVar(&interval)
kingpin.Flag("kubeconfig", "Path to a kubeconfig file").StringVar(&kubeconfig)
kingpin.Flag("labels", "A set of labels to restrict the list of affected pods. Defaults to everything.").StringVar(&labelString)
kingpin.Flag("master", "The address of the Kubernetes cluster to target").StringVar(&master)
kingpin.Flag("namespaces", "A set of namespaces to restrict the list of affected pods. Defaults to everything.").StringVar(&nsString)
kingpin.Flag("percentage", "How likely should a pod be killed every single run").Default("0.0").Float64Var(&percentage)
kingpin.Flag("run-from", "Start chaoskube daily at hours:minutes, e.g. 9:00").Default("0:00").StringVar(&runFrom)
kingpin.Flag("run-until", "Stop chaoskube daily at hours:minutes, e.g. 17:00").Default("0:00").StringVar(&runUntil)
}
func main() {
kingpin.Version(version)
kingpin.Parse()
if debug {
log.SetLevel(log.DebugLevel)
}
if dryRun {
log.Infof("Dry run enabled. I won't kill anything. Use --no-dry-run when you're ready.")
}
client, err := newClient()
if err != nil {
log.Fatal(err)
}
labelSelector, err := labels.Parse(labelString)
if err != nil {
log.Fatal(err)
}
annotations, err := labels.Parse(annString)
if err != nil {
log.Fatal(err)
}
namespaces, err := labels.Parse(nsString)
if err != nil {
log.Fatal(err)
}
if !labelSelector.Empty() {
log.Infof("Filtering pods by labels: %s", labelSelector.String())
}
if !annotations.Empty() {
log.Infof("Filtering pods by annotations: %s", annotations.String())
}
if !namespaces.Empty() {
log.Infof("Filtering pods by namespaces: %s", namespaces.String())
}
chaoskube := chaoskube.New(
client,
labelSelector,
annotations,
namespaces,
log.StandardLogger(),
dryRun,
time.Now().UTC().UnixNano(),
)
ticker := time.NewTicker(interval)
for {
select {
case <-ticker.C:
if util.ShouldRunNow(excludeWeekends, runFrom, runUntil) {
candidates, err := chaoskube.Candidates()
if err != nil {
log.Fatal(err)
}
for _, candidate := range candidates {
if util.PodShouldDie(candidate, interval, percentage) {
chaoskube.DeletePod(candidate)
}
}
}
}
}
}
func newClient() (*kubernetes.Clientset, error) {
if kubeconfig == "" {
if _, err := os.Stat(clientcmd.RecommendedHomeFile); err == nil {
kubeconfig = clientcmd.RecommendedHomeFile
}
}
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
return nil, err
}
log.Infof("Targeting cluster at %s", config.Host)
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return client, nil
}