-
Notifications
You must be signed in to change notification settings - Fork 120
/
main.go
126 lines (102 loc) · 2.9 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
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"
)
var (
labelString string
annString string
nsString string
master string
kubeconfig string
interval time.Duration
inCluster bool
dryRun bool
debug bool
version string
)
func init() {
kingpin.Flag("labels", "A set of labels to restrict the list of affected pods. Defaults to everything.").StringVar(&labelString)
kingpin.Flag("annotations", "A set of annotations to restrict the list of affected pods. Defaults to everything.").StringVar(&annString)
kingpin.Flag("namespaces", "A set of namespaces to restrict the list of affected pods. Defaults to everything.").StringVar(&nsString)
kingpin.Flag("master", "The address of the Kubernetes cluster to target").StringVar(&master)
kingpin.Flag("kubeconfig", "Path to a kubeconfig file").StringVar(&kubeconfig)
kingpin.Flag("interval", "Interval between Pod terminations").Default("10m").DurationVar(&interval)
kingpin.Flag("dry-run", "If true, don't actually do anything.").Default("true").BoolVar(&dryRun)
kingpin.Flag("debug", "Enable debug logging.").BoolVar(&debug)
}
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(),
)
for {
if err := chaoskube.TerminateVictim(); err != nil {
log.Fatal(err)
}
log.Debugf("Sleeping for %s...", interval)
time.Sleep(interval)
}
}
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
}