-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
65 lines (52 loc) · 1.35 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
package main
import (
"context"
"os"
"time"
"github.com/aws/aws-sdk-go/aws/session"
log "github.com/sirupsen/logrus"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"k8s.io/client-go/kubernetes"
)
type options struct {
kubeconfig string
olderThan time.Duration
debug bool
}
func main() {
ctx := context.Background()
opts := &options{}
kingpin.Flag("kubeconfig", "Path to kubeconfig.").StringVar(&opts.kubeconfig)
kingpin.Flag("older-than", "age of nodes to terminate").Required().DurationVar(&opts.olderThan)
kingpin.Flag("debug", "Debug mode").BoolVar(&opts.debug)
kingpin.Parse()
log.SetOutput(os.Stderr)
if opts.debug {
log.SetLevel(log.DebugLevel)
log.Debugln("Debug logging enabled")
}
config, err := createClientConfig(opts.kubeconfig)
if err != nil {
log.Fatalf("error creating kube config: %s", err)
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("error creating kube client: %s", err)
}
node, err := getNode(ctx, client, opts.olderThan)
if err != nil {
log.Fatalf("failed to get node: %s", err)
}
if node == "" {
log.Info("no nodes to terminate, shutting down")
os.Exit(0)
}
session, err := session.NewSession()
if err != nil {
log.Fatalf("failed to create AWS session: %s", err)
}
err = terminateNode(node, session)
if err != nil {
log.Fatalf("error terminating node: %s", err)
}
}