-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
96 lines (89 loc) · 2.71 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"time"
"github.com/maxlaverse/soft-pod-memory-evicter/pkg"
"github.com/urfave/cli/v2"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"
)
func main() {
opts := pkg.Options{
DryRun: false,
PodSelector: pkg.SelectorFlag{Selector: labels.Everything()},
EvictionPause: time.Duration(5) * time.Minute,
MemoryUsageCheckInterval: time.Duration(3) * time.Minute,
MemoryUsageThreshold: 95,
ChannelQueueSize: 100,
IgnoredNamespaces: *cli.NewStringSlice(),
}
app := &cli.App{
Name: "soft-pod-memory-evicter",
Usage: "Gracefully evict Pods before they get OOM killed",
Before: func(c *cli.Context) error {
fs := flag.NewFlagSet("", flag.PanicOnError)
klog.InitFlags(fs)
return fs.Set("v", strconv.Itoa(c.Int("loglevel")))
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dry-run",
Usage: "Output additional debug lines",
Value: opts.DryRun,
Destination: &opts.DryRun,
}, &cli.GenericFlag{
Name: "pod-selector",
Usage: "Evict only Pods matching this label selector",
Value: &opts.PodSelector,
Destination: &opts.PodSelector,
}, &cli.DurationFlag{
Name: "eviction-pause",
Usage: "Pause duration between evictions",
Value: opts.EvictionPause,
Destination: &opts.EvictionPause,
}, &cli.DurationFlag{
Name: "memory-usage-check-interval",
Usage: "Interval at which the Pod metrics are checked",
Value: opts.MemoryUsageCheckInterval,
Destination: &opts.MemoryUsageCheckInterval,
}, &cli.IntFlag{
Name: "memory-usage-threshold",
Usage: "Memory usage eviction threshold (0-100)",
Value: opts.MemoryUsageThreshold,
Destination: &opts.MemoryUsageThreshold,
}, &cli.IntFlag{
Name: "channel-queue-size",
Usage: "Size of the queue for pod eviction",
Value: opts.ChannelQueueSize,
Destination: &opts.ChannelQueueSize,
}, &cli.StringSliceFlag{
Name: "ignore-namespace",
Usage: "Do not evict Pods from this namespace. Can be used multiple times",
Value: &opts.IgnoredNamespaces,
Destination: &opts.IgnoredNamespaces,
},
&cli.IntFlag{
Name: "loglevel",
Aliases: []string{"v"},
Usage: "Log Level",
Value: 0,
},
},
Action: func(c *cli.Context) error {
defer klog.Flush()
ctx, cancel := signal.NotifyContext(c.Context, os.Interrupt)
defer cancel()
controller := pkg.NewController(opts)
return controller.Run(ctx)
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}