forked from linki/chaoskube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
288 lines (250 loc) · 8.52 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"context"
"fmt"
"math/rand"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
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"
restclient "k8s.io/client-go/rest"
"github.com/linki/chaoskube/chaoskube"
"github.com/linki/chaoskube/util"
"strings"
)
var (
version = "undefined"
)
var (
labelString string
annString string
nsString string
excludedWeekdays string
excludedTimesOfDay string
excludedDaysOfYear string
timezone string
minimumAge time.Duration
master string
kubeconfig string
interval time.Duration
dryRun bool
debug bool
metricsAddress string
exec string
execContainer string
logFormat string
logFields string
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
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("excluded-weekdays", "A list of weekdays when termination is suspended, e.g. Sat,Sun").StringVar(&excludedWeekdays)
kingpin.Flag("excluded-times-of-day", "A list of time periods of a day when termination is suspended, e.g. 22:00-08:00").StringVar(&excludedTimesOfDay)
kingpin.Flag("excluded-days-of-year", "A list of days of a year when termination is suspended, e.g. Apr1,Dec24").StringVar(&excludedDaysOfYear)
kingpin.Flag("timezone", "The timezone by which to interpret the excluded weekdays and times of day, e.g. UTC, Local, Europe/Berlin. Defaults to UTC.").Default("UTC").StringVar(&timezone)
kingpin.Flag("minimum-age", "Minimum age of pods to consider for termination").Default("0s").DurationVar(&minimumAge)
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("exec", "Execute the given terminal command on victim pods, rather than deleting pods, eg killall -9 bash").StringVar(&exec)
kingpin.Flag("exec-container", "Name of container to run --exec command in, defaults to first container in spec").Default("").StringVar(&execContainer)
kingpin.Flag("debug", "Enable debug logging.").BoolVar(&debug)
kingpin.Flag("log-format", "'plain' or 'json'").Default("plain").StringVar(&logFormat)
kingpin.Flag("log-fields", "key=value, comma separated list of fields to include in every log message").Default("").StringVar(&logFields)
kingpin.Flag("metrics-address", "Listening address for metrics handler").Default(":8080").StringVar(&metricsAddress)
}
func main() {
kingpin.Version(version)
kingpin.Parse()
logger := chaoskube.SetupLogging(debug, logFormat, logFields)
logger.WithFields(log.Fields{
"labels": labelString,
"annotations": annString,
"namespaces": nsString,
"excludedWeekdays": excludedWeekdays,
"excludedTimesOfDay": excludedTimesOfDay,
"excludedDaysOfYear": excludedDaysOfYear,
"timezone": timezone,
"minimumAge": minimumAge,
"master": master,
"kubeconfig": kubeconfig,
"interval": interval,
"dryRun": dryRun,
"exec": exec,
"execContainer": execContainer,
"debug": debug,
"metricsAddress": metricsAddress,
}).Info("reading config")
logger.WithFields(log.Fields{
"version": version,
"dryRun": dryRun,
"interval": interval,
}).Info("starting up")
config, err := newConfig(logger)
if err != nil {
logger.WithField("err", err).Fatal("failed to determine k8s client config")
}
client, err := newClient(config, logger)
if err != nil {
logger.WithField("err", err).Fatal("failed to connect to cluster")
}
var (
labelSelector = parseSelector(labelString, logger)
annotations = parseSelector(annString, logger)
namespaces = parseSelector(nsString, logger)
)
logger.WithFields(log.Fields{
"labels": labelSelector,
"annotations": annotations,
"namespaces": namespaces,
"minimumAge": minimumAge,
}).Info("setting pod filter")
parsedWeekdays := util.ParseWeekdays(excludedWeekdays)
parsedTimesOfDay, err := util.ParseTimePeriods(excludedTimesOfDay)
if err != nil {
logger.WithFields(log.Fields{
"timesOfDay": excludedTimesOfDay,
"err": err,
}).Fatal("failed to parse times of day")
}
parsedDaysOfYear, err := util.ParseDays(excludedDaysOfYear)
if err != nil {
logger.WithFields(log.Fields{
"daysOfYear": excludedDaysOfYear,
"err": err,
}).Fatal("failed to parse days of year")
}
logger.WithFields(log.Fields{
"weekdays": parsedWeekdays,
"timesOfDay": parsedTimesOfDay,
"daysOfYear": formatDays(parsedDaysOfYear),
}).Info("setting quiet times")
parsedTimezone, err := time.LoadLocation(timezone)
if err != nil {
logger.WithFields(log.Fields{
"timeZone": timezone,
"err": err,
}).Fatal("failed to detect time zone")
}
timezoneName, offset := time.Now().In(parsedTimezone).Zone()
logger.WithFields(log.Fields{
"name": timezoneName,
"location": parsedTimezone,
"offset": offset / int(time.Hour/time.Second),
}).Info("setting timezone")
var action chaoskube.ChaosAction
if dryRun {
action = chaoskube.NewDryRunAction()
} else if len(exec) > 0 {
action = chaoskube.NewExecAction(client.CoreV1().RESTClient(), config, execContainer, strings.Split(exec, " "))
} else {
action = chaoskube.NewDeletePodAction(client)
}
chaoskube := chaoskube.New(
client,
labelSelector,
annotations,
namespaces,
parsedWeekdays,
parsedTimesOfDay,
parsedDaysOfYear,
parsedTimezone,
minimumAge,
logger,
action,
)
if metricsAddress != "" {
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/healthz",
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "OK")
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>chaoskube</title></head>
<body>
<h1>chaoskube</h1>
<p><a href="/metrics">Metrics</a></p>
<p><a href="/healthz">Health Check</a></p>
</body>
</html>`))
})
go func() {
if err := http.ListenAndServe(metricsAddress, nil); err != nil {
logger.WithFields(log.Fields{
"err": err,
}).Fatal("failed to start HTTP server")
}
}()
}
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
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 newConfig(logger log.FieldLogger) (*restclient.Config, error) {
if kubeconfig == "" {
if _, err := os.Stat(clientcmd.RecommendedHomeFile); err == nil {
kubeconfig = clientcmd.RecommendedHomeFile
}
}
logger.WithFields(log.Fields{
"kubeconfig": kubeconfig,
"master": master,
}).Debug("using cluster config")
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
return nil, err
}
return config, nil
}
func newClient(config *restclient.Config, logger log.FieldLogger) (*kubernetes.Clientset, error) {
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
serverVersion, err := client.Discovery().ServerVersion()
if err != nil {
return nil, err
}
logger.WithFields(log.Fields{
"master": config.Host,
"serverVersion": serverVersion,
}).Info("connected to cluster")
return client, nil
}
func parseSelector(str string, logger log.FieldLogger) labels.Selector {
selector, err := labels.Parse(str)
if err != nil {
logger.WithFields(log.Fields{
"selector": str,
"err": err,
}).Fatal("failed to parse selector")
}
return selector
}
func formatDays(days []time.Time) []string {
formattedDays := make([]string, 0, len(days))
for _, d := range days {
formattedDays = append(formattedDays, d.Format(util.YearDay))
}
return formattedDays
}