-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
174 lines (145 loc) · 4.49 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
package main
import (
"context"
"fmt"
"io/ioutil"
"kubethanos/kubethanos"
"kubethanos/thanos"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/alecthomas/kingpin"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
log "github.com/sirupsen/logrus"
)
var (
namespaces string
includedPodNames string
includedNodeNames string
excludedPodNames string
master string
healthCheckAddr string
kubeconfig string
interval time.Duration
ratioToKill float64
dryRun bool
debug bool
)
func init() {
klog.SetOutput(ioutil.Discard)
kingpin.Flag("namespaces", "A namespace or a set of namespaces to restrict thanoskube").StringVar(&namespaces)
kingpin.Flag("included-pod-names", "A string to select which pods to kill").StringVar(&includedPodNames)
kingpin.Flag("node-names", "A string to select nodes, pods within the selected nodes will be killed").StringVar(&includedNodeNames)
kingpin.Flag("excluded-pod-names", "A string to exclude pods to kill").StringVar(&excludedPodNames)
kingpin.Flag("master", "The address of the Kubernetes cluster to target, if none looks under $HOME/.kube").StringVar(&master)
kingpin.Flag("healthcheck", "Listens this endpoint for healtcheck").Default(":8080").StringVar(&healthCheckAddr)
kingpin.Flag("kubeconfig", "Path to a kubeconfig file").StringVar(&kubeconfig)
kingpin.Flag("interval", "Interval between killing pods").Default("10m").DurationVar(&interval)
kingpin.Flag("ratio", "Ratio of pods to kill").Default("0.5").FloatVar(&ratioToKill)
kingpin.Flag("dry-run", "If true, print out the pod names without actually killing them.").Default("false").BoolVar(&dryRun)
kingpin.Flag("debug", "Enable debug logging.").BoolVar(&debug)
}
func main() {
kingpin.Parse()
if debug {
log.SetLevel(log.DebugLevel)
}
log.WithFields(log.Fields{
"namespaces": namespaces,
"includedPodNames": includedPodNames,
"nodeNames": includedNodeNames,
"excludedPodNames": excludedPodNames,
"master": master,
"kubeconfig": kubeconfig,
"interval": interval,
"ratioToKill": ratioToKill,
"dryRun": dryRun,
"debug": debug,
}).Info("started reading config")
client, err := newClient()
if err != nil {
log.WithField("err", err).Fatal("failed to connect to cluster.. exiting")
}
var namespaces = parseNamespaces(namespaces)
log.WithFields(log.Fields{
"namespaces": namespaces,
"includedPodNames": includedPodNames,
"nodeNames": includedNodeNames,
"excludedPodNames": excludedPodNames,
}).Info("setting pod filter")
kubeThanos := kubethanos.New(
client,
namespaces,
includedPodNames,
includedNodeNames,
excludedPodNames,
ratioToKill,
dryRun,
thanos.NewThanos(client, log.StandardLogger()),
)
go startHealthCheck(healthCheckAddr)
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()
kubeThanos.Run(ctx, ticker.C)
}
func newClient() (*kubernetes.Clientset, error) {
// look for kubeconfig in home if not set
if kubeconfig == "" {
if _, err := os.Stat(clientcmd.RecommendedHomeFile); err == nil {
kubeconfig = clientcmd.RecommendedHomeFile
}
}
log.WithFields(log.Fields{
"kubeconfig": kubeconfig,
"master": master,
}).Info("found config with parameters: ")
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
return nil, err
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
serverVersion, err := client.Discovery().ServerVersion()
if err != nil {
return nil, err
}
log.WithFields(log.Fields{
"master": config.Host,
"serverVersion": serverVersion,
}).Info("connected to cluster")
return client, nil
}
func parseNamespaces(str string) labels.Selector {
selector, err := labels.Parse(str)
if err != nil {
log.WithFields(log.Fields{
"selector": str,
"err": err,
}).Fatal("failed to parse namespaces")
}
return selector
}
func startHealthCheck(healthCheckAddress string) {
http.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintln(w, "OK")
})
if err := http.ListenAndServe(healthCheckAddress, nil); err != nil {
log.WithField("err", err).Fatal("failed to start health check endpoint")
}
}