-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
133 lines (103 loc) · 3.25 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/AirHelp/autoscaler/config"
"github.com/AirHelp/autoscaler/k8s"
"github.com/AirHelp/autoscaler/logger"
"github.com/AirHelp/autoscaler/notification"
"github.com/AirHelp/autoscaler/notification/slack"
"github.com/AirHelp/autoscaler/scaler"
flag "github.com/spf13/pflag"
"go.uber.org/zap"
)
const (
configMapName = "autoscaler-config"
)
var cfg config.Config
type ScalerEntity interface {
Start(context.Context)
}
func init() {
cfg = parseStartingFlags()
logLevel := "info"
if cfg.Verbose {
logLevel = "debug"
}
log.InitLogger(cfg.Namespace, cfg.Environment, logLevel)
}
func main() {
if cfg.Version {
fmt.Println(versionString())
os.Exit(0)
}
zap.S().Infof("autoscaler starting, version: %v", strings.TrimSpace(version))
if cfg.Verbose {
zap.S().Info("running in verbose logging mode")
}
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
zap.S().Debug("initializing k8s client and config")
k8sSvc, err := k8s.New(cfg.Namespace)
if err != nil {
zap.S().With("error", err).Error("failed to initialize K8s client")
panic(err)
}
zap.S().Debug("successfully initialized k8s client and config")
configMap, err := k8sSvc.GetConfigMap(ctx, configMapName)
if err != nil {
zap.S().With("error", err).Error("failed to get autoscaler configmap")
panic(err)
}
var notifiers []notification.Notifier
if cfg.SlackWebhookUrl != "" {
zap.S().Debug("initializing Slack client")
notifiers = append(notifiers, slack.NewClient(cfg.SlackWebhookUrl, cfg.SlackChannel, cfg.ClusterName, "autoscaler"))
zap.S().Debug("slack client initialized successfully")
}
zap.S().Debug("initializing scalers on the all enabled deployments")
waitGroup := sync.WaitGroup{}
for deployment, rawYamlConfig := range configMap.Data {
var scalerInstance ScalerEntity
scalerInstance, err := scaler.New(scaler.NewScalerInput{
Ctx: ctx,
DeploymentName: deployment,
RawYamlConfig: rawYamlConfig,
Notifiers: notifiers,
K8sService: k8sSvc,
GlobalConfig: cfg,
})
if err != nil {
zap.S().With("error", err).Errorf("failed to initialize autoscaler for %v: , skipping", deployment)
continue
}
go func() {
scalerInstance.Start(ctx)
waitGroup.Done()
}()
waitGroup.Add(1)
}
zap.S().Debug("initializing scalers on the all enabled deployments")
<-interruptChan
cancel()
waitGroup.Wait()
zap.S().Info("received shutdown, shutting down")
}
func parseStartingFlags() config.Config {
cfg := config.NewWithDefaults()
flag.BoolVarP(&cfg.Verbose, "verbose", "v", false, "Debug mode")
flag.BoolVar(&cfg.Version, "version", false, "Prints version number")
flag.StringVar(&cfg.Environment, "environment", "", "Environment name")
flag.StringVar(&cfg.Namespace, "namespace", "", "Namespace of autoscaler to run within")
flag.StringVar(&cfg.SlackWebhookUrl, "slack_url", "", "Slack Webhook URL to use")
flag.StringVar(&cfg.SlackChannel, "slack_channel", "", "Slack channel to send messages to")
flag.StringVar(&cfg.ClusterName, "cluster_name", "", "Name of cluster")
flag.Parse()
return cfg
}