-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
223 lines (173 loc) · 7.34 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"statusbay/api"
"statusbay/api/alerts"
apiKubernetes "statusbay/api/kubernetes"
"statusbay/api/metrics"
"statusbay/cache"
"statusbay/config"
"statusbay/serverutil"
"statusbay/state"
"statusbay/version"
"statusbay/visibility"
kuberneteswatcher "statusbay/watcher/kubernetes"
"statusbay/watcher/kubernetes/client"
"time"
"github.com/sethvargo/go-envconfig"
log "github.com/sirupsen/logrus"
)
const (
// DefaultGracefulShutDown is the default graceful shot down the server
DefaultGracefulShutDown = time.Second * 10
// DefaultConfigPath is the default configuration file path
DefaultConfigPath = "/etc/statusbay/config.yaml"
// DefaultEventsPath is the default events configuration file path
DefaultEventsPath = "/etc/statusbay/events.yaml"
//ModeAPI will upload a server for client Website UI
ModeAPI = "api"
//KubernetesWatcher start watch on kubernetes deployments
KubernetesWatcher = "kubernetes"
)
func main() {
var configPath, eventsPath, mode string
// parsing flags
flag.StringVar(&mode, "mode", "", fmt.Sprintf("Server mode to start. Must be either \"%s\" or \"%s\".", ModeAPI, KubernetesWatcher))
flag.StringVar(&configPath, "config", DefaultConfigPath, "Path to configuration file")
flag.StringVar(&eventsPath, "events", DefaultEventsPath, "Path to events configuration file")
// Only for kubernetes
var kubeconfig, apiserverHost string
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to kubeconfig file with authorization and master location information.")
flag.StringVar(&apiserverHost, "apiserverhost", "", "Path to kubeconfig file with authorization and master location information.")
flag.Parse()
ctx, cancelFn := context.WithCancel(context.Background())
var runner *serverutil.Runner
switch mode {
case ModeAPI:
runner = startAPIServer(ctx, configPath, eventsPath)
case KubernetesWatcher:
runner = startKubernetesWatcher(ctx, configPath, kubeconfig, apiserverHost)
default:
flag.Usage()
os.Exit(1)
}
//register signal handler
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop // block until we are requested to stop
runner.StopFunc(cancelFn)
}
func startKubernetesWatcher(ctx context.Context, configPath, kubeconfig, apiserverHost string) *serverutil.Runner {
version.NewVersion(ctx, "wacher_kubernetes", 12*time.Hour)
watcherConfig, err := config.LoadKubernetesConfig(configPath)
if err != nil {
log.WithError(err).Panic("could not load Kubernetes configuration file")
os.Exit(1)
}
if err := envconfig.ProcessWith(ctx, &envconfig.Config{
Target: &watcherConfig,
Lookuper: envconfig.PrefixLookuper("STATUSBAY_", envconfig.OsLookuper()),
}); err != nil {
log.WithError(err).Panic("could not parse environment variables")
os.Exit(1)
}
err = config.InitMetricAggregator(watcherConfig.Telemetry)
if err != nil {
log.WithError(err).Panic("failed to initialize telemetry")
os.Exit(1)
}
//Setup logging
visibility.SetupLogging(watcherConfig.Log.Level, watcherConfig.Log.GelfAddress, "wacher_kubernetes")
// Init kubernetes client
kubernetesClientManager, err := client.NewClientManager(kubeconfig, apiserverHost)
if err != nil {
log.WithError(err).Panic("failed to initialize Kubernetes client")
os.Exit(1)
}
kubernetesClientset := kubernetesClientManager.GetInsecureClient()
// Init mysql storage
mysqlManager := state.NewMysqlClient(watcherConfig.MySQL)
mysqlManager.Migration()
mysql := kuberneteswatcher.NewMysql(mysqlManager)
notifiers, err := watcherConfig.BuildNotifiers()
if err != nil {
log.WithError(err).Panic("failed to initialize notifiers")
os.Exit(1)
}
// Init Reporter
reporter := kuberneteswatcher.NewReporter(notifiers)
//Registry manager
registryManager := kuberneteswatcher.NewRegistryManager(watcherConfig.Applies.SaveInterval, watcherConfig.Applies.CheckFinishDelay, watcherConfig.Applies.CollectDataAfterApplyFinish, mysql, reporter, watcherConfig.ClusterName)
runningApplies := registryManager.LoadRunningApplies()
//Event manager
eventManager := kuberneteswatcher.NewEventsManager(kubernetesClientset)
//Service manager
serviceManager := kuberneteswatcher.NewServiceManager(kubernetesClientset, eventManager)
//Pvc manager
pvcManager := kuberneteswatcher.NewPvcManager(kubernetesClientset, eventManager)
//Pods manager
podsManager := kuberneteswatcher.NewPodsManager(kubernetesClientset, eventManager, pvcManager)
//Replicaset manager
replicasetManager := kuberneteswatcher.NewReplicasetManager(kubernetesClientset, eventManager, podsManager)
//Deployment manager
deploymentManager := kuberneteswatcher.NewDeploymentManager(kubernetesClientset, eventManager, registryManager, replicasetManager, serviceManager, runningApplies, watcherConfig.Applies.MaxApplyTime)
// ControllerRevision Manager
controllerRevisionManager := kuberneteswatcher.NewControllerRevisionManager(kubernetesClientset, podsManager)
// Daemonset manager
daemonsetManager := kuberneteswatcher.NewDaemonsetManager(kubernetesClientset, eventManager, registryManager, serviceManager, controllerRevisionManager, runningApplies, watcherConfig.Applies.MaxApplyTime)
//Statefulset manager
statefulsetManager := kuberneteswatcher.NewStatefulsetManager(kubernetesClientset, eventManager, registryManager, serviceManager, controllerRevisionManager, runningApplies, watcherConfig.Applies.MaxApplyTime)
servers := []serverutil.Server{
eventManager, podsManager, pvcManager, deploymentManager, daemonsetManager, statefulsetManager, replicasetManager, registryManager, serviceManager, reporter,
}
// Run a list of backround process for the server
return serverutil.RunAll(ctx, servers)
}
func startAPIServer(ctx context.Context, configPath string, eventsPath string) *serverutil.Runner {
version := version.NewVersion(ctx, "webserver", 12*time.Hour)
apiConfig, err := config.LoadConfigAPI(configPath)
if err != nil {
log.WithError(err).Panic("could not load API configuration file")
os.Exit(1)
}
if err := envconfig.ProcessWith(ctx, &envconfig.Config{
Target: &apiConfig,
Lookuper: envconfig.PrefixLookuper("STATUSBAY_", envconfig.OsLookuper()),
}); err != nil {
log.WithError(err).Panic("could not parse environment variables")
os.Exit(1)
}
eventsConfig, err := config.LoadEvents(eventsPath)
if err != nil {
log.WithError(err).Panic("could not load events configuration file")
os.Exit(1)
}
err = config.InitMetricAggregator(apiConfig.Telemetry)
if err != nil {
log.WithError(err).Panic("failed to initialize telemetry")
os.Exit(1)
}
cacheManager := cache.NewRedisClient(apiConfig.Redis)
//Setup logging
visibility.SetupLogging(apiConfig.Log.Level, apiConfig.Log.GelfAddress, "api")
mysqlManager := state.NewMysqlClient(apiConfig.MySQL)
mysqlManager.Migration()
// TODO:: should be more generic solution, we can start with this solution when we use only one orchestration
kubernetesStorage := apiKubernetes.NewMysql(mysqlManager)
metricsProviders := metrics.Load(apiConfig.MetricsProvider, cacheManager)
alertsProviders := alerts.Load(apiConfig.AlertProvider)
//Start the server
server := api.NewServer(kubernetesStorage, "8080", eventsConfig, metricsProviders, alertsProviders, version)
servers := []serverutil.Server{
server,
}
for _, metric := range metricsProviders {
servers = append(servers, metric)
}
//run lis of backround process for the server
return serverutil.RunAll(ctx, servers)
}