forked from AirHelp-OSP/autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (103 loc) · 3.33 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
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/notification"
"github.com/AirHelp/autoscaler/notification/slack"
"github.com/AirHelp/autoscaler/scaler"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
)
const configMapName = "autoscaler-config"
type ScalerEntity interface {
Start(context.Context)
}
func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
}
func main() {
cfg := parseStartingFlags()
if cfg.Version {
fmt.Println(versionString())
os.Exit(0)
}
log.Infof("Autoscaler starting, version: %v", strings.TrimSpace(version))
if cfg.Verbose {
log.Info("Starting autoscaler with verbose logging mode")
log.SetLevel(log.DebugLevel)
}
logger := log.WithFields(log.Fields{
"namespace": cfg.Namespace,
"environment": cfg.Environment,
})
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
logger.Debug("Initializing k8s client and config")
k8sSvc, err := k8s.New(cfg.Namespace, logger)
if err != nil {
logger.WithError(err).Error("Failed to initialize k8s client")
panic(err)
}
logger.Debug("Successfully initialized k8s client and config")
configMap, err := k8sSvc.GetConfigMap(ctx, configMapName)
if err != nil {
logger.WithError(err).Error("Error getting autoscaler configmap")
panic(err)
}
var notifiers []notification.Notifier
if cfg.SlackWebhookUrl != "" {
logger.Debug("Initializing Slack client")
notifiers = append(notifiers, slack.NewClient(cfg.SlackWebhookUrl, cfg.SlackChannel, cfg.ClusterName, "autoscaler"))
logger.Debug("Done initializing Slack client")
}
logger.Debug("Initializing scalers on 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,
Logger: logger,
})
if err != nil {
logger.WithError(err).Error(fmt.Sprintf("Failed to initialize autoscaler for %v, skipping.", deployment))
continue
}
go func() {
scalerInstance.Start(ctx)
waitGroup.Done()
}()
waitGroup.Add(1)
}
logger.Debug("Done initializing scalers on all enabled deployments")
<-interruptChan
cancel()
waitGroup.Wait()
logger.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
}